32#ifndef AH_FUNCTIONAL_H
33#define AH_FUNCTIONAL_H
94#if defined(__GNUC__) || defined(__clang__)
95 __builtin_unreachable();
96#elif defined(_MSC_VER)
104#if defined(__GNUC__) || defined(__clang__)
105 __builtin_unreachable();
106#elif defined(_MSC_VER)
111 [[nodiscard]]
bool is_found() const noexcept
override
133 [[nodiscard]]
bool is_found() const noexcept
override
150template <
typename tgtT,
typename srcT>
160 tgtT
operator()(
const srcT &item)
const noexcept(
noexcept(
static_cast<tgtT
>(item)))
161 requires requires(
const srcT &x) {
static_cast<tgtT
>(x); }
163 return static_cast<tgtT
>(item);
172template <
typename TR,
typename TD>
182 noexcept(std::is_nothrow_default_constructible_v<TR>)
183 requires requires { TR(); }
193template <
typename Key,
class Cmp>
194 requires EqualityComparator<Cmp, Key>
197namespace functional_detail {
202using get_it_t = std::decay_t<decltype(std::declval<const C &>().get_it())>;
217template <
typename T,
class Eq,
typename =
void>
218struct Dyn_Set_Lhash_Available :
public std::false_type
223template <
typename T,
class Eq>
224struct Dyn_Set_Lhash_Available<
T, Eq,
std::void_t<decltype(sizeof(DynSetLhash<T, Eq>))>>
225 :
public std::true_type
229template <
typename T,
class Eq>
232template <AlephSequentialIterableContainer Container,
class Equal>
235 for (
auto it1 = container.get_it(); it1.has_curr(); it1.next_ne())
239 for (; it2.has_curr(); it2.next_ne())
240 if (
eq(it1.get_curr(), it2.get_curr()))
246template <AlephSequentialIterableContainer Container,
class Hash,
class Equal>
247 requires std::copy_constructible<typename Container::Item_Type>
248 and requires(
const typename Container::Item_Type &v, Hash
h, Equal e) {
249 {
h(v) } -> std::convertible_to<size_t>;
250 { e(v, v) } -> std::convertible_to<bool>;
254 using T =
typename Container::Item_Type;
255 using Eq_Type = std::remove_cvref_t<Equal>;
257 if constexpr (dyn_set_lhash_available<T, Eq_Type>)
260 using Hash_Fct =
typename Set_Type::Hash_Fct;
262 Hash_Fct hash_fct([&hash](
const T &
value)
264 return static_cast<size_t>(hash(
value));
269 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
270 if (seen.contains_or_insert(it.get_curr()).second)
295template <
typename T =
int,
template <
typename>
class Container = DynList>
299 for (
T i = start; i <= end; i += step)
314template <
typename T =
int,
template <
typename>
class Container = DynList>
322 ret_val.append(start);
326 const auto step =
static_cast<double>(end - start) / (n - 1);
327 for (
size_t i = 0; i < n; ++i)
328 ret_val.append(
static_cast<T>(start + i * step));
344template <
typename T =
int,
template <
typename>
class Container = DynList,
class Op>
345[[nodiscard]]
inline auto set_range(
const T start,
const T end,
const T step, Op &op)
346 ->
Container<std::decay_t<
decltype(op(start))>>
348 Container<std::decay_t<
decltype(op(start))>> ret_val;
349 for (
T i = start; i <= end; i += step)
350 ret_val.append(op(i));
355template <
typename T =
int,
template <
typename>
class Container = DynList,
class Op>
356[[nodiscard]]
inline auto set_range(
const T start,
const T end,
const T step = 1, Op &&op = Op())
357 ->
Container<std::decay_t<
decltype(op(start))>>
359 return set_range<T, Container, Op>(start, end, step, op);
381template <
typename T =
int,
template <
typename>
class Container = DynList>
385 for (
size_t i = 0; i < n; ++i)
386 ret_val.append(start++);
397template <
typename T =
int,
template <
typename>
class Container = DynList>
401 for (
T i = 0; i < n; ++i)
414template <
typename T =
int>
418 for (
size_t i = 0; i < n; ++i)
424template <
typename T =
int>
427 return rep<T>(n, item);
462template <AlephSequentialContainer C>
463void fill(C &container,
const typename C::Item_Type &
value)
465 container.mutable_for_each([&
value](
typename C::Item_Type &item)
497template <AlephSequentialContainer C>
498 requires requires(
typename C::Item_Type v) { ++v; }
499void iota(C &container,
typename C::Item_Type start)
501 container.mutable_for_each([&start](
typename C::Item_Type &item)
532template <AlephSequentialContainer C>
533 requires requires(
typename C::Item_Type v,
typename C::Item_Type s) { v += s; }
534void iota(C &container,
typename C::Item_Type start,
const typename C::Item_Type step)
536 container.mutable_for_each([&start, &step](
typename C::Item_Type &item)
565template <
class Container>
570 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
571 ret.
append(&it.get_curr());
578template <
class Container>
582 using T =
const typename Container::Item_Type *;
584 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
585 ret.
append(&it.get_curr());
608 requires CallableWith<Op &>
609void each(
const size_t start,
const size_t end, Op &op)
611 for (
size_t i = start; i <= end; ++i)
617 requires CallableWith<Op &>
618void each(
size_t start,
size_t end, Op &&op)
620 each(start, end, op);
625 requires CallableWith<Op &>
626void each(
const size_t n, Op &op)
635 requires CallableWith<Op &>
648template <
class Container>
655 for (
auto it = c.get_it(pos); it.has_curr();
each(0, stride - 1, [&it]()
659 ret.
append(it.get_curr());
661 catch (
const std::overflow_error &)
669template <
class Container>
698template <
class Container,
class Operation>
701 container.traverse([&operation](
const auto &item)
710template <
class Container,
class Operation>
713 container.traverse([&operation](
const auto &item)
722template <
class Container,
class Operation>
725 return for_each<Container, Operation>(container, operation);
729template <
class Container,
class Operation>
732 return for_each<Container, Operation>(container, operation);
762template <
class Container,
class Operation>
763 requires CallableWith<Operation &, functional_detail::get_curr_t<Container>, std::size_t &>
767 for (
auto it = container.get_it(); it.has_curr(); it.next_ne(), ++i)
768 operation(it.get_curr(), i);
772template <
class Container,
class Operation>
773 requires CallableWith<Operation &, functional_detail::get_curr_t<Container>, std::size_t &>
802template <
class Container,
class Operation>
803 requires requires(
Container &c, Operation &op) { { c.traverse(op) } -> std::convertible_to<bool>; }
806 return container.traverse(operation);
810template <
class Container,
class Operation>
811 requires requires(
const Container &c, Operation &op) { { c.template traverse<Operation>(op) } -> std::convertible_to<bool>; }
814 return container.template traverse<Operation>(operation);
818template <
class Container,
class Operation>
819 requires requires(
Container &c, Operation &op) { { c.traverse(op) } -> std::convertible_to<bool>; }
820inline bool all(
Container &container, Operation &&operation = Operation())
822 return all<Container, Operation>(container, operation);
826template <
class Container,
class Operation>
827 requires requires(
const Container &c, Operation &op) { { c.template traverse<Operation>(op) } -> std::convertible_to<bool>; }
828inline bool all(
const Container &container, Operation &&operation = Operation())
830 return all<Container, Operation>(container, operation);
856template <
class Container,
class Operation>
859 return not container.traverse([&operation](
const auto &item)
861 return not operation(item);
866template <
class Container,
class Operation>
869 return not container.traverse([&operation](
const auto &item)
871 return not operation(item);
876template <
class Container,
class Operation>
879 return exists<Container, Operation>(container, operation);
883template <
class Container,
class Operation>
886 return exists<Container, Operation>(container, operation);
908template <
class Container1,
910 class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
911[[nodiscard]]
inline Container2<typename Container1::Item_Type>
filter(Container1 &container,
912 Operation &operation)
914 Container2<typename Container1::Item_Type> ret_val;
915 container.
for_each([&ret_val, &operation](
const auto &item)
918 ret_val.append(item);
924template <
class Container1,
926 class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
927[[nodiscard]]
inline Container2<typename Container1::Item_Type>
filter(
const Container1 &container,
928 Operation &operation)
930 Container2<typename Container1::Item_Type> ret_val;
931 container.
for_each([&ret_val, &operation](
const auto &item)
934 ret_val.append(item);
940template <
class Container1,
942 class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
943[[nodiscard]]
inline Container2<typename Container1::Item_Type>
filter(Container1 &container,
944 Operation &&operation)
946 return filter<Container1, Container2, Operation>(container, operation);
950template <
class Container1,
952 class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
953[[nodiscard]]
inline Container2<typename Container1::Item_Type>
filter(
const Container1 &container,
954 Operation &&operation)
956 return filter<Container1, Container2, Operation>(container, operation);
967template <
typename T,
class C,
class Op>
968 requires requires(DynList<T> &ret, Op &op) { ret.append(op(std::declval<functional_detail::get_curr_t<C>>())); }
972 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
973 ret.
append(op(it.get_curr()));
983template <
typename T,
class Container,
class Operation>
987 if constexpr (std::ranges::range<Container>)
993 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
994 ret_val = operation(ret_val, it.get_curr());
1023template <
class Container1,
class Container2>
1024[[nodiscard]]
inline DynList<std::pair<typename Container1::Item_Type, typename Container2::Item_Type>>
1025zip(
const Container1 &a,
const Container2 &b)
1027 typedef typename Container1::Item_Type T1;
1028 typedef typename Container2::Item_Type T2;
1032 auto it2 = b.get_it();
1033 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1034 ret_val.
append(std::pair<T1, T2>(it1.get_curr(), it2.get_curr()));
1065template <
class Container1,
class Container2>
1066[[nodiscard]]
inline DynList<std::tuple<typename Container1::Item_Type, typename Container2::Item_Type>>
1067tzip(
const Container1 &a,
const Container2 &b)
1069 typedef typename Container1::Item_Type T1;
1070 typedef typename Container2::Item_Type T2;
1071 using Tuple = std::tuple<T1, T2>;
1075 auto it2 = b.get_it();
1076 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1077 ret_val.
append(
Tuple(it1.get_curr(), it2.get_curr()));
1108template <
class Container1,
class Container2>
1109[[nodiscard]]
inline DynList<std::pair<typename Container1::Item_Type, typename Container2::Item_Type>>
1110zipEq(
const Container1 &a,
const Container2 &b)
1112 typedef typename Container1::Item_Type T1;
1113 typedef typename Container2::Item_Type T2;
1117 auto it2 = b.get_it();
1118 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1119 ret_val.
append(std::pair<T1, T2>(it1.get_curr(), it2.get_curr()));
1142template <
class Container1,
class Container2>
1143[[nodiscard]]
inline DynList<std::tuple<typename Container1::Item_Type, typename Container2::Item_Type>>
1144tzipEq(
const Container1 &a,
const Container2 &b)
1146 typedef typename Container1::Item_Type T1;
1147 typedef typename Container2::Item_Type T2;
1148 using Tuple = std::tuple<T1, T2>;
1152 auto it2 = b.get_it();
1153 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1154 ret_val.
append(
Tuple(it1.get_curr(), it2.get_curr()));
1186template <
class Container>
1190 using Item =
typename Container::Item_Type;
1191 using Pair = std::pair<Item, size_t>;
1194 c.for_each([&i, &ret](
const Item &item)
1196 ret.
append(Pair(item, i++));
1229template <
class C1,
class C2,
class Eq = std::equal_to<
typename C1::Item_Type>>
1230[[nodiscard]]
inline bool eq(
const C1 &c1,
const C2 &c2, Eq e = Eq())
1233 auto it2 = c2.get_it();
1234 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1235 if (not(e(it1.get_curr(), it2.get_curr())))
1238 return not(it1.has_curr() or it2.has_curr());
1242template <
typename T>
1249template <
class C1,
class C2,
class Eq>
1252 return eq(c1, c2, e);
1281template <
class C1,
class C2,
class Eq = std::equal_to<
typename C1::Item_Type>>
1282[[nodiscard]]
inline std::tuple<bool, size_t, typename C1::Item_Type, typename C2::Item_Type>
1283are_eq(
const C1 &c1,
const C2 &c2, Eq e = Eq())
1285 using T =
typename C1::Item_Type;
1286 auto it1 = c1.get_it();
1287 auto it2 = c2.get_it();
1289 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne(), n++)
1291 auto &i1 = it1.get_curr();
1292 auto &i2 = it2.get_curr();
1294 return std::make_tuple(
false, n, i1, i2);
1297 return std::make_tuple(not(it1.has_curr() or it2.has_curr()), n,
T(),
T());
1326template <
class C1,
class C2,
class Cmp = std::less<
typename C1::Item_Type>>
1329 auto it1 = c1.get_it();
1330 auto it2 = c2.get_it();
1331 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1333 auto &curr1 = it1.get_curr();
1334 auto &curr2 = it2.get_curr();
1335 if (
cmp(curr1, curr2))
1337 if (
cmp(curr2, curr1))
1341 if (not it1.has_curr()
and not it2.has_curr())
1344 return it2.has_curr();
1363template <
class C1,
class C2,
class Eq = std::equal_to<
typename C1::Item_Type>>
1364[[nodiscard]]
inline bool diff(
const C1 &c1,
const C2 &c2, Eq e = Eq())
1366 return not
eq(c1, c2, e);
1393template <
class Container>
1396 using T1 = std::decay_t<
decltype(
l.
get_first().first)>;
1397 using T2 = std::decay_t<
decltype(
l.
get_first().second)>;
1400 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
1402 auto &curr = it.get_curr();
1407 return std::make_pair(std::move(
l1), std::move(
l2));
1436template <
template <
typename>
class Container,
typename T1,
typename T2>
1442 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
1444 auto &curr = it.get_curr();
1449 return std::make_tuple(std::move(
l1), std::move(
l2));
1458template <
class SrcContainer,
template <
typename>
class TgtContainer =
Aleph::DynList>
1459[[nodiscard]]
inline std::pair<TgtContainer<typename SrcContainer::Item_Type>,
1460 TgtContainer<typename SrcContainer::Item_Type>>
1462 std::function<
bool(
const typename SrcContainer::Item_Type &)> operation)
1464 typedef typename SrcContainer::Item_Type
Type;
1465 typedef std::pair<TgtContainer<Type>, TgtContainer<Type>> Pair;
1470 if (operation(item))
1471 ret_val.first.append(item);
1473 ret_val.second.append(item);
1479template <
class Container>
1482 using T =
typename Container::Key_Type;
1483 using Pair = std::pair<size_t, T>;
1486 return c.Container::template maps<Pair>([&i](
const T &d)
1488 return Pair(i++, d);
1493template <
class Container>
1497 using T =
typename Container::Key_Type;
1498 using Tuple = std::tuple<size_t, typename Container::Key_Type>;
1500 return c.Container::template maps<std::tuple<size_t, T>>([&i](
const T &d)
1502 return Tuple(i++, d);
1526template <
typename T,
template <
typename>
class Container>
1532 ret_val.insert(item);
1558template <
class Container>
1562 using T =
typename Container::Item_Type;
1563 auto it = c.get_it();
1565 for (
size_t i = 0; i < n; ++i, it.next())
1570 for (; it.has_curr(); it.next_ne())
1605template <
typename T,
template <
typename>
class Container,
class Equal>
1609 using P = std::pair<DynList<DynList<T>>,
size_t>;
1617 auto it = c.get_it();
1618 auto curr_item = it.get_curr();
1619 group->
append(curr_item);
1623 for (it.next(); it.has_curr(); it.next_ne())
1625 auto &curr = it.get_curr();
1626 if (not
eq(curr, curr_item))
1640template <
typename T,
template <
typename>
class Container,
class Equal = std::equal_to<T>>
1642 Equal &&
eq = Equal())
1674template <
typename T,
template <
typename>
class Container,
class Equal>
1677 using P = std::pair<DynList<T>,
size_t>;
1683 auto it = c.get_it();
1684 auto curr_item = it.get_curr();
1689 for (it.next(); it.has_curr(); it.next_ne())
1691 auto &curr = it.get_curr();
1692 if (not
eq(curr, curr_item))
1704template <
typename T,
template <
typename>
class Container,
class Equal = std::equal_to<T>>
1706 Equal &&
eq = Equal())
1735template <
class Itor1,
class Itor2 = Itor1>
1749 template <
class C1,
class C2>
1750 requires (not std::same_as<std::remove_cvref_t<C1>, Itor1>)
1759 return it1.has_curr() and it2.has_curr();
1765 return it1.has_curr();
1771 return it2.has_curr();
1780 return std::make_pair(it1.get_curr(), it2.get_curr());
1789 return std::make_pair(it1.get_curr_ne(), it2.get_curr_ne());
1809 void next_ne() noexcept(noexcept(
std::declval<Itor1 &>().next_ne())
1810 and noexcept(
std::declval<Itor2 &>().next_ne()))
1822 return not(it1.has_curr() or it2.has_curr());
1840template <
class C1,
class C2>
1849template <
class C1,
class C2>
1850[[nodiscard]]
inline auto get_pair_it(C1 &c1, C2 &c2,
const size_t pos)
1854 for (
size_t i = 0; i < pos; ++i)
1863namespace functional_detail {
1869template <
class Result>
1870constexpr bool added_item(
const Result &result)
noexcept
1872 if constexpr (std::is_pointer_v<Result>)
1873 return result !=
nullptr;
1879template <
class C,
class Item>
1880bool append_one(C &c,
Item &&item)
1882 if constexpr (std::is_void_v<
decltype(c.append(std::forward<Item>(item)))>)
1884 c.append(std::forward<Item>(item));
1888 return added_item(c.append(std::forward<Item>(item)));
1892template <
class C,
class Item>
1893bool insert_one(C &c,
Item &&item)
1895 if constexpr (std::is_void_v<
decltype(c.insert(std::forward<Item>(item)))>)
1897 c.insert(std::forward<Item>(item));
1901 return added_item(c.insert(std::forward<Item>(item)));
1914template <
class C,
typename... Args>
1918 ((n += (functional_detail::insert_one(c, std::forward<Args>(args)) ? 1 : 0)), ...);
1929template <
class C,
typename... Args>
1933 ((n += (functional_detail::append_one(c, std::forward<Args>(args)) ? 1 : 0)), ...);
1943template <
class C,
typename... Args>
1952template <
class SrcC,
class TgtC>
1956 for (
auto it = srcc.get_it(); it.has_curr(); it.next_ne())
1957 ret.append(it.get_curr());
1963template <
typename T,
typename... Args>
1966 return build_container<DynList<T>>(std::forward<Args>(args)...);
1974template <
class C,
typename T,
typename... Args>
1986template <
class C,
typename... Args>
1997template <
typename T,
template <
typename>
class Container>
2001template <
typename T,
template <
typename>
class Container>
2005template <
typename T,
template <
typename>
class Container>
2009template <
typename T,
template <
typename>
class Container>
2013template <
typename T,
template <
typename>
class Container>
2036template <
typename T,
template <
typename>
class C1,
template <
typename>
class C2>
2040 for (
auto it_c = c.get_it(); it_c.has_curr(); it_c.next_ne())
2042 const auto &curr_c = it_c.get_curr();
2043 for (
auto it = curr_c.get_it(); it.has_curr(); it.next_ne())
2044 ret.
append(it.get_curr());
2060template <
typename T,
template <
typename>
class C1,
template <
typename>
class C2,
template <
typename>
class C3>
2064 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
2080template <
typename T,
2081 template <
typename>
class C1,
2082 template <
typename>
class C2,
2083 template <
typename>
class C3,
2084 template <
typename>
class C4>
2088 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
2104template <
typename T,
2105 template <
typename>
class C1,
2106 template <
typename>
class C2,
2107 template <
typename>
class C3,
2108 template <
typename>
class C4,
2109 template <
typename>
class C5>
2113 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
2137template <
typename T>
2140 for (
const auto &v : values)
2163template <
typename T>
2176template <
typename T,
typename U,
typename... Args>
2177[[nodiscard]]
inline bool is_equal(
const T &val,
const U &rhs,
const Args &...args)
2179 return (val == rhs) or
is_equal(val, args...);
2188template <
class Container,
class Operation>
2191 return not
exists(container, operation);
2195template <
class Container,
class Operation>
2196[[nodiscard]]
inline bool none(
const Container &container, Operation &&operation = Operation())
2198 return none<Container, Operation>(container, operation);
2228template <
class Container,
class Pred>
2231 typename Container::Item_Type *result =
nullptr;
2234 container.traverse([&result, &
pred](
typename Container::Item_Type &item)
2247template <
class Container,
class Pred>
2251 const typename Container::Item_Type *result =
nullptr;
2252 container.traverse([&result, &
pred](
const auto &item)
2265template <
class Container,
class Pred>
2268 return find_ptr<Container, Pred>(container,
pred);
2272template <
class Container,
class Pred>
2276 return find_ptr<Container, Pred>(container,
pred);
2301template <
class Container,
class Pred>
2302[[nodiscard]]
inline std::optional<typename Container::Item_Type>
find_opt(
const Container &container,
2305 std::optional<typename Container::Item_Type> result;
2306 container.traverse([&result, &
pred](
const auto &item)
2319template <
class Container,
class Pred>
2320[[nodiscard]]
inline std::optional<typename Container::Item_Type>
find_opt(
const Container &container,
2323 return find_opt<Container, Pred>(container,
pred);
2335template <
typename T,
class Container,
class Operation>
2336 requires requires(
T &acc, Operation &op,
T &x) { acc = op(x, acc); }
2340 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2341 reversed.insert(it.get_curr());
2344 for (
auto it = reversed.get_it(); it.has_curr(); it.next_ne())
2345 ret_val = operation(it.get_curr(), ret_val);
2356template <
class Container,
typename T =
typename Container::Item_Type>
2360 if constexpr (std::ranges::range<Container>)
2366 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2367 result = result + it.get_curr();
2379template <
class Container,
typename T =
typename Container::Item_Type>
2383 if constexpr (std::ranges::range<Container>)
2389 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2390 result = result * it.get_curr();
2402template <
class C1,
class C2>
2406 for (
auto it = c1.get_it(); it.has_curr(); it.next_ne())
2407 result.
append(it.get_curr());
2408 for (
auto it = c2.get_it(); it.has_curr(); it.next_ne())
2409 result.
append(it.get_curr());
2420template <
class Container,
class Pred>
2421 requires PredicateWith<Pred &, functional_detail::get_curr_cref_t<Container>>
2425 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
2427 const auto &item = it.get_curr();
2442template <
class Container,
class Pred>
2443 requires PredicateWith<Pred &, functional_detail::get_curr_cref_t<Container>>
2447 bool dropping =
true;
2448 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
2450 const auto &item = it.get_curr();
2468template <
class Container,
class Op>
2472 using ResultItemType =
2473 typename std::decay_t<decltype(op(std::declval<typename Container::Item_Type>()))>
::Item_Type;
2475 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2477 auto sub = op(it.get_curr());
2478 for (
auto sit = sub.get_it(); sit.has_curr(); sit.next_ne())
2479 result.
append(sit.get_curr());
2493template <
typename T,
class Container>
2499 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2501 acc = acc + it.get_curr();
2517template <
typename T,
class Container,
class Op>
2518 requires requires(
T &acc, Op &op) { acc = op(acc, std::declval<functional_detail::get_curr_t<Container>>()); }
2524 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2526 acc = op(acc, it.get_curr());
2539template <
class Container,
class Cmp = std::less<
typename Container::Item_Type>>
2540 requires PredicateWith<Cmp &, functional_detail::get_curr_cref_t<Container>,
const typename Container::Item_Type &>
2544 auto it = container.get_it();
2545 if (not it.has_curr())
2548 const typename Container::Item_Type *min_elem = &it.get_curr();
2549 for (it.next_ne(); it.has_curr(); it.next_ne())
2551 const auto &curr = it.get_curr();
2552 if (
cmp(curr, *min_elem))
2565template <
class Container,
class Cmp = std::less<
typename Container::Item_Type>>
2566 requires PredicateWith<Cmp &, const typename Container::Item_Type &, functional_detail::get_curr_cref_t<Container>>
2570 auto it = container.get_it();
2571 if (not it.has_curr())
2574 const typename Container::Item_Type *max_elem = &it.get_curr();
2575 for (it.next_ne(); it.has_curr(); it.next_ne())
2577 const auto &curr = it.get_curr();
2578 if (
cmp(*max_elem, curr))
2591template <
class Container,
class Cmp = std::less<
typename Container::Item_Type>>
2592 requires PredicateWith<Cmp &, functional_detail::get_curr_cref_t<Container>,
const typename Container::Item_Type &>
and
2593 PredicateWith<Cmp &, const typename Container::Item_Type &, functional_detail::get_curr_cref_t<Container>>
2594[[nodiscard]]
inline std::pair<const typename Container::Item_Type *, const typename Container::Item_Type *>
2597 using T =
typename Container::Item_Type;
2598 auto it = container.get_it();
2599 if (not it.has_curr())
2600 return {
nullptr,
nullptr};
2602 const T *min_elem = &it.get_curr();
2603 const T *max_elem = min_elem;
2604 for (it.next_ne(); it.has_curr(); it.next_ne())
2606 const auto &curr = it.get_curr();
2607 if (
cmp(curr, *min_elem))
2609 if (
cmp(*max_elem, curr))
2612 return {min_elem, max_elem};
2622template <
class Container,
class Pred>
2623 requires PredicateWith<Pred &, functional_detail::get_curr_t<Container>>
2627 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2628 if (
pred(it.get_curr()))
2649template <AlephSequentialIterableContainer Container,
class Equal>
2652 using T =
typename Container::Item_Type;
2653 using Eq = std::remove_cvref_t<Equal>;
2655 if constexpr (std::is_same_v<Eq, std::equal_to<T>>
and std::copy_constructible<T>
2656 and requires(
const T &v) {
2657 { std::hash<T>{}(v) } -> std::convertible_to<size_t>;
2660 auto hash = [](
const T &
value) ->
size_t
2662 return std::hash<T>{}(
value);
2684template <AlephSequentialIterableContainer
Container,
2685 class Equal = std::equal_to<typename Container::Item_Type>>
2688 return all_unique<Container, Equal>(container,
eq);
2706template <AlephSequentialIterableContainer Container,
class Hash,
class Equal>
2707 requires std::copy_constructible<typename Container::Item_Type>
2708 and requires(
const typename Container::Item_Type &v, Hash
h, Equal e) {
2709 {
h(v) } -> std::convertible_to<size_t>;
2710 { e(v, v) } -> std::convertible_to<bool>;
2717template <AlephSequentialIterableContainer Container,
class Hash,
class Equal>
2718 requires std::copy_constructible<typename Container::Item_Type>
2719 and requires(
const typename Container::Item_Type &v, Hash
h, Equal e) {
2720 {
h(v) } -> std::convertible_to<size_t>;
2721 { e(v, v) } -> std::convertible_to<bool>;
2726 using Hash_Type = std::remove_reference_t<Hash>;
2727 using Eq_Type = std::remove_reference_t<Equal>;
2729 Hash_Type hash_obj = std::forward<Hash>(hash);
2730 Eq_Type eq_obj = std::forward<Equal>(
eq);
2731 return all_unique(container, hash_obj, eq_obj);
2741template <
class Container>
2743 const typename Container::Item_Type &
value)
2745 for (
auto it = container.get_it(); it.has_curr(); it.next_ne())
2746 if (it.get_curr() ==
value)
2759template <
class Container>
2763 using T =
typename Container::Item_Type;
2764 using Tuple = std::tuple<size_t, T>;
2767 for (
auto it = container.get_it(); it.has_curr(); it.next_ne(), ++i)
2806template <
class Container1,
class Container2>
2807[[nodiscard]]
inline DynList<std::pair<typename Container1::Item_Type, typename Container2::Item_Type>>
2809 const Container2 &b,
2810 const typename Container1::Item_Type &fill_a =
typename Container1::Item_Type(),
2811 const typename Container2::Item_Type &fill_b =
typename Container2::Item_Type())
2813 using T1 =
typename Container1::Item_Type;
2814 using T2 =
typename Container2::Item_Type;
2818 auto it2 = b.get_it();
2821 while (it1.has_curr()
and it2.has_curr())
2823 ret_val.
append(std::pair<T1, T2>(it1.get_curr(), it2.get_curr()));
2829 while (it1.has_curr())
2831 ret_val.
append(std::pair<T1, T2>(it1.get_curr(), fill_b));
2836 while (it2.has_curr())
2838 ret_val.
append(std::pair<T1, T2>(fill_a, it2.get_curr()));
2876template <
class Container1,
class Container2>
2877[[nodiscard]]
inline DynList<std::tuple<typename Container1::Item_Type, typename Container2::Item_Type>>
2879 const Container2 &b,
2880 const typename Container1::Item_Type &fill_a =
typename Container1::Item_Type(),
2881 const typename Container2::Item_Type &fill_b =
typename Container2::Item_Type())
2883 using T1 =
typename Container1::Item_Type;
2884 using T2 =
typename Container2::Item_Type;
2885 using Tuple = std::tuple<T1, T2>;
2889 auto it2 = b.get_it();
2891 while (it1.has_curr()
and it2.has_curr())
2893 ret_val.
append(
Tuple(it1.get_curr(), it2.get_curr()));
2898 while (it1.has_curr())
2904 while (it2.has_curr())
2948template <
class Container1,
class Container2>
2949[[nodiscard]]
inline DynList<std::pair<std::optional<typename Container1::Item_Type>,
2950 std::optional<typename Container2::Item_Type>>>
2953 using T1 =
typename Container1::Item_Type;
2954 using T2 =
typename Container2::Item_Type;
2955 using Opt1 = std::optional<T1>;
2956 using Opt2 = std::optional<T2>;
2960 auto it2 = b.get_it();
2962 while (it1.has_curr()
and it2.has_curr())
2964 ret_val.
append(std::make_pair(Opt1(it1.get_curr()), Opt2(it2.get_curr())));
2969 while (it1.has_curr())
2971 ret_val.
append(std::make_pair(Opt1(it1.get_curr()), std::nullopt));
2975 while (it2.has_curr())
2977 ret_val.
append(std::make_pair(std::nullopt, Opt2(it2.get_curr())));
3032template <
typename T,
template <
typename>
class Container,
class KeyFunc>
3037 using Key = std::invoke_result_t<KeyFunc, const T &>;
3045 auto it = c.get_it();
3046 Key curr_key = key_func(it.get_curr());
3047 Group *curr_group = &ret.append(std::make_pair(curr_key, Group())).second;
3048 curr_group->append(it.get_curr());
3050 for (it.next_ne(); it.has_curr(); it.next_ne())
3052 const auto &item = it.get_curr();
3053 Key new_key = key_func(item);
3055 if (new_key != curr_key)
3058 curr_group = &ret.append(std::make_pair(curr_key, Group())).second;
3061 curr_group->append(item);
3098template <
typename T,
template <
typename>
class Container,
class KeyFunc,
class KeyEqual>
3100DynList<std::pair<std::invoke_result_t<KeyFunc, const T &>, DynList<T>>>
3103 using Key = std::invoke_result_t<KeyFunc, const T &>;
3111 auto it = c.get_it();
3112 Key curr_key = key_func(it.get_curr());
3113 Group *curr_group = &ret.append(std::make_pair(curr_key, Group())).second;
3114 curr_group->append(it.get_curr());
3116 for (it.next_ne(); it.has_curr(); it.next_ne())
3118 const auto &item = it.get_curr();
3119 Key new_key = key_func(item);
3121 if (not key_equal(new_key, curr_key))
3124 curr_group = &ret.append(std::make_pair(curr_key, Group())).second;
3127 curr_group->append(item);
3170template <
typename T,
template <
typename>
class Container,
class KeyFunc,
class Reducer>
3173 std::pair<std::invoke_result_t<KeyFunc, const T &>, std::invoke_result_t<Reducer, const DynList<T> &>>>
3175 using Key = std::invoke_result_t<KeyFunc, const T &>;
3176 using ReducedType = std::invoke_result_t<Reducer, const DynList<T> &>;
3179 auto groups =
group_by(c, key_func);
3182 for (
auto it = groups.get_it(); it.has_curr(); it.next_ne())
3184 auto &[key, group] = it.get_curr();
3185 ret.append(std::make_pair(key, reducer(group)));
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_invalid_argument()
Throws std::invalid_argument unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
STL-compatible iterator adapters for Aleph containers.
C++20 Ranges support and adaptors for Aleph-w containers.
size_t size_t int32_t value
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
T remove_first()
See remove().
Iterator that zips two other iterators.
auto get_curr_ne() const noexcept
Get current pair (no bounds check).
void next_ne() noexcept(noexcept(std::declval< Itor1 & >().next_ne()) and noexcept(std::declval< Itor2 & >().next_ne()))
Advance both iterators (no bounds check).
bool has_curr1() const noexcept
Check if first iterator has current element.
Pair_Iterator(Itor1 i1, Itor2 i2)
Construct from two iterators.
bool has_curr() const noexcept
Check if both iterators have current elements.
bool was_traversed() const noexcept
Check if both iterators were completely traversed.
Pair_Iterator(C1 &c1, C2 &c2)
Construct from two containers.
bool has_curr2() const noexcept
Check if second iterator has current element.
void next()
Advance both iterators (bounds-checked).
auto get_curr() const
Get current pair (bounds-checked).
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Common hash table utilities and base classes.
Freq_Node * pred
Predecessor node in level-order traversal.
constexpr T ranges_fold_left(Container &&c, T init, BinaryOp &&op)
Fallback fold_left using range-based for loop.
decltype(std::declval< get_it_t< C > & >().get_curr()) get_curr_t
Type of it.get_curr() on a get_it_t<C> lvalue.
const std::remove_reference_t< get_curr_t< C > > & get_curr_cref_t
Type of const auto &item = it.get_curr().
bool pairwise_all_unique(const Container &container, Equal &eq)
bool all_unique_hash_path(const Container &container, Hash &hash, Equal &eq)
constexpr bool dyn_set_lhash_available
std::decay_t< decltype(std::declval< const C & >().get_it())> get_it_t
Type of auto it = c.get_it() for a const C &c.
Main namespace for Aleph-w library functions.
std::pair< DynList< T >, size_t > unique_sequential(const Container< T > &c, Equal &eq)
Extract unique consecutive items.
std::tuple< bool, size_t, typename C1::Item_Type, typename C2::Item_Type > are_eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Detailed equality check returning mismatch position and values.
DynList< T > flatten(const C2< C1< T > > &c)
Flatten a nested container of one level.
void each(const size_t start, const size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
Container< T > nrange(const T start, const T end, const size_t n)
Generate exactly n values evenly spaced between [start, end].
auto group_by_reduce(const Container< T > &c, KeyFunc key_func, Reducer reducer) -> DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, std::invoke_result_t< Reducer, const DynList< T > & > > >
Group consecutive elements and apply a reducer to each group.
DynList< std::pair< std::optional< typename Container1::Item_Type >, std::optional< typename Container2::Item_Type > > > zip_longest_opt(const Container1 &a, const Container2 &b)
Zip two containers using optionals for missing values.
auto unzip(const Container &l)
Separate a list of pairs into two lists.
void reverse(Itor beg, Itor end)
Reverse elements in a range.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
T Item_Type
Type of elements from the first container.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
auto set_range(const T start, const T end, const T step, Op &op) -> Container< std::decay_t< decltype(op(start))> >
Generate a range [start, end] and apply an operation to each value.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zipEq(const Container1 &a, const Container2 &b)
Zip two containers; throw if lengths differ.
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
void enum_for_each(const Container &container, Operation &operation)
Apply an operation to each element and its index.
std::pair< TgtContainer< typename SrcContainer::Item_Type >, TgtContainer< typename SrcContainer::Item_Type > > partition(const SrcContainer &c, std::function< bool(const typename SrcContainer::Item_Type &)> operation)
Partition a container into two based on a predicate.
bool containers_eq(const C1 &c1, const C2 &c2, Eq e)
See eq.
bool all_unique(const Container &container, Equal &eq)
Check if all elements in a container are unique using a comparator.
DynList< std::pair< T, size_t > > repeated_with_index(const Container< T > &c)
Return repeated elements paired with their occurrence count.
T foldr(const Container &container, const T &init, Operation operation)
Right fold (reduce).
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
size_t remove_from_container(C &c, Args... args)
Remove multiple items from a container.
bool is_inside(const T &val, const DynList< T > &values)
Check if a value is present in a list.
std::string concat(const Args &...args)
Concatenate multiple arguments into a single std::string.
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
const float hash_default_upper_alpha
const Container::Item_Type * max_ptr(const Container &container, Cmp cmp=Cmp())
Find the maximum element in a container.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
auto enumerate(const Container &c)
Return pairs of (element, index).
bool is_equal(const T &val)
Variadic check for equality against multiple values.
T foldl(const Container &container, const T &init, Operation operation)
Classic left fold (reduce).
std::decay_t< typename HeadC::Item_Type > T
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
auto gen_seq_list_tuples(const Container &c, size_t n)
Generate all sequential tuples (sliding windows) of size n.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
void iota(C &container, typename C::Item_Type start)
Fill all elements of a container with unit-step sequential values.
std::optional< typename Container::Item_Type > find_opt(const Container &container, Pred &pred)
Find the first element satisfying pred (safe version).
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
auto flat_map(const Container &container, Op op) -> DynList< typename std::decay_t< decltype(op(std::declval< typename Container::Item_Type >()))>::Item_Type >
Apply operation and flatten results (flatMap/concatMap).
auto get_curr() const
Return the current tuple (bounds-checked).
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > tzip(const Container1 &a, const Container2 &b)
Zip two containers into a list of tuples.
and
Check uniqueness with explicit hash + equality functors.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
and PredicateWith< Cmp &, const typename Container::Item_Type &, functional_detail::get_curr_cref_t< Container > > std::pair< const typename Container::Item_Type *, const typename Container::Item_Type * > minmax_ptr(const Container &container, Cmp cmp=Cmp())
Find both min and max elements in a single pass.
DynList< typename Container::Item_Type * > pointers_list(Container &c)
Create a list of pointers to items in a container.
DynList< std::pair< size_t, typename Container::Key_Type > > indexes(const Container &c)
Return pairs of (index, key).
bool lesser(const C1 &c1, const C2 &c2, Cmp cmp=Cmp())
Lexicographical comparison between two containers.
DynList< typename Container::Item_Type > take_while(const Container &c, Pred pred)
Return elements while predicate is true (take_while).
size_t append_in_container(C &c, Args &&... args)
Append multiple items into a container.
TgtC assign_container(const SrcC &srcc)
Convert one container type to another.
DynList< std::tuple< size_t, typename Container::Item_Type > > enumerate_tuple(const Container &container)
Zip containers with an index (enumerate with zip).
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > tzipEq(const Container1 &a, const Container2 &b)
Zip two containers into tuples; throw if lengths differ.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
C build_container(Args &&... args)
Build a container with the given items.
std::pair< DynList< DynList< T > >, size_t > sequential_groups(const Container< T > &c, Equal &eq)
Group consecutive equal elements together.
DynList< std::tuple< size_t, typename Container::Key_Type > > tindexes(const Container &c)
Return tuples of (index, key).
Itor::difference_type count_if(Itor beg, const Itor &end, Operation op)
Count elements satisfying a predicate.
DynList< T > scanl_sum(const Container &container, const T &init)
Compute running sums (scanl with addition).
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > tzip_longest(const Container1 &a, const Container2 &b, const typename Container1::Item_Type &fill_a=typename Container1::Item_Type(), const typename Container2::Item_Type &fill_b=typename Container2::Item_Type())
Zip two containers into tuples, padding the shorter one.
DynList< T > scanl(const Container &container, const T &init, Op op)
Prefix scan with custom operation.
DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, DynList< T > > > group_by_eq(const Container< T > &c, KeyFunc key_func, KeyEqual key_equal)
Group consecutive elements by a key function with custom equality.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip_longest(const Container1 &a, const Container2 &b, const typename Container1::Item_Type &fill_a=typename Container1::Item_Type(), const typename Container2::Item_Type &fill_b=typename Container2::Item_Type())
Zip two containers, padding the shorter one with a fill value.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
DynList< typename Container::Item_Type > sublist(const Container &c, size_t pos, const size_t stride)
Extract a sublist using a stride.
Container::Item_Type * find_ptr(Container &container, Pred &pred)
Find the first element satisfying pred.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, DynList< T > > > group_by(const Container< T > &c, KeyFunc key_func)
Group consecutive elements by a key function.
Container< T > contiguous_range(T start, const size_t n)
Generate n contiguous values starting from start.
DynList< T > build_dynlist(Args &&... args)
Build a DynList with the given items; an rvalue is moved.
const float hash_default_lower_alpha
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
auto get_pair_it(C1 &c1, C2 &c2)
Create a Pair_Iterator for two containers.
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
DynList< typename Container::Item_Type > drop_while(const Container &c, Pred pred)
Skip elements while predicate is true (drop_while).
const Container::Item_Type * min_ptr(const Container &container, Cmp cmp=Cmp())
Find the minimum element in a container.
size_t insert_in_container(C &c, Args &&... args)
Insert multiple items into a container.
@ Tuple
Tuple type such as (Int, Bool).
std::tuple< Container< T1 >, Container< T2 > > tunzip(const Container< std::tuple< T1, T2 > > &l)
Separate a list of tuples into two containers.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Default filter operation (always true).
bool operator()(const T &) const noexcept
Default folding operation (returns default-constructed accumulator).
TR operator()(const TR &, const TD &) const noexcept(std::is_nothrow_default_constructible_v< TR >)
Builds an accumulator by default, whatever it is given.
Default mapping operation: the conversion of the item, which is its copy when the two types are the s...
tgtT operator()(const srcT &item) const noexcept(noexcept(static_cast< tgtT >(item)))
Converts an item to the type of the result.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
Abstract base class for optional-like results.
virtual bool is_found() const noexcept=0
virtual const T & get_item() const =0
Represents a missing value.
const T & get_item() const override
bool is_found() const noexcept override
Represents a found value (stored by reference).
const T & get_item() const override
bool is_found() const noexcept override