Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-dry.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef AH_DRY_H
45# define AH_DRY_H
46
47# include "ahFunctional.H"
48# include <utility>
49# include <vector>
50# include <ah-errors.H>
51
52namespace Aleph { template <class> class Array; }
53
65template <class Container>
67{
68private:
69 template <class Operation>
70 static constexpr bool traverse_is_noexcept() noexcept
71 {
72 return noexcept(std::declval<Operation&>()(
73 std::declval<typename Container::Item_Type&>()));
74 }
75
76public:
96 template <class Operation>
97 bool traverse(Operation & operation) noexcept(traverse_is_noexcept<Operation>())
98 {
99 for (typename Container::Iterator it(*static_cast<Container*>(this));
100 it.has_curr(); it.next())
101 if (not operation(it.get_curr()))
102 return false;
103 return true;
104 }
105
107 template <class Operation>
108 bool traverse(Operation & operation) const noexcept(traverse_is_noexcept<Operation>())
109 {
110 return const_cast<GenericTraverse*>(this)->traverse<Operation>(operation);
111 }
112
116 template <class Operation>
117 bool traverse(Operation && operation) const noexcept(traverse_is_noexcept<Operation>())
118 {
119 return traverse<Operation>(operation);
120 }
121
125 template <class Operation>
126 bool traverse(Operation && operation) noexcept(traverse_is_noexcept<Operation>())
127 {
128 return traverse<Operation>(operation);
129 }
130};
131
144template <class Container, class Operation>
145bool traverse(const Container & c, Operation & op)
146 noexcept(noexcept(c.traverse(op)))
147{
148 return c.traverse(op);
149}
150
165template <class Container, class Operation>
166bool traverse(const Container & c, Operation && op)
167 noexcept(noexcept(c.traverse(op)))
168{
169 return c.traverse(op);
170}
171
194template <class Container, typename Type>
196{
197 Container * me() noexcept { return static_cast<Container*>(this); }
198
199 const Container * const_me() const noexcept
200 {
201 return static_cast<const Container*>(this);
202 }
203
205 {
206 return const_cast<LocateFunctions*>(this);
207 }
208
209 template <class Operation>
210 static constexpr bool operation_is_noexcept() noexcept
211 {
212 return noexcept(std::declval<Operation&>()(std::declval<Type&>()));
213 }
214
215public:
216
222 auto get_it() const
223 {
224 return typename Container::Iterator(*const_me());
225 }
226
227 // auto get_it()
228 // {
229 // return typename Container::Iterator(*me());
230 // }
231
238 auto get_it(const size_t pos) const
239 {
240 auto ret = typename Container::Iterator(*const_me());
241 for (size_t i = 0; i < pos; ++i)
242 ret.next();
243 return ret;
244 }
245
250 auto get_itor() const { return get_it(); }
251
258 Type & nth_ne(const size_t n) noexcept
259 {
260 Type * ptr = nullptr;
261 size_t i = 0;
262 me()->traverse([&ptr, &i, &n] (Type & item)
263 {
264 if (i++ < n)
265 return true;
266 ptr = &item;
267 return false;
268 });
269 return *ptr;
270 }
271
278 const Type & nth_ne(const size_t n) const noexcept
279 {
280 return base()->nth_ne(n);
281 }
282
299 Type & nth(const size_t n)
300 {
301 Type * ptr = nullptr;
302 size_t i = 0;
303 me()->traverse([&ptr, &i, &n] (Type & item)
304 {
305 if (i++ < n)
306 return true;
307 ptr = &item;
308 return false;
309 });
310
311 ah_out_of_range_error_if(i != n + 1) << "index out of range";
312
313 return *ptr;
314 }
315
322 const Type & nth(const size_t n) const
323 {
324 return base()->nth(n);
325 }
326
348 template <class Operation>
349 Type * find_ptr(Operation & operation)
350 noexcept(operation_is_noexcept<Operation>())
351 {
352 Type * ptr = nullptr;
353 me()->traverse([&ptr,&operation] (Type & item)
354 {
355 if (operation(item))
356 {
357 ptr = &item;
358 return false;
359 }
360 return true;
361 });
362 return ptr;
363 }
364
370 template <class Operation>
371 const Type * find_ptr(Operation & operation) const
372 noexcept(operation_is_noexcept<Operation>())
373 {
374 return base()->find_ptr(operation);
375 }
376
388 template <class Operation>
389 const Type * find_ptr(Operation && operation) const
390 noexcept(operation_is_noexcept<Operation>())
391 {
392 return find_ptr<Operation>(operation);
393 }
394
406 template <class Operation>
407 Type * find_ptr(Operation && operation)
408 noexcept(operation_is_noexcept<Operation>())
409 {
410 return find_ptr<Operation>(operation);
411 }
412
435 template <class Operation>
436 size_t find_index(Operation & operation) const
437 noexcept(operation_is_noexcept<Operation>())
438 {
439 size_t i = 0;
440 const_me()->traverse([&i,&operation] (Type & item)
441 {
442 if (operation(item))
443 return false;
444 ++i;
445 return true;
446 });
447 return i;
448 }
449
461 template <class Operation>
462 size_t find_index(Operation && operation) const
463 noexcept(operation_is_noexcept<Operation>())
464 {
465 return find_index<Operation>(operation);
466 }
467
488 template <class Operation>
489 std::tuple<bool, Type> find_item(Operation & operation)
490 noexcept(operation_is_noexcept<Operation>())
491 {
492 using TT = std::tuple<bool, Type>;
493 auto ptr = find_ptr(operation);
494 return ptr ? TT(true, *ptr) : TT(false, Type());
495 }
496
498 template <class Operation>
499 std::tuple<bool, Type> find_item(Operation & operation) const
500 noexcept(operation_is_noexcept<Operation>())
501 {
502 using TT = std::tuple<bool, Type>;
503 auto ptr = find_ptr(operation);
504 return ptr ? TT(true, *ptr) : TT(false, Type());
505 }
506
518 template <class Operation>
519 std::tuple<bool, Type> find_item(Operation && operation)
520 noexcept(operation_is_noexcept<Operation>())
521 {
522 return find_item<Operation>(operation);
523 }
524
536 template <class Operation>
537 std::tuple<bool, Type> find_item(Operation && operation) const
538 noexcept(operation_is_noexcept<Operation>())
539 {
540 return find_item<Operation>(operation);
541 }
542
550 template <class Operation>
551 bool contains_if(Operation && operation) const
552 noexcept(operation_is_noexcept<Operation>())
553 {
554 return find_ptr(operation) != nullptr;
555 }
556
563 bool contains(const Type & item) const
564 {
565 return contains_if([&item] (const Type & x) { return x == item; });
566 }
567};
568
584template <class Container, typename T>
586{
587 SpecialCtors() = default;
588
589 SpecialCtors(const SpecialCtors&) = default;
590
592
594
595 SpecialCtors & operator = (SpecialCtors&&) noexcept { return *this; }
596
602 {
603 l.for_each([this] (const T & item)
604 {
605 static_cast<Container*>(this)->append(item);
606 });
607 }
608
614 template <class It>
616 {
617 for (It it = b; it != e; ++it)
618 static_cast<Container*>(this)->append(*it);
619 }
620
625 SpecialCtors(std::initializer_list<T> l)
626 {
627 for (const auto & item : l)
628 static_cast<Container*>(this)->append(item);
629 }
630};
631
632
640template <class Container, typename T>
642{
643 Container * me() { return static_cast<Container*>(this); }
644
646 {
647 return const_cast<FunctionalMethods<Container, T>*>(this);
648 }
649
650 const Container * const_me() const noexcept
651 {
652 return static_cast<const Container*>(this);
653 }
654
655public:
656
675 template <typename ...Args>
676 void emplace(Args && ... args)
677 {
678 (void) me()->append(T(std::forward<Args>(args)...));
679 }
680
682 template <typename ...Args>
683 void emplace_end(Args && ... args)
684 {
685 (void) me()->append(T(std::forward<Args>(args)...));
686 }
687
706 template <typename ...Args>
707 void emplace_ins(Args && ... args)
708 {
709 (void) me()->insert(T(std::forward<Args>(args)...));
710 }
711
712private:
713
714 void nninsert(size_t&) {}
715 void nnappend(size_t&) {}
716
717 template <typename ... Args>
718 void nninsert(size_t & n, const T & item, Args & ... args)
719 {
720 (void) me()->insert(item);
721 ++n;
722 nninsert(n, args...);
723 }
724
725 template <typename ... Args>
726 void nnappend(size_t & n, const T & item, Args & ... args)
727 {
728 (void) me()->append(item);
729 ++n;
730 nnappend(n, args...);
731 }
732
733public:
734
740 template <typename ... Args>
741 size_t ninsert(Args ... args)
742 {
743 size_t n = 0;
744 nninsert(n, args...);
745 return n;
746 }
747
753 template <typename ... Args>
754 size_t nappend(Args ... args)
755 {
756 size_t n = 0;
757 nnappend(n, args...);
758 return n;
759 }
760
778 template <class Operation>
779 void for_each(Operation & operation)
780 {
781 me()->traverse([&operation] (const T & item)
782 {
783 operation(item);
784 return true;
785 });
786 }
787
789 template <class Operation>
790 void for_each(Operation & operation) const
791 {
792 base()->for_each(operation);
793 }
794
805 template <class Operation>
806 void for_each(Operation && operation) const
807 {
808 for_each<Operation>(operation);
809 }
810
821 template <class Operation>
822 void for_each(Operation && operation)
823 {
824 for_each<Operation>(operation);
825 }
826
828 template <class Operation>
829 void each(Operation & operation)
830 {
831 for_each(operation);
832 }
833
835 template <class Operation>
836 void each(Operation & operation) const
837 {
838 for_each(operation);
839 }
840
851 template <class Operation>
852 void each(Operation && operation) const
853 {
854 for_each<Operation>(operation);
855 }
856
867 template <class Operation>
868 void each(Operation && operation)
869 {
870 for_each<Operation>(operation);
871 }
872
893 template <class Operation>
894 void each(size_t pos, const size_t slice, Operation & operation) const
895 {
896 if (slice == 0)
897 return;
898 auto it = const_me()->get_it(pos);
899 while (true)
900 {
901 operation(it.get_curr());
902 for (size_t k = 0; k < slice; ++k)
903 {
904 it.next();
905 if (not it.has_curr())
906 return;
907 }
908 }
909 }
910
912 template <class Operation>
913 void each(const size_t pos, const size_t slice, Operation && operation) const
914 {
915 each<Operation>(pos, slice, operation);
916 }
917
927 template <class Operation>
928 void mutable_for_each(Operation & operation)
929 {
930 me()->traverse([&operation] (T & item)
931 {
932 operation(item);
933 return true;
934 });
935 }
936
938 template <class Operation>
939 void mutable_for_each(Operation && operation)
940 {
941 mutable_for_each<Operation>(operation);
942 }
943
956 template <class Operation>
957 bool all(Operation & operation) const
958 {
959 return const_me()->traverse(operation);
960 }
961
973 template <class Operation>
974 bool all(Operation && operation) const
975 {
976 return all<Operation>(operation);
977 }
978
992 template <class Operation>
993 bool exists(Operation & op) const
994 {
995 return not const_me()->
996 traverse([&op] (const T & i) { return not op(i); });
997 }
998
1010 template <class Operation>
1011 bool exists(Operation && op) const
1012 {
1013 return exists<Operation>(op);
1014 }
1015
1056 template <typename __T = T, class Operation = Aleph::Dft_Map_Op<T, __T>>
1057 Aleph::DynList<__T> maps(Operation & op) const
1058 {
1059 Aleph::DynList<__T> ret_val;
1060 const_me()->for_each([&ret_val, &op] (const T & item)
1061 {
1062 ret_val.append(op(item));
1063 });
1064 return ret_val;
1065 }
1066
1079 template <typename __T = T, class Operation = Aleph::Dft_Map_Op<__T, __T>>
1080 Aleph::DynList<__T> maps(Operation && op) const { return maps<__T, Operation>(op); }
1081
1098 template <typename __T = T, class Prop, class Operation>
1099 Aleph::DynList<__T> maps_if(Prop prop, Operation & op) const
1100 {
1101 Aleph::DynList<__T> ret_val;
1102 const_me()->for_each([&ret_val, &prop, &op] (const T & item)
1103 {
1104 if (prop(item))
1105 ret_val.append(op(item));
1106 });
1107 return ret_val;
1108 }
1109
1124 template <typename __T = T, class Prop, class Operation>
1125 Aleph::DynList<__T> maps_if(Prop prop, Operation && op) const
1126 {
1127 return maps_if<__T, Prop, Operation>(prop, op);
1128 }
1129
1135 {
1136 return maps([] (auto & item) { return item; });
1137 }
1138
1143 std::vector<T> to_vector() const
1144 {
1145 std::vector<T> ret;
1146 if constexpr (requires { const_me()->size(); })
1147 ret.reserve(const_me()->size());
1148 const_me()->for_each([&ret] (const T & item) { ret.push_back(item); });
1149 return ret;
1150 }
1151
1199 template <typename __T = T, class Op = Aleph::Dft_Fold_Op<__T, T>>
1200 __T foldl(const __T & init, Op & op) const
1201 {
1202 __T ret_val = init;
1203 const_me()->for_each([&ret_val, &op] (const T & item)
1204 {
1205 ret_val = op(ret_val, item);
1206 });
1207 return ret_val;
1208 }
1209
1223 template <typename __T = T, class Op = Aleph::Dft_Fold_Op<__T, T>>
1224 __T foldl(const __T & init, Op && op = Op()) const
1225 {
1226 return foldl<__T, Op>(init, op);
1227 }
1228
1236 template <typename __T = T, class Op = Aleph::Dft_Fold_Op<__T, T>>
1237 __T fold_left(const __T & init, Op & op) const
1238 {
1239 return const_me()->template foldl<__T>(init, op);
1240 }
1241
1255 template <typename __T = T, class Op = Aleph::Dft_Fold_Op<__T, T>>
1256 __T fold_left(const __T & init, Op && op = Op()) const
1257 {
1258 return const_me()->template foldl<__T>(init, op);
1259 }
1260
1266 template <class Operation>
1267 T fold(const T & init, Operation & operation) const
1268 {
1269 auto ret_val = init;
1270 const_me()->for_each([&ret_val, &operation] (const T & item)
1271 {
1272 ret_val = operation(ret_val, item);
1273 });
1274 return ret_val;
1275 }
1276
1289 template <class Operation>
1290 T fold(const T & init, Operation && operation) const
1291 {
1292 return fold<Operation>(init, operation);
1293 }
1294
1318 template <class Operation>
1319 Aleph::DynList<T> filter(Operation & operation) const
1320 {
1321 Aleph::DynList<T> ret_val;
1322 const_me()->for_each([&ret_val, &operation] (const T & item)
1323 {
1324 if (operation(item))
1325 ret_val.append(item);
1326 });
1327 return ret_val;
1328 }
1329
1341 template <class Operation>
1342 Aleph::DynList<T> filter(Operation && operation) const
1343 {
1344 return filter<Operation>(operation);
1345 }
1346
1370 template <class Operation>
1371 Aleph::DynList<const T*> ptr_filter(Operation & operation) const
1372 {
1374 const_me()->for_each([&ret_val, &operation] (const T & item)
1375 {
1376 if (operation(item))
1377 ret_val.append(&item);
1378 });
1379 return ret_val;
1380 }
1381
1393 template <class Operation>
1394 Aleph::DynList<const T*> ptr_filter(Operation && operation) const
1395 {
1396 return ptr_filter<Operation>(operation);
1397 }
1398
1418 template <class Operation>
1419 Aleph::DynList<std::tuple<T, size_t>> pfilter(Operation & operation) const
1420 {
1421 using TT = std::tuple<T, size_t>;
1422 Aleph::DynList<TT> ret_val;
1423 size_t i = 0;
1424 const_me()->for_each([&ret_val, &operation, &i] (const T & item)
1425 {
1426 if (operation(item))
1427 ret_val.append(TT(item, i));
1428 ++i;
1429 });
1430 return ret_val;
1431 }
1432
1444 template <class Operation>
1445 Aleph::DynList<std::tuple<T, size_t>> pfilter(Operation && operation) const
1446 {
1447 return pfilter<Operation>(operation);
1448 }
1449
1467 template <class Operation>
1468 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> partition(Operation & op) const
1469 {
1470 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> ret_val;
1471 const_me()->for_each([&ret_val, &op] (const T & item)
1472 {
1473 if (op(item))
1474 ret_val.first.append(item);
1475 else
1476 ret_val.second.append(item);
1477 });
1478 return ret_val;
1479 }
1480
1492 template <class Operation>
1493 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> partition(Operation && op) const
1494 {
1495 return partition<Operation>(op);
1496 }
1497
1508 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> partition(size_t n) const
1509 {
1510 size_t i = 0;
1511 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> ret_val;
1512 const_me()->for_each([&ret_val, &i, n] (const T & item)
1513 {
1514 if (i++ < n)
1515 ret_val.first.append(item);
1516 else
1517 ret_val.second.append(item);
1518 });
1519 return ret_val;
1520 }
1521
1528 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> split_half() const
1529 {
1530 size_t i = 0;
1531 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> ret_val;
1532 const_me()->for_each([&ret_val, &i] (const T & item)
1533 {
1534 if (i % 2 == 0)
1535 ret_val.first.append(item);
1536 else
1537 ret_val.second.append(item);
1538 i++;
1539 });
1540 return ret_val;
1541 }
1542
1556 template <class Operation>
1557 std::tuple<Aleph::DynList<T>, Aleph::DynList<T>> tpartition(Operation & op) const
1558 {
1559 Aleph::DynList<T> r1, r2;
1560 const_me()->for_each([&r1, &r2, &op] (const T & item)
1561 {
1562 if (op(item))
1563 r1.append(item);
1564 else
1565 r2.append(item);
1566 });
1567 return std::tuple<Aleph::DynList<T>, Aleph::DynList<T>>(r1, r2);
1568 }
1569
1581 template <class Operation>
1582 std::tuple<Aleph::DynList<T>, Aleph::DynList<T>> tpartition(Operation && op) const
1583 {
1584 return tpartition<Operation>(op);
1585 }
1586
1598 [[nodiscard]] size_t length() const noexcept
1599 {
1600 size_t count = 0;
1601 const_me()->for_each([&count] (const T &) { ++count; });
1602 return count;
1603 }
1604
1613 {
1615 const_me()->for_each([&ret] (const T & i) { ret.insert(i); });
1616 return ret;
1617 }
1618
1631 Aleph::DynList<T> take(const size_t n) const
1632 {
1633 size_t i = 0;
1635 const_me()->traverse([&i, &ret, n] (const T & item)
1636 {
1637 if (i++ >= n)
1638 return false;
1639 ret.append(item);
1640 return true;
1641 });
1642 return ret;
1643 }
1644
1657 Aleph::DynList<T> take(size_t i, const size_t j, const size_t step = 1) const
1658 {
1660 if (step == 0)
1661 return ret;
1662 for (auto it = const_me()->get_it(i); i <= j and it.has_curr();
1663 it.next_ne(), i += step)
1664 ret.append(it.get_curr());
1665 return ret;
1666 }
1667
1680 Aleph::DynList<T> drop(const size_t n) const
1681 {
1682 size_t i = 0;
1684 const_me()->traverse([&i, &ret, n] (const T & item)
1685 {
1686 if (i++ >= n)
1687 ret.append(item);
1688 return true;
1689 });
1690 return ret;
1691 }
1692
1701 void mutable_drop(const size_t n)
1702 {
1703 for (size_t i = 0; i < n; ++i)
1704 me()->remove();
1705 }
1706};
1707
1712template <class Container, typename T>
1714{
1723 {
1724 return static_cast<const Container*>(this)->
1725 Container::template maps<T> ([] (const T & key) { return key; });
1726 }
1727
1729 Aleph::DynList<T> keys() const { return items(); }
1730};
1731
1732
1737template <class Container, typename T>
1739
1740
1754template <class Container>
1756{
1757 const Container * const_me() const noexcept
1758 {
1759 return static_cast<const Container*>(this);
1760 }
1761
1762public:
1763
1775 bool equal_to(const Container & r) const
1776 {
1777 if (const_me() == &r)
1778 return true;
1779
1780 if (const_me()->size() != r.size())
1781 return false;
1782
1783 auto it1 = const_me()->get_it();
1784 auto it2 = r.get_it();
1785
1786 while (it1.has_curr())
1787 {
1788 if (not (it1.get_curr() == it2.get_curr()))
1789 return false;
1790
1791 it1.next();
1792 it2.next();
1793 }
1794
1795 return true;
1796 }
1797
1803 bool operator == (const Container & r) const
1804 {
1805 return equal_to(r);
1806 }
1807
1813 bool operator != (const Container & r) const
1814 {
1815 return not equal_to(r);
1816 }
1817};
1818
1819
1824template <class Container>
1826{
1827 const Container * const_me() const
1828 {
1829 return static_cast<const Container*>(this);
1830 }
1831
1832public:
1833
1852 bool equal_to(const Container & r) const noexcept
1853 {
1854 if (const_me() == &r) // Use const_me() to get correct pointer in case of multiple inheritance
1855 return true;
1856
1857 if (const_me()->size() != r.size())
1858 return false;
1859
1860 return const_me()->all([&r] (const typename Container::Key_Type & k)
1861 { return r.search(k) != nullptr; });
1862 }
1863
1865 bool operator == (const Container & r) const noexcept
1866 {
1867 return equal_to(r);
1868 }
1869
1871 bool operator != (const Container & r) const noexcept
1872 {
1873 return not equal_to(r);
1874 }
1875};
1876
1881template <class Container, typename Key, typename Data>
1883{
1884 const Container * const_me() const
1885 {
1886 return static_cast<const Container*>(this);
1887 }
1888
1889public:
1890
1904 template <template <typename> class C = Aleph::DynList>
1905 C<Key> keys() const
1906 {
1907 C<Key> ret_val;
1908 const_me()->for_each([&ret_val] (const Key & p)
1909 {
1910 ret_val.append(p.first);
1911 });
1912 return ret_val;
1913 }
1914
1928 template <template <typename> class C = Aleph::DynList>
1929 C<Data> values() const
1930 {
1931 C<Data> ret_val;
1932 const_me()->for_each([&ret_val] (const std::pair<Key, Data> & p)
1933 {
1934 ret_val.append(p.second);
1935 });
1936 return ret_val;
1937 }
1938
1954 template <template <typename> class C = Aleph::DynList>
1955 C<Data*> values_ptr() const
1956 {
1957 C<Data*> ret_val;
1958 const_me()->for_each([&ret_val] (std::pair<Key, Data> & p)
1959 { ret_val.append(&p.second); });
1960 return ret_val;
1961 }
1962
1972 template <template <typename> class C = Aleph::DynList>
1973 C<std::pair<Key, Data>> items() const
1974 {
1975 C<std::pair<Key, Data>> ret;
1976 const_me()->for_each([&ret] (std::pair<Key, Data> & p) { ret.append(p); });
1977 return ret;
1978 }
1979
1991 template <template <typename> class C = Aleph::DynList>
1992 C<std::pair<Key, Data*>> items_ptr() const
1993 {
1994 C<std::pair<Key, Data*>> ret_val;
1995 const_me()->for_each([&ret_val] (std::pair<Key, Data> & p)
1996 {
1997 ret_val.append(std::pair<Key,Data*>(p.first,
1998 &p.second));
1999 });
2000 return ret_val;
2001 }
2002
2008 Data & operator () (const Key & key)
2009 {
2010 return this->find(key);
2011 }
2012
2018 const Data & operator () (const Key & key) const
2019 {
2020 return this->find(key);
2021 }
2022};
2023# endif // AH_DRY_H
bool traverse(const Container &c, Operation &op) noexcept(noexcept(c.traverse(op)))
Invoke c.traverse(op) as a free function (lvalue operation).
Definition ah-dry.H:145
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:579
Functional programming utilities for Aleph-w containers.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & insert(const T &item)
Definition htlist.H:1220
T & append(const T &item)
Definition htlist.H:1271
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:1756
bool operator==(const Container &r) const
Equality operator.
Definition ah-dry.H:1803
bool operator!=(const Container &r) const
Inequality operator.
Definition ah-dry.H:1813
bool equal_to(const Container &r) const
Equality test between this and r.
Definition ah-dry.H:1775
const Container * const_me() const noexcept
Definition ah-dry.H:1757
Equality test for containers.
Definition ah-dry.H:1826
const Container * const_me() const
Definition ah-dry.H:1827
bool operator!=(const Container &r) const noexcept
Negation of equal_to()
Definition ah-dry.H:1871
bool operator==(const Container &r) const noexcept
Definition ah-dry.H:1865
bool equal_to(const Container &r) const noexcept
Test if elements of this are exactly contained in another container.
Definition ah-dry.H:1852
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:642
void mutable_for_each(Operation &operation)
Apply a mutable operation to each element of the container.
Definition ah-dry.H:928
Aleph::DynList< std::tuple< T, size_t > > pfilter(Operation &&operation) const
Overload of pfilter() that accepts rvalues.
Definition ah-dry.H:1445
void each(Operation &operation)
Alias of for_each(Operation&).
Definition ah-dry.H:829
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Definition ah-dry.H:1200
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:1319
Aleph::DynList< __T > maps(Operation &&op) const
Overload of maps() that accepts rvalues.
Definition ah-dry.H:1080
std::vector< T > to_vector() const
Convert container to std::vector.
Definition ah-dry.H:1143
void nnappend(size_t &)
Definition ah-dry.H:715
Aleph::DynList< T > filter(Operation &&operation) const
Overload of filter() that accepts rvalues.
Definition ah-dry.H:1342
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(size_t n) const
Exclusive partition of container in the nth item.
Definition ah-dry.H:1508
__T foldl(const __T &init, Op &&op=Op()) const
Overload of foldl() that accepts rvalues.
Definition ah-dry.H:1224
Aleph::DynList< T > to_dynlist() const
Convert container to DynList.
Definition ah-dry.H:1134
void emplace(Args &&... args)
Appends a new element into the container by constructing it in-place with the given args.
Definition ah-dry.H:676
void each(Operation &operation) const
Const alias of for_each(Operation&).
Definition ah-dry.H:836
size_t ninsert(Args ... args)
Insert n variadic items.
Definition ah-dry.H:741
__T fold_left(const __T &init, Op &&op=Op()) const
Overload of fold_left() that accepts rvalues.
Definition ah-dry.H:1256
Aleph::DynList< __T > maps_if(Prop prop, Operation &op) const
Conditional mapping of the elements of the container.
Definition ah-dry.H:1099
void nninsert(size_t &)
Definition ah-dry.H:714
FunctionalMethods< Container, T > * base() const noexcept
Definition ah-dry.H:645
Aleph::DynList< const T * > ptr_filter(Operation &&operation) const
Overload of ptr_filter() that accepts rvalues.
Definition ah-dry.H:1394
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &op) const
Exclusive partition of container according to a filter criterion.
Definition ah-dry.H:1468
void each(const size_t pos, const size_t slice, Operation &&operation) const
Definition ah-dry.H:913
void emplace_ins(Args &&... args)
Insert a new element into the container by constructing it in-place with the given args.
Definition ah-dry.H:707
void mutable_for_each(Operation &&operation)
Definition ah-dry.H:939
Aleph::DynList< T > take(const size_t n) const
Return a list with the first n elements seen in the container during its traversal.
Definition ah-dry.H:1631
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
Definition ah-dry.H:993
void nninsert(size_t &n, const T &item, Args &... args)
Definition ah-dry.H:718
void each(Operation &&operation) const
Const alias of each() that accepts rvalues.
Definition ah-dry.H:852
std::tuple< Aleph::DynList< T >, Aleph::DynList< T > > tpartition(Operation &op) const
Exclusive partition of container according to a filter criterion.
Definition ah-dry.H:1557
const Container * const_me() const noexcept
Definition ah-dry.H:650
void each(size_t pos, const size_t slice, Operation &operation) const
Traverse the container starting at pos taking one item every slice, performing a mutable operation on...
Definition ah-dry.H:894
size_t nappend(Args ... args)
Append n variadic items.
Definition ah-dry.H:754
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Definition ah-dry.H:957
Aleph::DynList< T > rev() const
Return a list with the elements of container in reverse order respect to its traversal order.
Definition ah-dry.H:1612
void emplace_end(Args &&... args)
Definition ah-dry.H:683
void nnappend(size_t &n, const T &item, Args &... args)
Definition ah-dry.H:726
bool all(Operation &&operation) const
Overload of all() that accepts rvalues.
Definition ah-dry.H:974
bool exists(Operation &&op) const
Overload of exists() that accepts rvalues.
Definition ah-dry.H:1011
size_t length() const noexcept
Count the number of elements of a container.
Definition ah-dry.H:1598
void for_each(Operation &&operation) const
Overload of for_each() const that accepts rvalues.
Definition ah-dry.H:806
Aleph::DynList< __T > maps_if(Prop prop, Operation &&op) const
Overload of maps_if() that accepts rvalues.
Definition ah-dry.H:1125
T fold(const T &init, Operation &&operation) const
Overload of fold() that accepts rvalues.
Definition ah-dry.H:1290
void mutable_drop(const size_t n)
Drop the first n elements seen from container.
Definition ah-dry.H:1701
T fold(const T &init, Operation &operation) const
Simplified version of foldl() where the folded type is the same type of elements stored in the contai...
Definition ah-dry.H:1267
Aleph::DynList< std::tuple< T, size_t > > pfilter(Operation &operation) const
Filter the elements of a container according to a matching criterion and determine its positions resp...
Definition ah-dry.H:1419
Aleph::DynList< const T * > ptr_filter(Operation &operation) const
Filter the elements of a container according to a matching criterion and return a pointer to the matc...
Definition ah-dry.H:1371
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
void each(Operation &&operation)
Alias of each() that accepts rvalues.
Definition ah-dry.H:868
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:1057
std::tuple< Aleph::DynList< T >, Aleph::DynList< T > > tpartition(Operation &&op) const
Overload of tpartition() that accepts rvalues.
Definition ah-dry.H:1582
void for_each(Operation &operation) const
Const overload of for_each(Operation&).
Definition ah-dry.H:790
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &&op) const
Overload of partition() that accepts rvalues.
Definition ah-dry.H:1493
Container * me()
Definition ah-dry.H:643
Aleph::DynList< T > take(size_t i, const size_t j, const size_t step=1) const
Return a list with elements seen in the container between i and j position respect to its traversal.
Definition ah-dry.H:1657
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > split_half() const
Split the container into two halves by alternating elements.
Definition ah-dry.H:1528
__T fold_left(const __T &init, Op &op) const
Alias for foldl with the same accumulator type.
Definition ah-dry.H:1237
void for_each(Operation &&operation)
Overload of for_each() that accepts rvalues.
Definition ah-dry.H:822
Aleph::DynList< T > drop(const size_t n) const
Drop the first n elements seen in the container during its traversal.
Definition ah-dry.H:1680
Common sequential searching methods on containers.
Definition ah-dry.H:196
static constexpr bool operation_is_noexcept() noexcept
Definition ah-dry.H:210
size_t find_index(Operation &&operation) const noexcept(operation_is_noexcept< Operation >())
Overload of find_index() that accepts rvalues.
Definition ah-dry.H:462
std::tuple< bool, Type > find_item(Operation &operation) noexcept(operation_is_noexcept< Operation >())
Safe sequential searching of an item matching a criterion.
Definition ah-dry.H:489
Type * find_ptr(Operation &operation) noexcept(operation_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criterion.
Definition ah-dry.H:349
auto get_itor() const
Alias of get_it().
Definition ah-dry.H:250
size_t find_index(Operation &operation) const noexcept(operation_is_noexcept< Operation >())
Find the position of an item in the container according to a searching criterion.
Definition ah-dry.H:436
Type * find_ptr(Operation &&operation) noexcept(operation_is_noexcept< Operation >())
Overload of find_ptr() that accepts rvalues.
Definition ah-dry.H:407
Container * me() noexcept
Definition ah-dry.H:197
Type & nth_ne(const size_t n) noexcept
Return the n‑th element without bounds checking.
Definition ah-dry.H:258
bool contains(const Type &item) const
Test if an item is present in the container using equality.
Definition ah-dry.H:563
const Type * find_ptr(Operation &&operation) const noexcept(operation_is_noexcept< Operation >())
Overload of find_ptr() const that accepts rvalues.
Definition ah-dry.H:389
LocateFunctions< Container, Type > * base() const
Definition ah-dry.H:204
const Type & nth(const size_t n) const
Const overload of nth(size_t).
Definition ah-dry.H:322
Type & nth(const size_t n)
Return the n-th item of the container.
Definition ah-dry.H:299
std::tuple< bool, Type > find_item(Operation &&operation) const noexcept(operation_is_noexcept< Operation >())
Overload of find_item() const that accepts rvalues.
Definition ah-dry.H:537
const Type * find_ptr(Operation &operation) const noexcept(operation_is_noexcept< Operation >())
Const overload of find_ptr(Operation&).
Definition ah-dry.H:371
std::tuple< bool, Type > find_item(Operation &operation) const noexcept(operation_is_noexcept< Operation >())
Definition ah-dry.H:499
const Container * const_me() const noexcept
Definition ah-dry.H:199
std::tuple< bool, Type > find_item(Operation &&operation) noexcept(operation_is_noexcept< Operation >())
Overload of find_item() that accepts rvalues.
Definition ah-dry.H:519
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:222
auto get_it(const size_t pos) const
Return a properly initialized iterator positioned at the pos item on the container.
Definition ah-dry.H:238
bool contains_if(Operation &&operation) const noexcept(operation_is_noexcept< Operation >())
Test if an item satisfying a criterion is present in the container.
Definition ah-dry.H:551
const Type & nth_ne(const size_t n) const noexcept
Const overload of nth_ne(size_t).
Definition ah-dry.H:278
Common methods to mapping containers.
Definition ah-dry.H:1883
C< Key > keys() const
Return the domain set of container.
Definition ah-dry.H:1905
C< std::pair< Key, Data * > > items_ptr() const
Return a container with the entire mapping where the range could be modified.
Definition ah-dry.H:1992
C< Data > values() const
Return the range set of container.
Definition ah-dry.H:1929
C< std::pair< Key, Data > > items() const
Return a container with the entire mapping.
Definition ah-dry.H:1973
Data & operator()(const Key &key)
Return the image or element mapped to key.
Definition ah-dry.H:2008
const Container * const_me() const
Definition ah-dry.H:1884
C< Data * > values_ptr() const
Return a container of pointers to the elements of range set of container.
Definition ah-dry.H:1955
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
#define TT
Definition ran_array.c:29
Generic list of items stored in a container.
Definition ah-dry.H:1714
Aleph::DynList< T > keys() const
Definition ah-dry.H:1729
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Definition ah-dry.H:1722
Generic traversal of the container through its iterator.
Definition ah-dry.H:67
bool traverse(Operation &&operation) const noexcept(traverse_is_noexcept< Operation >())
Overload of traverse(Operation&) const that accepts rvalues.
Definition ah-dry.H:117
bool traverse(Operation &operation) const noexcept(traverse_is_noexcept< Operation >())
Const overload of traverse(Operation&).
Definition ah-dry.H:108
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:97
bool traverse(Operation &&operation) noexcept(traverse_is_noexcept< Operation >())
Overload of traverse(Operation&) that accepts rvalues.
Definition ah-dry.H:126
static constexpr bool traverse_is_noexcept() noexcept
Definition ah-dry.H:70
Special constructors common to Aleph-w ( ) containers.
Definition ah-dry.H:586
SpecialCtors(const SpecialCtors &)=default
SpecialCtors(SpecialCtors &&) noexcept
Definition ah-dry.H:591
SpecialCtors(std::initializer_list< T > l)
Construct the container from an initializer list.
Definition ah-dry.H:625
SpecialCtors(It b, It e)
Construct the container from a range of iterators.
Definition ah-dry.H:615
SpecialCtors(const Aleph::DynList< T > &l)
Build the container by inserting all items of the list l.
Definition ah-dry.H:601
SpecialCtors()=default
SpecialCtors & operator=(const SpecialCtors &)=default
static int * k
Filter_Iterator< DynList< int >, DynList< int >::Iterator, Par > It
Definition test_htlist.C:56
gsl_rng * r
DynList< int > l