Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
iter-zip.hpp
Go to the documentation of this file.
1/*
2 ITER-ZIP.hpp - join several iterators into a iterator-of-tuples
3
4 Copyright (C)
5 2024, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
28#ifndef LIB_ITER_ZIP_H
29#define LIB_ITER_ZIP_H
30
31
32#include "lib/iter-adapter.hpp"
33#include "lib/iter-explorer.hpp"
35
36#include <utility>
37
38
39namespace lib {
40 namespace iter {
41
43 template<typename...ITS>
44 auto
45 buildIterTuple (ITS&& ...iters)
46 {
47 return std::make_tuple (lib::explore (std::forward<ITS> (iters)) ...);
48 }
49
54 template<meta::tuple_like ITUP>
56 {
57 ITUP iters_;
58
59 public:
60 ProductCore(ITUP&& iterTup)
61 : iters_{move (iterTup)}
62 { }
63
64 ProductCore() = default;
65 // default copy acceptable
66
67 friend bool
68 operator== (ProductCore const& cor1, ProductCore const& cor2)
69 {
70 return cor1.iters_ == cor2.iters_;
71 }
72
73
74 /* === »state core« protocol API === */
75
76 bool
77 checkPoint() const
78 { //note: short-circuit
79 return meta::apply ([](auto&... its) { return (bool(its) and ...); }
80 , iters_);
81 }
82
83 ITUP&
84 yield() const
85 {
86 return util::unConst(iters_);
87 }
88
89 void
91 {
93 ,[](auto& it){ ++it; });
94 }
95
96
97 /* === connector for IterAdapter internal protocol === */
98
104
106 void
108 {
110 ,[](auto& it){ it.expandChildren(); });
111 }
112
113 size_t
114 depth() const
115 {
116 size_t maxDepth{0};
118 ,[&](auto& it){ maxDepth = std::max (maxDepth, it.depth()); });
119 return maxDepth;
120 }
121 };
122 } // namespace lib::iter
123
124
125
126
136 template<class...ITS>
137 inline auto
138 zip (ITS&& ...iters)
139 {
140 auto access_result = [ ](auto& it)->decltype(auto){ return *it; }; // Note: pass-through result type (maybe reference)
141 auto tuple_results = [&](auto& it){ return meta::mapEach (*it, access_result); };
142 //
143 auto core = iter::ProductCore{iter::buildIterTuple (std::forward<ITS> (iters)...)};
144 //
145 return explore (std::move(core))
146 .transform (tuple_results);
147 }
148
150 template<class...ITS>
151 inline auto
152 izip (ITS&& ...iters)
153 {
154 return zip (eachNum<size_t>(), std::forward<ITS>(iters)...);
155 }
156
157
158
159} // namespace lib
160#endif /*LIB_ITER_ZIP_H*/
Building block for a tupeled-iterator.
Definition iter-zip.hpp:56
void expandChildren()
delegate to the IterExplorers in the tuple
Definition iter-zip.hpp:107
bool checkPoint() const
Definition iter-zip.hpp:77
size_t depth() const
Definition iter-zip.hpp:114
ProductCore(ITUP &&iterTup)
Definition iter-zip.hpp:60
ITUP TAG_IterExplorer_BaseAdapter
instruct a follow-up IterAdapter not to add a BaseAdapter but rather to connect to the dispatcher fun...
Definition iter-zip.hpp:103
ITUP & yield() const
< exposing the iterator-tuple itself as »product«
Definition iter-zip.hpp:84
friend bool operator==(ProductCore const &cor1, ProductCore const &cor2)
Definition iter-zip.hpp:68
Helper template(s) for creating Lumiera Forward Iterators.
Building tree expanding and backtracking evaluations within hierarchical scopes.
auto buildIterTuple(ITS &&...iters)
construction-helper: apply IterExplorer builder packaged tuple
Definition iter-zip.hpp:45
constexpr void forEach(TUP &&tuple, FUN fun)
Tuple iteration: perform some arbitrary operation on each element of a tuple.
constexpr decltype(auto) apply(FUN &&f, TUP &&tup) noexcept(can_nothrow_invoke_tup< FUN, TUP >)
Replacement for std::apply — yet applicable to tuple-like custom types.
constexpr auto mapEach(TUP &&tuple, FUN fun)
Apply some arbitrary function onto all elements of a tuple.
Implementation namespace for support and library code.
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
auto zip(ITS &&...iters)
Build a tuple-combining iterator builder.
Definition iter-zip.hpp:138
auto izip(ITS &&...iters)
tuple-combining iterator prefixed by index sequence
Definition iter-zip.hpp:152
OBJ * unConst(const OBJ *)
shortcut to save some typing when having to define const and non-const variants of member functions
Definition util.hpp:358
Metaprogramming with tuples-of-types and the std::tuple record.