Lumiera  0.pre.03
»edit your freedom«
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 
39 namespace 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<class 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 std::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
90  iterNext()
91  {
92  meta::forEach (iters_
93  ,[](auto& it){ ++it; });
94  }
95 
96 
97  /* === connector for IterAdapter internal protocol === */
98 
104 
106  void
108  {
109  meta::forEach (iters_
110  ,[](auto& it){ it.expandChildren(); });
111  }
112 
113  size_t
114  depth() const
115  {
116  size_t maxDepth{0};
117  meta::forEach (iters_
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*/
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
auto izip(ITS &&...iters)
tuple-combining iterator prefixed by index sequence
Definition: iter-zip.hpp:152
Helper template(s) for creating Lumiera Forward Iterators.
auto buildIterTuple(ITS &&...iters)
construction-helper: apply IterExplorer builder packaged tuple
Definition: iter-zip.hpp:45
void expandChildren()
delegate to the IterExplorers in the tuple
Definition: iter-zip.hpp:107
Implementation namespace for support and library code.
Metaprogramming with tuples-of-types and the std::tuple record.
ITUP & yield() const
< exposing the iterator-tuple itself as »product«
Definition: iter-zip.hpp:84
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
Building block for a tupeled-iterator.
Definition: iter-zip.hpp:55
Building tree expanding and backtracking evaluations within hierarchical scopes.
auto zip(ITS &&...iters)
Build a tuple-combining iterator builder.
Definition: iter-zip.hpp:138