Skip to content

Commit e4df318

Browse files
committed
[c++/m] Add Section findSections
1 parent 954e2c7 commit e4df318

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

+nix/Section.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@
107107
'Section::sectionsFiltered', @nix.Section);
108108
end
109109

110+
% maxdepth is an index
111+
function sec = find_sections(obj, max_depth)
112+
sec = obj.find_filtered_sections(max_depth, nix.Filter.accept_all, '');
113+
end
114+
115+
% maxdepth is an index
116+
function sec = find_filtered_sections(obj, max_depth, filter, val)
117+
if (~isnumeric(max_depth))
118+
error('Provide a valid search depth');
119+
end
120+
121+
valid = nix.Utils.valid_filter(filter, val);
122+
if(~isempty(valid))
123+
error(valid);
124+
end
125+
126+
ret = nix_mx('Section::findSections', obj.nix_handle, ...
127+
max_depth, uint8(filter), val);
128+
sec = cell(length(ret), 1);
129+
for i = 1:length(ret)
130+
sec{i} = nix.Section(ret{i});
131+
end;
132+
end
133+
110134
% ----------------
111135
% Property methods
112136
% ----------------

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ void mexFunction(int nlhs,
417417
methods->add("Section::compare", nixsection::compare);
418418
methods->add("Section::sectionsFiltered", nixsection::sectionsFiltered);
419419
methods->add("Section::propertiesFiltered", nixsection::propertiesFiltered);
420+
methods->add("Section::findSections", nixsection::findSections);
420421

421422
classdef<nix::Feature>("Feature", methods)
422423
.desc(&nixfeature::describe)

src/nixsection.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,17 @@ namespace nixsection {
142142
output.set(0, res);
143143
}
144144

145+
void findSections(const extractor &input, infusor &output) {
146+
nix::Section currObj = input.entity<nix::Section>(1);
147+
size_t max_depth = (size_t)input.num<double>(2);
148+
uint8_t filter_id = input.num<uint8_t>(3);
149+
150+
std::vector<nix::Section> res = filterNameTypeEntity<nix::Section>(input, filter_id, 4, max_depth,
151+
[currObj](const nix::util::Filter<nix::Section>::type &filter, size_t &md) {
152+
return currObj.findSections(filter, md);
153+
});
154+
155+
output.set(0, res);
156+
}
157+
145158
} // namespace nixsection

src/nixsection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace nixsection {
3939

4040
void propertiesFiltered(const extractor &input, infusor &output);
4141

42+
void findSections(const extractor &input, infusor &output);
43+
4244
} // namespace nixsection
4345

4446
#endif

tests/TestSection.m

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
funcs{end+1} = @test_compare;
4242
funcs{end+1} = @test_filter_section;
4343
funcs{end+1} = @test_filter_property;
44+
funcs{end+1} = @test_find_section;
45+
funcs{end+1} = @test_find_section_filtered;
4446
end
4547

4648
%% Test: Create Section
@@ -734,3 +736,122 @@
734736
assert(strcmp(ME.message, err));
735737
end
736738
end
739+
740+
%% Test: Find sections w/o filter
741+
function [] = test_find_section
742+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
743+
main = f.create_section('testSection', 'nixSection');
744+
sl1 = main.create_section('sectionLvl1', 'nixSection');
745+
746+
sl21 = sl1.create_section('sectionLvl2_1', 'nixSection');
747+
sl22 = sl1.create_section('sectionLvl2_2', 'nixSection');
748+
749+
sl31 = sl21.create_section('sectionLvl3_1', 'nixSection');
750+
sl32 = sl21.create_section('sectionLvl3_2', 'nixSection');
751+
sl33 = sl21.create_section('sectionLvl3_3', 'nixSection');
752+
753+
sl41 = sl31.create_section('sectionLvl4_1', 'nixSection');
754+
sl42 = sl31.create_section('sectionLvl4_2', 'nixSection');
755+
sl43 = sl31.create_section('sectionLvl4_3', 'nixSection');
756+
sl44 = sl31.create_section('sectionLvl4_4', 'nixSection');
757+
758+
side = f.create_section('sideSection', 'nixSection');
759+
side1 = side.create_section('sideSubSection', 'nixSection');
760+
761+
% Check invalid entry
762+
err = 'Provide a valid search depth';
763+
try
764+
sl1.find_sections('hurra');
765+
catch ME
766+
assert(strcmp(ME.message, err));
767+
end
768+
769+
% find all
770+
filtered = sl1.find_sections(4);
771+
assert(size(filtered, 1) == 10);
772+
773+
% find until level 4
774+
filtered = sl1.find_sections(3);
775+
assert(size(filtered, 1) == 10);
776+
777+
% find until level 3
778+
filtered = sl1.find_sections(2);
779+
assert(size(filtered, 1) == 6);
780+
781+
% find until level 2
782+
filtered = sl1.find_sections(1);
783+
assert(size(filtered, 1) == 3);
784+
785+
% find until level 1
786+
filtered = sl1.find_sections(0);
787+
assert(size(filtered, 1) == 1);
788+
end
789+
790+
%% Test: Find sections with filters
791+
function [] = test_find_section_filtered
792+
findSection = 'nixFindSection';
793+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
794+
main = f.create_section('testSection', 'nixSection');
795+
sl1 = main.create_section('sectionLvl1', 'nixSection');
796+
797+
sl21 = sl1.create_section('sectionLvl2_1', 'nixSection');
798+
sl22 = sl1.create_section('sectionLvl2_2', findSection);
799+
800+
sl31 = sl21.create_section('sectionLvl3_1', findSection);
801+
sl32 = sl21.create_section('sectionLvl3_2', 'nixSection');
802+
sl33 = sl21.create_section('sectionLvl3_3', 'nixSection');
803+
804+
sl41 = sl31.create_section('sectionLvl4_1', findSection);
805+
sl42 = sl31.create_section('sectionLvl4_2', 'nixSection');
806+
sl43 = sl31.create_section('sectionLvl4_3', 'nixSection');
807+
sl44 = sl31.create_section('sectionLvl4_4', 'nixSection');
808+
809+
sideName = 'sideSubSection';
810+
side = f.create_section('sideSection', 'nixSection');
811+
side1 = side.create_section(sideName, 'nixSection');
812+
813+
% test find by id
814+
filtered = sl1.find_filtered_sections(0, nix.Filter.id, sl41.id);
815+
assert(isempty(filtered));
816+
filtered = sl1.find_filtered_sections(3, nix.Filter.id, sl41.id);
817+
assert(size(filtered, 1) == 1);
818+
assert(strcmp(filtered{1}.id, sl41.id));
819+
820+
% test find by ids
821+
filterids = {sl1.id, sl41.id};
822+
filtered = sl1.find_filtered_sections(0, nix.Filter.ids, filterids);
823+
assert(size(filtered, 1) == 1);
824+
filtered = sl1.find_filtered_sections(3, nix.Filter.ids, filterids);
825+
assert(size(filtered, 1) == 2);
826+
827+
% test find by name
828+
filtered = sl1.find_filtered_sections(4, nix.Filter.name, sideName);
829+
assert(isempty(filtered));
830+
filtered = sl1.find_filtered_sections(0, nix.Filter.name, sl41.name);
831+
assert(isempty(filtered));
832+
filtered = sl1.find_filtered_sections(3, nix.Filter.name, sl41.name);
833+
assert(size(filtered, 1) == 1);
834+
assert(strcmp(filtered{1}.name, sl41.name));
835+
836+
% test find by type
837+
filtered = sl1.find_filtered_sections(0, nix.Filter.type, findSection);
838+
assert(isempty(filtered));
839+
filtered = sl1.find_filtered_sections(3, nix.Filter.type, findSection);
840+
assert(size(filtered, 1) == 3);
841+
assert(strcmp(filtered{1}.type, findSection));
842+
843+
% test fail on nix.Filter.metadata
844+
err = 'unknown or unsupported filter';
845+
try
846+
sl1.find_filtered_sections(1, nix.Filter.metadata, 'metadata');
847+
catch ME
848+
assert(strcmp(ME.message, err));
849+
end
850+
851+
% test fail on nix.Filter.source
852+
try
853+
sl1.find_filtered_sections(1, nix.Filter.source, 'source');
854+
catch ME
855+
assert(strcmp(ME.message, err));
856+
end
857+
end

0 commit comments

Comments
 (0)