Skip to content

Commit a0d4f9e

Browse files
committed
[c++/m] Add Section propertiesFilter
Closes #122
1 parent ee0bb1f commit a0d4f9e

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

+nix/Section.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@
166166
c = nix_mx('Section::propertyCount', obj.nix_handle);
167167
end
168168

169+
function filtered = filter_properties(obj, filter, val)
170+
filtered = nix.Utils.filter(obj, filter, val, ...
171+
'Section::propertiesFiltered', @nix.Property);
172+
end
173+
169174
% ----------------
170175
% Referring entity methods
171176
% ----------------

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ void mexFunction(int nlhs,
415415
methods->add("Section::openPropertyIdx", nixsection::openPropertyIdx);
416416
methods->add("Section::compare", nixsection::compare);
417417
methods->add("Section::sectionsFiltered", nixsection::sectionsFiltered);
418+
methods->add("Section::propertiesFiltered", nixsection::propertiesFiltered);
418419

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

src/filters.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,28 @@ std::vector<T> filterFeature(const extractor &input, FN ff) {
9595
return res;
9696
}
9797

98+
template<typename T, typename FN>
99+
std::vector<T> filterProperty(const extractor &input, FN ff) {
100+
std::vector<T> res;
101+
102+
switch (input.num<uint8_t>(2)) {
103+
case switchFilter::AcceptAll:
104+
res = ff(nix::util::AcceptAll<T>());
105+
break;
106+
case switchFilter::Id:
107+
res = ff(nix::util::IdFilter<T>(input.str(3)));
108+
break;
109+
case switchFilter::Ids:
110+
// this will crash matlab, if its not a vector of strings...
111+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(3)));
112+
break;
113+
case switchFilter::Name:
114+
res = ff(nix::util::NameFilter<T>(input.str(3)));
115+
break;
116+
default: throw std::invalid_argument("unknown or unsupported filter");
117+
}
118+
119+
return res;
120+
}
121+
98122
#endif

src/nixsection.cc

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

136+
void propertiesFiltered(const extractor &input, infusor &output) {
137+
nix::Section currObj = input.entity<nix::Section>(1);
138+
std::vector<nix::Property> res = filterProperty<nix::Property>(input,
139+
[currObj](const nix::util::Filter<nix::Property>::type &filter) {
140+
return currObj.properties(filter);
141+
});
142+
output.set(0, res);
143+
}
144+
136145
} // namespace nixsection

src/nixsection.h

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

3838
void sectionsFiltered(const extractor &input, infusor &output);
3939

40+
void propertiesFiltered(const extractor &input, infusor &output);
41+
4042
} // namespace nixsection
4143

4244
#endif

tests/TestSection.m

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
funcs{end+1} = @test_referring_blocks;
4141
funcs{end+1} = @test_compare;
4242
funcs{end+1} = @test_filter_section;
43+
funcs{end+1} = @test_filter_property;
4344
end
4445

4546
%% Test: Create Section
@@ -677,3 +678,59 @@
677678
assert(strcmp(ME.message, err));
678679
end
679680
end
681+
682+
%% Test: filter properties
683+
function [] = test_filter_property( varargin )
684+
filterName = 'filterMe';
685+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
686+
ms = f.create_section('testSection', 'nixSection');
687+
p = ms.create_property(filterName, nix.DataType.Double);
688+
filterID = p.id;
689+
s = ms.create_property('testProperty', nix.DataType.Bool);
690+
filterIDs = {filterID, s.id};
691+
692+
% test empty id filter
693+
assert(isempty(f.sections{1}.filter_properties(nix.Filter.id, 'IdoNotExist')));
694+
695+
% test nix.Filter.accept_all
696+
filtered = f.sections{1}.filter_properties(nix.Filter.accept_all, '');
697+
assert(size(filtered, 1) == 2);
698+
699+
% test nix.Filter.id
700+
filtered = f.sections{1}.filter_properties(nix.Filter.id, filterID);
701+
assert(size(filtered, 1) == 1);
702+
assert(strcmp(filtered{1}.id, filterID));
703+
704+
% test nix.Filter.ids
705+
filtered = f.sections{1}.filter_properties(nix.Filter.ids, filterIDs);
706+
assert(size(filtered, 1) == 2);
707+
assert(strcmp(filtered{1}.id, filterIDs{1}) || strcmp(filtered{1}.id, filterIDs{2}));
708+
709+
% test nix.Filter.name
710+
filtered = f.sections{1}.filter_properties(nix.Filter.name, filterName);
711+
assert(size(filtered, 1) == 1);
712+
assert(strcmp(filtered{1}.name, filterName));
713+
714+
% test fail on nix.Filter.type
715+
err = 'unknown or unsupported filter';
716+
try
717+
f.sections{1}.filter_properties(nix.Filter.type, 'someType');
718+
catch ME
719+
assert(strcmp(ME.message, err));
720+
end
721+
722+
% test fail on nix.Filter.metadata
723+
err = 'unknown or unsupported filter';
724+
try
725+
f.sections{1}.filter_properties(nix.Filter.metadata, 'someMetadata');
726+
catch ME
727+
assert(strcmp(ME.message, err));
728+
end
729+
730+
% test fail on nix.Filter.source
731+
try
732+
f.sections{1}.filter_properties(nix.Filter.source, 'someSource');
733+
catch ME
734+
assert(strcmp(ME.message, err));
735+
end
736+
end

0 commit comments

Comments
 (0)