Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
zip.hh
Go to the documentation of this file.
1 #ifndef VCSN_MISC_ZIP_HH
2 # define VCSN_MISC_ZIP_HH
3 
4 # include <boost/iterator/iterator_facade.hpp>
5 
6 # include <vcsn/misc/raise.hh> // pass
7 # include <vcsn/misc/tuple.hh>
8 
9 namespace vcsn
10 {
11 
12  template <typename... Sequences>
14  {
16  using sequences_t = std::tuple<Sequences...>;
17 
19  template <std::size_t... I>
21 
23  static constexpr size_t size = sizeof...(Sequences);
24 
26  using indices_t = vcsn::detail::make_index_sequence<sizeof...(Sequences)>;
27 
29  template <typename Seq>
30  using seq_t = typename std::remove_reference<Seq>::type;
31 
33  using value_type
34  = std::tuple<typename seq_t<Sequences>::value_type...>;
35 
36  zip_sequences(const sequences_t& sequences)
37  : sequences_(sequences)
38  {}
39 
40  zip_sequences(Sequences... sequences)
41  : sequences_(sequences...)
42  {}
43 
45  using const_iterators_t
46  = std::tuple<typename seq_t<Sequences>::const_iterator...>;
47 
49  using iterators_t
50  = std::tuple<typename seq_t<Sequences>::iterator...>;
51 
53  template <typename ValueType,
54  typename IteratorsType>
55  struct zip_iterator
56  : public boost::iterator_facade<
57  zip_iterator<ValueType, IteratorsType>
58  , ValueType
59  , boost::forward_traversal_tag
60  >
61  {
63  using iterators_type = IteratorsType;
64 
66  : is_{is}
67  , ends_{ends}
68  {}
69 
70  template <typename OtherValue, typename OtherIterators>
72  : is_{that.is_}
73  , ends_{that.ends_}
74  {}
75 
80 
83  {
84  if (!next_())
85  done_();
86  return *this;
87  }
88 
90  {
91  return dereference_(indices_t{});
92  }
93 
94  private:
96 
98  void done_()
99  {
100  is_ = ends_;
101  }
102 
105  bool next_()
106  {
107  return next_(indices_t{});
108  }
109 
110  template <std::size_t... I>
112  {
113  bool res = true;
114  using swallow = int[];
115  (void) swallow
116  {
117  res
118  && (++std::get<I>(is_) == std::get<I>(ends_)
119  ? res = false
120  : true)
121  ...
122  };
123  return res;
124  }
125 
126  template <typename OtherValue, typename OtherIterators>
128  {
129  return equal_(that, indices_t{});
130  }
131 
132  template <typename OtherValue, typename OtherIterators,
133  std::size_t... I>
135  seq<I...>) const
136  {
137  for (auto n: {(std::get<I>(is_) == std::get<I>(that.is_))...})
138  if (!n)
139  return false;
140  return true;
141  }
142 
144  template <std::size_t... I>
146  {
147  return value_type{(*std::get<I>(is_))...};
148  }
149  };
150 
153 
156 
158  {
159  return cbegin_(indices_t{});
160  }
161 
163  {
164  return cend_(indices_t{});
165  }
166 
168  {
169  return cbegin();
170  }
171 
173  {
174  return cend();
175  }
176 
178  {
179  return begin_(indices_t{});
180  }
181 
183  {
184  return end_(indices_t{});
185  }
186 
187  private:
188  template <std::size_t... I>
190  {
191  return {const_iterators_t{std::get<I>(sequences_).cbegin()...},
192  const_iterators_t{std::get<I>(sequences_).cend()...}};
193  }
194 
195  template <std::size_t... I>
197  {
198  return {const_iterators_t{std::get<I>(sequences_).cend()...},
199  const_iterators_t{std::get<I>(sequences_).cend()...}};
200  }
201 
202 
203  template <std::size_t... I>
205  {
206  return {iterators_t{std::get<I>(sequences_).begin()...},
207  iterators_t{std::get<I>(sequences_).end()...}};
208  }
209 
210  template <std::size_t... I>
212  {
213  return {iterators_t{std::get<I>(sequences_).end()...},
214  iterators_t{std::get<I>(sequences_).end()...}};
215  }
216 
219  };
220 
221  template <typename... Sequences>
222  zip_sequences<Sequences...>
223  zip(Sequences&&... seqs)
224  {
225  return {std::forward<Sequences>(seqs)...};
226  }
227 
228  template <typename... Sequences>
229  zip_sequences<Sequences...>
230  zip_tuple(const std::tuple<Sequences...>& seqs)
231  {
232  return {seqs};
233  }
234 }
235 
236 #endif // !VCSN_MISC_ZIP_HH
const_iterator cbegin() const
Definition: zip.hh:157
const_iterator cbegin_(seq< I...>) const
Definition: zip.hh:189
iterators_type is_
The current position.
Definition: zip.hh:77
const_iterator cend() const
Definition: zip.hh:162
zip_sequences(Sequences...sequences)
Definition: zip.hh:40
IteratorsType iterators_type
Underlying iterators.
Definition: zip.hh:63
void done_()
We have reached the end, move all the cursors to this end.
Definition: zip.hh:98
zip_iterator(const iterators_type &is, const iterators_type &ends)
Definition: zip.hh:65
iterator begin_(seq< I...>)
Definition: zip.hh:204
zip_sequences(const sequences_t &sequences)
Definition: zip.hh:36
std::tuple< typename seq_t< Sequences >::value_type...> value_type
The type of the members.
Definition: zip.hh:34
std::tuple< typename seq_t< Sequences >::iterator...> iterators_t
Tuple of iterators.
Definition: zip.hh:50
const_iterator cend_(seq< I...>) const
Definition: zip.hh:196
zip_sequences< Sequences...> zip(Sequences &&...seqs)
Definition: zip.hh:223
std::tuple< typename seq_t< Sequences >::const_iterator...> const_iterators_t
Tuple of const_iterators.
Definition: zip.hh:46
value_type dereference_(seq< I...>) const
Tuple of values.
Definition: zip.hh:145
zip_iterator & operator++()
Advance to next position.
Definition: zip.hh:82
friend class boost::iterator_core_access
Definition: zip.hh:95
std::tuple< Sequences...> sequences_t
Type of the tuple of all the maps.
Definition: zip.hh:16
iterator end()
Definition: zip.hh:182
const_iterator begin() const
Definition: zip.hh:167
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
zip_sequences< Sequences...> zip_tuple(const std::tuple< Sequences...> &seqs)
Definition: zip.hh:230
Composite iterator.
Definition: zip.hh:55
const_iterator end() const
Definition: zip.hh:172
value_type operator*() const
Definition: zip.hh:89
typename std::remove_reference< Seq >::type seq_t
The type of the underlying sequences, without reference.
Definition: zip.hh:30
zip_iterator(zip_iterator< OtherValue, OtherIterators > const &that)
Definition: zip.hh:71
sequences_t sequences_
The sequences we iterate upon.
Definition: zip.hh:218
iterators_type ends_
The ends.
Definition: zip.hh:79
bool next_(seq< I...>)
Definition: zip.hh:111
bool equal(const zip_iterator< OtherValue, OtherIterators > &that) const
Definition: zip.hh:127
bool next_()
Move to the next position.
Definition: zip.hh:105
iterator begin()
Definition: zip.hh:177
bool equal_(const zip_iterator< OtherValue, OtherIterators > &that, seq< I...>) const
Definition: zip.hh:134
iterator end_(seq< I...>)
Definition: zip.hh:211
static constexpr size_t size
Number of sequences.
Definition: zip.hh:23