Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-zip-utils.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32#ifndef AH_ZIP_UTILS_H
33#define AH_ZIP_UTILS_H
34
35#include <type_traits>
36#include <tuple>
37#include <iterator>
38#include <utility>
39#include <optional>
40#include <functional>
41#include <ah-concepts.H>
42#include <ah-iterator.H>
43
83namespace Aleph {
84
85// ============================================================================
86// Type Traits for Container Detection
87// ============================================================================
88
89namespace uni_zip_detail {
90
95template <typename T>
96struct has_stl_iterator : std::bool_constant<StlIterableContainer<T>>
97{
98};
99
106template <typename T>
107struct has_aleph_iterator : std::bool_constant<AlephIterable<T>>
108{
109};
110
115template <typename T, typename = void>
117
118template <typename T>
119struct container_value_type<T, std::enable_if_t<has_stl_iterator<T>::value>>
120{
121 using type = typename T::value_type;
122};
123
124template <typename T>
125struct container_value_type<T, std::enable_if_t<has_aleph_iterator<T>::value && !has_stl_iterator<T>::value>>
126{
127 using type = std::decay_t<typename T::Item_Type>;
128};
129
130template <typename T>
132
133// ============================================================================
134// Unified Iterator Wrapper
135// ============================================================================
136
149template <typename Container>
151{
152 typename std::decay_t<Container>::const_iterator curr_;
153 typename std::decay_t<Container>::const_iterator end_;
154
155public:
156 using value_type = typename std::decay_t<Container>::value_type;
157
159 explicit StlIteratorWrapper(const Container &c) : curr_(c.begin()), end_(c.end()) {}
160
162 [[nodiscard]] bool has_curr() const noexcept
163 {
164 return curr_ != end_;
165 }
166
168 [[nodiscard]] bool completed() const noexcept
169 {
170 return curr_ == end_;
171 }
172
175 [[nodiscard]] auto get_curr() const
176 {
177 return *curr_;
178 }
179
183 void next() noexcept
184 {
185 ++curr_;
186 }
187
193 void next_ne() noexcept
194 {
195 next();
196 }
197};
198
211template <typename Container>
213{
214 // mutable: the wrapper's const accessors forward to the Aleph iterator,
215 // whose get_curr() may be non-const (e.g. the chained hash tables).
217
218public:
219 using value_type = std::decay_t<typename std::decay_t<Container>::Item_Type>;
220
222 explicit AlephIteratorWrapper(const Container &c) : it_(c) {}
223
225 [[nodiscard]] bool has_curr() const noexcept
226 {
227 return it_.has_curr();
228 }
229
231 [[nodiscard]] bool completed() const noexcept
232 {
233 return !it_.has_curr();
234 }
235
238 [[nodiscard]] auto get_curr() const
239 {
240 return it_.get_curr();
241 }
242
245 void next()
246 {
247 it_.next_ne();
248 }
249
255 void next_ne()
256 {
257 next();
258 }
259};
260
274template <typename Container, typename = void>
276
277// STL containers (prefer STL if both interfaces exist)
278template <typename Container>
279struct IteratorWrapperSelector<Container, std::enable_if_t<has_stl_iterator<std::decay_t<Container>>::value>>
280{
282};
283
284// Aleph-only containers (no STL interface)
285template <typename Container>
287 std::enable_if_t<has_aleph_iterator<std::decay_t<Container>>::value &&
288 !has_stl_iterator<std::decay_t<Container>>::value>>
289{
291};
292
293template <typename Container>
295
296// Static assertion helper for unsupported container types
297template <typename T>
298inline constexpr bool is_supported_container_v =
300
301} // namespace uni_zip_detail
302
303// ============================================================================
304// Sentinel type for O(1) end()
305// ============================================================================
306
318{
319};
320
321// ============================================================================
322// Unified Zip Iterator
323// ============================================================================
324
338template <typename... Containers>
340{
341 static_assert((uni_zip_detail::is_supported_container_v<Containers> && ...),
342 "All containers must have either STL (begin/end) or Aleph (Iterator) interface");
343
344public:
345 using value_type = std::tuple<uni_zip_detail::container_value_t<Containers>...>;
346
347private:
348 std::tuple<uni_zip_detail::iterator_wrapper_t<Containers>...> iters_;
349
350 template <size_t... Is>
351 [[nodiscard]] bool has_curr_impl(std::index_sequence<Is...>) const noexcept
352 {
353 return (... && std::get<Is>(iters_).has_curr());
354 }
355
356 template <size_t... Is>
357 [[nodiscard]] bool any_has_curr_impl(std::index_sequence<Is...>) const noexcept
358 {
359 return (... || std::get<Is>(iters_).has_curr());
360 }
361
362 template <size_t... Is>
363 [[nodiscard]] bool all_completed_impl(std::index_sequence<Is...>) const noexcept
364 {
365 return (... && std::get<Is>(iters_).completed());
366 }
367
368 template <size_t... Is>
369 [[nodiscard]] auto get_curr_impl(std::index_sequence<Is...>) const
370 {
371 return std::make_tuple(std::get<Is>(iters_).get_curr()...);
372 }
373
374 template <size_t... Is>
375 void next_impl(std::index_sequence<Is...>)
376 {
377 (std::get<Is>(iters_).next(), ...);
378 }
379
380public:
381 static constexpr size_t num_containers = sizeof...(Containers);
382
384 explicit UniZipIterator(const Containers &...cs)
385 : iters_(uni_zip_detail::iterator_wrapper_t<Containers>(cs)...)
386 {}
387
389
390 [[nodiscard]] bool has_curr() const noexcept
391 {
392 return has_curr_impl(std::make_index_sequence<num_containers>{});
393 }
394
396 [[nodiscard]] bool any_has_curr() const noexcept
397 {
398 return any_has_curr_impl(std::make_index_sequence<num_containers>{});
399 }
400
402 [[nodiscard]] bool all_completed() const noexcept
403 {
404 return all_completed_impl(std::make_index_sequence<num_containers>{});
405 }
406
408
409 [[nodiscard]] bool completed() const noexcept
410 {
411 return all_completed();
412 }
413
416 [[nodiscard]] auto get_curr() const
417 {
418 return get_curr_impl(std::make_index_sequence<num_containers>{});
419 }
420
423 void next()
424 {
425 next_impl(std::make_index_sequence<num_containers>{});
426 }
427
433 void next_ne()
434 {
435 next();
436 }
437
438 // STL iterator interface for range-based for
440 [[nodiscard]] auto operator * () const
441 {
442 return get_curr();
443 }
444
447 {
448 next();
449 return *this;
450 }
451
453 [[nodiscard]] bool operator == (UniZipSentinel) const noexcept
454 {
455 return !has_curr();
456 }
457
459 [[nodiscard]] bool operator != (UniZipSentinel s) const noexcept
460 {
461 return !(*this == s);
462 }
463
465 [[nodiscard]] bool operator == (const UniZipIterator &other) const noexcept
466 {
467 // Both at end (any container exhausted)
468 return !has_curr() && !other.has_curr();
469 }
470
472 [[nodiscard]] bool operator != (const UniZipIterator &other) const noexcept
473 {
474 return !(*this == other);
475 }
476};
477
486template <typename... Containers>
488{
489 std::tuple<const std::decay_t<Containers> &...> containers_;
490
491public:
495
497 explicit UniZipView(const Containers &...cs) : containers_(cs...) {}
498
500 [[nodiscard]] iterator begin() const
501 {
502 return std::apply([](const auto &...cs)
503 {
504 return iterator(cs...);
505 }, containers_);
506 }
507
509 [[nodiscard]] sentinel end() const noexcept
510 {
511 return sentinel{};
512 }
513
515 [[nodiscard]] bool empty() const
516 {
517 return !begin().has_curr();
518 }
519
521
524 [[nodiscard]] size_t size() const
525 {
526 size_t count = 0;
527 for (auto it = begin(); it != end(); ++it)
528 ++count;
529 return count;
530 }
531};
532
533// ============================================================================
534// Factory Functions
535// ============================================================================
536
556template <typename... Containers>
557[[nodiscard]] auto uni_zip(const Containers &...cs)
558{
559 static_assert(sizeof...(Containers) >= 2, "uni_zip requires at least 2 containers");
560 return UniZipView<Containers...>(cs...);
561}
562
572template <typename... Containers>
573[[nodiscard]] auto uni_zip_it(const Containers &...cs)
574{
576}
577
578// ============================================================================
579// Functional Operations
580// ============================================================================
581
592template <typename Pred, typename... Containers>
593[[nodiscard]] bool uni_zip_all(Pred &&pred, const Containers &...cs)
594{
595 auto &pred_ref = pred; // Preserve as lvalue to avoid invalidating in loop
596 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
597 if (!pred_ref(it.get_curr()))
598 return false;
599 return true;
600}
601
608template <typename Pred, typename... Containers>
609[[nodiscard]] bool uni_zip_all_eq(Pred &&pred, const Containers &...cs)
610{
611 auto &pred_ref = pred;
612 auto it = uni_zip_it(cs...);
613 for (; it.has_curr(); it.next())
614 if (!pred_ref(it.get_curr()))
615 return false;
616 return it.all_completed(); // All containers exhausted = equal length
617}
618
629template <typename Pred, typename... Containers>
630[[nodiscard]] bool uni_zip_exists(Pred &&pred, const Containers &...cs)
631{
632 auto &pred_ref = pred;
633 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
634 if (pred_ref(it.get_curr()))
635 return true;
636 return false;
637}
638
640template <typename Pred, typename... Containers>
641[[nodiscard]] bool uni_zip_any(Pred &&pred, const Containers &...cs)
642{
643 return uni_zip_exists(std::forward<Pred>(pred), cs...);
644}
645
652template <typename Pred, typename... Containers>
653[[nodiscard]] bool uni_zip_none(Pred &&pred, const Containers &...cs)
654{
655 return !uni_zip_exists(std::forward<Pred>(pred), cs...);
656}
657
667template <typename Op, typename... Containers>
668void uni_zip_for_each(Op &&op, const Containers &...cs)
669{
670 auto &op_ref = op;
671 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
672 op_ref(it.get_curr());
673}
674
684template <typename Op, typename... Containers>
685void uni_zip_for_each_indexed(Op &&op, const Containers &...cs)
686{
687 auto &op_ref = op;
688 size_t idx = 0;
689 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
690 op_ref(idx, it.get_curr());
691}
692
704template <typename T, typename Op, typename... Containers>
705[[nodiscard]] T uni_zip_foldl(T init, Op &&op, const Containers &...cs)
706{
707 auto &op_ref = op;
708 T acc = std::move(init);
709 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
710 acc = op_ref(std::move(acc), it.get_curr());
711 return acc;
712}
713
715template <typename T, typename Op, typename... Containers>
716[[nodiscard]] T uni_zip_reduce(T init, Op &&op, const Containers &...cs)
717{
718 return uni_zip_foldl(std::move(init), std::forward<Op>(op), cs...);
719}
720
731template <typename Op, typename... Containers>
732[[nodiscard]] auto uni_zip_map(Op &&op, const Containers &...cs)
733{
734 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
735 using ResultType = std::decay_t<decltype(op(std::declval<TupleType>()))>;
736
737 auto &op_ref = op;
738 std::vector<ResultType> result;
739 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
740 result.push_back(op_ref(it.get_curr()));
741 return result;
742}
743
754template <typename Pred, typename... Containers>
755[[nodiscard]] auto uni_zip_filter(Pred &&pred, const Containers &...cs)
756{
757 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
758
759 auto &pred_ref = pred;
760 std::vector<TupleType> result;
761 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
762 {
763 auto t = it.get_curr();
764 if (pred_ref(t))
765 result.push_back(t);
766 }
767 return result;
768}
769
780template <typename Pred, typename... Containers>
781[[nodiscard]] auto uni_zip_find_first(Pred &&pred, const Containers &...cs)
782{
783 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
784
785 auto &pred_ref = pred;
786 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
787 {
788 auto t = it.get_curr();
789 if (pred_ref(t))
790 return std::optional<TupleType>(t);
791 }
792 return std::optional<TupleType>{};
793}
794
801template <typename Pred, typename... Containers>
802[[nodiscard]] size_t uni_zip_count(Pred &&pred, const Containers &...cs)
803{
804 auto &pred_ref = pred;
805 size_t count = 0;
806 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
807 if (pred_ref(it.get_curr()))
808 ++count;
809 return count;
810}
811
818template <typename... Containers>
819[[nodiscard]] size_t uni_zip_length(const Containers &...cs)
820{
821 size_t count = 0;
822 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
823 ++count;
824 return count;
825}
826
833template <typename... Containers>
834[[nodiscard]] bool uni_zip_equal_length(const Containers &...cs)
835{
836 auto it = uni_zip_it(cs...);
837 while (it.has_curr())
838 it.next();
839 return it.all_completed(); // All exhausted = equal length
840}
841
848template <typename... Containers>
849[[nodiscard]] auto uni_zip_nth(size_t n, const Containers &...cs)
850{
851 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
852
853 size_t i = 0;
854 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next(), ++i)
855 if (i == n)
856 return std::optional<TupleType>(it.get_curr());
857 return std::optional<TupleType>{};
858}
859
866template <typename... Containers>
867[[nodiscard]] auto uni_zip_take(size_t n, const Containers &...cs)
868{
869 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
870
871 std::vector<TupleType> result;
872 result.reserve(n);
873 size_t count = 0;
874 for (auto it = uni_zip_it(cs...); it.has_curr() && count < n; it.next(), ++count)
875 result.push_back(it.get_curr());
876 return result;
877}
878
885template <typename... Containers>
886[[nodiscard]] auto uni_zip_drop(size_t n, const Containers &...cs)
887{
888 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
889
890 std::vector<TupleType> result;
891 auto it = uni_zip_it(cs...);
892
893 for (size_t i = 0; i < n && it.has_curr(); ++i)
894 it.next();
895
896 for (; it.has_curr(); it.next())
897 result.push_back(it.get_curr());
898 return result;
899}
900
907template <typename Pred, typename... Containers>
908[[nodiscard]] auto uni_zip_take_while(Pred &&pred, const Containers &...cs)
909{
910 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
911
912 auto &pred_ref = pred;
913 std::vector<TupleType> result;
914 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
915 {
916 auto t = it.get_curr();
917 if (!pred_ref(t))
918 break;
919 result.push_back(t);
920 }
921 return result;
922}
923
930template <typename Pred, typename... Containers>
931[[nodiscard]] auto uni_zip_drop_while(Pred &&pred, const Containers &...cs)
932{
933 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
934
935 auto &pred_ref = pred;
936 std::vector<TupleType> result;
937 auto it = uni_zip_it(cs...);
938
939 for (; it.has_curr() && pred_ref(it.get_curr()); it.next())
940 /* skip */;
941
942 for (; it.has_curr(); it.next())
943 result.push_back(it.get_curr());
944 return result;
945}
946
953template <typename... Containers>
954[[nodiscard]] auto uni_zip_first(const Containers &...cs)
955{
956 return uni_zip_nth(0, cs...);
957}
958
965template <typename... Containers>
966[[nodiscard]] auto uni_zip_last(const Containers &...cs)
967{
968 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
969
970 std::optional<TupleType> result;
971 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
972 result = it.get_curr();
973 return result;
974}
975
982template <typename Pred, typename... Containers>
983[[nodiscard]] auto uni_zip_partition(Pred &&pred, const Containers &...cs)
984{
985 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
986
987 auto &pred_ref = pred;
988 std::vector<TupleType> matching, non_matching;
989 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
990 {
991 auto t = it.get_curr();
992 if (pred_ref(t))
993 matching.push_back(t);
994 else
995 non_matching.push_back(t);
996 }
997 return std::make_pair(std::move(matching), std::move(non_matching));
998}
999
1006template <typename... Containers>
1007[[nodiscard]] auto uni_zip_to_vector(const Containers &...cs)
1008{
1009 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1010
1011 std::vector<TupleType> result;
1012 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
1013 result.push_back(it.get_curr());
1014 return result;
1015}
1016
1023template <typename Pred, typename... Containers>
1024[[nodiscard]] bool uni_zip_traverse(Pred &&pred, const Containers &...cs)
1025{
1026 auto &pred_ref = pred;
1027 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
1028 if (!pred_ref(it.get_curr()))
1029 return false;
1030 return true;
1031}
1032
1033// ============================================================================
1034// ML-style Additional Operations
1035// ============================================================================
1036
1047template <typename Op, typename... Containers>
1048[[nodiscard]] auto uni_zip_mapi(Op &&op, const Containers &...cs)
1049{
1050 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1051 using ResultType = std::decay_t<decltype(op(size_t{}, std::declval<TupleType>()))>;
1052
1053 auto &op_ref = op;
1054 std::vector<ResultType> result;
1055 size_t idx = 0;
1056 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
1057 result.push_back(op_ref(idx, it.get_curr()));
1058 return result;
1059}
1060
1071template <typename Pred, typename... Containers>
1072[[nodiscard]] auto uni_zip_filteri(Pred &&pred, const Containers &...cs)
1073{
1074 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1075
1076 auto &pred_ref = pred;
1077 std::vector<TupleType> result;
1078 size_t idx = 0;
1079 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
1080 {
1081 auto t = it.get_curr();
1082 if (pred_ref(idx, t))
1083 result.push_back(t);
1084 }
1085 return result;
1086}
1087
1099template <typename T, typename Op, typename... Containers>
1100[[nodiscard]] auto uni_zip_scan_left(T init, Op &&op, const Containers &...cs)
1101{
1102 auto &op_ref = op;
1103 std::vector<T> result;
1104 result.push_back(init);
1105
1106 T acc = std::move(init);
1107 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
1108 {
1109 acc = op_ref(std::move(acc), it.get_curr());
1110 result.push_back(acc);
1111 }
1112 return result;
1113}
1114
1125template <typename Op, typename... Containers>
1126[[nodiscard]] auto uni_zip_find_mapi(Op &&op, const Containers &...cs)
1127{
1128 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1129 using OptType = std::decay_t<decltype(op(size_t{}, std::declval<TupleType>()))>;
1130
1131 auto &op_ref = op;
1132 size_t idx = 0;
1133 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
1134 {
1135 auto result = op_ref(idx, it.get_curr());
1136 if (result)
1137 return result;
1138 }
1139 return OptType{};
1140}
1141
1152template <typename Eq, typename... Containers>
1153[[nodiscard]] bool uni_zip_equal_by(Eq &&eq, const Containers &...cs)
1154{
1155 auto &eq_ref = eq;
1156 auto it = uni_zip_it(cs...);
1157 for (; it.has_curr(); it.next())
1158 if (!eq_ref(it.get_curr()))
1159 return false;
1160 return it.all_completed(); // All exhausted = equal length
1161}
1162
1173template <typename Tuple, typename... Containers>
1174[[nodiscard]] bool uni_zip_mem(const Tuple &target, const Containers &...cs)
1175{
1176 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
1177 if (it.get_curr() == target)
1178 return true;
1179 return false;
1180}
1181
1192template <typename Key, typename... Containers>
1193[[nodiscard]] auto uni_zip_assoc(const Key &key, const Containers &...cs)
1194{
1195 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1196
1197 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
1198 {
1199 auto t = it.get_curr();
1200 if (std::get<0>(t) == key)
1201 return std::optional<TupleType>(t);
1202 }
1203 return std::optional<TupleType>{};
1204}
1205
1215template <typename... Containers>
1216[[nodiscard]] auto uni_zip_min(const Containers &...cs)
1217{
1218 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1219
1220 auto it = uni_zip_it(cs...);
1221 if (!it.has_curr())
1222 return std::optional<TupleType>{};
1223
1224 TupleType min_val = it.get_curr();
1225 it.next();
1226
1227 for (; it.has_curr(); it.next())
1228 {
1229 auto t = it.get_curr();
1230 if (t < min_val)
1231 min_val = t;
1232 }
1233 return std::optional<TupleType>(min_val);
1234}
1235
1245template <typename... Containers>
1246[[nodiscard]] auto uni_zip_max(const Containers &...cs)
1247{
1248 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1249
1250 auto it = uni_zip_it(cs...);
1251 if (!it.has_curr())
1252 return std::optional<TupleType>{};
1253
1254 TupleType max_val = it.get_curr();
1255 it.next();
1256
1257 for (; it.has_curr(); it.next())
1258 {
1259 auto t = it.get_curr();
1260 if (t > max_val)
1261 max_val = t;
1262 }
1263 return std::optional<TupleType>(max_val);
1264}
1265
1275template <typename... Containers>
1276[[nodiscard]] auto uni_zip_min_max(const Containers &...cs)
1277{
1278 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1279 using ResultType = std::optional<std::pair<TupleType, TupleType>>;
1280
1281 auto it = uni_zip_it(cs...);
1282 if (!it.has_curr())
1283 return ResultType{};
1284
1285 TupleType min_val = it.get_curr();
1286 TupleType max_val = min_val;
1287 it.next();
1288
1289 for (; it.has_curr(); it.next())
1290 {
1291 auto t = it.get_curr();
1292 if (t < min_val)
1293 min_val = t;
1294 if (t > max_val)
1295 max_val = t;
1296 }
1297 return ResultType(std::make_pair(min_val, max_val));
1298}
1299
1300// ============================================================================
1301// Unzip Operations (returns DynList for mixed containers)
1302// ============================================================================
1303
1304} // end namespace Aleph
1305
1306// Need DynList for unzip operations
1307#include <htlist.H>
1308
1309namespace Aleph {
1310
1330template <typename Container>
1331[[nodiscard]] auto uni_unzip(const Container &c)
1332{
1334 using T1 = std::decay_t<decltype(std::declval<PairType>().first)>;
1335 using T2 = std::decay_t<decltype(std::declval<PairType>().second)>;
1336
1337 DynList<T1> firsts;
1338 DynList<T2> seconds;
1339
1341 {
1342 for (const auto &p : c)
1343 {
1344 firsts.append(p.first);
1345 seconds.append(p.second);
1346 }
1347 }
1348 else
1349 {
1350 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
1351 {
1352 const auto &p = it.get_curr();
1353 firsts.append(p.first);
1354 seconds.append(p.second);
1355 }
1356 }
1357
1358 return std::make_pair(std::move(firsts), std::move(seconds));
1359}
1360
1361namespace uni_unzip_detail {
1362
1363template <typename Tuple, size_t... Is>
1364auto make_dynlist_tuple(std::index_sequence<Is...>)
1365{
1366 return std::make_tuple(DynList<std::tuple_element_t<Is, Tuple>>()...);
1367}
1368
1369template <typename Tuple, typename ResultTuple, size_t... Is>
1370void append_tuple_elements(const Tuple &t, ResultTuple &result, std::index_sequence<Is...>)
1371{
1372 ((std::get<Is>(result).append(std::get<Is>(t))), ...);
1373}
1374
1375} // namespace uni_unzip_detail
1376
1398template <typename Container>
1399[[nodiscard]] auto uni_unzip_tuple(const Container &c)
1400{
1402 constexpr size_t N = std::tuple_size_v<TupleType>;
1403
1404 auto result = uni_unzip_detail::make_dynlist_tuple<TupleType>(std::make_index_sequence<N>{});
1405
1407 {
1408 for (const auto &t : c)
1409 uni_unzip_detail::append_tuple_elements(t, result, std::make_index_sequence<N>{});
1410 }
1411 else
1412 {
1413 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
1414 uni_unzip_detail::append_tuple_elements(it.get_curr(), result, std::make_index_sequence<N>{});
1415 }
1416
1417 return result;
1418}
1419
1431template <typename... Containers>
1432[[nodiscard]] auto uni_zip_to_dynlist(const Containers &...cs)
1433{
1434 using TupleType = typename UniZipIterator<std::decay_t<Containers>...>::value_type;
1435
1436 DynList<TupleType> result;
1437 for (auto it = uni_zip_it(cs...); it.has_curr(); it.next())
1438 result.append(it.get_curr());
1439 return result;
1440}
1441
1442} // end namespace Aleph
1443
1444#endif // AH_ZIP_UTILS_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.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Iterator that traverses multiple containers (STL or Aleph) in lockstep.
std::tuple< uni_zip_detail::container_value_t< Containers >... > value_type
std::tuple< uni_zip_detail::iterator_wrapper_t< Containers >... > iters_
auto operator*() const
Dereference operator for STL compatibility.
void next()
Advance to the next element.
bool all_completed() const noexcept
Returns true if ALL containers are exhausted (equal length check)
bool any_has_curr() const noexcept
Returns true if ANY container still has elements (for length checking)
static constexpr size_t num_containers
auto get_curr_impl(std::index_sequence< Is... >) const
auto get_curr() const
Get the current element.
bool any_has_curr_impl(std::index_sequence< Is... >) const noexcept
bool has_curr_impl(std::index_sequence< Is... >) const noexcept
void next_ne()
Advance without checking; same as next(), which does not check.
bool has_curr() const noexcept
Returns true if ALL containers have current element (zip continues)
void next_impl(std::index_sequence< Is... >)
UniZipIterator(const Containers &...cs)
Constructor initializing wrappers for all containers.
bool operator==(UniZipSentinel) const noexcept
Comparison with sentinel - iterator is "at end" when any container exhausted.
bool completed() const noexcept
Check if iterator has reached the end.
bool operator!=(UniZipSentinel s) const noexcept
Inequality comparison with sentinel.
bool all_completed_impl(std::index_sequence< Is... >) const noexcept
UniZipIterator & operator++()
Pre-increment operator for STL compatibility.
Lazy view over multiple zipped containers (STL or Aleph).
size_t size() const
Note: O(n) - iterates through all elements to count.
UniZipView(const Containers &...cs)
Constructor from containers.
iterator begin() const
Returns an iterator to the beginning.
bool empty() const
Check if the zipped view is empty.
UniZipIterator< std::decay_t< Containers >... > iterator
typename iterator::value_type value_type
std::tuple< const std::decay_t< Containers > &... > containers_
sentinel end() const noexcept
O(1) end() using sentinel.
Wrapper that provides a uniform interface for Aleph iterators.
bool completed() const noexcept
Check if iterator has reached the end.
void next_ne()
Advance without checking; same as next(), which does not check.
AlephIteratorWrapper(const Container &c)
Constructor initializing from container.
bool has_curr() const noexcept
Check if iterator has a current element.
std::decay_t< typename std::decay_t< Container >::Item_Type > value_type
auto get_curr() const
Get the current element.
void next()
Advance to the next element.
Aleph::const_iterator_t< std::decay_t< Container > > it_
Wrapper that provides a uniform interface for STL iterators.
typename std::decay_t< Container >::value_type value_type
StlIteratorWrapper(const Container &c)
Constructor initializing from container.
bool completed() const noexcept
Check if iterator has reached the end.
std::decay_t< Container >::const_iterator curr_
void next_ne() noexcept
Advance without checking; same as next(), which does not check.
std::decay_t< Container >::const_iterator end_
void next() noexcept
Advance to the next element.
bool has_curr() const noexcept
Check if iterator has a current element.
auto get_curr() const
Get the current element.
Singly linked list implementations with head-tail access.
Freq_Node * pred
Predecessor node in level-order traversal.
void append_tuple_elements(const Tuple &t, ResultTuple &result, std::index_sequence< Is... >)
auto make_dynlist_tuple(std::index_sequence< Is... >)
typename IteratorWrapperSelector< Container >::type iterator_wrapper_t
typename container_value_type< std::decay_t< T > >::type container_value_t
constexpr bool is_supported_container_v
Main namespace for Aleph-w library functions.
auto uni_zip_find_mapi(Op &&op, const Containers &...cs)
Find and map with index (find_mapi in ML).
T uni_zip_reduce(T init, Op &&op, const Containers &...cs)
Alias for uni_zip_foldl.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
void uni_zip_for_each_indexed(Op &&op, const Containers &...cs)
Apply operation to each tuple with index.
auto uni_zip_assoc(const Key &key, const Containers &...cs)
Find value associated with key in zipped pairs (assoc in ML).
auto uni_zip_min(const Containers &...cs)
Get minimum tuple.
bool uni_zip_equal_length(const Containers &...cs)
Check if all containers have equal length.
auto uni_zip_filter(Pred &&pred, const Containers &...cs)
Filter zipped tuples by predicate.
bool uni_zip_none(Pred &&pred, const Containers &...cs)
Check if no tuple satisfies the predicate.
auto uni_zip_find_first(Pred &&pred, const Containers &...cs)
Find first tuple satisfying predicate.
auto uni_zip_min_max(const Containers &...cs)
Get both min and max in a single pass.
auto uni_zip_partition(Pred &&pred, const Containers &...cs)
Partition tuples by predicate.
auto uni_zip_drop_while(Pred &&pred, const Containers &...cs)
Skip tuples while predicate is true, return the rest.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
auto uni_zip_take(size_t n, const Containers &...cs)
Take first n tuples.
auto uni_zip_map(Op &&op, const Containers &...cs)
Map operation over zipped tuples.
auto uni_zip_last(const Containers &...cs)
Get last tuple.
auto uni_unzip_tuple(const Container &c)
Unzip a container of tuples into a tuple of DynLists.
bool uni_zip_traverse(Pred &&pred, const Containers &...cs)
Traverse while predicate returns true.
auto uni_zip_mapi(Op &&op, const Containers &...cs)
Map with index (mapi in ML).
bool uni_zip_all(Pred &&pred, const Containers &...cs)
Check if predicate holds for all zipped tuples.
size_t uni_zip_length(const Containers &...cs)
Get zip length (minimum of all container sizes).
auto uni_zip_scan_left(T init, Op &&op, const Containers &...cs)
Scan left - fold with intermediate results.
auto uni_zip_nth(size_t n, const Containers &...cs)
Get n-th tuple from zipped containers.
auto uni_unzip(const Container &c)
Unzip a container of pairs into two DynLists.
auto uni_zip_to_vector(const Containers &...cs)
Materialize zipped tuples into a vector.
bool uni_zip_mem(const Tuple &target, const Containers &...cs)
Check if a tuple exists in the zipped sequence (mem in ML).
bool uni_zip_all_eq(Pred &&pred, const Containers &...cs)
Check if all tuples satisfy predicate AND containers have equal length.
auto uni_zip(const Containers &...cs)
Create a lazy zip view over any combination of STL/Aleph containers.
auto uni_zip_to_dynlist(const Containers &...cs)
Materialize a zipped view into a DynList of tuples.
auto uni_zip_filteri(Pred &&pred, const Containers &...cs)
Filter with index (filteri in ML).
auto uni_zip_drop(size_t n, const Containers &...cs)
Skip first n tuples, return the rest.
size_t uni_zip_count(Pred &&pred, const Containers &...cs)
Count tuples satisfying predicate.
auto uni_zip_first(const Containers &...cs)
Get first tuple.
T uni_zip_foldl(T init, Op &&op, const Containers &...cs)
Left fold over zipped tuples.
bool uni_zip_any(Pred &&pred, const Containers &...cs)
Alias for uni_zip_exists.
static std::atomic< bool > init
Definition hash-fct.C:54
auto uni_zip_max(const Containers &...cs)
Get maximum tuple.
bool uni_zip_exists(Pred &&pred, const Containers &...cs)
Check if predicate holds for any zipped tuple.
auto uni_zip_it(const Containers &...cs)
Get a unified zip iterator.
bool uni_zip_equal_by(Eq &&eq, const Containers &...cs)
Check equality with custom comparator.
@ Tuple
Tuple type such as (Int, Bool).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
auto uni_zip_take_while(Pred &&pred, const Containers &...cs)
Take tuples while predicate is true.
void uni_zip_for_each(Op &&op, const Containers &...cs)
Apply operation to each zipped tuple.
STL namespace.
Sentinel type for UniZipIterator end comparison.
Selects the appropriate iterator wrapper based on the container type.
Type trait to deduce the underlying value type of a container.
Trait to detect if a type has an Aleph-style Iterator interface.
Trait to detect if a type has an STL-style iterator interface (begin/end).