Skip to content

Commit 27d7f4a

Browse files
committed
[c++/m] Add Block getByIndex functions
Implements the getByIndex functions on the c++ and the Matlab side and adds corresponding tests. - getSource - getDataArray - getTag - getMultiTag - getGroup
1 parent 8051484 commit 27d7f4a

File tree

5 files changed

+143
-2
lines changed

5 files changed

+143
-2
lines changed

+nix/Block.m

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
'Block::getGroup', id_or_name, @nix.Group);
5151
end;
5252

53+
function retObj = open_group_idx(obj, idx)
54+
retObj = nix.Utils.open_entity(obj, ...
55+
'Block::openGroupIdx', idx, @nix.Group);
56+
end
57+
5358
function delCheck = delete_group(obj, del)
5459
delCheck = nix.Utils.delete_entity(obj, ...
5560
del, 'nix.Group', 'Block::deleteGroup');
@@ -67,7 +72,12 @@
6772
retObj = nix.Utils.open_entity(obj, ...
6873
'Block::openDataArray', id_or_name, @nix.DataArray);
6974
end;
70-
75+
76+
function retObj = open_data_array_idx(obj, idx)
77+
retObj = nix.Utils.open_entity(obj, ...
78+
'Block::openDataArrayIdx', idx, @nix.DataArray);
79+
end
80+
7181
function da = create_data_array(obj, name, nixtype, datatype, shape)
7282
%-- Quick fix to enable alias range dimension with
7383
%-- 1D data arrays created with this function.
@@ -154,6 +164,11 @@
154164
'Block::openSource', id_or_name, @nix.Source);
155165
end;
156166

167+
function retObj = open_source_idx(obj, idx)
168+
retObj = nix.Utils.open_entity(obj, ...
169+
'Block::openSourceIdx', idx, @nix.Source);
170+
end
171+
157172
% -----------------
158173
% Tags methods
159174
% -----------------
@@ -170,7 +185,12 @@
170185
retObj = nix.Utils.open_entity(obj, ...
171186
'Block::openTag', id_or_name, @nix.Tag);
172187
end;
173-
188+
189+
function retObj = open_tag_idx(obj, idx)
190+
retObj = nix.Utils.open_entity(obj, ...
191+
'Block::openTagIdx', idx, @nix.Tag);
192+
end
193+
174194
function tag = create_tag(obj, name, type, position)
175195
th = nix_mx('Block::createTag', obj.nix_handle, ...
176196
name, type, position);
@@ -199,6 +219,11 @@
199219
'Block::openMultiTag', id_or_name, @nix.MultiTag);
200220
end;
201221

222+
function retObj = open_multi_tag_idx(obj, idx)
223+
retObj = nix.Utils.open_entity(obj, ...
224+
'Block::openMultiTagIdx', idx, @nix.MultiTag);
225+
end
226+
202227
%-- Creating a multitag requires an already existing data array
203228
function multitag = create_multi_tag(obj, name, type, add_data_array)
204229
if(strcmp(class(add_data_array), 'nix.DataArray'))

nix_mx.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ void mexFunction(int nlhs,
160160
methods->add("Block::createDataArray", nixblock::createDataArray);
161161
methods->add("Block::createMultiTag", nixblock::createMultiTag);
162162
methods->add("Block::createGroup", nixblock::createGroup);
163+
methods->add("Block::openGroupIdx", nixblock::openGroupIdx);
164+
methods->add("Block::openDataArrayIdx", nixblock::openDataArrayIdx);
165+
methods->add("Block::openTagIdx", nixblock::openTagIdx);
166+
methods->add("Block::openMultiTagIdx", nixblock::openMultiTagIdx);
167+
methods->add("Block::openSourceIdx", nixblock::openSourceIdx);
163168

164169
classdef<nix::Group>("Group", methods)
165170
.desc(&nixgroup::describe)

src/nixblock.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,34 @@ namespace nixblock {
6060
output.set(0, group);
6161
}
6262

63+
void openGroupIdx(const extractor &input, infusor &output) {
64+
nix::Block currObj = input.entity<nix::Block>(1);
65+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
66+
output.set(0, currObj.getGroup(idx));
67+
}
68+
69+
void openDataArrayIdx(const extractor &input, infusor &output) {
70+
nix::Block currObj = input.entity<nix::Block>(1);
71+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
72+
output.set(0, currObj.getDataArray(idx));
73+
}
74+
75+
void openTagIdx(const extractor &input, infusor &output) {
76+
nix::Block currObj = input.entity<nix::Block>(1);
77+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
78+
output.set(0, currObj.getTag(idx));
79+
}
80+
81+
void openMultiTagIdx(const extractor &input, infusor &output) {
82+
nix::Block currObj = input.entity<nix::Block>(1);
83+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
84+
output.set(0, currObj.getMultiTag(idx));
85+
}
86+
87+
void openSourceIdx(const extractor &input, infusor &output) {
88+
nix::Block currObj = input.entity<nix::Block>(1);
89+
nix::ndsize_t idx = (nix::ndsize_t)input.num<double>(2);
90+
output.set(0, currObj.getSource(idx));
91+
}
92+
6393
} // namespace nixblock

src/nixblock.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ namespace nixblock {
2121

2222
void createGroup(const extractor &input, infusor &output);
2323

24+
void openGroupIdx(const extractor &input, infusor &output);
25+
26+
void openDataArrayIdx(const extractor &input, infusor &output);
27+
28+
void openTagIdx(const extractor &input, infusor &output);
29+
30+
void openMultiTagIdx(const extractor &input, infusor &output);
31+
32+
void openSourceIdx(const extractor &input, infusor &output);
33+
2434
} // namespace nixblock
2535

2636
#endif

tests/TestBlock.m

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
funcs{end+1} = @test_open_tag;
3030
funcs{end+1} = @test_open_multitag;
3131
funcs{end+1} = @test_open_source;
32+
funcs{end+1} = @test_open_group_idx;
33+
funcs{end+1} = @test_open_data_array_idx;
34+
funcs{end+1} = @test_open_tag_idx;
35+
funcs{end+1} = @test_open_multi_tag_idx;
36+
funcs{end+1} = @test_open_source_idx;
3237
funcs{end+1} = @test_has_data_array;
3338
funcs{end+1} = @test_has_source;
3439
funcs{end+1} = @test_has_multitag;
@@ -417,6 +422,72 @@
417422
assert(isempty(getSource));
418423
end
419424

425+
function [] = test_open_group_idx( varargin )
426+
%% Test Open Group by index
427+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
428+
b = f.create_block('testBlock', 'nixBlock');
429+
g1 = b.create_group('testGroup1', 'nixGroup');
430+
g2 = b.create_group('testGroup2', 'nixGroup');
431+
g3 = b.create_group('testGroup3', 'nixGroup');
432+
433+
assert(strcmp(f.blocks{1}.open_group_idx(0).name, g1.name));
434+
assert(strcmp(f.blocks{1}.open_group_idx(1).name, g2.name));
435+
assert(strcmp(f.blocks{1}.open_group_idx(2).name, g3.name));
436+
end
437+
438+
function [] = test_open_data_array_idx( varargin )
439+
%% Test Open DataArray by index
440+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
441+
b = f.create_block('testBlock', 'nixBlock');
442+
d1 = b.create_data_array('testDataArray1', 'nixDataArray', nix.DataType.Double, [3 2]);
443+
d2 = b.create_data_array('testDataArray2', 'nixDataArray', nix.DataType.Double, [6 2]);
444+
d3 = b.create_data_array('testDataArray3', 'nixDataArray', nix.DataType.Double, [9 2]);
445+
446+
assert(strcmp(f.blocks{1}.open_data_array_idx(0).name, d1.name));
447+
assert(strcmp(f.blocks{1}.open_data_array_idx(1).name, d2.name));
448+
assert(strcmp(f.blocks{1}.open_data_array_idx(2).name, d3.name));
449+
end
450+
451+
function [] = test_open_tag_idx( varargin )
452+
%% Test Open Tag by index
453+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
454+
b = f.create_block('testBlock', 'nixBlock');
455+
t1 = b.create_tag('testTag1', 'nixTag', [1 2]);
456+
t2 = b.create_tag('testTag2', 'nixTag', [1 2]);
457+
t3 = b.create_tag('testTag3', 'nixTag', [1 2]);
458+
459+
assert(strcmp(f.blocks{1}.open_tag_idx(0).name, t1.name));
460+
assert(strcmp(f.blocks{1}.open_tag_idx(1).name, t2.name));
461+
assert(strcmp(f.blocks{1}.open_tag_idx(2).name, t3.name));
462+
end
463+
464+
function [] = test_open_multi_tag_idx( varargin )
465+
%% Test Open MultiTag by index
466+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
467+
b = f.create_block('testBlock', 'nixBlock');
468+
d = b.create_data_array('testDataArray', 'nixDataArray', nix.DataType.Bool, [2 3]);
469+
t1 = b.create_multi_tag('testMultiTag1', 'nixMultiTag', d);
470+
t2 = b.create_multi_tag('testMultiTag2', 'nixMultiTag', d);
471+
t3 = b.create_multi_tag('testMultiTag3', 'nixMultiTag', d);
472+
473+
assert(strcmp(f.blocks{1}.open_multi_tag_idx(0).name, t1.name));
474+
assert(strcmp(f.blocks{1}.open_multi_tag_idx(1).name, t2.name));
475+
assert(strcmp(f.blocks{1}.open_multi_tag_idx(2).name, t3.name));
476+
end
477+
478+
function [] = test_open_source_idx( varargin )
479+
%% Test Open Source by index
480+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
481+
b = f.create_block('testBlock', 'nixBlock');
482+
s1 = b.create_source('testSource1', 'nixSource');
483+
s2 = b.create_source('testSource2', 'nixSource');
484+
s3 = b.create_source('testSource3', 'nixSource');
485+
486+
assert(strcmp(f.blocks{1}.open_source_idx(0).name, s1.name));
487+
assert(strcmp(f.blocks{1}.open_source_idx(1).name, s2.name));
488+
assert(strcmp(f.blocks{1}.open_source_idx(2).name, s3.name));
489+
end
490+
420491
function [] = test_has_multitag( varargin )
421492
%% Test: Block has multi tag by ID or name
422493
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);

0 commit comments

Comments
 (0)