-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilter-predicate.h
231 lines (198 loc) · 5.6 KB
/
filter-predicate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef FILTER_PREDICATE_H_
#define FILTER_PREDICATE_H_
// This class provides a filter predicate tree to support filtering vectors
// with arbitrary function calls via the FilterVector() function. A sample use
// case:
//
// vector<TestObj*> test_objects;
// BooleanFilterPredicate<TestObj> bfp(TestObj::AliveWrapper);
// vector<TestObj*> filtered = bfp.FilterVector(test_objects);
// for (int i = 0; i < filtered.size(); ++i) {
// if (!filtered[i]->Alive()) {
// ERROR("All filtered items should have Alive() == true.");
// success = false;
// }
// }
#include <string>
#include <vector>
using std::string;
using std::vector;
// Filter Predicate
template <class T>
class FilterPredicate {
public:
FilterPredicate() { is_not_ = false; }
virtual ~FilterPredicate() {}
virtual bool ObjectPasses(T* t) = 0;
virtual vector<T*> FilterVector(const vector<T*>& list) {
vector<T*> out;
for (int i = 0; i < list.size(); ++i) {
if (ObjectPasses(list[i])) {
out.push_back(list[i]);
}
}
return out;
}
void SetIsNot(bool n) { is_not_ = n; }
protected:
bool is_not_;
};
template <class T>
class BooleanFilterPredicate : public FilterPredicate<T> {
public:
explicit BooleanFilterPredicate(bool (*bool_getter_function)(T*)) {
bool_getter_function_ = bool_getter_function;
}
virtual ~BooleanFilterPredicate() {}
bool ObjectPasses(T* t) {
if (this->is_not_) {
return !bool_getter_function_(t);
}
return bool_getter_function_(t);
}
private:
bool (*bool_getter_function_)(T*);
};
// Equality Filter Predicate
template <class T1, class T2>
class EqualityFilterPredicate : public FilterPredicate<T1> {
public:
EqualityFilterPredicate(T2 val, T2 (*value_getter_function)(T1*)) {
val_ = val;
value_getter_function_ = value_getter_function;
}
virtual ~EqualityFilterPredicate() {}
bool ObjectPasses(T1* t) {
if (this->is_not_) {
return value_getter_function_(t) != val_;
}
return value_getter_function_(t) == val_;
}
private:
T2 val_;
T2 (*value_getter_function_)(T1*);
};
// Greater Than Filter Predicate. Takes two types. The first type is the type
// in the list to be filtered. The second type is the type of value to be
// comparing against (usually int, float, etc.).
// The following example leaves only items > 11:
// vector<MyObj*> list;
// GTFilterPredicate<MyObj, int> gtfp(11, MyObj::StaticValueWrapper);
// vector<MyObj*> filtered_list = gtfp.FilterVector(list);
template <class T1, class T2>
class GTFilterPredicate : public FilterPredicate<T1> {
public:
GTFilterPredicate(T2 val, T2 (*value_getter_function)(T1*)) {
val_ = val;
value_getter_function_ = value_getter_function;
}
virtual ~GTFilterPredicate() {}
bool ObjectPasses(T1* t) {
if (this->is_not_) {
return value_getter_function_(t) <= val_;
}
return value_getter_function_(t) > val_;
}
private:
T2 val_;
T2 (*value_getter_function_)(T1*);
};
// Less Than Filter Predicate
template <class T1, class T2>
class LTFilterPredicate : public FilterPredicate<T1> {
public:
LTFilterPredicate(T2 val, T2 (*value_getter_function)(T1*)) {
val_ = val;
value_getter_function_ = value_getter_function;
}
virtual ~LTFilterPredicate() {}
bool ObjectPasses(T1* t) {
if (this->is_not_) {
return value_getter_function_(t) >= val_;
}
return value_getter_function_(t) < val_;
}
private:
T2 val_;
T2 (*value_getter_function_)(T1*);
};
// String Contains Filter Predicate
template <class T>
class StringContainsFilterPredicate : public FilterPredicate<T> {
public:
StringContainsFilterPredicate(string needle,
string (*text_getter_function)(T*)) {
needle_ = needle;
text_getter_function_ = text_getter_function;
}
virtual ~StringContainsFilterPredicate() {}
virtual bool ObjectPasses(T* t) {
const string text = text_getter_function_(t);
if (this->is_not_) {
return (text.find(needle_) == string::npos);
}
return (text.find(needle_) != string::npos);
}
private:
string needle_;
string (*text_getter_function_)(T*);
};
// AND Filter Predicate
template <class T>
class AndFilterPredicate : public FilterPredicate<T> {
public:
AndFilterPredicate() {}
~AndFilterPredicate() {
for (int i = 0; i < children_.size(); ++i) {
delete children_[i];
}
}
virtual bool ObjectPasses(T* t) {
for (int i = 0; i < children_.size(); ++i) {
if (!children_[i]->ObjectPasses(t)) {
return this->is_not_ ? true : false;
}
}
if (this->is_not_) return false;
return true;
}
virtual void AddChild(FilterPredicate<T>* f) { children_.push_back(f); }
virtual void Clear() {
for (int i = 0; i < children_.size(); ++i) {
delete children_[i];
}
children_.clear();
}
private:
vector<FilterPredicate<T>*> children_;
};
// OR Filter Predicate
template <class T>
class OrFilterPredicate : public FilterPredicate<T> {
public:
OrFilterPredicate() {}
~OrFilterPredicate() {
for (int i = 0; i < children_.size(); ++i) {
delete children_[i];
}
}
virtual bool ObjectPasses(T* t) {
for (int i = 0; i < children_.size(); ++i) {
if (children_[i]->ObjectPasses(t)) {
return this->is_not_ ? false : true;
}
}
if (this->is_not_) return true;
return false;
}
virtual void AddChild(FilterPredicate<T>* f) { children_.push_back(f); }
virtual void Clear() {
for (int i = 0; i < children_.size(); ++i) {
delete children_[i];
}
children_.clear();
}
private:
vector<FilterPredicate<T>*> children_;
};
#endif // FILTER_PREDICATE_H_