32# ifndef AH_UNI_FUNCTIONAL_H
33# define AH_UNI_FUNCTIONAL_H
71# include <type_traits>
88 namespace uni_functional_detail
103 template <
typename C>
110 template <
typename T>
119 template <
typename T,
typename =
void>
122 template <
typename T>
125 using type =
typename T::value_type;
128 template <
typename T>
130 has_aleph_iterator<T>::value and not has_stl_iterator<T>::value>>
132 using type = std::decay_t<typename T::Item_Type>;
135 template <
typename T>
139 template <
typename T>
143 template <
typename T>
163 template <
typename Op,
typename Container>
167 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
168 for (
const auto & item: c)
172 op_ref(it.get_curr());
183 template <
typename Op,
typename Container>
188 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
189 for (
const auto & item: c)
193 op_ref(i, it.get_curr());
205 template <
typename Op,
typename Container>
209 using R = std::decay_t<decltype(std::forward<Op>(op)(std::declval<T>()))>;
211 std::vector<R> result;
214 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
216 result.reserve(c.size());
217 for (
const auto & item: c)
218 result.push_back(op_ref(item));
222 result.push_back(op_ref(it.get_curr()));
236 template <
typename Op,
typename Container>
240 using R = std::decay_t<decltype(std::forward<Op>(op)(
size_t{}, std::declval<T>()))>;
242 std::vector<R> result;
246 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
248 result.reserve(c.size());
249 for (
const auto & item: c)
250 result.push_back(op_ref(i++, item));
254 result.push_back(op_ref(i, it.get_curr()));
268 template <
typename Pred,
typename Container>
273 std::vector<T> result;
274 auto & pred_ref =
pred;
276 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
278 for (
const auto & item: c)
280 result.push_back(item);
285 if (
const auto & item = it.get_curr(); pred_ref(item))
286 result.push_back(item);
300 template <
typename Pred,
typename Container>
305 std::vector<T> result;
306 auto & pred_ref =
pred;
309 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
310 for (
const auto & item: c)
312 if (pred_ref(i, item))
313 result.push_back(item);
318 if (
const auto & item = it.get_curr(); pred_ref(i, item))
319 result.push_back(item);
334 template <
typename T,
typename Op,
typename Container>
337 T acc = std::move(
init);
340 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
341 for (
const auto & item: c)
342 acc = op_ref(std::move(acc), item);
345 acc = op_ref(std::move(acc), it.get_curr());
350 template <
typename T,
typename Op,
typename Container>
366 template <
typename T,
typename Op,
typename Container>
369 std::vector<T> result;
370 result.push_back(
init);
372 T acc = std::move(
init);
375 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
376 for (
const auto & item: c)
378 acc = op_ref(std::move(acc), item);
379 result.push_back(acc);
384 acc = op_ref(std::move(acc), it.get_curr());
385 result.push_back(acc);
403 template <
typename Pred,
typename Container>
406 auto & pred_ref =
pred;
407 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
409 for (
const auto & item: c)
410 if (not pred_ref(item))
416 if (not pred_ref(it.get_curr()))
431 template <
typename Pred,
typename Container>
434 auto & pred_ref =
pred;
435 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
437 for (
const auto & item: c)
444 if (pred_ref(it.get_curr()))
451 template <
typename Pred,
typename Container>
462 template <
typename Pred,
typename Container>
481 template <
typename Pred,
typename Container>
486 auto & pred_ref =
pred;
487 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
489 for (
const auto & item: c)
491 return std::optional<T>(item);
496 if (
const auto & item = it.get_curr(); pred_ref(item))
497 return std::optional<T>(item);
499 return std::optional<T>{};
511 template <
typename Pred,
typename Container>
514 auto & pred_ref =
pred;
517 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
519 for (
const auto & item: c)
529 if (pred_ref(it.get_curr()))
544 template <
typename Op,
typename Container>
548 using OptType = std::decay_t<decltype(std::forward<Op>(op)(
size_t{}, std::declval<T>()))>;
553 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
555 for (
const auto & item: c)
556 if (
auto result = op_ref(i++, item))
562 if (
auto result = op_ref(i, it.get_curr()))
577 template <
typename T,
typename Container>
580 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
582 for (
const auto & item: c)
589 if (it.get_curr() == target)
608 template <
typename Pred,
typename Container>
611 auto & pred_ref =
pred;
614 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
616 for (
const auto & item: c)
623 if (pred_ref(it.get_curr()))
637 template <
typename Container>
640 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
666 template <
typename Container>
671 std::vector<T> result;
675 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
677 for (
const auto & item: c)
679 if (
count >= n)
break;
680 result.push_back(item);
687 result.push_back(it.get_curr());
701 template <
typename Container>
706 std::vector<T> result;
709 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
711 for (
const auto & item: c)
714 result.push_back(item);
722 result.push_back(it.get_curr());
736 template <
typename Pred,
typename Container>
741 std::vector<T> result;
742 auto & pred_ref =
pred;
744 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
746 for (
const auto & item: c)
748 if (not pred_ref(item))
750 result.push_back(item);
757 const auto & item = it.get_curr();
758 if (not pred_ref(item))
760 result.push_back(item);
775 template <
typename Pred,
typename Container>
780 std::vector<T> result;
781 auto & pred_ref =
pred;
782 bool dropping =
true;
784 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
786 for (
const auto & item: c)
788 if (dropping
and pred_ref(item))
791 result.push_back(item);
798 const auto & item = it.get_curr();
799 if (dropping
and pred_ref(item))
802 result.push_back(item);
820 template <
typename Container>
825 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
829 return std::optional<T>{};
830 return std::optional<T>(*it);
835 if (not it.has_curr())
836 return std::optional<T>{};
837 return std::optional<T>(it.get_curr());
849 template <
typename Container>
854 std::optional<T> result;
856 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
858 for (
const auto & item: c)
864 result = it.get_curr();
878 template <
typename Container>
885 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
887 for (
const auto & item: c)
890 return std::optional<T>(item);
898 return std::optional<T>(it.get_curr());
900 return std::optional<T>{};
915 template <
typename Container>
920 std::optional<T> result;
922 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
930 for (; it != c.end(); ++it)
933 return std::optional<T>(min_val);
938 if (not it.has_curr())
941 T min_val = it.get_curr();
943 for (; it.has_curr(); it.next_ne())
945 const auto & item = it.get_curr();
949 return std::optional<T>(min_val);
961 template <
typename Container>
966 std::optional<T> result;
968 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
976 for (; it != c.end(); ++it)
979 return std::optional<T>(max_val);
984 if (not it.has_curr())
987 T max_val = it.get_curr();
989 for (; it.has_curr(); it.next_ne())
991 const auto & item = it.get_curr();
995 return std::optional<T>(max_val);
1007 template <
typename Container>
1011 using ResultType = std::optional<std::pair<T, T>>;
1013 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1015 auto it = c.begin();
1017 return ResultType{};
1023 for (; it != c.end(); ++it)
1025 if (*it < min_val) min_val = *it;
1026 if (*it > max_val) max_val = *it;
1028 return ResultType(std::make_pair(min_val, max_val));
1033 if (not it.has_curr())
1034 return ResultType{};
1036 T min_val = it.get_curr();
1037 T max_val = min_val;
1040 for (; it.has_curr(); it.next_ne())
1042 const auto & item = it.get_curr();
1043 if (item < min_val) min_val = item;
1044 if (item > max_val) max_val = item;
1046 return ResultType(std::make_pair(min_val, max_val));
1062 template <
typename Container>
1068 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1070 for (
const auto & item: c)
1076 sum =
sum + it.get_curr();
1089 template <
typename Container>
1094 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1096 auto it = c.begin();
1097 if (it == c.end())
return T{};
1101 for (; it != c.end(); ++it)
1102 prod = prod * (*it);
1108 if (not it.has_curr())
return T{};
1110 T prod = it.get_curr();
1112 for (; it.has_curr(); it.next_ne())
1113 prod = prod * it.get_curr();
1131 template <
typename Pred,
typename Container>
1136 std::vector<T> matching, non_matching;
1137 auto & pred_ref =
pred;
1139 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1141 for (
const auto & item: c)
1144 matching.push_back(item);
1146 non_matching.push_back(item);
1153 const auto & item = it.get_curr();
1155 matching.push_back(item);
1157 non_matching.push_back(item);
1160 return std::make_pair(std::move(matching), std::move(non_matching));
1176 template <
typename Container1,
typename Container2>
1177 [[nodiscard]]
auto uni_concat(
const Container1 & c1,
const Container2 & c2)
1181 std::vector<T> result;
1183 if constexpr (uni_functional_detail::is_stl_container_v<Container1>)
1184 for (
const auto & item: c1)
1185 result.push_back(item);
1188 result.push_back(it.get_curr());
1190 if constexpr (uni_functional_detail::is_stl_container_v<Container2>)
1191 for (
const auto & item: c2)
1192 result.push_back(item);
1195 result.push_back(it.get_curr());
1208 template <
typename Container>
1214 std::vector<T> result;
1216 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1218 for (
const auto & inner: c)
1220 if constexpr (uni_functional_detail::is_stl_container_v<InnerContainer>)
1221 for (
const auto & item: inner)
1222 result.push_back(item);
1225 result.push_back(it.get_curr());
1232 const auto & inner = it.get_curr();
1233 if constexpr (uni_functional_detail::is_stl_container_v<InnerContainer>)
1234 for (
const auto & item: inner)
1235 result.push_back(item);
1238 result.push_back(it2.get_curr());
1254 template <
typename Op,
typename Container>
1258 using InnerContainer = std::decay_t<decltype(std::forward<Op>(op)(std::declval<T>()))>;
1261 std::vector<R> result;
1264 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1266 for (
const auto & item: c)
1268 auto inner = op_ref(item);
1269 if constexpr (uni_functional_detail::is_stl_container_v<InnerContainer>)
1270 for (
const auto & inner_item: inner)
1271 result.push_back(inner_item);
1274 result.push_back(it.get_curr());
1281 auto inner = op_ref(it.get_curr());
1282 if constexpr (uni_functional_detail::is_stl_container_v<InnerContainer>)
1283 for (
const auto & inner_item: inner)
1284 result.push_back(inner_item);
1287 result.push_back(it2.get_curr());
1309 template <
typename Container>
1314 std::vector<T> result;
1316 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1318 for (
const auto & item: c)
1319 if (std::find(result.begin(), result.end(), item) == result.end())
1320 result.push_back(item);
1325 if (
const auto & item = it.get_curr(); std::find(result.begin(), result.end(), item) == result.end())
1326 result.push_back(item);
1341 template <
typename Key,
typename Container>
1345 using K = std::decay_t<decltype(std::forward<Key>(key)(std::declval<T>()))>;
1347 std::vector<std::pair<K, std::vector<T>>> result;
1348 auto & key_ref = key;
1350 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1352 for (
const auto & item: c)
1354 auto k = key_ref(item);
1355 auto it = std::find_if(result.begin(), result.end(),
1356 [&
k](
const auto & p) { return p.first == k; });
1357 if (it != result.end())
1358 it->second.push_back(item);
1360 result.emplace_back(std::move(
k), std::vector<T>{item});
1367 const auto & item = it.get_curr();
1368 auto k = key_ref(item);
1369 auto res_it = std::find_if(result.begin(), result.end(),
1370 [&
k](
const auto & p) { return p.first == k; });
1371 if (res_it != result.end())
1372 res_it->second.push_back(item);
1374 result.emplace_back(std::move(
k), std::vector<T>{item});
1389 template <
typename Container>
1394 std::vector<std::pair<T, size_t>> result;
1396 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1398 for (
const auto & item: c)
1400 auto it = std::find_if(result.begin(), result.end(),
1401 [&item](
const auto & p) { return p.first == item; });
1402 if (it != result.end())
1405 result.emplace_back(item, 1);
1412 const auto & item = it.get_curr();
1413 auto res_it = std::find_if(result.begin(), result.end(),
1414 [&item](
const auto & p) { return p.first == item; });
1415 if (res_it != result.end())
1418 result.emplace_back(item, 1);
1437 template <
typename Container>
1442 std::vector<T> result;
1444 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1446 result.reserve(c.size());
1447 for (
const auto & item: c)
1448 result.push_back(item);
1453 result.push_back(it.get_curr());
1474 template <
typename Container1,
typename Container2>
1475 [[nodiscard]]
bool uni_equal(
const Container1 & c1,
const Container2 & c2)
1477 constexpr bool c1_is_stl = uni_functional_detail::is_stl_container_v<Container1>;
1478 constexpr bool c2_is_stl = uni_functional_detail::is_stl_container_v<Container2>;
1480 if constexpr (c1_is_stl
and c2_is_stl)
1483 auto it1 = c1.begin();
1484 auto it2 = c2.begin();
1485 for (; it1 != c1.end()
and it2 != c2.end(); ++it1, ++it2)
1486 if (not (*it1 == *it2))
1488 return it1 == c1.end()
and it2 == c2.end();
1490 else if constexpr (c1_is_stl
and not c2_is_stl)
1493 auto it1 = c1.begin();
1495 for (; it1 != c1.end()
and it2.has_curr(); ++it1, it2.next_ne())
1496 if (not (*it1 == it2.get_curr()))
1498 return it1 == c1.end()
and not it2.has_curr();
1500 else if constexpr (not c1_is_stl
and c2_is_stl)
1504 auto it2 = c2.begin();
1505 for (; it1.has_curr()
and it2 != c2.end(); it1.next_ne(), ++it2)
1506 if (not (it1.get_curr() == *it2))
1508 return not it1.has_curr()
and it2 == c2.end();
1515 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1516 if (not (it1.get_curr() == it2.get_curr()))
1518 return not it1.has_curr()
and not it2.has_curr();
1534 template <
typename Container1,
typename Container2>
1535 [[nodiscard]]
int uni_compare(
const Container1 & c1,
const Container2 & c2)
1537 constexpr bool c1_is_stl = uni_functional_detail::is_stl_container_v<Container1>;
1538 constexpr bool c2_is_stl = uni_functional_detail::is_stl_container_v<Container2>;
1540 if constexpr (c1_is_stl
and c2_is_stl)
1543 auto it1 = c1.begin();
1544 auto it2 = c2.begin();
1545 for (; it1 != c1.end()
and it2 != c2.end(); ++it1, ++it2)
1547 if (*it1 < *it2)
return -1;
1548 if (*it2 < *it1)
return 1;
1550 if (it1 == c1.end()
and it2 == c2.end())
return 0;
1551 return (it1 == c1.end()) ? -1 : 1;
1553 else if constexpr (c1_is_stl
and not c2_is_stl)
1556 auto it1 = c1.begin();
1558 for (; it1 != c1.end()
and it2.has_curr(); ++it1, it2.next_ne())
1560 if (*it1 < it2.get_curr())
return -1;
1561 if (it2.get_curr() < *it1)
return 1;
1563 if (it1 == c1.end()
and not it2.has_curr())
return 0;
1564 return (it1 == c1.end()) ? -1 : 1;
1566 else if constexpr (not c1_is_stl
and c2_is_stl)
1570 auto it2 = c2.begin();
1571 for (; it1.has_curr()
and it2 != c2.end(); it1.next_ne(), ++it2)
1573 if (it1.get_curr() < *it2)
return -1;
1574 if (*it2 < it1.get_curr())
return 1;
1576 if (not it1.has_curr()
and it2 == c2.end())
return 0;
1577 return (not it1.has_curr()) ? -1 : 1;
1584 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
1586 if (it1.get_curr() < it2.get_curr())
return -1;
1587 if (it2.get_curr() < it1.get_curr())
return 1;
1589 if (not it1.has_curr()
and not it2.has_curr())
return 0;
1590 return (not it1.has_curr()) ? -1 : 1;
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
STL-compatible iterator adapters for Aleph containers.
size_t size_t int32_t value
Read-only view of an Aleph iterator: the iterator of a constant container.
Freq_Node * pred
Predecessor node in level-order traversal.
constexpr bool is_stl_container_v
auto aleph_iterator(const C &c)
constexpr bool is_aleph_only_v
typename container_value_type< std::decay_t< T > >::type value_t
Main namespace for Aleph-w library functions.
T uni_reduce(T init, Op &&op, const Container &c)
Alias for uni_foldl.
auto uni_distinct(const Container &c)
Remove all duplicates (keeps first occurrence).
bool uni_none(Pred &&pred, const Container &c)
Check if no element satisfies predicate.
auto uni_filter(Pred &&pred, const Container &c)
Filter elements satisfying predicate.
auto uni_nth(size_t n, const Container &c)
Get n-th element.
int uni_compare(const Container1 &c1, const Container2 &c2)
Compare two containers lexicographically.
bool uni_equal(const Container1 &c1, const Container2 &c2)
Check equality of two containers.
std::vector< T > uni_scan_left(T init, Op &&op, const Container &c)
Scan left - fold with all intermediate results.
size_t uni_length(const Container &c)
Get container length.
auto uni_tally(const Container &c)
Count occurrences of each element (frequency count).
auto uni_flatten(const Container &c)
Flatten a container of containers.
auto uni_flat_map(Op &&op, const Container &c)
Flat map - map then flatten.
auto uni_drop_while(Pred &&pred, const Container &c)
Drop elements while predicate is true, return the rest.
std::decay_t< typename HeadC::Item_Type > T
auto uni_product(const Container &c)
Product of all elements.
auto uni_find(Pred &&pred, const Container &c)
Find first element satisfying predicate.
bool uni_mem(const T &target, const Container &c)
Check if element exists in container (mem in ML).
and
Check uniqueness with explicit hash + equality functors.
auto uni_group_by(Key &&key, const Container &c)
Group elements by key function.
auto uni_filteri(Pred &&pred, const Container &c)
Filter with index (filteri in ML).
auto uni_map(Op &&op, const Container &c)
Map operation - transform each element.
auto uni_concat(const Container1 &c1, const Container2 &c2)
Concatenate two containers.
auto uni_last(const Container &c)
Get last element.
auto uni_min_max(const Container &c)
Get both min and max in a single pass.
auto uni_min(const Container &c)
Get minimum element.
size_t uni_count(Pred &&pred, const Container &c)
Count elements satisfying predicate.
void uni_for_each_indexed(Op &&op, const Container &c)
Apply operation to each element with index.
T uni_foldl(T init, Op &&op, const Container &c)
Left fold (foldl) - reduce from left to right.
auto uni_drop(size_t n, const Container &c)
Drop first n elements, return the rest.
auto uni_max(const Container &c)
Get maximum element.
void uni_for_each(Op &&op, const Container &c)
Apply operation to each element (for_each).
auto uni_find_mapi(Op &&op, const Container &c)
Find and map with index (find_mapi in ML).
auto uni_take_while(Pred &&pred, const Container &c)
Take elements while predicate is true.
bool uni_all(Pred &&pred, const Container &c)
Check if all elements satisfy predicate.
auto uni_first(const Container &c)
Get first element.
auto uni_partition(Pred &&pred, const Container &c)
Partition elements by predicate.
static std::atomic< bool > init
auto uni_to_vector(const Container &c)
Convert container to std::vector.
auto uni_mapi(Op &&op, const Container &c)
Map with index (mapi in ML).
bool uni_any(Pred &&pred, const Container &c)
Alias for uni_exists.
auto uni_take(size_t n, const Container &c)
Take first n elements.
auto uni_sum(const Container &c)
Sum all elements.
bool uni_exists(Pred &&pred, const Container &c)
Check if any element satisfies predicate.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
std::optional< size_t > uni_find_index(Pred &&pred, const Container &c)
Find index of first element satisfying predicate.
typename T::value_type type
std::decay_t< typename T::Item_Type > type