8 # include <ahFunctional.H>
15 # define Generic_Traverse(Type) \
16 template <class Operation> \
17 bool traverse(Operation & operation) const \
19 for (Iterator it(*this); it.has_curr(); it.next()) \
20 if (not operation(it.get_curr())) \
25 template <class Operation> \
26 bool traverse(Operation & operation) \
28 for (Iterator it(*this); it.has_curr(); it.next()) \
29 if (not operation(it.get_curr())) \
34 template <class Operation> \
35 bool traverse(Operation && operation = Operation()) const \
37 return traverse<Operation>(operation); \
40 template <class Operation> \
41 bool traverse(Operation && operation = Operation()) \
43 return traverse<Operation>(operation); \
46 # define Functional_Methods(Type) \
47 template <class Operation> \
48 void for_each(Operation & operation) const \
50 this->traverse([&operation] (const Type & item) \
57 template <class Operation> \
58 void for_each(Operation & operation) \
60 this->traverse([&operation] (const Type & item) \
67 template <class Operation> \
68 void for_each(Operation && operation = Operation()) const \
70 this->for_each<Operation>(operation); \
73 template <class Operation> \
74 void for_each(Operation && operation = Operation()) \
76 this->for_each<Operation>(operation); \
79 template <class Operation> \
80 bool all(Operation & operation) const \
82 return this->traverse<Operation>(operation); \
85 template <class Operation> \
86 bool all(Operation & operation) \
88 return this->traverse<Operation>(operation); \
91 template <class Operation> \
92 bool all(Operation && operation = Operation()) const \
94 return all<Operation>(operation); \
97 template <class Operation> \
98 bool all(Operation && operation = Operation()) \
100 return all<Operation>(operation); \
103 template <class Operation> \
104 bool forall(Operation & operation) const \
106 return all<Operation>(operation); \
109 template <class Operation> \
110 bool forall(Operation & operation) \
112 return all<Operation>(operation); \
115 template <class Operation> \
116 bool forall(Operation && operation = Operation()) const \
118 return all<Operation>(operation); \
121 template <class Operation> \
122 bool forall(Operation && operation = Operation()) \
124 return all<Operation>(operation); \
127 template <class Operation> \
128 bool exists(Operation & operation) const \
130 return not this->traverse([&operation] (const Type & item) \
132 return not operation(item); \
136 template <class Operation> \
137 bool exists(Operation & operation) \
139 return not this->traverse([&operation] (const Type & item) \
141 return not operation(item); \
145 template <class Operation> \
146 bool exists(Operation && operation = Operation()) const \
148 return exists<Operation>(operation); \
151 template <class Operation> \
152 bool exists(Operation && operation = Operation()) \
154 return exists<Operation>(operation); \
157 template <class Operation> \
158 Type * find_ptr(Operation & operation) \
161 this->traverse([&ptr,&operation] (Type & item) \
163 if (operation(item)) \
173 template <class Operation> \
174 Type * find_ptr(Operation & operation) const \
177 this->traverse([&ptr,&operation] (Type & item) \
179 if (operation(item)) \
189 template <class Operation> \
190 Type * find_ptr(Operation && operation = Operation()) const \
192 return find_ptr<Operation>(operation); \
195 template <class Operation> \
196 Type * find_ptr(Operation && operation = Operation()) \
198 return find_ptr<Operation>(operation); \
201 template <typename __T = Type, \
202 template <typename> class Container = Aleph::DynList, \
203 class Operation = Dft_Map_Op<Type, __T>> \
204 Container<__T> map(Operation & operation) const \
206 Container<__T> ret_val; \
207 this->for_each([&ret_val, &operation] (const Type & item) \
209 ret_val.append(operation(item)); \
214 template < typename __T = Type, \
215 template <typename> class Container = Aleph::DynList, \
216 class Operation = Dft_Map_Op<__T, __T>> \
217 Container<__T> map(Operation && operation = Operation()) const \
219 return map<__T, Container, Operation>(operation); \
223 template <typename __T = Type> \
224 __T foldl(const __T & init, \
225 std::function<__T(const __T&, const Type &)> operation) const \
227 __T ret_val = init; \
228 this->for_each([&ret_val, &operation] (const Type & item) \
230 ret_val = operation(ret_val, item); \
235 template <class Operation> \
236 DynList<Type> filter(Operation & operation) const \
238 DynList<Type> ret_val; \
239 this->for_each([&ret_val, &operation] (const Type & item) \
241 if (operation(item)) \
242 ret_val.append(item); \
247 template <class Operation> \
248 DynList<Type> filter(Operation & operation) \
250 DynList<Type> ret_val; \
251 this->for_each([&ret_val, &operation] (const Type & item) \
253 if (operation(item)) \
254 ret_val.append(item); \
259 template <class Operation> \
260 DynList<Type> filter(Operation && operation = Operation()) const \
262 return filter<Operation>(operation); \
265 template <class Operation> \
266 DynList<Type> filter(Operation && operation = Operation()) \
268 return filter<Operation>(operation); \
271 template <class Operation> \
272 std::pair<DynList<Type>, DynList<Type>> partition(Operation & op) const \
274 std::pair<DynList<Type>, DynList<Type>> ret_val; \
275 this->for_each([&ret_val, &op] (const Type & item) \
278 ret_val.first.append(item); \
280 ret_val.second.append(item); \
285 template <class Operation> \
286 std::pair<DynList<Type>, DynList<Type>> partition(Operation & op) \
288 std::pair<DynList<Type>, DynList<Type>> ret_val; \
289 this->for_each([&ret_val, &op] (const Type & item) \
292 ret_val.first.append(item); \
294 ret_val.second.append(item); \
299 template <class Operation> \
300 std::pair<DynList<Type>, DynList<Type>> \
301 partition(Operation && op = Operation()) const \
303 return partition<Operation>(op); \
306 template <class Operation> \
307 std::pair<DynList<Type>, DynList<Type>> \
308 partition(Operation && op = Operation()) \
310 return partition<Operation>(op); \
313 size_t length() const \
316 this->for_each([&count] (const Type &) { ++count; }); \
320 Type & nth(const size_t n) const \
324 this->traverse([&ptr, &i, &n] (Type & item) \
333 throw std::out_of_range("nth"); \
338 template <template <typename> class Container = Aleph::DynList> \
339 Container<Type> rev() const \
341 Container<Type> ret_val; \
342 for_each([&ret_val] (const Type & item) \
344 ret_val.insert(item); \
350 # define Generic_Keys(Type) \
351 template <template <typename> class Container = DynList> \
352 Container<Type> keys() const \
354 return this->template map<Type, Container>([] (const Type & key) \
358 # define Generic_Items(Type) \
359 template <template <typename> class Container = DynList> \
360 Container<Type> items() const \
362 return this->template map<Type, Container> ([] (const Type & key) \
367 # define Equal_To_Method(class_name) \
368 bool equal_to(const class_name & r) const \
373 if (this->size() != r.size()) \
376 return this->all( [&r] (const Key & k) \
377 { return r.search(k) != NULL; }); \
380 bool operator == (const class_name & r) const \
382 return equal_to(r); \
385 bool operator != (const class_name & r) const \
387 return not equal_to(r); \
391 # define Map_Sequences_Methods() \
392 template <template <typename> class Container = ::DynList> \
393 Container<Key> keys() const \
395 Container<Key> ret_val; \
396 this->for_each([&ret_val] (const std::pair<Key, Data> & p) \
398 ret_val.append(p.first); \
403 template <template <typename> class Container = ::DynList> \
404 Container<Data> values() const \
406 Container<Data> ret_val; \
407 this->for_each([&ret_val] (const std::pair<Key, Data> & p) \
409 ret_val.append(p.second); \
414 template <template <typename> class Container = ::DynList> \
415 Container<Data*> values_ptr() const \
417 Container<Data*> ret_val; \
418 this->for_each([&ret_val] (std::pair<Key, Data> & p) \
420 ret_val.append(&p.second); \
425 template <template <typename> class Container = ::DynList> \
426 Container<std::pair<Key, Data>> items() const \
428 return this->Base::keys(); \
431 template <template <typename> class Container = ::DynList> \
432 Container<std::pair<Key, Data*>> items_ptr() const \
434 Container<Data> ret_val; \
435 this->for_each([&ret_val] (std::pair<Key, Data> & p) \
437 ret_val.append(std::pair<Key,Data*>(p.first, p.second)); \
442 Data & operator () (const Key & key) \
444 return this->find(key); \
447 # define Generate_Proxy_Operator(Class_Name) \
450 Class_Name & container; \
456 Proxy(Class_Name & c, const Key &_key) : container(c), key(_key) \
458 data_ptr = container.search(key); \
461 Proxy & operator = (const Data & data) \
463 if (data_ptr == NULL) \
464 container.insert(key, data); \
471 Proxy & operator = (const Proxy & proxy) \
473 if (this == &proxy) \
476 if (proxy.data_ptr == NULL) \
477 throw std::domain_error("key not found"); \
479 if (&container == &proxy.tree and key == proxy.key) \
482 if (data_ptr == NULL) \
483 container.insert(key, *proxy.data_ptr); \
485 *data_ptr = proxy.data_ptr; \
490 operator Data & () const \
492 if (data_ptr == NULL) \
493 throw std::domain_error("key not found"); \
499 Proxy operator [] (const Key & key) const \
500 Exception_Prototypes(std::domain_error) \
502 return Proxy(*this, key); \
505 Proxy operator [] (const Key & key) \
506 Exception_Prototypes(std::domain_error) \
508 return Proxy(*this, key); \
511 template <
typename Type>
inline
512 std::string to_str(
const Type & d)
514 std::ostringstream os;
522 template <
typename Key,
typename Data,
class Cmp = std::equal_to<Key>>
525 bool operator () (
const std::pair<Key, Data> & p1,
526 const std::pair<Key, Data> & p2)
const
528 return Cmp () (p1.first, p2.first);
532 template <
typename Key,
typename Data>
533 std::pair<Key, Data> * key_to_pair(Key * ptr)
535 return (std::pair<Key, Data>*) ptr;
538 template <
typename Key,
typename Data>
539 std::pair<Key, Data> * data_to_pair(Data * ptr)
541 std::pair<Key, Data> * zero = 0;
542 return (std::pair<Key, Data>*) ((long) ptr - (
long) &zero->second);