Skip to content

Commit 5ceaf83

Browse files
authored
Merge pull request #147 from mpsonntag/filter
Filter
2 parents ec039d3 + e035b14 commit 5ceaf83

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

+nix/Filter.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
classdef Filter < uint8
2+
%FILTER available nix custom filters
3+
4+
enumeration
5+
accept_all (0); % requires an empty value
6+
id (1);
7+
ids (2); % requires a cell array
8+
type (3);
9+
name (4);
10+
metadata (5);
11+
source (6);
12+
end
13+
14+
end

+nix/Utils.m

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@
8181
retObj = objConstructor(handle);
8282
end;
8383
end;
84-
84+
85+
% -----------------------------------------------------------
86+
% nix.Filter helper functions
87+
% -----------------------------------------------------------
88+
89+
function err = valid_filter(filter, val)
90+
err = '';
91+
if (~isa(filter, 'nix.Filter'))
92+
err = 'Valid nix.Filter required';
93+
return
94+
end
95+
96+
% Currently matlab will crash, if anything other than
97+
% a cell array is handed over to a nix.Filter.ids.
98+
if (filter == nix.Filter.ids && ~iscell(val))
99+
err = 'nix.Filter.ids requires a cell array';
100+
return
101+
end
102+
end
103+
104+
function currData = filter(obj, filter, val, mxMethod, objConstructor)
105+
valid = nix.Utils.valid_filter(filter, val);
106+
if(~isempty(valid))
107+
error(valid);
108+
end
109+
110+
currList = nix_mx(mxMethod, obj.nix_handle, uint8(filter), val);
111+
currData = cell(length(currList), 1);
112+
for i = 1:length(currList)
113+
currData{i} = objConstructor(currList{i});
114+
end;
115+
end
116+
85117
end;
86118
end

src/filters.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2017, German Neuroinformatics Node (G-Node)
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted under the terms of the BSD License. See
7+
// LICENSE file in the root of the Project.
8+
9+
#ifndef NIX_MX_FILTERS
10+
#define NIX_MX_FILTERS
11+
12+
#include <nix.hpp>
13+
#include "mex.h"
14+
#include "datatypes.h"
15+
16+
// filter template
17+
template<typename T, typename FN>
18+
std::vector<T> filterFileEntity(const extractor &input, FN ff) {
19+
std::vector<T> res;
20+
21+
switch (input.num<uint8_t>(2)) {
22+
case switchFilter::AcceptAll:
23+
res = ff(nix::util::AcceptAll<T>());
24+
break;
25+
case switchFilter::Id:
26+
res = ff(nix::util::IdFilter<T>(input.str(3)));
27+
break;
28+
case switchFilter::Ids:
29+
// this will crash matlab, if its not a vector of strings...
30+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(3)));
31+
break;
32+
case switchFilter::Type:
33+
res = ff(nix::util::TypeFilter<T>(input.str(3)));
34+
break;
35+
case switchFilter::Name:
36+
res = ff(nix::util::NameFilter<T>(input.str(3)));
37+
break;
38+
default: throw std::invalid_argument("unknown or unsupported filter");
39+
}
40+
41+
return res;
42+
}
43+
44+
template<typename T, typename FN>
45+
std::vector<T> filterEntity(const extractor &input, FN ff) {
46+
std::vector<T> res;
47+
48+
switch (input.num<uint8_t>(2)) {
49+
case switchFilter::AcceptAll:
50+
res = ff(nix::util::AcceptAll<T>());
51+
break;
52+
case switchFilter::Id:
53+
res = ff(nix::util::IdFilter<T>(input.str(3)));
54+
break;
55+
case switchFilter::Ids:
56+
// this will crash matlab, if its not a vector of strings...
57+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(3)));
58+
break;
59+
case switchFilter::Type:
60+
res = ff(nix::util::TypeFilter<T>(input.str(3)));
61+
break;
62+
case switchFilter::Name:
63+
res = ff(nix::util::NameFilter<T>(input.str(3)));
64+
break;
65+
case switchFilter::Metadata:
66+
res = ff(nix::util::MetadataFilter<T>(input.str(3)));
67+
break;
68+
case switchFilter::Source:
69+
res = ff(nix::util::SourceFilter<T>(input.str(3)));
70+
break;
71+
default: throw std::invalid_argument("unknown or unsupported filter");
72+
}
73+
74+
return res;
75+
}
76+
77+
#endif

src/utils/datatypes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
#include <nix/DataType.hpp>
1313
#include <mex.h>
1414

15+
enum switchFilter : int8_t {
16+
AcceptAll,
17+
Id,
18+
Ids,
19+
Type,
20+
Name,
21+
Metadata,
22+
Source
23+
};
24+
1525
inline nix::DataType dtype_mex2nix(const mxArray *array) {
1626
mxClassID cid = mxGetClassID(array);
1727

0 commit comments

Comments
 (0)