Skip to content

Commit 954e2c7

Browse files
committed
[c++/m] Add File findSections function
1 parent 14b87f9 commit 954e2c7

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

+nix/File.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@
112112
filtered = nix.Utils.filter(obj, filter, val, ...
113113
'File::sectionsFiltered', @nix.Section);
114114
end
115+
116+
% maxdepth is an index
117+
function sec = find_sections(obj, max_depth)
118+
sec = obj.find_filtered_sections(max_depth, nix.Filter.accept_all, '');
119+
end
120+
121+
% maxdepth is an index
122+
function sec = find_filtered_sections(obj, max_depth, filter, val)
123+
if (~isnumeric(max_depth))
124+
error('Provide a valid search depth');
125+
end
126+
127+
valid = nix.Utils.valid_filter(filter, val);
128+
if(~isempty(valid))
129+
error(valid);
130+
end
131+
132+
ret = nix_mx('File::findSections', obj.nix_handle, ...
133+
max_depth, uint8(filter), val);
134+
sec = cell(length(ret), 1);
135+
for i = 1:length(ret)
136+
sec{i} = nix.Section(ret{i});
137+
end;
138+
end
115139
end
116140

117141
end

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void mexFunction(int nlhs,
123123
methods->add("File::openSectionIdx", nixfile::openSectionIdx);
124124
methods->add("File::sectionsFiltered", nixfile::sectionsFiltered);
125125
methods->add("File::blocksFiltered", nixfile::blocksFiltered);
126+
methods->add("File::findSections", nixfile::findSections);
126127

127128
classdef<nix::Block>("Block", methods)
128129
.desc(&nixblock::describe)

src/nixfile.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,17 @@ namespace nixfile {
133133
output.set(0, res);
134134
}
135135

136+
void findSections(const extractor &input, infusor &output) {
137+
nix::File currObj = input.entity<nix::File>(1);
138+
size_t max_depth = (size_t)input.num<double>(2);
139+
uint8_t filter_id = input.num<uint8_t>(3);
140+
141+
std::vector<nix::Section> res = filterNameTypeEntity<nix::Section>(input, filter_id, 4, max_depth,
142+
[currObj](const nix::util::Filter<nix::Section>::type &filter, size_t &md) {
143+
return currObj.findSections(filter, md);
144+
});
145+
146+
output.set(0, res);
147+
}
148+
136149
} // namespace nixfile

src/nixfile.h

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

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

32+
void findSections(const extractor &input, infusor &output);
33+
3234
} // namespace nixfile
3335

3436
#endif

tests/TestFile.m

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
funcs{end+1} = @test_has_section;
3434
funcs{end+1} = @test_filter_section;
3535
funcs{end+1} = @test_filter_block;
36+
funcs{end+1} = @test_find_section;
37+
funcs{end+1} = @test_find_section_filtered;
3638
end
3739

3840
%% Test: Open HDF5 file in ReadOnly mode
@@ -411,3 +413,111 @@
411413
end
412414

413415
end
416+
417+
%% Test: Find sections w/o filter
418+
function [] = test_find_section
419+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
420+
sl1 = f.create_section('sectionLvl1', 'nixSection');
421+
422+
sl21 = sl1.create_section('sectionLvl2_1', 'nixSection');
423+
sl22 = sl1.create_section('sectionLvl2_2', 'nixSection');
424+
425+
sl31 = sl21.create_section('sectionLvl3_1', 'nixSection');
426+
sl32 = sl21.create_section('sectionLvl3_2', 'nixSection');
427+
sl33 = sl21.create_section('sectionLvl3_3', 'nixSection');
428+
429+
sl41 = sl31.create_section('sectionLvl4_1', 'nixSection');
430+
sl42 = sl31.create_section('sectionLvl4_2', 'nixSection');
431+
sl43 = sl31.create_section('sectionLvl4_3', 'nixSection');
432+
sl44 = sl31.create_section('sectionLvl4_4', 'nixSection');
433+
434+
% Check invalid entry
435+
err = 'Provide a valid search depth';
436+
try
437+
f.find_sections('hurra');
438+
catch ME
439+
assert(strcmp(ME.message, err));
440+
end
441+
442+
% find all
443+
filtered = f.find_sections(4);
444+
assert(size(filtered, 1) == 10);
445+
446+
% find until level 4
447+
filtered = f.find_sections(3);
448+
assert(size(filtered, 1) == 10);
449+
450+
% find until level 3
451+
filtered = f.find_sections(2);
452+
assert(size(filtered, 1) == 6);
453+
454+
% find until level 2
455+
filtered = f.find_sections(1);
456+
assert(size(filtered, 1) == 3);
457+
458+
% find until level 1
459+
filtered = f.find_sections(0);
460+
assert(size(filtered, 1) == 1);
461+
end
462+
463+
%% Test: Find sections with filter
464+
function [] = test_find_section_filtered
465+
findSection = 'nixFindSection';
466+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
467+
sl1 = f.create_section('sectionLvl1', 'nixSection');
468+
469+
sl21 = sl1.create_section('sectionLvl2_1', 'nixSection');
470+
sl22 = sl1.create_section('sectionLvl2_2', findSection);
471+
472+
sl31 = sl21.create_section('sectionLvl3_1', 'nixSection');
473+
sl32 = sl21.create_section('sectionLvl3_2', 'nixSection');
474+
sl33 = sl21.create_section('sectionLvl3_3', findSection);
475+
476+
sl41 = sl31.create_section('sectionLvl4_1', 'nixSection');
477+
sl42 = sl31.create_section('sectionLvl4_2', 'nixSection');
478+
sl43 = sl31.create_section('sectionLvl4_3', findSection);
479+
sl44 = sl31.create_section('sectionLvl4_4', 'nixSection');
480+
481+
% test find by id
482+
filtered = f.find_filtered_sections(0, nix.Filter.id, sl41.id);
483+
assert(isempty(filtered));
484+
filtered = f.find_filtered_sections(3, nix.Filter.id, sl41.id);
485+
assert(size(filtered, 1) == 1);
486+
assert(strcmp(filtered{1}.id, sl41.id));
487+
488+
% test find by ids
489+
filterids = {sl1.id, sl41.id};
490+
filtered = f.find_filtered_sections(0, nix.Filter.ids, filterids);
491+
assert(size(filtered, 1) == 1);
492+
filtered = f.find_filtered_sections(3, nix.Filter.ids, filterids);
493+
assert(size(filtered, 1) == 2);
494+
495+
% test find by name
496+
filtered = f.find_filtered_sections(0, nix.Filter.name, sl41.name);
497+
assert(isempty(filtered));
498+
filtered = f.find_filtered_sections(3, nix.Filter.name, sl41.name);
499+
assert(size(filtered, 1) == 1);
500+
assert(strcmp(filtered{1}.name, sl41.name));
501+
502+
% test find by type
503+
filtered = f.find_filtered_sections(0, nix.Filter.type, findSection);
504+
assert(isempty(filtered));
505+
filtered = f.find_filtered_sections(3, nix.Filter.type, findSection);
506+
assert(size(filtered, 1) == 3);
507+
assert(strcmp(filtered{1}.type, findSection));
508+
509+
% test fail on nix.Filter.metadata
510+
err = 'unknown or unsupported filter';
511+
try
512+
f.find_filtered_sections(1, nix.Filter.metadata, 'metadata');
513+
catch ME
514+
assert(strcmp(ME.message, err));
515+
end
516+
517+
% test fail on nix.Filter.source
518+
try
519+
f.find_filtered_sections(1, nix.Filter.source, 'source');
520+
catch ME
521+
assert(strcmp(ME.message, err));
522+
end
523+
end

0 commit comments

Comments
 (0)