Skip to content

Commit f754f70

Browse files
committed
[c++/m] Add Section findRelated function
1 parent e4df318 commit f754f70

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

+nix/Section.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@
106106
filtered = nix.Utils.filter(obj, filter, val, ...
107107
'Section::sectionsFiltered', @nix.Section);
108108
end
109+
110+
% find_related returns the nearest occurrence downstream of a
111+
% nix.Section matching the filter.
112+
% If no section can be found downstream, it will look for the
113+
% nearest occurrence upstream of a nix.Section matching the filter.
114+
function filtered = find_related(obj, filter, val)
115+
filtered = nix.Utils.filter(obj, filter, val, ...
116+
'Section::findRelated', @nix.Section);
117+
end
109118

110119
% maxdepth is an index
111120
function sec = find_sections(obj, max_depth)

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ void mexFunction(int nlhs,
418418
methods->add("Section::sectionsFiltered", nixsection::sectionsFiltered);
419419
methods->add("Section::propertiesFiltered", nixsection::propertiesFiltered);
420420
methods->add("Section::findSections", nixsection::findSections);
421+
methods->add("Section::findRelated", nixsection::findRelated);
421422

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

src/nixsection.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,13 @@ namespace nixsection {
155155
output.set(0, res);
156156
}
157157

158+
void findRelated(const extractor &input, infusor &output) {
159+
nix::Section currObj = input.entity<nix::Section>(1);
160+
std::vector<nix::Section> res = filterNameTypeEntity<nix::Section>(input,
161+
[currObj](const nix::util::Filter<nix::Section>::type &filter) {
162+
return currObj.findRelated(filter);
163+
});
164+
output.set(0, res);
165+
}
166+
158167
} // namespace nixsection

src/nixsection.h

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

4242
void findSections(const extractor &input, infusor &output);
4343

44+
void findRelated(const extractor &input, infusor &output);
45+
4446
} // namespace nixsection
4547

4648
#endif

tests/TestSection.m

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
funcs{end+1} = @test_filter_property;
4444
funcs{end+1} = @test_find_section;
4545
funcs{end+1} = @test_find_section_filtered;
46+
funcs{end+1} = @test_find_related;
4647
end
4748

4849
%% Test: Create Section
@@ -855,3 +856,66 @@
855856
assert(strcmp(ME.message, err));
856857
end
857858
end
859+
860+
%% Test: Find sections related to the current section
861+
function [] = test_find_related
862+
findSectionType = 'nixFindSection';
863+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
864+
main = f.create_section('testSection', 'nixSection');
865+
sl1 = main.create_section('sectionLvl1', 'nixSection');
866+
867+
sl21 = sl1.create_section('sectionLvl2_1', findSectionType);
868+
sl22 = sl1.create_section('sectionLvl2_2', findSectionType);
869+
870+
sl31 = sl21.create_section('sectionLvl3_1', findSectionType);
871+
sl32 = sl21.create_section('sectionLvl3_2', 'nixSection');
872+
sl33 = sl21.create_section('sectionLvl3_3', 'nixSection');
873+
874+
sl41 = sl31.create_section('sectionLvl4_1', findSectionType);
875+
sl42 = sl31.create_section('sectionLvl4_2', findSectionType);
876+
sl43 = sl31.create_section('sectionLvl4_3', 'nixSection');
877+
sl44 = sl31.create_section('sectionLvl4_4', 'nixSection');
878+
879+
sideName = 'sideSubSection';
880+
side = f.create_section('sideSection', 'nixSection');
881+
side1 = side.create_section(sideName, 'nixSection');
882+
883+
% find first downstream by id
884+
rel = sl21.find_related(nix.Filter.id, sl44.id);
885+
assert(size(rel, 1) == 1);
886+
887+
% find first updstream by ids
888+
rel = sl33.find_related(nix.Filter.ids, {sl21.id, sl1.id});
889+
assert(size(rel, 1) == 1);
890+
891+
% find first downstream by name, one occurrence
892+
rel = sl21.find_related(nix.Filter.name, 'sectionLvl4_4');
893+
assert(size(rel, 1) == 1);
894+
895+
% find first downstream by type, one occurrence
896+
rel = sl21.find_related(nix.Filter.type, findSectionType);
897+
assert(size(rel, 1) == 1);
898+
899+
% find first downstream by type, multiple occurrences
900+
rel = sl31.find_related(nix.Filter.type, findSectionType);
901+
assert(size(rel, 1) == 2);
902+
903+
% find first upstream by name, one occurrence
904+
rel = sl31.find_related(nix.Filter.name, 'sectionLvl2_1');
905+
assert(size(rel, 1) == 1);
906+
907+
% test fail on nix.Filter.metadata
908+
err = 'unknown or unsupported filter';
909+
try
910+
sl1.find_related(nix.Filter.metadata, 'metadata');
911+
catch ME
912+
assert(strcmp(ME.message, err));
913+
end
914+
915+
% test fail on nix.Filter.source
916+
try
917+
sl1.find_related(nix.Filter.source, 'source');
918+
catch ME
919+
assert(strcmp(ME.message, err));
920+
end
921+
end

0 commit comments

Comments
 (0)