32 # include <functional> 34 # include <initializer_list> 36 # include <ahFunctional.H> 43 # define Generic_Traverse(Type) \ 44 template <class Operation> \ 45 bool traverse(Operation & operation) const noexcept(noexcept(operation)) \ 47 for (Iterator it(*this); it.has_curr(); it.next_ne()) \ 48 if (not operation(it.get_curr_ne())) \ 53 template <class Operation> \ 54 bool traverse(Operation & operation) noexcept(noexcept(operation)) \ 56 for (Iterator it(*this); it.has_curr(); it.next_ne()) \ 57 if (not operation(it.get_curr_ne())) \ 62 template <class Operation> \ 63 bool traverse(Operation && operation = Operation()) const \ 64 noexcept(noexcept(operation)) \ 66 return traverse<Operation>(operation); \ 69 template <class Operation> \ 70 bool traverse(Operation && operation = Operation()) \ 71 noexcept(noexcept(operation)) \ 73 return traverse<Operation>(operation); \ 76 # define Special_Ctors(Set_Type, Type) \ 77 template <template <typename> class List> \ 78 Set_Type(const List<Type> & l) : Set_Type() \ 80 l.for_each([this] (const Type & item) { this->append(item); }); \ 84 Set_Type(It b, It e) : Set_Type() \ 86 for (It it = b; it != e; ++it) \ 90 Set_Type(std::initializer_list<Type> l) : Set_Type() \ 92 for (const auto & item : l) \ 97 # define Locate_Functions(Type) \ 98 Type & nth(const size_t n) const \ 100 Type * ptr = nullptr; \ 102 this->traverse([&ptr, &i, &n] (Type & item) \ 111 throw std::out_of_range("index out of range"); \ 116 Type & nth_ne(const size_t n) const noexcept \ 118 Type * ptr = nullptr; \ 120 this->traverse([&ptr, &i, &n] (Type & item) \ 130 template <class Operation> \ 131 Type * find_ptr(Operation & operation) noexcept(noexcept(operation)) \ 133 Type * ptr = nullptr; \ 134 this->traverse([&ptr,&operation] (Type & item) \ 136 if (operation(item)) \ 146 template <class Operation> \ 147 Type * find_ptr(Operation & operation) const noexcept(noexcept(operation)) \ 149 Type * ptr = nullptr; \ 150 this->traverse([&ptr, &operation] (Type & item) \ 152 if (operation(item)) \ 162 template <class Operation> \ 163 Type * find_ptr(Operation && operation = Operation()) const \ 164 noexcept(noexcept(operation)) \ 166 return find_ptr<Operation>(operation); \ 169 template <class Operation> \ 170 Type * find_ptr(Operation && operation = Operation()) \ 171 noexcept(noexcept(operation)) \ 173 return find_ptr<Operation>(operation); \ 176 template <class Operation> \ 177 std::tuple<bool, Type> find_item(Operation & operation) \ 178 noexcept(noexcept(operation)) \ 180 auto ptr = find_ptr(operation); \ 181 return ptr ? std::make_tuple(true, *ptr) : std::make_tuple(false, Type()); \ 184 template <class Operation> \ 185 std::tuple<bool, Type> find_item(Operation & operation) const \ 186 noexcept(noexcept(operation)) \ 188 auto ptr = find_ptr(operation); \ 189 return ptr ? std::make_tuple(true, *ptr) : std::make_tuple(false, Type()); \ 192 template <class Operation> \ 193 std::tuple<bool, Type> find_item(Operation && operation = Operation()) \ 194 noexcept(noexcept(operation)) \ 196 return find_item(operation); \ 199 template <class Operation> \ 200 std::tuple<bool, Type> find_item(Operation && operation = Operation()) const \ 201 noexcept(noexcept(operation)) \ 203 return find_item(operation); \ 206 # define Functional_Methods(Type) \ 207 template <class Operation> \ 208 auto for_each(Operation & operation) const \ 209 noexcept(noexcept(operation))-> decltype(*this) \ 211 this->traverse([&operation] (const Type & item) \ 219 template <class Operation> \ 220 auto for_each(Operation & operation) noexcept(noexcept(operation)) \ 223 this->traverse([&operation] (const Type & item) \ 231 template <class Operation> \ 232 auto for_each(Operation && operation = Operation()) const \ 233 noexcept(noexcept(operation)) -> decltype(*this) \ 235 return this->for_each<Operation>(operation); \ 238 template <class Operation> \ 239 auto for_each(Operation && operation = Operation()) \ 240 noexcept(noexcept(operation)) -> decltype(*this) \ 242 return this->for_each<Operation>(operation); \ 245 template <class Operation> \ 246 auto mutable_for_each(Operation & operation) \ 247 noexcept(noexcept(operation)) -> decltype(*this) \ 249 this->traverse([&operation] (Type & item) \ 257 template <class Operation> \ 258 auto mutable_for_each(Operation && operation = Operation()) \ 259 noexcept(noexcept(operation)) -> decltype(*this) \ 261 return this->mutable_for_each<Operation>(operation); \ 264 template <class Operation> \ 265 bool all(Operation & operation) const \ 266 noexcept(noexcept(operation)) \ 268 return this->template traverse<Operation>(operation); \ 271 template <class Operation> \ 272 bool all(Operation && operation = Operation()) const \ 273 noexcept(noexcept(operation)) \ 275 return all<Operation>(operation); \ 278 template <class Operation> \ 279 bool forall(Operation & operation) const \ 280 noexcept(noexcept(operation)) \ 282 return all<Operation>(operation); \ 285 template <class Operation> \ 286 bool forall(Operation && operation = Operation()) const \ 287 noexcept(noexcept(operation)) \ 289 return all<Operation>(operation); \ 292 template <class Operation> \ 293 bool exists(Operation & operation) const \ 294 noexcept(noexcept(operation)) \ 296 return not this->traverse([&operation] (const Type & item) \ 298 return not operation(item); \ 302 template <class Operation> \ 303 bool exists(Operation && operation = Operation()) const \ 304 noexcept(noexcept(operation)) \ 306 return exists<Operation>(operation); \ 309 template <typename __Type = Type, \ 310 template <typename> class Container = Aleph::DynList, \ 311 class Operation = Dft_Map_Op<Type, __Type>> \ 312 Container<__Type> maps(Operation & operation) const \ 314 Container<__Type> ret_val; \ 315 this->for_each([&ret_val, &operation] (const Type & item) \ 317 ret_val.append(operation(item)); \ 322 template < typename __Type = Type, \ 323 template <typename> class Container = Aleph::DynList, \ 324 class Operation = Dft_Map_Op<__Type, __Type>> \ 325 Container<__Type> maps(Operation && operation = Operation()) const \ 327 return maps<__Type, Container, Operation>(operation); \ 330 template <typename __Type = Type> \ 331 __Type foldl(const __Type & init, \ 332 std::function<__Type(const __Type&, const Type &)> operation) const \ 333 noexcept(noexcept(operation)) \ 335 auto ret_val = init; \ 336 this->for_each([&ret_val, &operation] (const Type & item) \ 338 ret_val = operation(ret_val, item); \ 343 template <class Operation> \ 344 Type fold(const Type & init, Operation & operation) const \ 345 noexcept(noexcept(operation)) \ 347 auto ret_val = init; \ 348 this->for_each([&ret_val, &operation] (const Type & item) \ 350 ret_val = operation(ret_val, item); \ 355 template <class Operation> \ 356 Type fold(const Type & init, Operation && operation = Operation()) const \ 357 noexcept(noexcept(operation)) \ 359 return fold<Operation>(init, operation); \ 362 template <class Operation> \ 363 DynList<Type> filter(Operation & operation) const \ 365 DynList<Type> ret_val; \ 366 this->for_each([&ret_val, &operation] (const Type & item) \ 368 if (operation(item)) \ 369 ret_val.append(item); \ 374 template <class Operation> \ 375 DynList<Type> filter(Operation && operation = Operation()) const \ 377 return filter<Operation>(operation); \ 380 template <class Operation> \ 381 DynList<std::tuple<Type, size_t>> pfilter(Operation & operation) const \ 383 DynList<std::tuple<Type, size_t>> ret_val; \ 385 this->for_each([&ret_val, &operation, &i] (const Type & item) \ 387 if (operation(item)) \ 388 ret_val.append(std::make_tuple(item, i)); \ 394 template <class Operation> \ 395 DynList<std::tuple<Type, size_t>> \ 396 pfilter(Operation && operation = Operation()) const \ 398 return pfilter<Operation>(operation); \ 401 template <class Operation> \ 402 std::pair<DynList<Type>, DynList<Type>> partition(Operation & op) const \ 404 std::pair<DynList<Type>, DynList<Type>> ret_val; \ 405 this->for_each([&ret_val, &op] (const Type & item) \ 408 ret_val.first.append(item); \ 410 ret_val.second.append(item); \ 415 template <class Operation> \ 416 std::pair<DynList<Type>, DynList<Type>> \ 417 partition(Operation && op = Operation()) const \ 419 return partition<Operation>(op); \ 422 template <class Operation> \ 423 std::tuple<DynList<Type>, DynList<Type>> tpartition(Operation & op) const \ 425 DynList<Type> r1, r2; \ 426 this->for_each([&r1, &r2, &op] (const Type & item) \ 433 return std::make_tuple(r1, r2); \ 436 template <class Operation> \ 437 std::tuple<DynList<Type>, DynList<Type>> \ 438 tpartition(Operation && op = Operation()) const \ 440 return partition<Operation>(op); \ 443 size_t length() const noexcept \ 446 this->for_each([&count] (const Type &) { ++count; }); \ 450 template <template <typename> class Container = Aleph::DynList> \ 451 Container<Type> rev() const \ 453 Container<Type> ret_val; \ 454 for_each([&ret_val] (const Type & item) \ 456 ret_val.insert(item); \ 461 template <template <typename> class Container = Aleph::DynList> \ 462 Container<Type> take(const size_t n) const \ 465 Container<Type> ret; \ 466 this->traverse([&i, &ret, n] (const Type & item) \ 476 template <template <typename> class Container = Aleph::DynList> \ 477 Container<Type> drop(const size_t n) const \ 480 Container<Type> ret; \ 481 this->traverse([&i, &ret, n] (const Type & item) \ 490 # define Generic_Keys(Type) \ 491 template <template <typename> class Container = DynList> \ 492 Container<Type> keys() const \ 494 return this->template maps<Type, Container>([] (const Type & key) \ 498 # define Generic_Items(Type) \ 499 template <template <typename> class Container = DynList> \ 500 Container<Type> items() const \ 502 return this->template maps<Type, Container> ([] (const Type & key) \ 507 # define Equal_To_Method(class_name) \ 508 bool equal_to(const class_name & r) const noexcept \ 513 if (this->size() != r.size()) \ 516 return this->all([&r] (const Key & k) \ 517 { return r.search(k) != nullptr; }); \ 520 bool operator == (const class_name & r) const noexcept \ 522 return equal_to(r); \ 525 bool operator != (const class_name & r) const noexcept \ 527 return not equal_to(r); \ 531 # define Map_Sequences_Methods() \ 532 template <template <typename> class Container = ::DynList> \ 533 Container<Key> keys() const \ 535 Container<Key> ret_val; \ 536 this->for_each([&ret_val] (const std::pair<Key, Data> & p) \ 538 ret_val.append(p.first); \ 543 template <template <typename> class Container = ::DynList> \ 544 Container<Data> values() const \ 546 Container<Data> ret_val; \ 547 this->for_each([&ret_val] (const std::pair<Key, Data> & p) \ 549 ret_val.append(p.second); \ 554 template <template <typename> class Container = ::DynList> \ 555 Container<Data*> values_ptr() \ 557 Container<Data*> ret_val; \ 558 this->for_each([&ret_val] (std::pair<Key, Data> & p) \ 560 ret_val.append(&p.second); \ 565 template <template <typename> class Container = ::DynList> \ 566 Container<std::pair<Key, Data>> items() const \ 568 return this->Base::keys(); \ 571 template <template <typename> class Container = ::DynList> \ 572 Container<std::pair<Key, Data*>> items_ptr() const \ 574 Container<std::pair<Key, Data*>> ret_val; \ 575 this->for_each([&ret_val] (std::pair<Key, Data> & p) \ 577 ret_val.append(std::pair<Key,Data*>(p.first, p.second)); \ 582 Data & operator () (const Key & key) \ 584 return this->find(key); \ 587 const Data & operator () (const Key & key) const \ 589 return this->find(key); \ 592 # define Generate_Proxy_Operator(Class_Name) \ 593 const Data & operator [] (const Key & key) const \ 598 Data & operator [] (const Key & key) \ 603 template <
typename Type>
inline 604 std::string to_str(
const Type & d)
606 std::ostringstream os;
614 template <
typename Key,
typename Data,
class Cmp = std::equal_to<Key>>
618 Dft_Pair_Cmp(Cmp __cmp = Cmp()) : cmp(__cmp) {}
619 bool operator () (
const std::pair<Key, Data> & p1,
620 const std::pair<Key, Data> & p2)
const noexcept
622 return cmp (p1.first, p2.first);
626 template <
typename Key,
typename Data>
627 std::pair<Key, Data> * key_to_pair(Key * ptr)
629 return (std::pair<Key, Data>*) ptr;
632 template <
typename Key,
typename Data>
633 std::pair<Key, Data> * data_to_pair(Data * ptr)
635 std::pair<Key, Data> * zero = 0;
636 return (std::pair<Key, Data>*) ((long) ptr - (
long) &zero->second);