Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-dry-mixin.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
68#ifndef AH_DRY_MIXIN_H
69#define AH_DRY_MIXIN_H
70
71#include <cstddef>
72#include <tuple>
73#include <functional>
74#include <utility>
75#include <type_traits>
76#include <vector>
77#include <string>
78
79#include <ah-concepts.H>
80#include <ah-iterator.H>
81#include <ah-errors.H>
82
83namespace Aleph {
84
85// Forward declarations
86template <typename T>
87class DynList;
88
89// Dft_Map_Op is defined in ahFunctional.H, we just forward declare here
90// to avoid circular dependency. When ahFunctional.H is included, it provides
91// the actual definition. The type of the result goes first.
92template <typename tgtT, typename srcT>
93struct Dft_Map_Op;
94
95// =============================================================================
96// TraverseMixin - Basic traversal operations
97// =============================================================================
98
115template <typename Derived, typename Type>
117{
118protected:
120 [[nodiscard]] const Derived &self() const noexcept
121 {
122 return static_cast<const Derived &>(*this);
123 }
124
126 [[nodiscard]] Derived &self() noexcept
127 {
128 return static_cast<Derived &>(*this);
129 }
130
131public:
166 template <class Operation>
168 bool traverse(Operation &operation) const
169 {
170 for (auto it = Aleph::iterator_for(self()); it.has_curr(); it.next_ne())
171 if (not operation(it.get_curr()))
172 return false;
173 return true;
174 }
175
177 template <class Operation>
179 bool traverse(Operation &operation)
180 {
181 for (auto it = Aleph::iterator_for(self()); it.has_curr(); it.next_ne())
182 if (not operation(it.get_curr()))
183 return false;
184 return true;
185 }
186
189 template <class Operation>
191 bool traverse(Operation and operation = Operation()) const
192 {
193 return traverse<Operation>(operation);
194 }
195
197 template <class Operation>
199 bool traverse(Operation and operation = Operation())
200 {
201 return traverse<Operation>(operation);
202 }
203};
204
205// =============================================================================
206// LocateMixin - Element location operations
207// =============================================================================
208
225template <typename Derived, typename Type>
227{
228protected:
229 [[nodiscard]] const Derived &self() const noexcept
230 {
231 return static_cast<const Derived &>(*this);
232 }
233
234 [[nodiscard]] Derived &self() noexcept
235 {
236 return static_cast<Derived &>(*this);
237 }
238
239public:
260 Type &nth(const size_t n)
261 {
262 Type *ptr = nullptr;
263 size_t i = 0;
264 self().traverse([&ptr, &i, n](Type &item)
265 {
266 if (i++ != n)
267 return true;
268 ptr = &item;
269 return false;
270 });
271
272 // Test the pointer, not the count: i == n + 1 overflows for n == SIZE_MAX
273 ah_out_of_range_error_if(ptr == nullptr) << "index " << n << " out of range";
274 return *ptr;
275 }
276
279 const Type &nth(const size_t n) const
280 {
281 const Type *ptr = nullptr;
282 size_t i = 0;
283 self().traverse([&ptr, &i, n](const Type &item)
284 {
285 if (i++ != n)
286 return true;
287 ptr = &item;
288 return false;
289 });
290
291 // Test the pointer, not the count: i == n + 1 overflows for n == SIZE_MAX
292 ah_out_of_range_error_if(ptr == nullptr) << "index " << n << " out of range";
293 return *ptr;
294 }
295
310 Type &nth_ne(const size_t n) noexcept
311 {
312 Type *ptr = nullptr;
313 size_t i = 0;
314 self().traverse([&ptr, &i, &n](Type &item)
315 {
316 if (i++ < n)
317 return true;
318 ptr = &item;
319 return false;
320 });
321 return *ptr;
322 }
323
326 const Type &nth_ne(const size_t n) const noexcept
327 {
328 const Type *ptr = nullptr;
329 size_t i = 0;
330 self().traverse([&ptr, &i, &n](const Type &item)
331 {
332 if (i++ < n)
333 return true;
334 ptr = &item;
335 return false;
336 });
337 return *ptr;
338 }
339
365 template <class Operation>
367 Type *find_ptr(Operation &operation)
368 {
369 Type *ptr = nullptr;
370 self().traverse([&ptr, &operation](Type &item)
371 {
372 if (operation(item))
373 {
374 ptr = &item;
375 return false;
376 }
377 return true;
378 });
379 return ptr;
380 }
381
384 template <class Operation>
386 const Type *find_ptr(Operation &operation) const
387 {
388 const Type *ptr = nullptr;
389 self().traverse([&ptr, &operation](const Type &item)
390 {
391 if (operation(item))
392 {
393 ptr = &item;
394 return false;
395 }
396 return true;
397 });
398 return ptr;
399 }
400
403 template <class Operation>
405 const Type *find_ptr(Operation and operation = Operation()) const
406 {
407 return find_ptr<Operation>(operation);
408 }
409
411 template <class Operation>
413 Type *find_ptr(Operation and operation = Operation())
414 {
415 return find_ptr<Operation>(operation);
416 }
417
442 template <class Operation>
444 std::tuple<bool, Type> find_item(Operation &operation)
445 {
446 auto ptr = find_ptr(operation);
447 return ptr ? std::make_tuple(true, *ptr) : std::make_tuple(false, Type());
448 }
449
451 template <class Operation>
453 std::tuple<bool, Type> find_item(Operation &operation) const
454 {
455 auto ptr = find_ptr(operation);
456 return ptr ? std::make_tuple(true, *ptr) : std::make_tuple(false, Type());
457 }
458
460 template <class Operation>
462 std::tuple<bool, Type> find_item(Operation &&operation = Operation())
463 {
464 return find_item(operation);
465 }
466
468 template <class Operation>
470 std::tuple<bool, Type> find_item(Operation &&operation = Operation()) const
471 {
472 return find_item(operation);
473 }
474};
475
476// =============================================================================
477// FunctionalMixin - Functional programming operations
478// =============================================================================
479
502template <typename Derived, typename Type>
504{
505protected:
506 [[nodiscard]] const Derived &self() const noexcept
507 {
508 return static_cast<const Derived &>(*this);
509 }
510
511 [[nodiscard]] Derived &self() noexcept
512 {
513 return static_cast<Derived &>(*this);
514 }
515
516public:
517 // ---------------------------------------------------------------------------
518 // Iteration
519 // ---------------------------------------------------------------------------
520
537 template <class Operation>
539 auto for_each(Operation &operation) const -> decltype(self())
540 {
541 self().traverse([&operation](const Type &item)
542 {
543 operation(item);
544 return true;
545 });
546 return self();
547 }
548
550 template <class Operation>
552 auto for_each(Operation &operation) -> decltype(self())
553 {
554 self().traverse([&operation](const Type &item)
555 {
556 operation(item);
557 return true;
558 });
559 return self();
560 }
561
563 template <class Operation>
565 auto for_each(Operation &&operation = Operation()) const -> decltype(self())
566 {
567 return for_each<Operation>(operation);
568 }
569
571 template <class Operation>
573 auto for_each(Operation &&operation = Operation()) -> decltype(self())
574 {
575 return for_each<Operation>(operation);
576 }
577
593 template <class Operation>
595 auto mutable_for_each(Operation &operation) -> decltype(self())
596 {
597 self().traverse([&operation](Type &item)
598 {
599 operation(item);
600 return true;
601 });
602 return self();
603 }
604
606 template <class Operation>
608 auto mutable_for_each(Operation &&operation = Operation()) -> decltype(self())
609 {
610 return mutable_for_each<Operation>(operation);
611 }
612
613 // ---------------------------------------------------------------------------
614 // Predicates
615 // ---------------------------------------------------------------------------
616
635 template <class Operation>
636 requires requires(const typename concepts_detail::defer<Derived, Operation>::type &c,
637 Operation &op) {
638 { c.template traverse<Operation>(op) } -> std::convertible_to<bool>;
639 }
640 [[nodiscard]] bool all(Operation &operation) const
641 {
642 return self().template traverse<Operation>(operation);
643 }
644
646 template <class Operation>
647 requires requires(const typename concepts_detail::defer<Derived, Operation>::type &c,
648 Operation &op) {
649 { c.template traverse<Operation>(op) } -> std::convertible_to<bool>;
650 }
651 [[nodiscard]] bool all(Operation &&operation = Operation()) const
652 {
653 return all<Operation>(operation);
654 }
655
659 template <class Operation>
660 requires requires(const typename concepts_detail::defer<Derived, Operation>::type &c,
661 Operation &op) {
662 { c.template traverse<Operation>(op) } -> std::convertible_to<bool>;
663 }
664 [[nodiscard]] bool forall(Operation &operation) const
665 {
666 return all<Operation>(operation);
667 }
668
670 template <class Operation>
671 requires requires(const typename concepts_detail::defer<Derived, Operation>::type &c,
672 Operation &op) {
673 { c.template traverse<Operation>(op) } -> std::convertible_to<bool>;
674 }
675 [[nodiscard]] bool forall(Operation &&operation = Operation()) const
676 {
677 return all<Operation>(operation);
678 }
679
698 template <class Operation>
700 [[nodiscard]] bool exists(Operation &operation) const
701 {
702 return not self().traverse([&operation](const Type &item)
703 {
704 return not operation(item);
705 });
706 }
707
709 template <class Operation>
711 [[nodiscard]] bool exists(Operation &&operation = Operation()) const
712 {
713 return exists<Operation>(operation);
714 }
715
716 // ---------------------------------------------------------------------------
717 // Transformation
718 // ---------------------------------------------------------------------------
719
754 template <typename R = void, template <typename> class Container = Aleph::DynList, class Operation>
755 requires std::invocable<Operation &, const Type &> and
757 const Type &item) { l.append(std::invoke(op, item)); }
759 Operation &operation) const
760 {
762 // The default operation between items of one type is their copy: the
763 // container makes it here, once, where the operation would build a
764 // copy for the container to move, or to copy again. A container that
765 // takes only rvalues is given what the operation builds
766 if constexpr (std::is_same_v<std::remove_cv_t<Operation>, Dft_Map_Op<Type, Type>> and
767 requires(decltype(ret_val) &l, const Type &item) { l.append(item); })
768 for_each([&ret_val](const Type &item) { ret_val.append(item); });
769 else
770 for_each([&ret_val, &operation](const Type &item)
771 {
772 ret_val.append(std::invoke(operation, item));
773 });
774 return ret_val;
775 }
776
778 template <typename R = void, template <typename> class Container = Aleph::DynList,
780 requires std::invocable<Operation &, const Type &> and
782 const Type &item) { l.append(std::invoke(op, item)); }
784 Operation &&operation = Operation()) const
785 {
786 return maps<R, Container, Operation>(operation);
787 }
788
818 template <typename Acc = Type>
819 [[nodiscard]] Acc foldl(const Acc &init, std::function<Acc(const Acc &, const Type &)> operation) const
820 {
821 auto ret_val = init;
822 for_each([&ret_val, &operation](const Type &item)
823 {
824 ret_val = operation(ret_val, item);
825 });
826 return ret_val;
827 }
828
832 template <typename Acc = Type>
833 [[nodiscard]] Acc fold_left(std::function<Acc(const Acc &, const Type &)> operation,
834 const Acc &init) const
835 {
836 return foldl<Acc>(init, operation);
837 }
838
848 template <class Operation>
849 requires requires(Type &acc, Operation &op, const Type &item) { acc = op(acc, item); }
850 [[nodiscard]] Type fold(const Type &init, Operation &operation) const
851 {
852 auto ret_val = init;
853 for_each([&ret_val, &operation](const Type &item)
854 {
855 ret_val = operation(ret_val, item);
856 });
857 return ret_val;
858 }
859
861 template <class Operation>
862 requires requires(Type &acc, Operation &op, const Type &item) { acc = op(acc, item); }
863 [[nodiscard]] Type fold(const Type &init, Operation &&operation = Operation()) const
864 {
865 return fold<Operation>(init, operation);
866 }
867
887 template <class Operation>
889 [[nodiscard]] DynList<Type> filter(Operation &operation) const
890 {
891 DynList<Type> ret_val;
892 for_each([&ret_val, &operation](const Type &item)
893 {
894 if (operation(item))
895 ret_val.append(item);
896 });
897 return ret_val;
898 }
899
901 template <class Operation>
903 [[nodiscard]] DynList<Type> filter(Operation &&operation = Operation()) const
904 {
905 return filter<Operation>(operation);
906 }
907
923 template <class Operation>
925 [[nodiscard]] DynList<std::tuple<Type, size_t>> pfilter(Operation &operation) const
926 {
928 size_t i = 0;
929 for_each([&ret_val, &operation, &i](const Type &item)
930 {
931 if (operation(item))
932 ret_val.append(std::make_tuple(item, i));
933 ++i;
934 });
935 return ret_val;
936 }
937
939 template <class Operation>
941 [[nodiscard]] DynList<std::tuple<Type, size_t>> pfilter(Operation &&operation = Operation()) const
942 {
943 return pfilter<Operation>(operation);
944 }
945
962 template <class Operation>
964 [[nodiscard]] std::pair<DynList<Type>, DynList<Type>> partition(Operation &op) const
965 {
966 std::pair<DynList<Type>, DynList<Type>> ret_val;
967 for_each([&ret_val, &op](const Type &item)
968 {
969 if (op(item))
970 ret_val.first.append(item);
971 else
972 ret_val.second.append(item);
973 });
974 return ret_val;
975 }
976
978 template <class Operation>
980 [[nodiscard]] std::pair<DynList<Type>, DynList<Type>> partition(Operation &&op = Operation()) const
981 {
982 return partition<Operation>(op);
983 }
984
988 template <class Operation>
990 [[nodiscard]] std::tuple<DynList<Type>, DynList<Type>> tpartition(Operation &op) const
991 {
992 DynList<Type> r1, r2;
993 for_each([&r1, &r2, &op](const Type &item)
994 {
995 if (op(item))
996 r1.append(item);
997 else
998 r2.append(item);
999 });
1000 return std::make_tuple(r1, r2);
1001 }
1002
1004 template <class Operation>
1006 [[nodiscard]] std::tuple<DynList<Type>, DynList<Type>> tpartition(Operation &&op = Operation()) const
1007 {
1008 return tpartition<Operation>(op);
1009 }
1010
1011 // ---------------------------------------------------------------------------
1012 // Utility
1013 // ---------------------------------------------------------------------------
1014
1024 [[nodiscard]] size_t length() const noexcept
1025 {
1026 size_t count = 0;
1027 for_each([&count](const Type &)
1028 {
1029 ++count;
1030 });
1031 return count;
1032 }
1033
1049 template <template <typename> class Container = Aleph::DynList>
1050 [[nodiscard]] Container<Type> rev() const
1051 {
1052 Container<Type> ret_val;
1053 for_each([&ret_val](const Type &item)
1054 {
1055 ret_val.insert(item);
1056 });
1057 return ret_val;
1058 }
1059
1060 // ---------------------------------------------------------------------------
1061 // Slicing
1062 // ---------------------------------------------------------------------------
1063
1080 template <template <typename> class Container = Aleph::DynList>
1081 [[nodiscard]] Container<Type> take(const size_t n) const
1082 {
1083 size_t i = 0;
1084 Container<Type> ret;
1085 self().traverse([&i, &ret, n](const Type &item)
1086 {
1087 if (i++ >= n)
1088 return false;
1089 ret.append(item);
1090 return true;
1091 });
1092 return ret;
1093 }
1094
1111 template <template <typename> class Container = Aleph::DynList>
1112 [[nodiscard]] Container<Type> drop(const size_t n) const
1113 {
1114 size_t i = 0;
1115 Container<Type> ret;
1116 self().traverse([&i, &ret, n](const Type &item)
1117 {
1118 if (i++ >= n)
1119 ret.append(item);
1120 return true;
1121 });
1122 return ret;
1123 }
1124
1125 // ---------------------------------------------------------------------------
1126 // Aggregation (Numeric)
1127 // ---------------------------------------------------------------------------
1128
1147 [[nodiscard]] Type sum(const Type &init = Type{}) const
1148 requires requires(Type a, Type b) {
1149 { a + b } -> std::convertible_to<Type>;
1150 }
1151 {
1152 Type result = init;
1153 for_each([&result](const Type &item)
1154 {
1155 result = result + item;
1156 });
1157 return result;
1158 }
1159
1177 [[nodiscard]] Type product(const Type &init) const
1178 requires requires(Type a, Type b) {
1179 { a * b } -> std::convertible_to<Type>;
1180 }
1181 {
1182 Type result = init;
1183 for_each([&result](const Type &item)
1184 {
1185 result = result * item;
1186 });
1187 return result;
1188 }
1189
1206 [[nodiscard]] const Type *min() const
1207 requires requires(Type a, Type b) {
1208 { a < b } -> std::convertible_to<bool>;
1209 }
1210 {
1211 const Type *result = nullptr;
1212 for_each([&result](const Type &item)
1213 {
1214 if (result == nullptr or item < *result)
1215 result = &item;
1216 });
1217 return result;
1218 }
1219
1236 [[nodiscard]] const Type *max() const
1237 requires requires(Type a, Type b) {
1238 { a < b } -> std::convertible_to<bool>;
1239 }
1240 {
1241 const Type *result = nullptr;
1242 for_each([&result](const Type &item)
1243 {
1244 if (result == nullptr or *result < item)
1245 result = &item;
1246 });
1247 return result;
1248 }
1249
1267 template <class Compare>
1268 requires PredicateWith<Compare &, const Type &, const Type &>
1269 [[nodiscard]] const Type *min_by(Compare cmp) const
1270 {
1271 const Type *result = nullptr;
1272 for_each([&result, &cmp](const Type &item)
1273 {
1274 if (result == nullptr or cmp(item, *result))
1275 result = &item;
1276 });
1277 return result;
1278 }
1279
1297 template <class Compare>
1299 [[nodiscard]] const Type *max_by(Compare cmp) const
1300 {
1301 const Type *result = nullptr;
1302 for_each([&result, &cmp](const Type &item)
1303 {
1304 if (result == nullptr or cmp(*result, item))
1305 result = &item;
1306 });
1307 return result;
1308 }
1309
1310 // ---------------------------------------------------------------------------
1311 // Search and Counting
1312 // ---------------------------------------------------------------------------
1313
1335 [[nodiscard]] bool has_value(const Type &val) const
1336 requires requires(Type a, Type b) {
1337 { a == b } -> std::convertible_to<bool>;
1338 }
1339 {
1340 return exists([&val](const Type &item)
1341 {
1342 return item == val;
1343 });
1344 }
1345
1363 template <class Predicate>
1364 requires PredicateWith<Predicate &, const Type &>
1365 [[nodiscard]] bool none(Predicate &pred) const
1366 {
1367 return not exists(pred);
1368 }
1369
1371 template <class Predicate>
1373 [[nodiscard]] bool none(Predicate &&pred) const
1374 {
1375 return not exists(std::forward<Predicate>(pred));
1376 }
1377
1393 template <class Predicate>
1395 [[nodiscard]] size_t count_if(Predicate pred) const
1396 {
1397 size_t count = 0;
1398 for_each([&count, &pred](const Type &item)
1399 {
1400 if (pred(item))
1401 ++count;
1402 });
1403 return count;
1404 }
1405
1406 // ---------------------------------------------------------------------------
1407 // First/Last Element Access
1408 // ---------------------------------------------------------------------------
1409
1423 [[nodiscard]] const Type *first() const
1424 {
1425 const Type *result = nullptr;
1426 self().traverse([&result](const Type &item)
1427 {
1428 result = &item;
1429 return false; // stop after first
1430 });
1431 return result;
1432 }
1433
1448 [[nodiscard]] Type first_or(const Type &default_val) const
1449 {
1450 auto ptr = first();
1451 return ptr ? *ptr : default_val;
1452 }
1453
1467 [[nodiscard]] const Type *last() const
1468 {
1469 const Type *result = nullptr;
1470 for_each([&result](const Type &item)
1471 {
1472 result = &item;
1473 });
1474 return result;
1475 }
1476
1491 [[nodiscard]] Type last_or(const Type &default_val) const
1492 {
1493 auto ptr = last();
1494 return ptr ? *ptr : default_val;
1495 }
1496
1497 // ---------------------------------------------------------------------------
1498 // Enumeration and Indexing
1499 // ---------------------------------------------------------------------------
1500
1517 template <template <typename> class Container = Aleph::DynList>
1519 {
1521 size_t idx = 0;
1522 for_each([&ret, &idx](const Type &item)
1523 {
1524 ret.append(std::make_pair(idx++, item));
1525 });
1526 return ret;
1527 }
1528
1546 template <class Predicate>
1548 [[nodiscard]] size_t find_index(Predicate pred) const
1549 {
1550 size_t idx = 0;
1551 self().traverse([&idx, &pred](const Type &item)
1552 {
1553 if (pred(item))
1554 return false;
1555 ++idx;
1556 return true;
1557 });
1558 return idx;
1559 }
1560
1579 [[nodiscard]] size_t index_of(const Type &val) const
1580 requires requires(Type a, Type b) {
1581 { a == b } -> std::convertible_to<bool>;
1582 }
1583 {
1584 return find_index([&val](const Type &item)
1585 {
1586 return item == val;
1587 });
1588 }
1589
1590 // ---------------------------------------------------------------------------
1591 // Transformation (Advanced)
1592 // ---------------------------------------------------------------------------
1593
1611 template <template <typename> class Container = Aleph::DynList>
1612 [[nodiscard]] Container<Type> unique() const
1613 requires requires(Type a, Type b) {
1614 { a == b } -> std::convertible_to<bool>;
1615 }
1616 {
1617 Container<Type> ret;
1618 const Type *prev = nullptr;
1619 for_each([&ret, &prev](const Type &item)
1620 {
1621 if (prev == nullptr or not (*prev == item))
1622 {
1623 ret.append(item);
1624 prev = &item;
1625 }
1626 });
1627 return ret;
1628 }
1629
1648 template <template <typename> class Container = Aleph::DynList, class EqPred>
1649 requires PredicateWith<EqPred &, const Type &, const Type &>
1650 [[nodiscard]] Container<Type> unique_by(EqPred eq) const
1651 {
1652 Container<Type> ret;
1653 const Type *prev = nullptr;
1654 for_each([&ret, &prev, &eq](const Type &item)
1655 {
1656 if (prev == nullptr or not eq(*prev, item))
1657 {
1658 ret.append(item);
1659 prev = &item;
1660 }
1661 });
1662 return ret;
1663 }
1664
1680 template <template <typename> class Container = Aleph::DynList>
1681 [[nodiscard]] Container<Type> intersperse(const Type &sep) const
1682 {
1683 Container<Type> ret;
1684 bool first = true;
1685 for_each([&ret, &sep, &first](const Type &item)
1686 {
1687 if (not first)
1688 ret.append(sep);
1689 ret.append(item);
1690 first = false;
1691 });
1692 return ret;
1693 }
1694
1695 // ---------------------------------------------------------------------------
1696 // Chunking and Windowing
1697 // ---------------------------------------------------------------------------
1698
1714 template <template <typename> class Container = Aleph::DynList>
1715 [[nodiscard]] Container<Container<Type>> chunk(size_t n) const
1716 {
1718 if (n == 0)
1719 return ret;
1720
1721 Container<Type> current;
1722 size_t count = 0;
1723
1724 for_each([&ret, &current, &count, n](const Type &item)
1725 {
1726 current.append(item);
1727 if (++count >= n)
1728 {
1729 ret.append(std::move(current));
1730 current = Container<Type>{};
1731 count = 0;
1732 }
1733 });
1734
1735 if (count > 0)
1736 ret.append(std::move(current));
1737
1738 return ret;
1739 }
1740
1758 template <template <typename> class Container = Aleph::DynList>
1759 [[nodiscard]] Container<Container<Type>> sliding(size_t size, size_t step = 1) const
1760 {
1762 if (size == 0 or step == 0)
1763 return ret;
1764
1765 // Collect all elements first
1767 for_each([&all](const Type &item)
1768 {
1769 all.append(item);
1770 });
1771
1772 size_t total = all.size();
1773 if (total < size)
1774 return ret;
1775
1776 for (size_t start = 0; start + size <= total; start += step)
1777 {
1778 Container<Type> window;
1779 size_t idx = 0;
1780 all.traverse([&window, &idx, start, size](const Type &item)
1781 {
1782 if (idx >= start and idx < start + size)
1783 window.append(item);
1784 ++idx;
1785 return idx < start + size;
1786 });
1787 ret.append(std::move(window));
1788 }
1789
1790 return ret;
1791 }
1792
1793 // ---------------------------------------------------------------------------
1794 // Conversion
1795 // ---------------------------------------------------------------------------
1796
1810 [[nodiscard]] std::vector<Type> to_vector() const
1811 {
1812 std::vector<Type> ret;
1813 if constexpr (requires { self().size(); })
1814 ret.reserve(self().size());
1815 for_each([&ret](const Type &item)
1816 {
1817 ret.push_back(item);
1818 });
1819 return ret;
1820 }
1821
1840 template <typename DynListType = DynList<Type>>
1841 [[nodiscard]] DynListType to_dynlist() const
1842 {
1843 DynListType ret;
1844 for_each([&ret](const Type &item)
1845 {
1846 ret.append(item);
1847 });
1848 return ret;
1849 }
1850
1868 template <typename StringType = std::string>
1869 [[nodiscard]] StringType join(const StringType &sep = StringType{", "}) const
1870 requires requires(Type a) { std::to_string(a); }
1871 {
1872 StringType ret;
1873 bool first = true;
1874 for_each([&ret, &sep, &first](const Type &item)
1875 {
1876 if (not first)
1877 ret += sep;
1878 ret += std::to_string(item);
1879 first = false;
1880 });
1881 return ret;
1882 }
1883
1898 [[nodiscard]] std::string join_str(const std::string &sep = ", ") const
1899 requires std::is_convertible_v<Type, std::string>
1900 {
1901 std::string ret;
1902 bool first = true;
1903 for_each([&ret, &sep, &first](const Type &item)
1904 {
1905 if (not first)
1906 ret += sep;
1907 ret += static_cast<std::string>(item);
1908 first = false;
1909 });
1910 return ret;
1911 }
1912
1913 // ---------------------------------------------------------------------------
1914 // Zip Operations
1915 // ---------------------------------------------------------------------------
1916
1936 template <class Other, template <typename> class Container = Aleph::DynList>
1938 {
1939 using OtherType = typename Other::Item_Type;
1941
1942 auto it1 = self().get_it();
1943 auto it2 = other.get_it();
1944
1945 while (it1.has_curr() and it2.has_curr())
1946 {
1947 ret.append(std::make_pair(it1.get_curr(), it2.get_curr()));
1948 it1.next();
1949 it2.next();
1950 }
1951
1952 return ret;
1953 }
1954};
1955
1956// =============================================================================
1957// KeysMixin - Keys/Items extraction
1958// =============================================================================
1959
1965template <typename Derived, typename Type>
1967{
1968protected:
1969 [[nodiscard]] const Derived &self() const noexcept
1970 {
1971 return static_cast<const Derived &>(*this);
1972 }
1973
1974public:
1980 template <template <typename> class Container = DynList>
1981 [[nodiscard]] Container<Type> keys() const
1982 {
1983 return self().template maps<Type, Container>([](const Type &key) -> const Type &
1984 {
1985 return key;
1986 });
1987 }
1988
1992 template <template <typename> class Container = DynList>
1993 [[nodiscard]] Container<Type> items() const
1994 {
1995 return keys<Container>();
1996 }
1997};
1998
1999} // end namespace Aleph
2000
2001#endif // AH_DRY_MIXIN_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
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:727
STL-compatible iterator adapters for Aleph containers.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
CRTP Mixin providing functional programming operations.
Derived & self() noexcept
bool all(Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t count_if(Predicate pred) const
Count elements satisfying a predicate.
Container< Type > drop(const size_t n) const
Skip the first n elements.
Container< std::pair< size_t, Type > > enumerate() const
Enumerate elements with their indices.
and
Transform elements using a mapping function.
bool forall(Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Container< Container< Type > > chunk(size_t n) const
Split into chunks of fixed size.
DynListType to_dynlist() const
Convert container to DynList.
std::pair< DynList< Type >, DynList< Type > > partition(Operation &op) const
Partition elements by a predicate.
std::tuple< DynList< Type >, DynList< Type > > tpartition(Operation &op) const
Partition returning tuple instead of pair.
DynList< std::tuple< Type, size_t > > pfilter(Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
auto mutable_for_each(Operation &operation) -> decltype(self())
Apply an operation to each element (mutable).
bool none(Predicate &&pred) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Type * min_by(Compare cmp) const
Find the minimum element using a custom comparator.
auto for_each(Operation &&operation=Operation()) -> decltype(self())
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Type * max() const
Find the maximum element.
Container< concepts_detail::map_result_t< R, Operation &, Type > > maps(Operation &&operation=Operation()) const
Container< Type > unique_by(EqPred eq) const
Remove consecutive duplicates using a custom equality predicate.
Type fold(const Type &init, Operation &operation) const
Simple fold with same type for accumulator and elements.
bool none(Predicate &pred) const
Check if no element satisfies a predicate.
Container< Type > rev() const
Create a reversed copy.
Acc foldl(const Acc &init, std::function< Acc(const Acc &, const Type &)> operation) const
Left fold (reduce) with initial value.
bool exists(Operation &operation) const
Test if any element satisfies a predicate.
Container< Container< Type > > sliding(size_t size, size_t step=1) const
Create sliding windows of fixed size.
Container< Type > take(const size_t n) const
Take the first n elements.
std::string join_str(const std::string &sep=", ") const
Join string elements with separator.
Container< Type > unique() const
Remove consecutive duplicate elements.
std::pair< DynList< Type >, DynList< Type > > partition(Operation &&op=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Type fold(const Type &init, Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Type * first() const
Get the first element.
Container< std::pair< Type, typename Other::Item_Type > > zip_with(const Other &other) const
Zip with another container.
Type last_or(const Type &default_val) const
Get the last element or a default value.
Container< concepts_detail::map_result_t< R, Operation &, Type > > maps(Operation &operation) const
auto for_each(Operation &operation) const -> decltype(self())
Apply an operation to each element (read-only).
bool has_value(const Type &val) const
Check if container has a value.
StringType join(const StringType &sep=StringType{", "}) const
Join elements into a string with separator.
size_t index_of(const Type &val) const
Find the index of a specific value.
DynList< std::tuple< Type, size_t > > pfilter(Operation &operation) const
Filter with position information.
Acc fold_left(std::function< Acc(const Acc &, const Type &)> operation, const Acc &init) const
Left fold with operation first (alternative signature).
bool exists(Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Type product(const Type &init) const
Compute the product of all elements.
bool all(Operation &operation) const
Test if all elements satisfy a predicate.
auto for_each(Operation &&operation=Operation()) const -> decltype(self())
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t length() const noexcept
Count the number of elements.
DynList< Type > filter(Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Type sum(const Type &init=Type{}) const
Compute the sum of all elements.
size_t find_index(Predicate pred) const
Find the index of the first element satisfying a predicate.
std::vector< Type > to_vector() const
Convert to std::vector.
std::tuple< DynList< Type >, DynList< Type > > tpartition(Operation &&op=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Type * max_by(Compare cmp) const
Find the maximum element using a custom comparator.
const Derived & self() const noexcept
Container< Type > intersperse(const Type &sep) const
Intersperse a separator between elements.
bool forall(Operation &operation) const
Alias for all().
Type first_or(const Type &default_val) const
Get the first element or a default value.
auto mutable_for_each(Operation &&operation=Operation()) -> decltype(self())
This is an overloaded member function, provided for convenience. It differs from the above function o...
auto for_each(Operation &operation) -> decltype(self())
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynList< Type > filter(Operation &operation) const
Filter elements by a predicate.
const Type * last() const
Get the last element.
const Type * min() const
Find the minimum element.
CRTP Mixin for extracting keys from set-like containers.
Container< Type > items() const
Alias for keys().
const Derived & self() const noexcept
Container< Type > keys() const
Extract all keys as a list.
CRTP Mixin providing element location operations.
std::tuple< bool, Type > find_item(Operation &operation) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::tuple< bool, Type > find_item(Operation &operation)
Find element with success flag.
const Type * find_ptr(Operation and operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Type & nth_ne(const size_t n) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::tuple< bool, Type > find_item(Operation &&operation=Operation())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Type * find_ptr(Operation and operation=Operation())
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::tuple< bool, Type > find_item(Operation &&operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Type * find_ptr(Operation &operation) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Derived & self() const noexcept
const Type & nth(const size_t n) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Type * find_ptr(Operation &operation)
Find the first element satisfying a predicate.
Type & nth(const size_t n)
Access the n-th element (bounds-checked).
Type & nth_ne(const size_t n) noexcept
Access the n-th element (unchecked).
Derived & self() noexcept
CRTP Mixin providing traversal operations.
bool traverse(Operation &operation) const
Traverse all elements, applying an operation to each.
bool traverse(Operation and operation=Operation()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Derived & self() const noexcept
Access the derived class (const version).
bool traverse(Operation and operation=Operation())
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool traverse(Operation &operation)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Derived & self() noexcept
Access the derived class (mutable version).
f(args...) is a valid call expression, exactly as written.
f(args...) is a valid call whose result is usable as a condition.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
auto iterator_for(C &c)
Iterator on c positioned on its first item: the container's own iterator on a modifiable container.
static std::atomic< bool > init
Definition hash-fct.C:54
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Default mapping operation: the conversion of the item, which is its copy when the two types are the s...
DynList< int > l