Aleph-w 7.10.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
73#ifndef AH_DRY_H
74#define AH_DRY_H
75
76#include "ahFunctional.H"
77#include <ah-concepts.H>
78#include <concepts>
79#include <functional>
80#include <limits>
81#include <optional>
82#include <tuple>
83#include <type_traits>
84#include <utility>
85#include <vector>
86#include <ah-errors.H>
87#include <ah-iterator.H>
88
89namespace Aleph {
90template <class>
91class Array;
92}
93
95{
104 template <class T>
106 {
111 bool operator () (T & item) const noexcept;
112 };
113
122 template <class T>
124 {
129 bool operator () (const T & item) const noexcept;
130 };
131
142 template <class Container, class T>
144 requires(Container & c, Modifying_Probe<T> & op) { c.traverse(op); };
145
151 template <class Container>
153 requires(const Container & c, const typename Container::Key_Type & k)
154 {
155 c.size();
156 c.search(k) == nullptr;
157 };
158
168 template <class Container, class T>
169 inline constexpr bool const_traversal_is_noexcept =
170 noexcept(std::declval<const Container &>().traverse(std::declval<Reading_Probe<T> &>()));
171
179 template <class Container, class T>
180 inline constexpr bool mutable_traversal_is_noexcept =
181 noexcept(std::declval<Container &>().traverse(std::declval<Modifying_Probe<T> &>()));
182
184 template <class It>
185 inline constexpr bool advance_is_noexcept = []
186 {
187 if constexpr (requires(It & it) { it.next_ne(); })
188 return noexcept(std::declval<It &>().next_ne());
189 else
190 return noexcept(std::declval<It &>().next());
191 }();
192
195 template <class It>
196 inline constexpr bool current_is_noexcept = []
197 {
198 if constexpr (requires(It & it) { it.get_curr_ne(); })
199 return noexcept(std::declval<It &>().get_curr_ne());
200 else
201 return noexcept(std::declval<It &>().get_curr());
202 }();
203
213 template <class It>
214 void advance(It & it) noexcept(advance_is_noexcept<It>)
215 {
216 if constexpr (requires { it.next_ne(); })
217 it.next_ne();
218 else
219 it.next();
220 }
221
232 template <class It>
233 decltype(auto) current(It & it) noexcept(current_is_noexcept<It>)
234 {
235 if constexpr (requires { it.get_curr_ne(); })
236 return it.get_curr_ne();
237 else
238 return it.get_curr();
239 }
240
242
245 template <class C>
247 {
248 using type = typename C::value_type;
249 };
250
253 template <class C>
254 requires requires { typename C::Item_Type; }
255 struct inner_item<C>
256 {
257 using type = typename C::Item_Type;
258 };
259
269 template <class Op, class Acc, class T>
271 requires(Acc & acc, Op & op, const T & item) { acc = std::invoke(op, std::move(acc), item); }
272 or requires(Acc & acc, Op & op, const T & item) { acc = std::invoke(op, acc, item); };
273
283 template <class Op, class Acc, class T>
285 requires(Acc & acc, Op & op, const T & item) { acc = std::invoke(op, item, std::move(acc)); }
286 or requires(Acc & acc, Op & op, const T & item) { acc = std::invoke(op, item, acc); };
287
298 template <class Acc, class Step>
299 void assign_fold(Acc & acc, Step && step)
300 {
301 if constexpr (std::is_reference_v<std::invoke_result_t<Step &>>)
302 {
303 Acc next = step();
304 acc = std::move(next);
305 }
306 else
307 acc = step();
308 }
309
321 template <class Acc, class Op, class T>
322 void fold_left_step(Acc & acc, Op & op, const T & item)
323 {
324 if constexpr (std::is_invocable_v<Op &, Acc &&, const T &>)
325 assign_fold(acc, [&] () -> decltype(auto) { return std::invoke(op, std::move(acc), item); });
326 else
327 assign_fold(acc, [&] () -> decltype(auto) { return std::invoke(op, acc, item); });
328 }
329
337 template <class Acc, class Op, class T>
338 void fold_right_step(Acc & acc, Op & op, const T & item)
339 {
340 if constexpr (std::is_invocable_v<Op &, const T &, Acc &&>)
341 assign_fold(acc, [&] () -> decltype(auto) { return std::invoke(op, item, std::move(acc)); });
342 else
343 assign_fold(acc, [&] () -> decltype(auto) { return std::invoke(op, item, acc); });
344 }
345} // namespace Aleph::dry_detail
346
361template <class Container>
363{
364private:
365 // True if building the iterator on `Source`, walking it and calling
366 // `operation` with `Arg` cannot throw.
367 template <class Operation, class Arg, class Source>
368 static constexpr bool traverse_is_noexcept() noexcept
369 {
370 using It = typename Container::Iterator;
371 return std::is_nothrow_constructible_v<It, Source>
372 and noexcept(std::declval<It &>().has_curr())
373 and Aleph::dry_detail::advance_is_noexcept<It>
374 and Aleph::dry_detail::current_is_noexcept<It>
376 }
377
378 // The container a constant traversal builds its iterator on: the
379 // constant one, or the same object without `const` when the iterator
380 // only accepts a modifiable container; the traversal only reads.
381 template <class Deferred>
383 std::conditional_t<std::is_constructible_v<
388
389public:
409 template <class Operation>
411 bool traverse(Operation & operation)
412 noexcept(traverse_is_noexcept<Operation,
414 Container &>())
415 {
416 for (typename Container::Iterator it(*static_cast<Container *>(this)); it.has_curr();
418 if (not static_cast<bool>(operation(Aleph::dry_detail::current(it))))
419 return false;
420 return true;
421 }
422
434 template <class Operation>
436 bool traverse(Operation & operation) const
437 noexcept(traverse_is_noexcept<Operation,
440 {
441 using Source = const_source_t<Operation>;
442 auto & self = *const_cast<Container *>(static_cast<const Container *>(this));
443 for (auto it = Aleph::iterator_for(static_cast<Source>(self)); it.has_curr();
445 {
446 const auto & item = Aleph::dry_detail::current(it);
447 if (not static_cast<bool>(operation(item)))
448 return false;
449 }
450 return true;
451 }
452
462 template <class Operation>
464 bool traverse(Operation && operation) const
465 noexcept(traverse_is_noexcept<Operation,
468 {
469 return traverse<Operation>(operation);
470 }
471
481 template <class Operation>
483 bool traverse(Operation && operation)
484 noexcept(traverse_is_noexcept<Operation,
486 Container &>())
487 {
488 return traverse<Operation>(operation);
489 }
490};
491
505template <class Container, class Operation>
506bool traverse(const Container & c, Operation & op) noexcept(noexcept(c.traverse(op)))
507{
508 return c.traverse(op);
509}
510
525template <class Container, class Operation>
526bool traverse(const Container & c, Operation && op) noexcept(noexcept(c.traverse(op)))
527{
528 return c.traverse(op);
529}
530
553template <class Container, typename Type>
555{
556 Container * me() noexcept
557 {
558 return static_cast<Container *>(this);
559 }
560
561 const Container * const_me() const noexcept
562 {
563 return static_cast<const Container *>(this);
564 }
565
566 // True if calling `operation` with an item, through the constant
567 // traversal of the container, cannot throw, nor the conversion of its
568 // result to bool that the search makes, as a condition does.
569 template <class Operation>
570 static constexpr bool const_search_is_noexcept() noexcept
571 {
573 return requires (Operation & op, const Type & item)
574 { { static_cast<bool>(std::invoke(op, item)) } noexcept; }
575 and Aleph::dry_detail::const_traversal_is_noexcept<C, Type>;
576 }
577
578 // True if calling `operation` with an item, through the modifiable
579 // traversal of the container, cannot throw, nor the conversion of its
580 // result to bool that the search makes, as a condition does.
581 template <class Operation>
582 static constexpr bool mutable_search_is_noexcept() noexcept
583 {
585 return requires (Operation & op, Type & item)
586 { { static_cast<bool>(std::invoke(op, item)) } noexcept; }
587 and Aleph::dry_detail::mutable_traversal_is_noexcept<C, Type>;
588 }
589
594 template <class Operation>
595 static constexpr bool find_item_is_noexcept() noexcept
596 {
597 using TT = std::tuple<bool, Type>;
598 return const_search_is_noexcept<Operation>()
599 and std::is_nothrow_constructible_v<TT, bool, const Type &>
600 and std::is_nothrow_default_constructible_v<Type>
601 and std::is_nothrow_constructible_v<TT, bool, Type>;
602 }
603
604 // Address of the first item that satisfies `operation`, found with the
605 // constant traversal, or nullptr.
606 template <class Operation>
607 const Type * first_const_ptr(Operation & operation) const
608 noexcept(const_search_is_noexcept<Operation>())
609 {
610 const Type * ptr = nullptr;
611 const_me()->traverse([&ptr, &operation] (const Type & item)
612 {
613 if (static_cast<bool>(std::invoke(operation, item)))
614 {
615 ptr = &item;
616 return false;
617 }
618 return true;
619 });
620 return ptr;
621 }
622
623 // Address of the n-th item, found with the constant traversal, or
624 // nullptr if there are no more than n items.
625 const Type * const_nth_ptr(const size_t n) const
626 noexcept(Aleph::dry_detail::const_traversal_is_noexcept<Container, Type>)
627 {
628 const Type * ptr = nullptr;
629 size_t i = 0;
630 const_me()->traverse([&ptr, &i, n] (const Type & item)
631 {
632 if (i++ != n)
633 return true;
634 ptr = &item;
635 return false;
636 });
637 return ptr;
638 }
639
640 // Address of the n-th item, found with the modifiable traversal, or
641 // nullptr if there are no more than n items.
642 Type * mutable_nth_ptr(const size_t n)
643 noexcept(Aleph::dry_detail::mutable_traversal_is_noexcept<Container, Type>)
644 {
645 Type * ptr = nullptr;
646 size_t i = 0;
647 me()->traverse([&ptr, &i, n] (Type & item)
648 {
649 if (i++ != n)
650 return true;
651 ptr = &item;
652 return false;
653 });
654 return ptr;
655 }
656
657public:
663 [[nodiscard]] auto get_it()
664 {
665 return typename Container::Iterator(*me());
666 }
667
672 [[nodiscard]] auto get_it() const
673 {
675 }
676
685 [[nodiscard]] auto get_it(const size_t pos)
686 {
687 auto ret = typename Container::Iterator(*me());
688 for (size_t i = 0; i < pos; ++i)
689 ret.next();
690 return ret;
691 }
692
699 [[nodiscard]] auto get_it(const size_t pos) const
700 {
702 for (size_t i = 0; i < pos; ++i)
703 ret.next();
704 return ret;
705 }
706
711 [[nodiscard]] auto get_itor()
712 {
713 return get_it();
714 }
715
717 [[nodiscard]] auto get_itor() const
718 {
719 return get_it();
720 }
721
732 template <class C = Container> // a template so that Clang 14 checks the constraint on use
734 Type & nth_ne(const size_t n)
735 noexcept(Aleph::dry_detail::mutable_traversal_is_noexcept<C, Type>)
736 {
737 return *mutable_nth_ptr(n);
738 }
739
747 const Type & nth_ne(const size_t n) const
748 noexcept(Aleph::dry_detail::const_traversal_is_noexcept<Container, Type>)
749 {
750 return *const_nth_ptr(n);
751 }
752
772 template <class C = Container> // a template so that Clang 14 checks the constraint on use
774 Type & nth(const size_t n)
775 {
776 Type * ptr = mutable_nth_ptr(n);
777 // Test the pointer, not the count: i == n + 1 overflows for n == SIZE_MAX
778 ah_out_of_range_error_if(ptr == nullptr) << "index out of range";
779 return *ptr;
780 }
781
788 const Type & nth(const size_t n) const
789 {
790 const Type * ptr = const_nth_ptr(n);
791 ah_out_of_range_error_if(ptr == nullptr) << "index out of range";
792 return *ptr;
793 }
794
821 template <class Operation>
825 [[nodiscard]] Type * find_ptr(Operation & operation)
826 noexcept(mutable_search_is_noexcept<Operation>())
827 {
828 Type * ptr = nullptr;
829 me()->traverse([&ptr, &operation] (Type & item)
830 {
831 if (static_cast<bool>(std::invoke(operation, item)))
832 {
833 ptr = &item;
834 return false;
835 }
836 return true;
837 });
838 return ptr;
839 }
840
848 template <class Operation>
850 [[nodiscard]] const Type * find_ptr(Operation & operation) const
851 noexcept(const_search_is_noexcept<Operation>())
852 {
853 return first_const_ptr(operation);
854 }
855
867 template <class Operation>
869 [[nodiscard]] const Type * find_ptr(Operation && operation) const
870 noexcept(const_search_is_noexcept<Operation>())
871 {
872 return find_ptr<Operation>(operation);
873 }
874
886 template <class Operation>
890 [[nodiscard]] Type * find_ptr(Operation && operation)
891 noexcept(mutable_search_is_noexcept<Operation>())
892 {
893 return find_ptr<Operation>(operation);
894 }
895
919 template <class Operation>
921 [[nodiscard]] size_t find_index(Operation & operation) const
922 noexcept(const_search_is_noexcept<Operation>())
923 {
924 size_t i = 0;
925 const_me()->traverse([&i, &operation] (const Type & item)
926 {
927 if (static_cast<bool>(std::invoke(operation, item)))
928 return false;
929 ++i;
930 return true;
931 });
932 return i;
933 }
934
946 template <class Operation>
948 [[nodiscard]] size_t find_index(Operation && operation) const
949 noexcept(const_search_is_noexcept<Operation>())
950 {
951 return find_index<Operation>(operation);
952 }
953
980 template <class Operation, class T = Type> // T defers the checks on Type to calls (Clang 14)
982 and std::default_initializable<T> and std::copy_constructible<T>
983 [[nodiscard]] std::tuple<bool, Type> find_item(Operation & operation) const
984 noexcept(find_item_is_noexcept<Operation>())
985 {
986 using TT = std::tuple<bool, Type>;
987 // The container's constant find_ptr(): a container may hide this
988 // class's, as DynArray does.
989 auto ptr = const_me()->find_ptr(operation);
990 return ptr ? TT(true, *ptr) : TT(false, Type());
991 }
992
1004 template <class Operation, class T = Type>
1006 and std::default_initializable<T> and std::copy_constructible<T>
1007 [[nodiscard]] std::tuple<bool, Type> find_item(Operation && operation) const
1008 noexcept(find_item_is_noexcept<Operation>())
1009 {
1010 return find_item<Operation>(operation);
1011 }
1012
1025 template <class Operation, class T = Type> // T defers the checks on Type to calls (Clang 14)
1027 and std::copy_constructible<T>
1028 [[nodiscard]] std::optional<Type> find_opt(Operation & operation) const
1029 {
1030 auto ptr = const_me()->find_ptr(operation);
1031 if (ptr == nullptr)
1032 return std::nullopt;
1033 return std::optional<Type>(*ptr);
1034 }
1035
1043 template <class Operation, class T = Type>
1045 and std::copy_constructible<T>
1046 [[nodiscard]] std::optional<Type> find_opt(Operation && operation) const
1047 {
1048 return find_opt<Operation>(operation);
1049 }
1050
1058 template <class Operation>
1060 [[nodiscard]] bool contains_if(Operation && operation) const
1061 noexcept(const_search_is_noexcept<Operation>())
1062 {
1063 return first_const_ptr(operation) != nullptr;
1064 }
1065
1072 template <class T = Type> // a template so that Clang 14 checks the constraint on use
1073 requires requires(const T & a, const T & b) { { a == b } -> std::convertible_to<bool>; }
1074 [[nodiscard]] bool contains(const Type & item) const
1075 {
1076 return contains_if([&item] (const Type & x)
1077 {
1078 return x == item;
1079 });
1080 }
1081};
1082
1098template <class Container, typename T>
1100{
1102 SpecialCtors() = default;
1103
1105 SpecialCtors(const SpecialCtors &) = default;
1106
1109
1112
1117 {
1118 return *this;
1119 }
1120
1129 {
1130 build_apart([&l] (Container & c)
1131 {
1132 l.for_each([&c] (const T & item) { c.append(item); });
1133 });
1134 }
1135
1147 template <class It>
1148 requires (not std::is_arithmetic_v<It>
1149 and requires(It it) { *it; ++it; it != it; })
1151 {
1152 build_apart([&b, &e] (Container & c)
1153 {
1154 for (It it = b; it != e; ++it)
1155 c.append(*it);
1156 });
1157 }
1158
1166 SpecialCtors(std::initializer_list<T> l)
1167 {
1168 build_apart([&l] (Container & c)
1169 {
1170 for (const auto & item : l)
1171 c.append(item);
1172 });
1173 }
1174
1175private:
1182 void free_appended() noexcept
1183 {
1184 if constexpr (requires (Container & c) { c.empty(); })
1185 static_cast<Container *>(this)->empty();
1186 }
1187
1198 template <class Fill>
1200 {
1201 if constexpr (std::is_default_constructible_v<Container> and
1202 requires (Container & a, Container & b) { a.swap(b); })
1203 {
1204 Container built;
1205 fill(built);
1206 static_cast<Container *>(this)->swap(built);
1207 }
1208 else
1209 try
1210 {
1211 fill(*static_cast<Container *>(this));
1212 }
1213 catch (...)
1214 {
1215 free_appended();
1216 throw;
1217 }
1218 }
1219};
1220
1233template <class Container, typename T>
1235{
1236 Container * me() noexcept
1237 {
1238 return static_cast<Container *>(this);
1239 }
1240
1241 const Container * const_me() const noexcept
1242 {
1243 return static_cast<const Container *>(this);
1244 }
1245
1246 // Constant traversal handing each item, as `const T &`, to `operation`.
1247 template <class Operation>
1248 bool const_traverse(Operation && operation) const
1249 {
1250 return const_me()->traverse(operation);
1251 }
1252
1253 // What insert() and append() receive for an argument of type `Arg`: the
1254 // argument itself when it is a T, or else a T converted from it, so that
1255 // the container's overload for an item is the one called (DynList, for
1256 // instance, also has an append() that concatenates a whole list).
1257 template <class Arg>
1258 using Item_Arg = std::conditional_t<std::is_same_v<std::remove_cvref_t<Arg>, T>, Arg, T>;
1259
1260 template <class Arg>
1261 static decltype(auto) as_item(Arg && arg)
1262 {
1263 if constexpr (std::is_same_v<std::remove_cvref_t<Arg>, T>)
1264 return std::forward<Arg>(arg);
1265 else
1266 return static_cast<T>(std::forward<Arg>(arg));
1267 }
1268
1269public:
1270 template <typename... Args>
1271 requires std::constructible_from<T, Args...>
1272 and requires(typename Aleph::concepts_detail::defer<Container, std::tuple<Args...>>::type & c, T && x)
1273 { c.append(std::move(x)); }
1292 void emplace(Args &&... args)
1293 {
1294 T item(std::forward<Args>(args)...);
1295 (void) me()->append(std::move(item));
1296 }
1297
1298 template <typename... Args>
1299 requires std::constructible_from<T, Args...>
1300 and requires(typename Aleph::concepts_detail::defer<Container, std::tuple<Args...>>::type & c, T && x)
1301 { c.append(std::move(x)); }
1303 void emplace_end(Args &&... args)
1304 {
1305 T item(std::forward<Args>(args)...);
1306 (void) me()->append(std::move(item));
1307 }
1308
1325 template <typename... Args>
1326 requires std::constructible_from<T, Args...>
1327 and requires(typename Aleph::concepts_detail::defer<Container, std::tuple<Args...>>::type & c, T && x)
1328 { c.insert(std::move(x)); }
1329 void emplace_ins(Args &&... args)
1330 {
1331 T item(std::forward<Args>(args)...);
1332 (void) me()->insert(std::move(item));
1333 }
1334
1349 template <typename... Args>
1350 requires ((std::convertible_to<Args &&, T> and ...)
1351 and (Aleph::InsertableWith<typename Aleph::concepts_detail::defer<Container, std::tuple<Args...>>::type,
1352 Item_Arg<Args>> and ...))
1353 size_t ninsert(Args &&... args)
1354 {
1355 size_t n = 0;
1356 ((n += (Aleph::functional_detail::insert_one(*me(), as_item(std::forward<Args>(args))) ? 1 : 0)), ...);
1357 return n;
1358 }
1359
1375 template <typename... Args>
1376 requires ((std::convertible_to<Args &&, T> and ...)
1377 and (Aleph::AppendableWith<typename Aleph::concepts_detail::defer<Container, std::tuple<Args...>>::type,
1378 Item_Arg<Args>> and ...))
1379 size_t nappend(Args &&... args)
1380 {
1381 size_t n = 0;
1382 ((n += (Aleph::functional_detail::append_one(*me(), as_item(std::forward<Args>(args))) ? 1 : 0)), ...);
1383 return n;
1384 }
1385
1419 template <typename... Items>
1420 requires ((std::convertible_to<Items &&, T> and ...)
1421 and (Aleph::AppendableWith<typename Aleph::concepts_detail::defer<Container, std::tuple<Items...>>::type,
1422 Item_Arg<Items>> and ...))
1423 [[nodiscard]] static Container build(Items &&... items)
1424 {
1425 return build_as<Container>(std::forward<Items>(items)...);
1426 }
1427
1442 template <class D, typename... Items>
1443 requires (std::derived_from<D, typename Aleph::concepts_detail::defer<Container, D>::type>
1444 and std::default_initializable<D>
1445 and (std::convertible_to<Items &&, T> and ...)
1446 and (Aleph::AppendableWith<typename Aleph::concepts_detail::defer<Container, std::tuple<D, Items...>>::type,
1447 Item_Arg<Items>> and ...))
1448 [[nodiscard]] static D build_as(Items &&... items)
1449 {
1450 D d;
1451 auto & c = static_cast<Container &>(d);
1452 ((void) c.append(as_item(std::forward<Items>(items))), ...);
1453 return d;
1454 }
1455
1473 template <class Operation>
1475 void for_each(Operation & operation) const
1476 {
1477 const_traverse([&operation] (const T & item)
1478 {
1479 std::invoke(operation, item);
1480 return true;
1481 });
1482 }
1483
1494 template <class Operation>
1496 void for_each(Operation && operation) const
1497 {
1498 for_each<Operation>(operation);
1499 }
1500
1505 template <class Operation>
1507 void each(Operation & operation) const
1508 {
1509 for_each(operation);
1510 }
1511
1522 template <class Operation>
1524 void each(Operation && operation) const
1525 {
1526 for_each<Operation>(operation);
1527 }
1528
1543 template <class Operation>
1545 void each(const size_t pos, const size_t slice, Operation & operation) const
1546 {
1547 if (slice == 0)
1548 return;
1549 size_t i = 0;
1550 size_t next = pos;
1551 const_traverse([&i, &next, slice, &operation] (const T & item)
1552 {
1553 if (i++ != next)
1554 return true;
1555 std::invoke(operation, item);
1556 if (std::numeric_limits<size_t>::max() - next < slice)
1557 return false; // the next position does not exist
1558 next += slice;
1559 return true;
1560 });
1561 }
1562
1564 template <class Operation>
1566 void each(const size_t pos, const size_t slice, Operation && operation) const
1567 {
1568 each<Operation>(pos, slice, operation);
1569 }
1570
1586 template <class Operation>
1590 void each(const size_t pos, const size_t slice, Operation & operation)
1591 {
1592 if (slice == 0)
1593 return;
1594 size_t i = 0;
1595 size_t next = pos;
1596 me()->traverse([&i, &next, slice, &operation] (T & item)
1597 {
1598 if (i++ != next)
1599 return true;
1600 std::invoke(operation, item);
1601 if (std::numeric_limits<size_t>::max() - next < slice)
1602 return false; // the next position does not exist
1603 next += slice;
1604 return true;
1605 });
1606 }
1607
1609 template <class Operation>
1613 void each(const size_t pos, const size_t slice, Operation && operation)
1614 {
1615 each<Operation>(pos, slice, operation);
1616 }
1617
1629 template <class Operation>
1633 void mutable_for_each(Operation & operation)
1634 {
1635 me()->traverse([&operation] (T & item)
1636 {
1637 std::invoke(operation, item);
1638 return true;
1639 });
1640 }
1641
1643 template <class Operation>
1647 void mutable_for_each(Operation && operation)
1648 {
1649 mutable_for_each<Operation>(operation);
1650 }
1651
1664 template <class Operation>
1666 [[nodiscard]] bool all(Operation & operation) const
1667 {
1668 return const_traverse([&operation] (const T & item) -> bool
1669 {
1670 return static_cast<bool>(std::invoke(operation, item));
1671 });
1672 }
1673
1685 template <class Operation>
1687 [[nodiscard]] bool all(Operation && operation) const
1688 {
1689 return all<Operation>(operation);
1690 }
1691
1705 template <class Operation>
1707 [[nodiscard]] bool exists(Operation & op) const
1708 {
1709 return not const_traverse([&op] (const T & item) -> bool
1710 {
1711 return not std::invoke(op, item);
1712 });
1713 }
1714
1726 template <class Operation>
1728 [[nodiscard]] bool exists(Operation && op) const
1729 {
1730 return exists<Operation>(op);
1731 }
1732
1744 template <class Operation>
1746 [[nodiscard]] bool none(Operation & op) const
1747 {
1748 return not exists(op);
1749 }
1750
1756 template <class Operation>
1758 [[nodiscard]] bool none(Operation && op) const
1759 {
1760 return none<Operation>(op);
1761 }
1762
1770 template <class Operation>
1772 [[nodiscard]] size_t count_if(Operation & op) const
1773 {
1774 size_t count = 0;
1775 const_traverse([&count, &op] (const T & item)
1776 {
1777 if (std::invoke(op, item))
1778 ++count;
1779 return true;
1780 });
1781 return count;
1782 }
1783
1789 template <class Operation>
1791 [[nodiscard]] size_t count_if(Operation && op) const
1792 {
1793 return count_if<Operation>(op);
1794 }
1795
1823 template <typename R = void, class Operation>
1826 Operation & op, const T & item)
1827 { l.append(std::invoke(op, item)); }
1829 maps(Operation & op) const
1830 {
1832 const_traverse([&ret_val, &op] (const T & item)
1833 {
1834 ret_val.append(std::invoke(op, item));
1835 return true;
1836 });
1837 return ret_val;
1838 }
1839
1853 template <typename R = void, class Operation>
1856 Operation & op, const T & item)
1857 { l.append(std::invoke(op, item)); }
1859 maps(Operation && op) const
1860 {
1861 return maps<R, Operation>(op);
1862 }
1863
1882 template <typename R = void, class Prop, class Operation>
1886 Operation & op, const T & item)
1887 { l.append(std::invoke(op, item)); }
1889 maps_if(Prop prop, Operation & op) const
1890 {
1892 const_traverse([&ret_val, &prop, &op] (const T & item)
1893 {
1894 if (std::invoke(prop, item))
1895 ret_val.append(std::invoke(op, item));
1896 return true;
1897 });
1898 return ret_val;
1899 }
1900
1916 template <typename R = void, class Prop, class Operation>
1920 Operation & op, const T & item)
1921 { l.append(std::invoke(op, item)); }
1923 maps_if(Prop prop, Operation && op) const
1924 {
1925 return maps_if<R, Prop, Operation>(prop, op);
1926 }
1927
1940 template <typename R = void, class Operation>
1943 Operation & op, const T & item)
1944 { l.append(std::invoke(op, item)); }
1946 map(Operation & op) const
1947 {
1948 return maps<R, Operation>(op);
1949 }
1950
1961 template <typename R = void, class Operation>
1964 Operation & op, const T & item)
1965 { l.append(std::invoke(op, item)); }
1967 map(Operation && op) const
1968 {
1969 return maps<R, Operation>(op);
1970 }
1971
1983 template <typename R = void, class Prop, class Operation>
1987 Operation & op, const T & item)
1988 { l.append(std::invoke(op, item)); }
1990 map_if(Prop prop, Operation & op) const
1991 {
1992 return maps_if<R, Prop, Operation>(prop, op);
1993 }
1994
2007 template <typename R = void, class Prop, class Operation>
2011 Operation & op, const T & item)
2012 { l.append(std::invoke(op, item)); }
2014 map_if(Prop prop, Operation && op) const
2015 {
2016 return maps_if<R, Prop, Operation>(prop, op);
2017 }
2018
2032 template <class Operation>
2034 [[nodiscard]] auto flat_map(Operation & op) const
2035 {
2036 using Inner = std::decay_t<std::invoke_result_t<Operation &, const T &>>;
2038 Aleph::DynList<R> ret_val;
2039 const_traverse([&ret_val, &op] (const T & item)
2040 {
2041 auto && sub = std::invoke(op, item);
2042 if constexpr (requires { sub.for_each(std::declval<void (*)(const R &)>()); })
2043 sub.for_each([&ret_val] (const R & x)
2044 {
2045 ret_val.append(x);
2046 });
2047 else
2048 for (auto && x : sub)
2049 ret_val.append(x);
2050 return true;
2051 });
2052 return ret_val;
2053 }
2054
2060 template <class Operation>
2062 [[nodiscard]] auto flat_map(Operation && op) const
2063 {
2064 return flat_map<Operation>(op);
2065 }
2066
2072 [[nodiscard]] Aleph::DynList<T> to_dynlist() const
2073 {
2075 const_traverse([&ret] (const T & item)
2076 {
2077 ret.append(item);
2078 return true;
2079 });
2080 return ret;
2081 }
2082
2088 [[nodiscard]] std::vector<T> to_vector() const
2089 {
2090 std::vector<T> ret;
2091 if constexpr (requires { const_me()->size(); })
2092 ret.reserve(const_me()->size());
2093 const_traverse([&ret] (const T & item)
2094 {
2095 ret.push_back(item);
2096 return true;
2097 });
2098 return ret;
2099 }
2100
2143 template <typename Acc = T, class Op>
2145 [[nodiscard]] Acc foldl(const Acc & init, Op & op) const
2146 {
2147 Acc acc = init;
2148 const_traverse([&acc, &op] (const T & item)
2149 {
2150 Aleph::dry_detail::fold_left_step(acc, op, item);
2151 return true;
2152 });
2153 return acc;
2154 }
2155
2169 template <typename Acc = T, class Op>
2171 [[nodiscard]] Acc foldl(const Acc & init, Op && op) const
2172 {
2173 return foldl<Acc, Op>(init, op);
2174 }
2175
2184 template <typename Acc = T, class Op>
2186 [[nodiscard]] Acc fold_left(const Acc & init, Op & op) const
2187 {
2188 return foldl<Acc, Op>(init, op);
2189 }
2190
2204 template <typename Acc = T, class Op>
2206 [[nodiscard]] Acc fold_left(const Acc & init, Op && op) const
2207 {
2208 return foldl<Acc, Op>(init, op);
2209 }
2210
2220 template <class Operation>
2222 [[nodiscard]] T fold(const T & init, Operation & operation) const
2223 {
2224 return foldl<T, Operation>(init, operation);
2225 }
2226
2239 template <class Operation>
2241 [[nodiscard]] T fold(const T & init, Operation && operation) const
2242 {
2243 return foldl<T, Operation>(init, operation);
2244 }
2245
2261 template <typename Acc = T, class Op>
2263 [[nodiscard]] Acc foldr(const Acc & init, Op & op) const
2264 {
2265 Aleph::DynList<const T *> reversed; // the last item first
2266 const_traverse([&reversed] (const T & item)
2267 {
2268 reversed.insert(&item);
2269 return true;
2270 });
2271 Acc acc = init;
2272 reversed.for_each([&acc, &op] (const T * item)
2273 {
2274 Aleph::dry_detail::fold_right_step(acc, op, *item);
2275 });
2276 return acc;
2277 }
2278
2285 template <typename Acc = T, class Op>
2287 [[nodiscard]] Acc foldr(const Acc & init, Op && op) const
2288 {
2289 return foldr<Acc, Op>(init, op);
2290 }
2291
2303 template <class Op, class U = T> // U defers the check on T to calls (Clang 14)
2304 requires Aleph::dry_detail::FoldOperation<Op, T, T> and std::copy_constructible<U>
2305 [[nodiscard]] std::optional<T> reduce(Op & op) const
2306 {
2307 std::optional<T> acc;
2308 const_traverse([&acc, &op] (const T & item)
2309 {
2310 if (not acc.has_value())
2311 acc.emplace(item);
2312 else
2313 Aleph::dry_detail::fold_left_step(*acc, op, item);
2314 return true;
2315 });
2316 return acc;
2317 }
2318
2324 template <class Op, class U = T>
2325 requires Aleph::dry_detail::FoldOperation<Op, T, T> and std::copy_constructible<U>
2326 [[nodiscard]] std::optional<T> reduce(Op && op) const
2327 {
2328 return reduce<Op>(op);
2329 }
2330
2354 template <class Operation>
2356 [[nodiscard]] Aleph::DynList<T> filter(Operation & operation) const
2357 {
2358 Aleph::DynList<T> ret_val;
2359 const_traverse([&ret_val, &operation] (const T & item)
2360 {
2361 if (std::invoke(operation, item))
2362 ret_val.append(item);
2363 return true;
2364 });
2365 return ret_val;
2366 }
2367
2379 template <class Operation>
2381 [[nodiscard]] Aleph::DynList<T> filter(Operation && operation) const
2382 {
2383 return filter<Operation>(operation);
2384 }
2385
2409 template <class Operation>
2411 [[nodiscard]] Aleph::DynList<const T *> ptr_filter(Operation & operation) const
2412 {
2414 const_traverse([&ret_val, &operation] (const T & item)
2415 {
2416 if (std::invoke(operation, item))
2417 ret_val.append(&item);
2418 return true;
2419 });
2420 return ret_val;
2421 }
2422
2434 template <class Operation>
2436 [[nodiscard]] Aleph::DynList<const T *> ptr_filter(Operation && operation) const
2437 {
2438 return ptr_filter<Operation>(operation);
2439 }
2440
2461 template <class Operation>
2463 [[nodiscard]] Aleph::DynList<std::tuple<T, size_t>> pfilter(Operation & operation) const
2464 {
2465 using TT = std::tuple<T, size_t>;
2466 Aleph::DynList<TT> ret_val;
2467 size_t i = 0;
2468 const_traverse([&ret_val, &operation, &i] (const T & item)
2469 {
2470 if (std::invoke(operation, item))
2471 ret_val.append(TT(item, i));
2472 ++i;
2473 return true;
2474 });
2475 return ret_val;
2476 }
2477
2489 template <class Operation>
2491 [[nodiscard]] Aleph::DynList<std::tuple<T, size_t>> pfilter(Operation && operation) const
2492 {
2493 return pfilter<Operation>(operation);
2494 }
2495
2513 template <class Operation>
2515 [[nodiscard]] std::pair<Aleph::DynList<T>, Aleph::DynList<T>> partition(Operation & op) const
2516 {
2517 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> ret_val;
2518 const_traverse([&ret_val, &op] (const T & item)
2519 {
2520 if (std::invoke(op, item))
2521 ret_val.first.append(item);
2522 else
2523 ret_val.second.append(item);
2524 return true;
2525 });
2526 return ret_val;
2527 }
2528
2540 template <class Operation>
2542 [[nodiscard]] std::pair<Aleph::DynList<T>, Aleph::DynList<T>> partition(Operation && op) const
2543 {
2544 return partition<Operation>(op);
2545 }
2546
2557 [[nodiscard]] std::pair<Aleph::DynList<T>, Aleph::DynList<T>> partition(size_t n) const
2558 {
2559 size_t i = 0;
2560 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> ret_val;
2561 const_traverse([&ret_val, &i, n] (const T & item)
2562 {
2563 if (i++ < n)
2564 ret_val.first.append(item);
2565 else
2566 ret_val.second.append(item);
2567 return true;
2568 });
2569 return ret_val;
2570 }
2571
2579 [[nodiscard]] std::pair<Aleph::DynList<T>, Aleph::DynList<T>> split_half() const
2580 {
2581 size_t i = 0;
2582 std::pair<Aleph::DynList<T>, Aleph::DynList<T>> ret_val;
2583 const_traverse([&ret_val, &i] (const T & item)
2584 {
2585 if (i % 2 == 0)
2586 ret_val.first.append(item);
2587 else
2588 ret_val.second.append(item);
2589 i++;
2590 return true;
2591 });
2592 return ret_val;
2593 }
2594
2608 template <class Operation>
2610 [[nodiscard]] std::tuple<Aleph::DynList<T>, Aleph::DynList<T>> tpartition(Operation & op) const
2611 {
2612 Aleph::DynList<T> r1, r2;
2613 const_traverse([&r1, &r2, &op] (const T & item)
2614 {
2615 if (std::invoke(op, item))
2616 r1.append(item);
2617 else
2618 r2.append(item);
2619 return true;
2620 });
2621 return std::tuple<Aleph::DynList<T>, Aleph::DynList<T>>(std::move(r1), std::move(r2));
2622 }
2623
2635 template <class Operation>
2637 [[nodiscard]] std::tuple<Aleph::DynList<T>, Aleph::DynList<T>> tpartition(Operation && op) const
2638 {
2639 return tpartition<Operation>(op);
2640 }
2641
2654 [[nodiscard]] size_t length() const
2655 noexcept(Aleph::dry_detail::const_traversal_is_noexcept<Container, T>)
2656 {
2657 size_t count = 0;
2658 const_traverse([&count] (const T &) noexcept
2659 {
2660 ++count;
2661 return true;
2662 });
2663 return count;
2664 }
2665
2673 [[nodiscard]] Aleph::DynList<T> rev() const
2674 {
2676 const_traverse([&ret] (const T & item)
2677 {
2678 ret.insert(item);
2679 return true;
2680 });
2681 return ret;
2682 }
2683
2696 [[nodiscard]] Aleph::DynList<T> take(const size_t n) const
2697 {
2698 size_t i = 0;
2700 const_traverse([&i, &ret, n] (const T & item)
2701 {
2702 if (i++ >= n)
2703 return false;
2704 ret.append(item);
2705 return true;
2706 });
2707 return ret;
2708 }
2709
2726 [[nodiscard]] Aleph::DynList<T> take(size_t i, const size_t j, const size_t step = 1) const
2727 {
2729 if (step == 0 or i > j)
2730 return ret;
2731 size_t pos = 0;
2732 const_traverse([&pos, &i, j, step, &ret] (const T & item)
2733 {
2734 if (pos++ != i)
2735 return true;
2736 ret.append(item);
2737 if (j - i < step) // the next position would pass j, or overflow
2738 return false;
2739 i += step;
2740 return true;
2741 });
2742 return ret;
2743 }
2744
2757 [[nodiscard]] Aleph::DynList<T> drop(const size_t n) const
2758 {
2759 size_t i = 0;
2761 const_traverse([&i, &ret, n] (const T & item)
2762 {
2763 if (i++ >= n)
2764 ret.append(item);
2765 return true;
2766 });
2767 return ret;
2768 }
2769
2780 template <class Operation>
2782 [[nodiscard]] Aleph::DynList<T> take_while(Operation & op) const
2783 {
2785 const_traverse([&ret, &op] (const T & item)
2786 {
2787 if (not std::invoke(op, item))
2788 return false;
2789 ret.append(item);
2790 return true;
2791 });
2792 return ret;
2793 }
2794
2800 template <class Operation>
2802 [[nodiscard]] Aleph::DynList<T> take_while(Operation && op) const
2803 {
2804 return take_while<Operation>(op);
2805 }
2806
2816 template <class Operation>
2818 [[nodiscard]] Aleph::DynList<T> drop_while(Operation & op) const
2819 {
2821 bool dropping = true;
2822 const_traverse([&ret, &op, &dropping] (const T & item)
2823 {
2824 if (dropping and std::invoke(op, item))
2825 return true;
2826 dropping = false;
2827 ret.append(item);
2828 return true;
2829 });
2830 return ret;
2831 }
2832
2839 template <class Operation>
2841 [[nodiscard]] Aleph::DynList<T> drop_while(Operation && op) const
2842 {
2843 return drop_while<Operation>(op);
2844 }
2845
2861 template <class C = Container> // a template so that Clang 14 checks the constraint on use
2862 requires requires (C & c) { c.remove(); }
2863 void mutable_drop(const size_t n)
2864 {
2865 for (size_t i = 0; i < n; ++i)
2866 me()->remove();
2867 }
2868};
2869
2881# define Derived_Build(Name) \
2882 template <typename... Items> \
2883 requires requires (Items &&... items) \
2884 { Name::template build_as<Name>(std::forward<Items>(items)...); } \
2885 [[nodiscard]] static Name build(Items &&... items) \
2886 { \
2887 return Name::template build_as<Name>(std::forward<Items>(items)...); \
2888 }
2889
2894template <class Container, typename T>
2896{
2904 [[nodiscard]] Aleph::DynList<T> items() const
2905 {
2906 return static_cast<const Container *>(this)->Container::template maps<T>([] (const T & key) -> const T &
2907 {
2908 return key;
2909 });
2910 }
2911
2916 [[nodiscard]] Aleph::DynList<T> keys() const
2917 {
2918 return items();
2919 }
2920};
2921
2926template <class Container, typename T>
2928
2943template <class Container>
2945{
2946 const Container * const_me() const noexcept
2947 {
2948 return static_cast<const Container *>(this);
2949 }
2950
2951public:
2963 [[nodiscard]] bool equal_to(const Container & r) const
2964 {
2965 if (const_me() == &r)
2966 return true;
2967
2968 if (const_me()->size() != r.size())
2969 return false;
2970
2973
2974 // Same size: it2 has a current item whenever it1 has one
2977 return false;
2978
2979 return true;
2980 }
2981
2994 [[nodiscard]] friend bool operator == (const Container & l, const Container & r)
2995 {
2996 return l.equal_to(r);
2997 }
2998
3005 [[nodiscard]] friend bool operator != (const Container & l, const Container & r)
3006 {
3007 return not l.equal_to(r);
3008 }
3009};
3010
3024template <class Container>
3026{
3027 const Container * const_me() const noexcept
3028 {
3029 return static_cast<const Container *>(this);
3030 }
3031
3032public:
3051 template <class C = Container> // checked on use: Clang 14 checks a
3052 // non-template member's constraint early
3053 [[nodiscard]] bool equal_to(const Container & r) const
3055 {
3056 if (const_me() == &r) // Use const_me() to get correct pointer in case of multiple inheritance
3057 return true;
3058
3059 if (const_me()->size() != r.size())
3060 return false;
3061
3062 return const_me()->all([&r] (const typename C::Key_Type & k)
3063 {
3064 return r.search(k) != nullptr;
3065 });
3066 }
3067
3080 template <class C = Container>
3082 [[nodiscard]] friend bool operator == (const Container & l, const Container & r)
3083 {
3084 return l.equal_to(r);
3085 }
3086
3093 template <class C = Container>
3095 [[nodiscard]] friend bool operator != (const Container & l, const Container & r)
3096 {
3097 return not l.equal_to(r);
3098 }
3099};
3100
3113template <class Container>
3115{
3116 const Container * const_me() const noexcept
3117 {
3118 return static_cast<const Container *>(this);
3119 }
3120
3121public:
3129 [[nodiscard]] bool equal_to(const Container & r) const
3130 {
3131 if (const_me() == &r)
3132 return true;
3133
3134 if (const_me()->size() != r.size())
3135 return false;
3136
3137 const auto cmp = const_me()->key_comp();
3140
3141 // Same size: it2 has a current item whenever it1 has one
3143 {
3144 const auto & a = Aleph::dry_detail::current(it1);
3145 const auto & b = Aleph::dry_detail::current(it2);
3146 if (cmp(a, b) or cmp(b, a))
3147 return false;
3148 }
3149
3150 return true;
3151 }
3152
3165 [[nodiscard]] friend bool operator == (const Container & l, const Container & r)
3166 {
3167 return l.equal_to(r);
3168 }
3169
3176 [[nodiscard]] friend bool operator != (const Container & l, const Container & r)
3177 {
3178 return not l.equal_to(r);
3179 }
3180};
3181
3188template <class Container, typename Key, typename Data>
3190{
3191 const Container * const_me() const noexcept
3192 {
3193 return static_cast<const Container *>(this);
3194 }
3195
3196 Container * me() noexcept
3197 {
3198 return static_cast<Container *>(this);
3199 }
3200
3201public:
3215 template <template <typename> class C = Aleph::DynList>
3216 [[nodiscard]] C<Key> keys() const
3217 {
3218 C<Key> ret_val;
3219 const_me()->for_each([&ret_val] (const std::pair<Key, Data> & p)
3220 {
3221 ret_val.append(p.first);
3222 });
3223 return ret_val;
3224 }
3225
3239 template <template <typename> class C = Aleph::DynList>
3240 [[nodiscard]] C<Data> values() const
3241 {
3242 C<Data> ret_val;
3243 const_me()->for_each([&ret_val] (const std::pair<Key, Data> & p)
3244 {
3245 ret_val.append(p.second);
3246 });
3247 return ret_val;
3248 }
3249
3265 template <template <typename> class C = Aleph::DynList>
3267 typename Aleph::concepts_detail::defer<Container, C<Data>>::type, std::pair<Key, Data>>
3268 [[nodiscard]] C<Data *> values_ptr()
3269 {
3270 C<Data *> ret_val;
3271 me()->traverse([&ret_val] (std::pair<Key, Data> & p)
3272 {
3273 ret_val.append(&p.second);
3274 return true;
3275 });
3276 return ret_val;
3277 }
3278
3284 template <template <typename> class C = Aleph::DynList>
3285 [[nodiscard]] C<const Data *> values_ptr() const
3286 {
3287 C<const Data *> ret_val;
3288 const_me()->for_each([&ret_val] (const std::pair<Key, Data> & p)
3289 {
3290 ret_val.append(&p.second);
3291 });
3292 return ret_val;
3293 }
3294
3304 template <template <typename> class C = Aleph::DynList>
3305 [[nodiscard]] C<std::pair<Key, Data>> items() const
3306 {
3307 C<std::pair<Key, Data>> ret;
3308 const_me()->for_each([&ret] (const std::pair<Key, Data> & p)
3309 {
3310 ret.append(p);
3311 });
3312 return ret;
3313 }
3314
3327 template <template <typename> class C = Aleph::DynList>
3329 typename Aleph::concepts_detail::defer<Container, C<Data>>::type, std::pair<Key, Data>>
3330 [[nodiscard]] C<std::pair<Key, Data *>> items_ptr()
3331 {
3332 C<std::pair<Key, Data *>> ret_val;
3333 me()->traverse([&ret_val] (std::pair<Key, Data> & p)
3334 {
3335 ret_val.append(std::pair<Key, Data *>(p.first, &p.second));
3336 return true;
3337 });
3338 return ret_val;
3339 }
3340
3346 template <template <typename> class C = Aleph::DynList>
3347 [[nodiscard]] C<std::pair<Key, const Data *>> items_ptr() const
3348 {
3349 C<std::pair<Key, const Data *>> ret_val;
3350 const_me()->for_each([&ret_val] (const std::pair<Key, Data> & p)
3351 {
3352 ret_val.append(std::pair<Key, const Data *>(p.first, &p.second));
3353 });
3354 return ret_val;
3355 }
3356
3363 Data & operator () (const Key & key)
3364 {
3365 // CRTP: `find` lives on the derived `Container`, not on this base.
3366 // Casting via `me()` is required for AppleClang (which performs the
3367 // member lookup eagerly at template definition time) and is the
3368 // correct pattern regardless of compiler leniency.
3369 return me()->find(key);
3370 }
3371
3378 const Data & operator () (const Key & key) const
3379 {
3380 return const_me()->find(key);
3381 }
3382};
3383#endif // AH_DRY_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
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:506
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.
Functional programming utilities for Aleph-w containers.
Read-only view of an Aleph iterator: the iterator of a constant container.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:2945
friend bool operator!=(const Container &l, const Container &r)
Inequality operator.
Definition ah-dry.H:3005
friend bool operator==(const Container &l, const Container &r)
Equality operator.
Definition ah-dry.H:2994
bool equal_to(const Container &r) const
Equality test between this and r.
Definition ah-dry.H:2963
const Container * const_me() const noexcept
Definition ah-dry.H:2946
Equality test for containers sorted by a comparator.
Definition ah-dry.H:3115
friend bool operator!=(const Container &l, const Container &r)
Inequality operator.
Definition ah-dry.H:3176
const Container * const_me() const noexcept
Definition ah-dry.H:3116
bool equal_to(const Container &r) const
Equality test between this and r.
Definition ah-dry.H:3129
friend bool operator==(const Container &l, const Container &r)
Equality operator.
Definition ah-dry.H:3165
Equality test for sets.
Definition ah-dry.H:3026
const Container * const_me() const noexcept
Definition ah-dry.H:3027
friend bool operator==(const Container &l, const Container &r)
Equality operator: l.equal_to(r).
Definition ah-dry.H:3082
bool equal_to(const Container &r) const
Test if elements of this are exactly contained in another container.
Definition ah-dry.H:3053
friend bool operator!=(const Container &l, const Container &r)
Negation of equal_to()
Definition ah-dry.H:3095
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
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:2515
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, T > void each(const size_t pos, const size_t slice, Operation &operation)
Traverse the container starting at pos taking one item every slice, performing an operation that may ...
Definition ah-dry.H:1590
void emplace(Args &&... args)
Appends a new element into the container by constructing it from the given args.
Definition ah-dry.H:1292
Acc foldr(const Acc &init, Op &op) const
Fold the elements from the last to the first.
Definition ah-dry.H:2263
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps(Operation &op) const
Definition ah-dry.H:1829
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, T > void mutable_for_each(Operation &&operation)
See mutable_for_each(Operation & operation).
Definition ah-dry.H:1647
std::vector< T > to_vector() const
Convert container to std::vector.
Definition ah-dry.H:2088
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map(Operation &op) const
Definition ah-dry.H:1946
size_t length() const noexcept(Aleph::dry_detail::const_traversal_is_noexcept< Container, T >)
Count the number of elements of a container.
Definition ah-dry.H:2654
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:2557
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
Definition ah-dry.H:1707
void each(Operation &operation) const
Alias of for_each().
Definition ah-dry.H:1507
Aleph::DynList< std::tuple< T, size_t > > pfilter(Operation &&operation) const
Overload of pfilter() that accepts rvalues.
Definition ah-dry.H:2491
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, T > void mutable_for_each(Operation &operation)
Apply a mutable operation to each element of the container.
Definition ah-dry.H:1633
Aleph::DynList< T > to_dynlist() const
Convert container to DynList.
Definition ah-dry.H:2072
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:2463
and std::copy_constructible< U > std::optional< T > reduce(Op &op) const
Fold the elements with no initial value.
Definition ah-dry.H:2305
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:2222
std::conditional_t< std::is_same_v< std::remove_cvref_t< Arg >, T >, Arg, T > Item_Arg
Definition ah-dry.H:1258
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps(Operation &&op) const
Definition ah-dry.H:1859
bool none(Operation &&op) const
Overload of none() that accepts rvalues.
Definition ah-dry.H:1758
and std::copy_constructible< U > std::optional< T > reduce(Op &&op) const
Overload of reduce() that accepts rvalues.
Definition ah-dry.H:2326
void each(Operation &&operation) const
Alias of for_each() that accepts rvalues.
Definition ah-dry.H:1524
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:2356
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:2610
auto flat_map(Operation &&op) const
Overload of flat_map() that accepts rvalues.
Definition ah-dry.H:2062
Aleph::DynList< const T * > ptr_filter(Operation &&operation) const
Overload of ptr_filter() that accepts rvalues.
Definition ah-dry.H:2436
Aleph::DynList< T > take_while(Operation &&op) const
Overload of take_while() that accepts rvalues.
Definition ah-dry.H:2802
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map_if(Prop prop, Operation &op) const
Definition ah-dry.H:1990
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Definition ah-dry.H:1666
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, T > void each(const size_t pos, const size_t slice, Operation &&operation)
See each(size_t pos, size_t slice, Operation & operation).
Definition ah-dry.H:1613
void emplace_end(Args &&... args)
See emplace(Args && ... args).
Definition ah-dry.H:1303
void each(const size_t pos, const size_t slice, Operation &&operation) const
See each(size_t pos, size_t slice, Operation & operation) const.
Definition ah-dry.H:1566
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:2696
void each(const size_t pos, const size_t slice, Operation &operation) const
Traverse the container starting at pos taking one item every slice, performing an operation on each v...
Definition ah-dry.H:1545
Acc foldl(const Acc &init, Op &&op) const
Overload of foldl() that accepts rvalues.
Definition ah-dry.H:2171
Acc foldr(const Acc &init, Op &&op) const
Overload of foldr() that accepts rvalues.
Definition ah-dry.H:2287
size_t nappend(Args &&... args)
Append n variadic items.
Definition ah-dry.H:1379
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
Definition ah-dry.H:2145
Aleph::DynList< T > drop_while(Operation &op) const
The elements after the longest prefix that satisfies a criterion.
Definition ah-dry.H:2818
const Container * const_me() const noexcept
Definition ah-dry.H:1241
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:2673
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &&op) const
Overload of partition() that accepts rvalues.
Definition ah-dry.H:2542
bool exists(Operation &&op) const
Overload of exists() that accepts rvalues.
Definition ah-dry.H:1728
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map(Operation &&op) const
Definition ah-dry.H:1967
Aleph::DynList< T > take_while(Operation &op) const
The longest prefix of elements that satisfy a criterion.
Definition ah-dry.H:2782
Container * me() noexcept
Definition ah-dry.H:1236
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps_if(Prop prop, Operation &op) const
Definition ah-dry.H:1889
auto flat_map(Operation &op) const
Map each element to a container and concatenate the results.
Definition ah-dry.H:2034
bool none(Operation &op) const
Test that no element satisfies a criterion.
Definition ah-dry.H:1746
size_t ninsert(Args &&... args)
Insert n variadic items.
Definition ah-dry.H:1353
static Container build(Items &&... items)
Build a container that holds the given items.
Definition ah-dry.H:1423
void for_each(Operation &&operation) const
Overload of for_each() that accepts rvalues.
Definition ah-dry.H:1496
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Aleph::DynList< T > drop_while(Operation &&op) const
Overload of drop_while() that accepts rvalues.
Definition ah-dry.H:2841
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps_if(Prop prop, Operation &&op) const
Definition ah-dry.H:1923
T fold(const T &init, Operation &&operation) const
Overload of fold() that accepts rvalues.
Definition ah-dry.H:2241
size_t count_if(Operation &op) const
Count the elements that satisfy a criterion.
Definition ah-dry.H:1772
bool all(Operation &&operation) const
Overload of all() that accepts rvalues.
Definition ah-dry.H:1687
size_t count_if(Operation &&op) const
Overload of count_if() that accepts rvalues.
Definition ah-dry.H:1791
Acc fold_left(const Acc &init, Op &op) const
Alias for foldl().
Definition ah-dry.H:2186
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map_if(Prop prop, Operation &&op) const
Definition ah-dry.H:2014
Aleph::DynList< T > filter(Operation &&operation) const
Overload of filter() that accepts rvalues.
Definition ah-dry.H:2381
bool const_traverse(Operation &&operation) const
Definition ah-dry.H:1248
Aleph::DynList< T > take(size_t i, const size_t j, const size_t step=1) const
Return a list with the elements at positions i, i + step, i + 2 step, ... that are not greater than j...
Definition ah-dry.H:2726
Acc fold_left(const Acc &init, Op &&op) const
Overload of fold_left() that accepts rvalues.
Definition ah-dry.H:2206
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > split_half() const
Split the container into two halves by alternating elements.
Definition ah-dry.H:2579
static D build_as(Items &&... items)
Build a D, a class derived from the container, holding the given items.
Definition ah-dry.H:1448
std::tuple< Aleph::DynList< T >, Aleph::DynList< T > > tpartition(Operation &&op) const
Overload of tpartition() that accepts rvalues.
Definition ah-dry.H:2637
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:2757
void emplace_ins(Args &&... args)
Definition ah-dry.H:1329
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:2411
void mutable_drop(const size_t n)
Drop the first n elements seen from container.
Definition ah-dry.H:2863
static decltype(auto) as_item(Arg &&arg)
Definition ah-dry.H:1261
Common sequential searching methods on containers.
Definition ah-dry.H:555
const Type * find_ptr(Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
Overload of find_ptr() const that accepts rvalues.
Definition ah-dry.H:869
size_t find_index(Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
Overload of find_index() that accepts rvalues.
Definition ah-dry.H:948
Type & nth(const size_t n)
Return the n-th item of the container.
Definition ah-dry.H:774
auto get_itor()
Alias of get_it().
Definition ah-dry.H:711
static constexpr bool const_search_is_noexcept() noexcept
Definition ah-dry.H:570
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, Type > Type * find_ptr(Operation &&operation) noexcept(mutable_search_is_noexcept< Operation >())
Overload of find_ptr() that accepts rvalues.
Definition ah-dry.H:890
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
auto get_itor() const
See get_itor().
Definition ah-dry.H:717
Container * me() noexcept
Definition ah-dry.H:556
and std::default_initializable< T > and std::copy_constructible< T > std::tuple< bool, Type > find_item(Operation &&operation) const noexcept(find_item_is_noexcept< Operation >())
Overload of find_item() that accepts rvalues.
Definition ah-dry.H:1007
const Type * const_nth_ptr(const size_t n) const noexcept(Aleph::dry_detail::const_traversal_is_noexcept< Container, Type >)
Definition ah-dry.H:625
and std::copy_constructible< T > std::optional< Type > find_opt(Operation &operation) const
Copy of the first item satisfying a criterion, if any.
Definition ah-dry.H:1028
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, Type > Type * find_ptr(Operation &operation) noexcept(mutable_search_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criterion.
Definition ah-dry.H:825
const Type & nth(const size_t n) const
Const overload of nth(size_t).
Definition ah-dry.H:788
bool contains(const Type &item) const
Test if an item is present in the container using equality.
Definition ah-dry.H:1074
size_t find_index(Operation &operation) const noexcept(const_search_is_noexcept< Operation >())
Find the position of an item in the container according to a searching criterion.
Definition ah-dry.H:921
auto get_it(const size_t pos)
Return a properly initialized iterator positioned at the pos item on the container.
Definition ah-dry.H:685
static constexpr bool find_item_is_noexcept() noexcept
True if find_item() cannot throw: neither the search nor building the returned tuple,...
Definition ah-dry.H:595
const Container * const_me() const noexcept
Definition ah-dry.H:561
const Type * find_ptr(Operation &operation) const noexcept(const_search_is_noexcept< Operation >())
Const overload of find_ptr(Operation&).
Definition ah-dry.H:850
Type & nth_ne(const size_t n) noexcept(Aleph::dry_detail::mutable_traversal_is_noexcept< C, Type >)
Return the n‑th element without bounds checking.
Definition ah-dry.H:734
and std::default_initializable< T > and std::copy_constructible< T > std::tuple< bool, Type > find_item(Operation &operation) const noexcept(find_item_is_noexcept< Operation >())
Safe sequential searching of an item matching a criterion.
Definition ah-dry.H:983
const Type & nth_ne(const size_t n) const noexcept(Aleph::dry_detail::const_traversal_is_noexcept< Container, Type >)
Constant overload of nth_ne(size_t).
Definition ah-dry.H:747
auto get_it() const
Read-only iterator positioned at the first item.
Definition ah-dry.H:672
bool contains_if(Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
Test if an item satisfying a criterion is present in the container.
Definition ah-dry.H:1060
and std::copy_constructible< T > std::optional< Type > find_opt(Operation &&operation) const
Overload of find_opt() that accepts rvalues.
Definition ah-dry.H:1046
auto get_it(const size_t pos) const
Read-only iterator positioned at the pos item.
Definition ah-dry.H:699
const Type * first_const_ptr(Operation &operation) const noexcept(const_search_is_noexcept< Operation >())
Definition ah-dry.H:607
static constexpr bool mutable_search_is_noexcept() noexcept
Definition ah-dry.H:582
Type * mutable_nth_ptr(const size_t n) noexcept(Aleph::dry_detail::mutable_traversal_is_noexcept< Container, Type >)
Definition ah-dry.H:642
Common methods to mapping containers.
Definition ah-dry.H:3190
C< Key > keys() const
Return the domain set of container.
Definition ah-dry.H:3216
C< std::pair< Key, const Data * > > items_ptr() const
Constant overload of items_ptr().
Definition ah-dry.H:3347
C< Data * > values_ptr()
Return a container of pointers to the elements of range set of container.
Definition ah-dry.H:3268
C< std::pair< Key, Data * > > items_ptr()
Return a container with the entire mapping where the range could be modified.
Definition ah-dry.H:3330
C< const Data * > values_ptr() const
Constant overload of values_ptr().
Definition ah-dry.H:3285
C< Data > values() const
Return the range set of container.
Definition ah-dry.H:3240
Container * me() noexcept
Definition ah-dry.H:3196
C< std::pair< Key, Data > > items() const
Return a container with the entire mapping.
Definition ah-dry.H:3305
const Container * const_me() const noexcept
Definition ah-dry.H:3191
Data & operator()(const Key &key)
Return the image or element mapped to key.
Definition ah-dry.H:3363
A container whose append() accepts an argument of type Item.
A container whose insert() accepts an argument of type Item.
std::invoke(f, args...) is valid and usable as a condition.
std::invoke(f, args...) is valid.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
op folds an accumulator Acc with an item T.
Definition ah-dry.H:270
op folds an item T with an accumulator Acc from the right.
Definition ah-dry.H:284
The modifiable traversal of Container hands out T &.
Definition ah-dry.H:143
Container can be compared as a set: it has size(), a Key_Type and a search() that returns a pointer.
Definition ah-dry.H:152
void swap(__gmp_expr< T, T > &x, __gmp_expr< T, T > &y) noexcept
Exchange two numbers of the same class without allocating.
Definition gmpfrxx.h:2524
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
void fill(size_t n)
typename std::conditional_t< std::is_void_v< R >, decayed_invoke_result< Op, T >, std::type_identity< R > >::type map_result_t
Item type of a mapping: R, or the decayed type that Op returns when R is void.
void advance(It &it) noexcept(advance_is_noexcept< It >)
Advance an iterator that is positioned on an item.
Definition ah-dry.H:214
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
Definition ah-dry.H:233
constexpr bool current_is_noexcept
Reading the current item of an iterator positioned on one cannot throw.
Definition ah-dry.H:196
void assign_fold(Acc &acc, Step &&step)
Store in acc the result of a folding step step().
Definition ah-dry.H:299
constexpr bool const_traversal_is_noexcept
The constant traversal of Container cannot throw by itself.
Definition ah-dry.H:169
void fold_left_step(Acc &acc, Op &op, const T &item)
One step of a left fold: acc = op(acc, item).
Definition ah-dry.H:322
void fold_right_step(Acc &acc, Op &op, const T &item)
One step of a right fold: acc = op(item, acc).
Definition ah-dry.H:338
constexpr bool advance_is_noexcept
Advancing an iterator positioned on an item cannot throw.
Definition ah-dry.H:185
constexpr bool mutable_traversal_is_noexcept
The modifiable traversal of Container cannot throw by itself.
Definition ah-dry.H:180
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
auto iterator_for(C &c)
Iterator on c positioned on its first item: the container's own iterator on a modifiable container.
#define TT
Definition ran_array.c:29
T itself, made dependent on Deferred.
decltype(std::declval< typename defer< Container, Deferred >::type::Iterator & >().get_curr()) type
const std::remove_reference_t< typename traverse_arg< Container, Deferred >::type > & type
Operation that needs to modify the item it receives.
Definition ah-dry.H:106
bool operator()(T &item) const noexcept
Declared only; never called.
Operation that only reads the item and never throws.
Definition ah-dry.H:124
bool operator()(const T &item) const noexcept
Declared only; never called.
typename C::Item_Type type
Definition ah-dry.H:257
Item type of a container returned by the operation of flat_map(): its Item_Type, or else its value_ty...
Definition ah-dry.H:247
typename C::value_type type
Definition ah-dry.H:248
Generic list of items stored in a container.
Definition ah-dry.H:2896
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Definition ah-dry.H:2904
Aleph::DynList< T > keys() const
See items().
Definition ah-dry.H:2916
Generic traversal of the container through its iterator.
Definition ah-dry.H:363
bool traverse(Operation &&operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Overload of traverse(Operation &) that accepts rvalues.
Definition ah-dry.H:483
static constexpr bool traverse_is_noexcept() noexcept
Definition ah-dry.H:368
std::conditional_t< std::is_constructible_v< typename Aleph::concepts_detail::defer< Container, Deferred >::type::Iterator, const typename Aleph::concepts_detail::defer< Container, Deferred >::type & >, const typename Aleph::concepts_detail::defer< Container, Deferred >::type &, typename Aleph::concepts_detail::defer< Container, Deferred >::type & > const_source_t
Definition ah-dry.H:387
bool traverse(Operation &operation) const noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_carg< Container, Operation >::type, const_source_t< Operation > >())
Traverse a constant container; each item is handed out as a constant reference.
Definition ah-dry.H:436
bool traverse(Operation &&operation) const noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_carg< Container, Operation >::type, const_source_t< Operation > >())
Overload of traverse(Operation &) const that accepts rvalues.
Definition ah-dry.H:464
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:411
Special constructors common to Aleph-w ( ) containers.
Definition ah-dry.H:1100
SpecialCtors(const SpecialCtors &)=default
Copy constructor; the derived container copies its items.
SpecialCtors(SpecialCtors &&) noexcept
Move constructor; the derived container moves its items.
Definition ah-dry.H:1108
SpecialCtors(std::initializer_list< T > l)
Construct the container from an initializer list.
Definition ah-dry.H:1166
SpecialCtors(It b, It e)
Construct the container from a range of iterators.
Definition ah-dry.H:1150
void build_apart(Fill fill)
Fill the container: in another one built apart, whose items it takes with swap(), if the container ha...
Definition ah-dry.H:1199
SpecialCtors(const Aleph::DynList< T > &l)
Build the container by inserting all items of the list l.
Definition ah-dry.H:1128
SpecialCtors()=default
Default constructor; it appends nothing.
void free_appended() noexcept
Free the items that a constructor of this base appended.
Definition ah-dry.H:1182
SpecialCtors & operator=(const SpecialCtors &)=default
Copy assignment; the derived container copies its items.
static int * k
Filter_Iterator< DynList< int >, DynList< int >::Iterator, Par > It
Definition test_htlist.C:61
Gsl_Compatible_Rng r
DynList< int > l