89namespace uni_zip_detail {
115template <
typename T,
typename =
void>
121 using type =
typename T::value_type;
127 using type = std::decay_t<typename T::Item_Type>;
149template <
typename Container>
152 typename std::decay_t<Container>::const_iterator
curr_;
153 typename std::decay_t<Container>::const_iterator
end_;
156 using value_type =
typename std::decay_t<Container>::value_type;
211template <
typename Container>
219 using value_type = std::decay_t<typename std::decay_t<Container>::Item_Type>;
227 return it_.has_curr();
233 return !
it_.has_curr();
240 return it_.get_curr();
274template <
typename Container,
typename =
void>
278template <
typename Container>
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>>
293template <
typename Container>
338template <
typename... Containers>
341 static_assert((uni_zip_detail::is_supported_container_v<Containers> && ...),
342 "All containers must have either STL (begin/end) or Aleph (Iterator) interface");
345 using value_type = std::tuple<uni_zip_detail::container_value_t<Containers>...>;
348 std::tuple<uni_zip_detail::iterator_wrapper_t<Containers>...>
iters_;
350 template <
size_t... Is>
356 template <
size_t... Is>
362 template <
size_t... Is>
368 template <
size_t... Is>
374 template <
size_t... Is>
377 (std::get<Is>(
iters_).next(), ...);
385 :
iters_(uni_zip_detail::iterator_wrapper_t<Containers>(cs)...)
392 return has_curr_impl(std::make_index_sequence<num_containers>{});
418 return get_curr_impl(std::make_index_sequence<num_containers>{});
425 next_impl(std::make_index_sequence<num_containers>{});
461 return !(*
this == s);
468 return !
has_curr() && !other.has_curr();
474 return !(*
this == other);
486template <
typename... Containers>
502 return std::apply([](
const auto &...cs)
524 [[nodiscard]]
size_t size()
const
527 for (
auto it =
begin(); it !=
end(); ++it)
556template <
typename... Containers>
557[[nodiscard]]
auto uni_zip(
const Containers &...cs)
559 static_assert(
sizeof...(Containers) >= 2,
"uni_zip requires at least 2 containers");
572template <
typename... Containers>
592template <
typename Pred,
typename... Containers>
595 auto &pred_ref =
pred;
596 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
597 if (!pred_ref(it.get_curr()))
608template <
typename Pred,
typename... Containers>
611 auto &pred_ref =
pred;
613 for (; it.has_curr(); it.next())
614 if (!pred_ref(it.get_curr()))
616 return it.all_completed();
629template <
typename Pred,
typename... Containers>
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()))
640template <
typename Pred,
typename... Containers>
652template <
typename Pred,
typename... Containers>
667template <
typename Op,
typename... Containers>
671 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
672 op_ref(it.get_curr());
684template <
typename Op,
typename... Containers>
689 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
690 op_ref(idx, it.get_curr());
704template <
typename T,
typename Op,
typename... Containers>
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());
715template <
typename T,
typename Op,
typename... Containers>
731template <
typename Op,
typename... Containers>
735 using ResultType = std::decay_t<decltype(op(std::declval<TupleType>()))>;
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()));
754template <
typename Pred,
typename... Containers>
759 auto &pred_ref =
pred;
760 std::vector<TupleType> result;
761 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
763 auto t = it.get_curr();
780template <
typename Pred,
typename... Containers>
785 auto &pred_ref =
pred;
786 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
788 auto t = it.get_curr();
790 return std::optional<TupleType>(t);
792 return std::optional<TupleType>{};
801template <
typename Pred,
typename... Containers>
804 auto &pred_ref =
pred;
806 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
807 if (pred_ref(it.get_curr()))
818template <
typename... Containers>
822 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
833template <
typename... Containers>
837 while (it.has_curr())
839 return it.all_completed();
848template <
typename... Containers>
854 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next(), ++i)
856 return std::optional<TupleType>(it.get_curr());
857 return std::optional<TupleType>{};
866template <
typename... Containers>
871 std::vector<TupleType> result;
875 result.push_back(it.get_curr());
885template <
typename... Containers>
890 std::vector<TupleType> result;
893 for (
size_t i = 0; i < n && it.has_curr(); ++i)
896 for (; it.has_curr(); it.next())
897 result.push_back(it.get_curr());
907template <
typename Pred,
typename... Containers>
912 auto &pred_ref =
pred;
913 std::vector<TupleType> result;
914 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
916 auto t = it.get_curr();
930template <
typename Pred,
typename... Containers>
935 auto &pred_ref =
pred;
936 std::vector<TupleType> result;
939 for (; it.has_curr() && pred_ref(it.get_curr()); it.next())
942 for (; it.has_curr(); it.next())
943 result.push_back(it.get_curr());
953template <
typename... Containers>
965template <
typename... Containers>
970 std::optional<TupleType> result;
971 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
982template <
typename Pred,
typename... Containers>
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())
991 auto t = it.get_curr();
993 matching.push_back(t);
995 non_matching.push_back(t);
997 return std::make_pair(std::move(matching), std::move(non_matching));
1006template <
typename... Containers>
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());
1023template <
typename Pred,
typename... Containers>
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()))
1047template <
typename Op,
typename... Containers>
1051 using ResultType = std::decay_t<
decltype(op(
size_t{}, std::declval<TupleType>()))>;
1054 std::vector<ResultType> result;
1056 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
1057 result.push_back(op_ref(idx, it.get_curr()));
1071template <
typename Pred,
typename... Containers>
1076 auto &pred_ref =
pred;
1077 std::vector<TupleType> result;
1079 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
1081 auto t = it.get_curr();
1082 if (pred_ref(idx, t))
1083 result.push_back(t);
1099template <
typename T,
typename Op,
typename... Containers>
1103 std::vector<T> result;
1104 result.push_back(
init);
1106 T acc = std::move(
init);
1107 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
1109 acc = op_ref(std::move(acc), it.get_curr());
1110 result.push_back(acc);
1125template <
typename Op,
typename... Containers>
1129 using OptType = std::decay_t<
decltype(op(
size_t{}, std::declval<TupleType>()))>;
1133 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next(), ++idx)
1135 auto result = op_ref(idx, it.get_curr());
1152template <
typename Eq,
typename... Containers>
1157 for (; it.has_curr(); it.next())
1158 if (!eq_ref(it.get_curr()))
1160 return it.all_completed();
1173template <
typename Tuple,
typename... Containers>
1176 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
1177 if (it.get_curr() == target)
1192template <
typename Key,
typename... Containers>
1197 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
1200 if (std::get<0>(t) == key)
1201 return std::optional<TupleType>(t);
1203 return std::optional<TupleType>{};
1215template <
typename... Containers>
1222 return std::optional<TupleType>{};
1224 TupleType min_val = it.get_curr();
1227 for (; it.has_curr(); it.next())
1229 auto t = it.get_curr();
1233 return std::optional<TupleType>(min_val);
1245template <
typename... Containers>
1252 return std::optional<TupleType>{};
1254 TupleType max_val = it.get_curr();
1257 for (; it.has_curr(); it.next())
1259 auto t = it.get_curr();
1263 return std::optional<TupleType>(max_val);
1275template <
typename... Containers>
1279 using ResultType = std::optional<std::pair<TupleType, TupleType>>;
1283 return ResultType{};
1285 TupleType min_val = it.get_curr();
1286 TupleType max_val = min_val;
1289 for (; it.has_curr(); it.next())
1291 auto t = it.get_curr();
1297 return ResultType(std::make_pair(min_val, max_val));
1330template <
typename Container>
1334 using T1 = std::decay_t<decltype(std::declval<PairType>().first)>;
1335 using T2 = std::decay_t<decltype(std::declval<PairType>().second)>;
1342 for (
const auto &p : c)
1345 seconds.
append(p.second);
1350 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
1352 const auto &p = it.get_curr();
1354 seconds.
append(p.second);
1358 return std::make_pair(std::move(firsts), std::move(seconds));
1361namespace uni_unzip_detail {
1363template <
typename Tuple,
size_t... Is>
1366 return std::make_tuple(
DynList<std::tuple_element_t<Is, Tuple>>()...);
1369template <
typename Tuple,
typename ResultTuple,
size_t... Is>
1372 ((std::get<Is>(result).append(std::get<Is>(t))), ...);
1398template <
typename Container>
1402 constexpr size_t N = std::tuple_size_v<TupleType>;
1404 auto result = uni_unzip_detail::make_dynlist_tuple<TupleType>(std::make_index_sequence<N>{});
1408 for (
const auto &t : c)
1413 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
1431template <
typename... Containers>
1437 for (
auto it =
uni_zip_it(cs...); it.has_curr(); it.next())
1438 result.
append(it.get_curr());
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.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Number Theoretic Transform over Z / MOD Z.
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
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
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.
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.
Sentinel type for UniZipIterator end comparison.
Selects the appropriate iterator wrapper based on the container type.
typename T::value_type type
std::decay_t< typename T::Item_Type > 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).