Skip to content

Commit 022085d

Browse files
committed
[c++/m] Add Tag getByIndex functions
Implements the getByIndex functions on the c++ and the Matlab side and adds corresponding tests. - getReference - getFeature - getSource
1 parent a4147f5 commit 022085d

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

+nix/Tag.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
'Tag::openReferenceDataArray', id_or_name, @nix.DataArray);
5959
end;
6060

61+
function retObj = open_reference_idx(obj, idx)
62+
retObj = nix.Utils.open_entity(obj, ...
63+
'Tag::openReferenceIdx', idx, @nix.DataArray);
64+
end;
65+
6166
function data = retrieve_data(obj, index)
6267
% convert Matlab-like to C-like index
6368
assert(index > 0, 'Subscript indices must be positive');
@@ -99,7 +104,12 @@
99104
retObj = nix.Utils.open_entity(obj, ...
100105
'Tag::openFeature', id_or_name, @nix.Feature);
101106
end;
102-
107+
108+
function retObj = open_feature_idx(obj, idx)
109+
retObj = nix.Utils.open_entity(obj, ...
110+
'Tag::openFeatureIdx', idx, @nix.Feature);
111+
end;
112+
103113
function data = retrieve_feature_data(obj, index)
104114
% convert Matlab-like to C-like index
105115
assert(index > 0, 'Subscript indices must be positive');

nix_mx.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ void mexFunction(int nlhs,
303303
methods->add("Tag::addSource", nixtag::addSource);
304304
methods->add("Tag::addSources", nixtag::addSources);
305305
methods->add("Tag::createFeature", nixtag::createFeature);
306+
methods->add("Tag::openReferenceIdx", nixtag::openReferenceIdx);
307+
methods->add("Tag::openFeatureIdx", nixtag::openFeatureIdx);
308+
methods->add("Tag::openSourceIdx", nixtag::openSourceIdx);
306309

307310
classdef<nix::MultiTag>("MultiTag", methods)
308311
.desc(&nixmultitag::describe)

src/nixtag.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,22 @@ namespace nixtag {
7878
output.set(0, data);
7979
}
8080

81+
void openReferenceIdx(const extractor &input, infusor &output) {
82+
nix::Tag currObj = input.entity<nix::Tag>(1);
83+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
84+
output.set(0, currObj.getReference(idx));
85+
}
86+
87+
void openFeatureIdx(const extractor &input, infusor &output) {
88+
nix::Tag currObj = input.entity<nix::Tag>(1);
89+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
90+
output.set(0, currObj.getFeature(idx));
91+
}
92+
93+
void openSourceIdx(const extractor &input, infusor &output) {
94+
nix::Tag currObj = input.entity<nix::Tag>(1);
95+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
96+
output.set(0, currObj.getSource(idx));
97+
}
98+
8199
} // namespace nixtag

src/nixtag.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ namespace nixtag {
2929

3030
void retrieveFeatureData(const extractor &input, infusor &output);
3131

32+
void openReferenceIdx(const extractor &input, infusor &output);
33+
34+
void openFeatureIdx(const extractor &input, infusor &output);
35+
36+
void openSourceIdx(const extractor &input, infusor &output);
37+
3238
} // namespace nixtag
3339

3440
#endif

tests/TestTag.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
funcs{end+1} = @test_fetch_features;
2626
funcs{end+1} = @test_feature_count;
2727
funcs{end+1} = @test_open_source;
28+
funcs{end+1} = @test_open_source_idx;
2829
funcs{end+1} = @test_has_source;
2930
funcs{end+1} = @test_source_count;
3031
funcs{end+1} = @test_open_feature;
32+
funcs{end+1} = @test_open_feature_idx;
3133
funcs{end+1} = @test_open_reference;
34+
funcs{end+1} = @test_open_reference_idx;
3235
funcs{end+1} = @test_set_metadata;
3336
funcs{end+1} = @test_open_metadata;
3437
funcs{end+1} = @test_retrieve_data;
@@ -350,6 +353,23 @@
350353
assert(isempty(getNonSource));
351354
end
352355

356+
function [] = test_open_source_idx( varargin )
357+
%% Test Open Source by index
358+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
359+
b = f.create_block('testBlock', 'nixBlock');
360+
t = b.create_tag('testTag', 'nixTag', [2 9]);
361+
s1 = b.create_source('testSource1', 'nixSource');
362+
s2 = b.create_source('testSource2', 'nixSource');
363+
s3 = b.create_source('testSource3', 'nixSource');
364+
t.add_source(s1);
365+
t.add_source(s2);
366+
t.add_source(s3);
367+
368+
assert(strcmp(f.blocks{1}.tags{1}.open_source_idx(0).name, s1.name));
369+
assert(strcmp(f.blocks{1}.tags{1}.open_source_idx(1).name, s2.name));
370+
assert(strcmp(f.blocks{1}.tags{1}.open_source_idx(2).name, s3.name));
371+
end
372+
353373
%% Test: nix.Tag has nix.Source by ID or entity
354374
function [] = test_has_source( varargin )
355375
fileName = 'testRW.h5';
@@ -402,6 +422,22 @@
402422
assert(isempty(getFeat));
403423
end
404424

425+
function [] = test_open_feature_idx( varargin )
426+
%% Test Open feature by index
427+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
428+
b = f.create_block('testBlock', 'nixBlock');
429+
t = b.create_tag('testTag', 'nixTag', [2 9]);
430+
d1 = b.create_data_array('testFeature1', 'nixDataArray', nix.DataType.Double, [1 2]);
431+
d2 = b.create_data_array('testFeature2', 'nixDataArray', nix.DataType.Double, [3 2]);
432+
d3 = b.create_data_array('testFeature3', 'nixDataArray', nix.DataType.Double, [7 2]);
433+
t.add_feature(d1, nix.LinkType.Tagged);
434+
t.add_feature(d2, nix.LinkType.Untagged);
435+
t.add_feature(d3, nix.LinkType.Indexed);
436+
437+
assert(f.blocks{1}.tags{1}.open_feature_idx(0).linkType == nix.LinkType.Tagged);
438+
assert(f.blocks{1}.tags{1}.open_feature_idx(1).linkType == nix.LinkType.Untagged);
439+
assert(f.blocks{1}.tags{1}.open_feature_idx(2).linkType == nix.LinkType.Indexed);
440+
end
405441

406442
%% Test: Open reference by ID or name
407443
function [] = test_open_reference( varargin )
@@ -423,6 +459,23 @@
423459
assert(isempty(getNonRef));
424460
end
425461

462+
function [] = test_open_reference_idx( varargin )
463+
%% Test Open reference by index
464+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
465+
b = f.create_block('testBlock', 'nixBlock');
466+
t = b.create_tag('testTag', 'nixTag', [2 9]);
467+
d1 = b.create_data_array('testReference1', 'nixDataArray', nix.DataType.Double, [1 2]);
468+
d2 = b.create_data_array('testReference2', 'nixDataArray', nix.DataType.Double, [3 2]);
469+
d3 = b.create_data_array('testReference3', 'nixDataArray', nix.DataType.Double, [7 2]);
470+
t.add_reference(d1);
471+
t.add_reference(d2);
472+
t.add_reference(d3);
473+
474+
assert(strcmp(f.blocks{1}.tags{1}.open_reference_idx(0).name, d1.name));
475+
assert(strcmp(f.blocks{1}.tags{1}.open_reference_idx(1).name, d2.name));
476+
assert(strcmp(f.blocks{1}.tags{1}.open_reference_idx(2).name, d3.name));
477+
end
478+
426479
%% Test: Set metadata
427480
function [] = test_set_metadata ( varargin )
428481
fileName = fullfile(pwd, 'tests', 'testRW.h5');

0 commit comments

Comments
 (0)