Skip to content

Commit a93139b

Browse files
authored
Merge pull request #149 from mpsonntag/findFunctions
Implement findSources and findSections Looking good!
2 parents 8c6a3fd + 56ce652 commit a93139b

19 files changed

+743
-1
lines changed

+nix/Block.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@
184184
'Block::sourcesFiltered', @nix.Source);
185185
end
186186

187+
% maxdepth is an index
188+
function sec = find_sources(obj, max_depth)
189+
sec = obj.find_filtered_sources(max_depth, nix.Filter.accept_all, '');
190+
end
191+
192+
% maxdepth is an index
193+
function sec = find_filtered_sources(obj, max_depth, filter, val)
194+
sec = nix.Utils.find(obj, ...
195+
max_depth, filter, val, 'Block::findSources', @nix.Source);
196+
end
197+
187198
% -----------------
188199
% Tags methods
189200
% -----------------
@@ -265,6 +276,6 @@
265276
filtered = nix.Utils.filter(obj, filter, val, ...
266277
'Block::multiTagsFiltered', @nix.MultiTag);
267278
end
279+
end
268280

269-
end;
270281
end

+nix/File.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@
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+
sec = nix.Utils.find(obj, ...
124+
max_depth, filter, val, 'File::findSections', @nix.Section);
125+
end
115126
end
116127

117128
end

+nix/Section.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@
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
118+
119+
% maxdepth is an index
120+
function sec = find_sections(obj, max_depth)
121+
sec = obj.find_filtered_sections(max_depth, nix.Filter.accept_all, '');
122+
end
123+
124+
% maxdepth is an index
125+
function sec = find_filtered_sections(obj, max_depth, filter, val)
126+
sec = nix.Utils.find(obj, ...
127+
max_depth, filter, val, 'Section::findSections', @nix.Section);
128+
end
109129

110130
% ----------------
111131
% Property methods

+nix/Source.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,18 @@
7878
filtered = nix.Utils.filter(obj, filter, val, ...
7979
'Source::sourcesFiltered', @nix.Source);
8080
end
81+
82+
% maxdepth is an index where idx = 0 corresponds to the calling
83+
% source
84+
function sec = find_sources(obj, max_depth)
85+
sec = obj.find_filtered_sources(max_depth, nix.Filter.accept_all, '');
86+
end
87+
88+
% maxdepth is an index where idx = 0 corresponds to the calling
89+
% source
90+
function sec = find_filtered_sources(obj, max_depth, filter, val)
91+
sec = nix.Utils.find(obj, ...
92+
max_depth, filter, val, 'Source::findSources', @nix.Source);
93+
end
8194
end;
8295
end

+nix/Utils.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,29 @@
114114
end;
115115
end
116116

117+
% -----------------------------------------------------------
118+
% findXXX helper functions
119+
% -----------------------------------------------------------
120+
121+
function currData = find(obj, max_depth, filter, val, ...
122+
mxMethod, objConstructor)
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+
currList = nix_mx(mxMethod, ...
133+
obj.nix_handle, max_depth, uint8(filter), val);
134+
135+
currData = cell(length(currList), 1);
136+
for i = 1:length(currList)
137+
currData{i} = objConstructor(currList{i});
138+
end;
139+
end
140+
117141
end;
118142
end

nix_mx.cc

Lines changed: 5 additions & 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)
@@ -173,6 +174,7 @@ void mexFunction(int nlhs,
173174
methods->add("Block::tagsFiltered", nixblock::tagsFiltered);
174175
methods->add("Block::multiTagsFiltered", nixblock::multiTagsFiltered);
175176
methods->add("Block::dataArraysFiltered", nixblock::dataArraysFiltered);
177+
methods->add("Block::findSources", nixblock::findSources);
176178

177179
classdef<nix::Group>("Group", methods)
178180
.desc(&nixgroup::describe)
@@ -285,6 +287,7 @@ void mexFunction(int nlhs,
285287
methods->add("Source::openSourceIdx", nixsource::openSourceIdx);
286288
methods->add("Source::compare", nixsource::compare);
287289
methods->add("Source::sourcesFiltered", nixsource::sourcesFiltered);
290+
methods->add("Source::findSources", nixsource::findSources);
288291

289292
classdef<nix::Tag>("Tag", methods)
290293
.desc(&nixtag::describe)
@@ -416,6 +419,8 @@ void mexFunction(int nlhs,
416419
methods->add("Section::compare", nixsection::compare);
417420
methods->add("Section::sectionsFiltered", nixsection::sectionsFiltered);
418421
methods->add("Section::propertiesFiltered", nixsection::propertiesFiltered);
422+
methods->add("Section::findSections", nixsection::findSections);
423+
methods->add("Section::findRelated", nixsection::findRelated);
419424

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

src/filters.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "mex.h"
1414
#include "datatypes.h"
1515

16+
// filter function templates
1617
template<typename T, typename FN>
1718
std::vector<T> filterFullEntity(const extractor &input, FN ff) {
1819
std::vector<T> res;
@@ -118,4 +119,67 @@ std::vector<T> filterEntity(const extractor &input, FN ff) {
118119
return res;
119120
}
120121

122+
// find filter function templates
123+
template<typename T, typename FN>
124+
std::vector<T> filterFullEntity(const extractor &input,
125+
uint8_t filter_id, size_t val_pos, size_t max_depth, FN ff) {
126+
std::vector<T> res;
127+
128+
switch (filter_id) {
129+
case switchFilter::AcceptAll:
130+
res = ff(nix::util::AcceptAll<T>(), max_depth);
131+
break;
132+
case switchFilter::Id:
133+
res = ff(nix::util::IdFilter<T>(input.str(val_pos)), max_depth);
134+
break;
135+
case switchFilter::Ids:
136+
// this will crash matlab, if its not a vector of strings...
137+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(val_pos)), max_depth);
138+
break;
139+
case switchFilter::Type:
140+
res = ff(nix::util::TypeFilter<T>(input.str(val_pos)), max_depth);
141+
break;
142+
case switchFilter::Name:
143+
res = ff(nix::util::NameFilter<T>(input.str(val_pos)), max_depth);
144+
break;
145+
case switchFilter::Metadata:
146+
res = ff(nix::util::MetadataFilter<T>(input.str(val_pos)), max_depth);
147+
break;
148+
case switchFilter::Source:
149+
res = ff(nix::util::SourceFilter<T>(input.str(val_pos)), max_depth);
150+
break;
151+
default: throw std::invalid_argument("unknown or unsupported filter");
152+
}
153+
154+
return res;
155+
}
156+
157+
template<typename T, typename FN>
158+
std::vector<T> filterNameTypeEntity(const extractor &input,
159+
uint8_t filter_id, size_t val_pos, size_t max_depth, FN ff) {
160+
std::vector<T> res;
161+
162+
switch (filter_id) {
163+
case switchFilter::AcceptAll:
164+
res = ff(nix::util::AcceptAll<T>(), max_depth);
165+
break;
166+
case switchFilter::Id:
167+
res = ff(nix::util::IdFilter<T>(input.str(val_pos)), max_depth);
168+
break;
169+
case switchFilter::Ids:
170+
// this will crash matlab, if its not a vector of strings...
171+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(val_pos)), max_depth);
172+
break;
173+
case switchFilter::Type:
174+
res = ff(nix::util::TypeFilter<T>(input.str(val_pos)), max_depth);
175+
break;
176+
case switchFilter::Name:
177+
res = ff(nix::util::NameFilter<T>(input.str(val_pos)), max_depth);
178+
break;
179+
default: throw std::invalid_argument("unknown or unsupported filter");
180+
}
181+
182+
return res;
183+
}
184+
121185
#endif

src/nixblock.cc

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

145+
void findSources(const extractor &input, infusor &output) {
146+
nix::Block currObj = input.entity<nix::Block>(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::Source> res = filterFullEntity<nix::Source>(input, filter_id, 4, max_depth,
151+
[currObj](const nix::util::Filter<nix::Source>::type &filter, size_t &md) {
152+
return currObj.findSources(filter, md);
153+
});
154+
155+
output.set(0, res);
156+
}
157+
145158
} // namespace nixblock

src/nixblock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace nixblock {
4343

4444
void dataArraysFiltered(const extractor &input, infusor &output);
4545

46+
void findSources(const extractor &input, infusor &output);
47+
4648
} // namespace nixblock
4749

4850
#endif

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

0 commit comments

Comments
 (0)