Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-uni-functional.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
32# ifndef AH_UNI_FUNCTIONAL_H
33# define AH_UNI_FUNCTIONAL_H
34
71# include <type_traits>
72# include <vector>
73# include <optional>
74# include <functional>
75# include <algorithm>
76# include <utility>
77# include <tuple>
78# include <iterator>
79# include <ah-concepts.H>
80#include <ah-iterator.H>
81
82namespace Aleph
83{
84 // ============================================================================
85 // Type Traits for Container Detection
86 // ============================================================================
87
88 namespace uni_functional_detail
89 {
90 // Detect if type has STL-style begin()/end()
91 template <typename T>
92 struct has_stl_iterator : std::bool_constant<StlIterableContainer<T>>
93 {};
94
95 // Detect if type has Aleph-style Iterator with has_curr()/get_curr()/next()
96 // Detect if type yields an Aleph iterator (shared with ah-zip-utils.H)
97 template <typename T>
98 struct has_aleph_iterator : std::bool_constant<AlephIterable<T>>
99 {};
100
101 // Build the Aleph iterator the way AlephIterable promises it can be
102 // built, so containers without get_it() (MemArray, raw hash tables) work.
103 template <typename C>
104 [[nodiscard]] auto aleph_iterator(const C & c)
105 {
107 }
108
109 // Detect if type has reverse iterators (rbegin/rend)
110 template <typename T>
111 struct has_reverse_iterator : std::bool_constant<requires(const T & c)
112 {
113 c.rbegin();
114 c.rend();
115 }>
116 {};
117
118 // Get value type from container
119 template <typename T, typename = void>
121
122 template <typename T>
123 struct container_value_type<T, std::enable_if_t<has_stl_iterator<T>::value>>
124 {
125 using type = typename T::value_type;
126 };
127
128 template <typename T>
129 struct container_value_type<T, std::enable_if_t<
130 has_aleph_iterator<T>::value and not has_stl_iterator<T>::value>>
131 {
132 using type = std::decay_t<typename T::Item_Type>;
133 };
134
135 template <typename T>
137
138 // Check if container is STL-style
139 template <typename T>
141
142 // Check if container is Aleph-only
143 template <typename T>
144 constexpr bool is_aleph_only_v =
147 } // namespace uni_functional_detail
148
149 // ============================================================================
150 // Core Functional Operations
151 // ============================================================================
152
163 template <typename Op, typename Container>
164 void uni_for_each(Op && op, const Container & c)
165 {
166 auto & op_ref = op;
167 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
168 for (const auto & item: c)
169 op_ref(item);
170 else
171 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
172 op_ref(it.get_curr());
173 }
174
183 template <typename Op, typename Container>
184 void uni_for_each_indexed(Op && op, const Container & c)
185 {
186 auto & op_ref = op;
187 size_t i = 0;
188 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
189 for (const auto & item: c)
190 op_ref(i++, item);
191 else
192 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++i)
193 op_ref(i, it.get_curr());
194 }
195
205 template <typename Op, typename Container>
206 [[nodiscard]] auto uni_map(Op && op, const Container & c)
207 {
209 using R = std::decay_t<decltype(std::forward<Op>(op)(std::declval<T>()))>;
210
211 std::vector<R> result;
212 auto & op_ref = op;
213
214 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
215 {
216 result.reserve(c.size());
217 for (const auto & item: c)
218 result.push_back(op_ref(item));
219 }
220 else
221 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
222 result.push_back(op_ref(it.get_curr()));
223
224 return result;
225 }
226
236 template <typename Op, typename Container>
237 [[nodiscard]] auto uni_mapi(Op && op, const Container & c)
238 {
240 using R = std::decay_t<decltype(std::forward<Op>(op)(size_t{}, std::declval<T>()))>;
241
242 std::vector<R> result;
243 auto & op_ref = op;
244 size_t i = 0;
245
246 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
247 {
248 result.reserve(c.size());
249 for (const auto & item: c)
250 result.push_back(op_ref(i++, item));
251 }
252 else
253 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++i)
254 result.push_back(op_ref(i, it.get_curr()));
255
256 return result;
257 }
258
268 template <typename Pred, typename Container>
269 [[nodiscard]] auto uni_filter(Pred && pred, const Container & c)
270 {
272
273 std::vector<T> result;
274 auto & pred_ref = pred;
275
276 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
277 {
278 for (const auto & item: c)
279 if (pred_ref(item))
280 result.push_back(item);
281 }
282 else
283 {
284 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
285 if (const auto & item = it.get_curr(); pred_ref(item))
286 result.push_back(item);
287 }
288 return result;
289 }
290
300 template <typename Pred, typename Container>
301 [[nodiscard]] auto uni_filteri(Pred && pred, const Container & c)
302 {
304
305 std::vector<T> result;
306 auto & pred_ref = pred;
307 size_t i = 0;
308
309 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
310 for (const auto & item: c)
311 {
312 if (pred_ref(i, item))
313 result.push_back(item);
314 ++i;
315 }
316 else
317 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++i)
318 if (const auto & item = it.get_curr(); pred_ref(i, item))
319 result.push_back(item);
320
321 return result;
322 }
323
334 template <typename T, typename Op, typename Container>
335 [[nodiscard]] T uni_foldl(T init, Op && op, const Container & c)
336 {
337 T acc = std::move(init);
338 auto & op_ref = op;
339
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);
343 else
344 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
345 acc = op_ref(std::move(acc), it.get_curr());
346 return acc;
347 }
348
350 template <typename T, typename Op, typename Container>
351 [[nodiscard]] T uni_reduce(T init, Op && op, const Container & c)
352 {
353 return uni_foldl(std::move(init), std::forward<Op>(op), c);
354 }
355
366 template <typename T, typename Op, typename Container>
367 [[nodiscard]] std::vector<T> uni_scan_left(T init, Op && op, const Container & c)
368 {
369 std::vector<T> result;
370 result.push_back(init);
371
372 T acc = std::move(init);
373 auto & op_ref = op;
374
375 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
376 for (const auto & item: c)
377 {
378 acc = op_ref(std::move(acc), item);
379 result.push_back(acc);
380 }
381 else
382 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
383 {
384 acc = op_ref(std::move(acc), it.get_curr());
385 result.push_back(acc);
386 }
387 return result;
388 }
389
390 // ============================================================================
391 // Predicates
392 // ============================================================================
393
403 template <typename Pred, typename Container>
404 [[nodiscard]] bool uni_all(Pred && pred, const Container & c)
405 {
406 auto & pred_ref = pred;
407 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
408 {
409 for (const auto & item: c)
410 if (not pred_ref(item))
411 return false;
412 }
413 else
414 {
415 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
416 if (not pred_ref(it.get_curr()))
417 return false;
418 }
419 return true;
420 }
421
431 template <typename Pred, typename Container>
432 [[nodiscard]] bool uni_exists(Pred && pred, const Container & c)
433 {
434 auto & pred_ref = pred;
435 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
436 {
437 for (const auto & item: c)
438 if (pred_ref(item))
439 return true;
440 }
441 else
442 {
443 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
444 if (pred_ref(it.get_curr()))
445 return true;
446 }
447 return false;
448 }
449
451 template <typename Pred, typename Container>
452 [[nodiscard]] bool uni_any(Pred && pred, const Container & c)
453 {
454 return uni_exists(std::forward<Pred>(pred), c);
455 }
456
462 template <typename Pred, typename Container>
463 [[nodiscard]] bool uni_none(Pred && pred, const Container & c)
464 {
465 return not uni_exists(std::forward<Pred>(pred), c);
466 }
467
468 // ============================================================================
469 // Finding Elements
470 // ============================================================================
471
481 template <typename Pred, typename Container>
482 [[nodiscard]] auto uni_find(Pred && pred, const Container & c)
483 {
485
486 auto & pred_ref = pred;
487 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
488 {
489 for (const auto & item: c)
490 if (pred_ref(item))
491 return std::optional<T>(item);
492 }
493 else
494 {
495 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
496 if (const auto & item = it.get_curr(); pred_ref(item))
497 return std::optional<T>(item);
498 }
499 return std::optional<T>{};
500 }
501
511 template <typename Pred, typename Container>
512 [[nodiscard]] std::optional<size_t> uni_find_index(Pred && pred, const Container & c)
513 {
514 auto & pred_ref = pred;
515 size_t i = 0;
516
517 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
518 {
519 for (const auto & item: c)
520 {
521 if (pred_ref(item))
522 return i;
523 ++i;
524 }
525 }
526 else
527 {
528 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++i)
529 if (pred_ref(it.get_curr()))
530 return i;
531 }
532 return std::nullopt;
533 }
534
544 template <typename Op, typename Container>
545 [[nodiscard]] auto uni_find_mapi(Op && op, const Container & c)
546 {
548 using OptType = std::decay_t<decltype(std::forward<Op>(op)(size_t{}, std::declval<T>()))>;
549
550 auto & op_ref = op;
551 size_t i = 0;
552
553 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
554 {
555 for (const auto & item: c)
556 if (auto result = op_ref(i++, item))
557 return result;
558 }
559 else
560 {
561 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++i)
562 if (auto result = op_ref(i, it.get_curr()))
563 return result;
564 }
565 return OptType{};
566 }
567
577 template <typename T, typename Container>
578 [[nodiscard]] bool uni_mem(const T & target, const Container & c)
579 {
580 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
581 {
582 for (const auto & item: c)
583 if (item == target)
584 return true;
585 }
586 else
587 {
588 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
589 if (it.get_curr() == target)
590 return true;
591 }
592 return false;
593 }
594
595 // ============================================================================
596 // Counting
597 // ============================================================================
598
608 template <typename Pred, typename Container>
609 [[nodiscard]] size_t uni_count(Pred && pred, const Container & c)
610 {
611 auto & pred_ref = pred;
612 size_t count = 0;
613
614 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
615 {
616 for (const auto & item: c)
617 if (pred_ref(item))
618 ++count;
619 }
620 else
621 {
622 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
623 if (pred_ref(it.get_curr()))
624 ++count;
625 }
626 return count;
627 }
628
637 template <typename Container>
638 [[nodiscard]] size_t uni_length(const Container & c)
639 {
640 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
641 {
642 return c.size();
643 }
644 else
645 {
646 size_t count = 0;
647 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
648 ++count;
649 return count;
650 }
651 }
652
653 // ============================================================================
654 // Taking and Dropping
655 // ============================================================================
656
666 template <typename Container>
667 [[nodiscard]] auto uni_take(size_t n, const Container & c)
668 {
670
671 std::vector<T> result;
672 result.reserve(n);
673 size_t count = 0;
674
675 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
676 {
677 for (const auto & item: c)
678 {
679 if (count >= n) break;
680 result.push_back(item);
681 ++count;
682 }
683 }
684 else
685 {
686 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr() and count < n; it.next_ne(), ++count)
687 result.push_back(it.get_curr());
688 }
689 return result;
690 }
691
701 template <typename Container>
702 [[nodiscard]] auto uni_drop(size_t n, const Container & c)
703 {
705
706 std::vector<T> result;
707 size_t count = 0;
708
709 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
710 {
711 for (const auto & item: c)
712 {
713 if (count >= n)
714 result.push_back(item);
715 ++count;
716 }
717 }
718 else
719 {
720 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++count)
721 if (count >= n)
722 result.push_back(it.get_curr());
723 }
724 return result;
725 }
726
736 template <typename Pred, typename Container>
737 [[nodiscard]] auto uni_take_while(Pred && pred, const Container & c)
738 {
740
741 std::vector<T> result;
742 auto & pred_ref = pred;
743
744 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
745 {
746 for (const auto & item: c)
747 {
748 if (not pred_ref(item))
749 break;
750 result.push_back(item);
751 }
752 }
753 else
754 {
755 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
756 {
757 const auto & item = it.get_curr();
758 if (not pred_ref(item))
759 break;
760 result.push_back(item);
761 }
762 }
763 return result;
764 }
765
775 template <typename Pred, typename Container>
776 [[nodiscard]] auto uni_drop_while(Pred && pred, const Container & c)
777 {
779
780 std::vector<T> result;
781 auto & pred_ref = pred;
782 bool dropping = true;
783
784 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
785 {
786 for (const auto & item: c)
787 {
788 if (dropping and pred_ref(item))
789 continue;
790 dropping = false;
791 result.push_back(item);
792 }
793 }
794 else
795 {
796 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
797 {
798 const auto & item = it.get_curr();
799 if (dropping and pred_ref(item))
800 continue;
801 dropping = false;
802 result.push_back(item);
803 }
804 }
805 return result;
806 }
807
808 // ============================================================================
809 // Accessing Elements
810 // ============================================================================
811
820 template <typename Container>
821 [[nodiscard]] auto uni_first(const Container & c)
822 {
824
825 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
826 {
827 auto it = c.begin();
828 if (it == c.end())
829 return std::optional<T>{};
830 return std::optional<T>(*it);
831 }
832 else
833 {
835 if (not it.has_curr())
836 return std::optional<T>{};
837 return std::optional<T>(it.get_curr());
838 }
839 }
840
849 template <typename Container>
850 [[nodiscard]] auto uni_last(const Container & c)
851 {
853
854 std::optional<T> result;
855
856 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
857 {
858 for (const auto & item: c)
859 result = item;
860 }
861 else
862 {
863 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
864 result = it.get_curr();
865 }
866 return result;
867 }
868
878 template <typename Container>
879 [[nodiscard]] auto uni_nth(size_t n, const Container & c)
880 {
882
883 size_t i = 0;
884
885 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
886 {
887 for (const auto & item: c)
888 {
889 if (i == n)
890 return std::optional<T>(item);
891 ++i;
892 }
893 }
894 else
895 {
896 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne(), ++i)
897 if (i == n)
898 return std::optional<T>(it.get_curr());
899 }
900 return std::optional<T>{};
901 }
902
903 // ============================================================================
904 // Min/Max
905 // ============================================================================
906
915 template <typename Container>
916 [[nodiscard]] auto uni_min(const Container & c)
917 {
919
920 std::optional<T> result;
921
922 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
923 {
924 auto it = c.begin();
925 if (it == c.end())
926 return result;
927
928 T min_val = *it;
929 ++it;
930 for (; it != c.end(); ++it)
931 if (*it < min_val)
932 min_val = *it;
933 return std::optional<T>(min_val);
934 }
935 else
936 {
938 if (not it.has_curr())
939 return result;
940
941 T min_val = it.get_curr();
942 it.next_ne();
943 for (; it.has_curr(); it.next_ne())
944 {
945 const auto & item = it.get_curr();
946 if (item < min_val)
947 min_val = item;
948 }
949 return std::optional<T>(min_val);
950 }
951 }
952
961 template <typename Container>
962 [[nodiscard]] auto uni_max(const Container & c)
963 {
965
966 std::optional<T> result;
967
968 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
969 {
970 auto it = c.begin();
971 if (it == c.end())
972 return result;
973
974 T max_val = *it;
975 ++it;
976 for (; it != c.end(); ++it)
977 if (*it > max_val)
978 max_val = *it;
979 return std::optional<T>(max_val);
980 }
981 else
982 {
984 if (not it.has_curr())
985 return result;
986
987 T max_val = it.get_curr();
988 it.next_ne();
989 for (; it.has_curr(); it.next_ne())
990 {
991 const auto & item = it.get_curr();
992 if (item > max_val)
993 max_val = item;
994 }
995 return std::optional<T>(max_val);
996 }
997 }
998
1007 template <typename Container>
1008 [[nodiscard]] auto uni_min_max(const Container & c)
1009 {
1011 using ResultType = std::optional<std::pair<T, T>>;
1012
1013 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1014 {
1015 auto it = c.begin();
1016 if (it == c.end())
1017 return ResultType{};
1018
1019 T min_val = *it;
1020 T max_val = *it;
1021 ++it;
1022
1023 for (; it != c.end(); ++it)
1024 {
1025 if (*it < min_val) min_val = *it;
1026 if (*it > max_val) max_val = *it;
1027 }
1028 return ResultType(std::make_pair(min_val, max_val));
1029 }
1030 else
1031 {
1033 if (not it.has_curr())
1034 return ResultType{};
1035
1036 T min_val = it.get_curr();
1037 T max_val = min_val;
1038 it.next_ne();
1039
1040 for (; it.has_curr(); it.next_ne())
1041 {
1042 const auto & item = it.get_curr();
1043 if (item < min_val) min_val = item;
1044 if (item > max_val) max_val = item;
1045 }
1046 return ResultType(std::make_pair(min_val, max_val));
1047 }
1048 }
1049
1050 // ============================================================================
1051 // Sum and Product
1052 // ============================================================================
1053
1062 template <typename Container>
1063 [[nodiscard]] auto uni_sum(const Container & c)
1064 {
1066 T sum{};
1067
1068 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1069 {
1070 for (const auto & item: c)
1071 sum = sum + item;
1072 }
1073 else
1074 {
1075 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1076 sum = sum + it.get_curr();
1077 }
1078 return sum;
1079 }
1080
1089 template <typename Container>
1090 [[nodiscard]] auto uni_product(const Container & c)
1091 {
1093
1094 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1095 {
1096 auto it = c.begin();
1097 if (it == c.end()) return T{};
1098
1099 T prod = *it;
1100 ++it;
1101 for (; it != c.end(); ++it)
1102 prod = prod * (*it);
1103 return prod;
1104 }
1105 else
1106 {
1108 if (not it.has_curr()) return T{};
1109
1110 T prod = it.get_curr();
1111 it.next_ne();
1112 for (; it.has_curr(); it.next_ne())
1113 prod = prod * it.get_curr();
1114 return prod;
1115 }
1116 }
1117
1118 // ============================================================================
1119 // Partitioning
1120 // ============================================================================
1121
1131 template <typename Pred, typename Container>
1132 [[nodiscard]] auto uni_partition(Pred && pred, const Container & c)
1133 {
1135
1136 std::vector<T> matching, non_matching;
1137 auto & pred_ref = pred;
1138
1139 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1140 {
1141 for (const auto & item: c)
1142 {
1143 if (pred_ref(item))
1144 matching.push_back(item);
1145 else
1146 non_matching.push_back(item);
1147 }
1148 }
1149 else
1150 {
1151 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1152 {
1153 const auto & item = it.get_curr();
1154 if (pred_ref(item))
1155 matching.push_back(item);
1156 else
1157 non_matching.push_back(item);
1158 }
1159 }
1160 return std::make_pair(std::move(matching), std::move(non_matching));
1161 }
1162
1163 // ============================================================================
1164 // Concatenation and Flattening
1165 // ============================================================================
1166
1176 template <typename Container1, typename Container2>
1177 [[nodiscard]] auto uni_concat(const Container1 & c1, const Container2 & c2)
1178 {
1180
1181 std::vector<T> result;
1182
1183 if constexpr (uni_functional_detail::is_stl_container_v<Container1>)
1184 for (const auto & item: c1)
1185 result.push_back(item);
1186 else
1187 for (auto it = uni_functional_detail::aleph_iterator(c1); it.has_curr(); it.next_ne())
1188 result.push_back(it.get_curr());
1189
1190 if constexpr (uni_functional_detail::is_stl_container_v<Container2>)
1191 for (const auto & item: c2)
1192 result.push_back(item);
1193 else
1194 for (auto it = uni_functional_detail::aleph_iterator(c2); it.has_curr(); it.next_ne())
1195 result.push_back(it.get_curr());
1196
1197 return result;
1198 }
1199
1208 template <typename Container>
1209 [[nodiscard]] auto uni_flatten(const Container & c)
1210 {
1211 using InnerContainer = uni_functional_detail::value_t<Container>;
1213
1214 std::vector<T> result;
1215
1216 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1217 {
1218 for (const auto & inner: c)
1219 {
1220 if constexpr (uni_functional_detail::is_stl_container_v<InnerContainer>)
1221 for (const auto & item: inner)
1222 result.push_back(item);
1223 else
1224 for (auto it = uni_functional_detail::aleph_iterator(inner); it.has_curr(); it.next_ne())
1225 result.push_back(it.get_curr());
1226 }
1227 }
1228 else
1229 {
1230 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1231 {
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);
1236 else
1237 for (auto it2 = uni_functional_detail::aleph_iterator(inner); it2.has_curr(); it2.next_ne())
1238 result.push_back(it2.get_curr());
1239 }
1240 }
1241
1242 return result;
1243 }
1244
1254 template <typename Op, typename Container>
1255 [[nodiscard]] auto uni_flat_map(Op && op, const Container & c)
1256 {
1258 using InnerContainer = std::decay_t<decltype(std::forward<Op>(op)(std::declval<T>()))>;
1260
1261 std::vector<R> result;
1262 auto & op_ref = op;
1263
1264 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1265 {
1266 for (const auto & item: c)
1267 {
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);
1272 else
1273 for (auto it = uni_functional_detail::aleph_iterator(inner); it.has_curr(); it.next_ne())
1274 result.push_back(it.get_curr());
1275 }
1276 }
1277 else
1278 {
1279 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1280 {
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);
1285 else
1286 for (auto it2 = uni_functional_detail::aleph_iterator(inner); it2.has_curr(); it2.next_ne())
1287 result.push_back(it2.get_curr());
1288 }
1289 }
1290
1291 return result;
1292 }
1293
1294 // ============================================================================
1295 // Uniqueness and Grouping
1296 // ============================================================================
1297
1309 template <typename Container>
1310 [[nodiscard]] auto uni_distinct(const Container & c)
1311 {
1313
1314 std::vector<T> result;
1315
1316 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1317 {
1318 for (const auto & item: c)
1319 if (std::find(result.begin(), result.end(), item) == result.end())
1320 result.push_back(item);
1321 }
1322 else
1323 {
1324 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1325 if (const auto & item = it.get_curr(); std::find(result.begin(), result.end(), item) == result.end())
1326 result.push_back(item);
1327 }
1328
1329 return result;
1330 }
1331
1341 template <typename Key, typename Container>
1342 [[nodiscard]] auto uni_group_by(Key && key, const Container & c)
1343 {
1345 using K = std::decay_t<decltype(std::forward<Key>(key)(std::declval<T>()))>;
1346
1347 std::vector<std::pair<K, std::vector<T>>> result;
1348 auto & key_ref = key;
1349
1350 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1351 {
1352 for (const auto & item: c)
1353 {
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);
1359 else
1360 result.emplace_back(std::move(k), std::vector<T>{item});
1361 }
1362 }
1363 else
1364 {
1365 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1366 {
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);
1373 else
1374 result.emplace_back(std::move(k), std::vector<T>{item});
1375 }
1376 }
1377
1378 return result;
1379 }
1380
1389 template <typename Container>
1390 [[nodiscard]] auto uni_tally(const Container & c)
1391 {
1393
1394 std::vector<std::pair<T, size_t>> result;
1395
1396 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1397 {
1398 for (const auto & item: c)
1399 {
1400 auto it = std::find_if(result.begin(), result.end(),
1401 [&item](const auto & p) { return p.first == item; });
1402 if (it != result.end())
1403 ++it->second;
1404 else
1405 result.emplace_back(item, 1);
1406 }
1407 }
1408 else
1409 {
1410 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1411 {
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())
1416 ++res_it->second;
1417 else
1418 result.emplace_back(item, 1);
1419 }
1420 }
1421
1422 return result;
1423 }
1424
1425 // ============================================================================
1426 // Conversion
1427 // ============================================================================
1428
1437 template <typename Container>
1438 [[nodiscard]] auto uni_to_vector(const Container & c)
1439 {
1441
1442 std::vector<T> result;
1443
1444 if constexpr (uni_functional_detail::is_stl_container_v<Container>)
1445 {
1446 result.reserve(c.size());
1447 for (const auto & item: c)
1448 result.push_back(item);
1449 }
1450 else
1451 {
1452 for (auto it = uni_functional_detail::aleph_iterator(c); it.has_curr(); it.next_ne())
1453 result.push_back(it.get_curr());
1454 }
1455 return result;
1456 }
1457
1458 // ============================================================================
1459 // Comparison
1460 // ============================================================================
1461
1474 template <typename Container1, typename Container2>
1475 [[nodiscard]] bool uni_equal(const Container1 & c1, const Container2 & c2)
1476 {
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>;
1479
1480 if constexpr (c1_is_stl and c2_is_stl)
1481 {
1482 // Both STL: use standard iterators
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))
1487 return false;
1488 return it1 == c1.end() and it2 == c2.end();
1489 }
1490 else if constexpr (c1_is_stl and not c2_is_stl)
1491 {
1492 // c1 STL, c2 Aleph
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()))
1497 return false;
1498 return it1 == c1.end() and not it2.has_curr();
1499 }
1500 else if constexpr (not c1_is_stl and c2_is_stl)
1501 {
1502 // c1 Aleph, c2 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))
1507 return false;
1508 return not it1.has_curr() and it2 == c2.end();
1509 }
1510 else
1511 {
1512 // Both Aleph
1515 for (; it1.has_curr() and it2.has_curr(); it1.next_ne(), it2.next_ne())
1516 if (not (it1.get_curr() == it2.get_curr()))
1517 return false;
1518 return not it1.has_curr() and not it2.has_curr();
1519 }
1520 }
1521
1534 template <typename Container1, typename Container2>
1535 [[nodiscard]] int uni_compare(const Container1 & c1, const Container2 & c2)
1536 {
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>;
1539
1540 if constexpr (c1_is_stl and c2_is_stl)
1541 {
1542 // Both STL: use standard iterators
1543 auto it1 = c1.begin();
1544 auto it2 = c2.begin();
1545 for (; it1 != c1.end() and it2 != c2.end(); ++it1, ++it2)
1546 {
1547 if (*it1 < *it2) return -1;
1548 if (*it2 < *it1) return 1;
1549 }
1550 if (it1 == c1.end() and it2 == c2.end()) return 0;
1551 return (it1 == c1.end()) ? -1 : 1;
1552 }
1553 else if constexpr (c1_is_stl and not c2_is_stl)
1554 {
1555 // c1 STL, c2 Aleph
1556 auto it1 = c1.begin();
1558 for (; it1 != c1.end() and it2.has_curr(); ++it1, it2.next_ne())
1559 {
1560 if (*it1 < it2.get_curr()) return -1;
1561 if (it2.get_curr() < *it1) return 1;
1562 }
1563 if (it1 == c1.end() and not it2.has_curr()) return 0;
1564 return (it1 == c1.end()) ? -1 : 1;
1565 }
1566 else if constexpr (not c1_is_stl and c2_is_stl)
1567 {
1568 // c1 Aleph, c2 STL
1570 auto it2 = c2.begin();
1571 for (; it1.has_curr() and it2 != c2.end(); it1.next_ne(), ++it2)
1572 {
1573 if (it1.get_curr() < *it2) return -1;
1574 if (*it2 < it1.get_curr()) return 1;
1575 }
1576 if (not it1.has_curr() and it2 == c2.end()) return 0;
1577 return (not it1.has_curr()) ? -1 : 1;
1578 }
1579 else
1580 {
1581 // Both Aleph
1584 for (; it1.has_curr() and it2.has_curr(); it1.next_ne(), it2.next_ne())
1585 {
1586 if (it1.get_curr() < it2.get_curr()) return -1;
1587 if (it2.get_curr() < it1.get_curr()) return 1;
1588 }
1589 if (not it1.has_curr() and not it2.has_curr()) return 0;
1590 return (not it1.has_curr()) ? -1 : 1;
1591 }
1592 }
1593} // end namespace Aleph
1594
1595# endif // AH_UNI_FUNCTIONAL_H
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
Definition ca-c-api.h:116
Read-only view of an Aleph iterator: the iterator of a constant container.
Freq_Node * pred
Predecessor node in level-order traversal.
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
Definition ah-zip.H:152
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
Definition hash-fct.C:54
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.
Definition ahAlgo.H:127
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.
STL namespace.
static int * k