Vcsn  2.3
Be Rational
zip.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/iterator/iterator_facade.hpp>
4 
5 #include <vcsn/misc/raise.hh> // pass
6 #include <vcsn/misc/tuple.hh>
7 #include <vcsn/misc/type_traits.hh> // std::enable_if_t
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>
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 
65  zip_iterator(const iterators_type& is, const iterators_type& ends)
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 
89  void increment()
90  {
91  ++(*this);
92  }
93 
94  void advance(std::size_t n)
95  {
96  for (size_t i = 0; i < n; ++i)
97  increment();
98  }
99 
101  {
102  return dereference_(indices_t{});
103  }
104 
105  const iterators_type& iterators() const
106  {
107  return is_;
108  }
109 
110  const iterators_type& end() const
111  {
112  return ends_;
113  }
114 
115  protected:
117 
119  void done_()
120  {
121  is_ = ends_;
122  }
123 
126  bool next_()
127  {
128  return next_(indices_t{});
129  }
130 
131  template <std::size_t... I>
133  {
134  bool res = true;
135  using swallow = int[];
136  (void) swallow
137  {
138  res
139  && (++std::get<I>(is_) == std::get<I>(ends_)
140  ? res = false
141  : true)
142  ...
143  };
144  return res;
145  }
146 
147  template <typename OtherValue, typename OtherIterators>
149  {
150  return equal_(that, indices_t{});
151  }
152 
153  template <typename OtherValue, typename OtherIterators,
154  std::size_t... I>
156  seq<I...>) const
157  {
158  for (auto n: {(std::get<I>(is_) == std::get<I>(that.is_))...})
159  if (!n)
160  return false;
161  return true;
162  }
163 
165  template <std::size_t... I>
167  {
168  return value_type{(*std::get<I>(is_))...};
169  }
170  };
171 
174 
177 
179  {
180  return cbegin_(indices_t{});
181  }
182 
184  {
185  return cend_(indices_t{});
186  }
187 
189  {
190  return cbegin();
191  }
192 
194  {
195  return cend();
196  }
197 
199  {
200  return begin_(indices_t{});
201  }
202 
204  {
205  return end_(indices_t{});
206  }
207 
208  private:
209  template <std::size_t... I>
211  {
212  return {const_iterators_t{std::get<I>(sequences_).cbegin()...},
213  const_iterators_t{std::get<I>(sequences_).cend()...}};
214  }
215 
216  template <std::size_t... I>
218  {
219  return {const_iterators_t{std::get<I>(sequences_).cend()...},
220  const_iterators_t{std::get<I>(sequences_).cend()...}};
221  }
222 
223 
224  template <std::size_t... I>
226  {
227  return {iterators_t{std::get<I>(sequences_).begin()...},
228  iterators_t{std::get<I>(sequences_).end()...}};
229  }
230 
231  template <std::size_t... I>
233  {
234  return {iterators_t{std::get<I>(sequences_).end()...},
235  iterators_t{std::get<I>(sequences_).end()...}};
236  }
237 
240  };
241 
242 
243 
244  /*------------------------.
245  | zip_sequences_padded. |
246  `------------------------*/
247 
248  template <typename ZipSequences>
250  {
252  using zip_sequences_t = ZipSequences;
253 
255  using sequences_t = typename zip_sequences_t::sequences_t;
256 
258  template <std::size_t... I>
260 
262  using indices_t = typename zip_sequences_t::indices_t;
263 
265  using value_type = typename zip_sequences_t::value_type;
266 
268  using const_iterators_t = typename zip_sequences_t::const_iterators_t;
269 
271  using iterators_t = typename zip_sequences_t::iterators_t;
272 
274 
275  template <typename... S>
277  const std::tuple<S...>& sequences)
278  : sequences_{sequences}, paddings_{pad}
279  {}
280 
281  template <typename... S>
282  zip_sequences_padded(const value_type& pad, S... sequences)
283  : sequences_(sequences...), paddings_{pad}
284  {}
285 
286  template <typename ValueType, typename IteratorsType>
288  : public zip_sequences_t::template zip_iterator<ValueType, IteratorsType>
289  {
290 
291  using super_t =
292  typename zip_sequences_t::template zip_iterator<ValueType,
293  IteratorsType>;
294 
296  using iterators_type = IteratorsType;
297 
299  const padding_t& pad)
300  : super_t(is, ends)
301  , pad_(pad)
302  {}
303 
304  template <typename OtherValue, typename OtherIterators>
306  const padding_t& pad)
307  : super_t(that.is_, that.ends_)
308  , pad_(pad)
309  {}
311  {
312  return dereference_(indices_t{});
313  }
314 
317  {
318  if (!next_())
319  this->done_();
320  return *this;
321  }
322 
323 
324  private:
325 
328  bool next_()
329  {
330  return next_(indices_t{});
331  }
332 
333  template <std::size_t... I>
335  {
336  bool res = false;
337  using swallow = int[];
338  (void) swallow
339  {
340  (std::get<I>(this->is_) == std::get<I>(this->ends_)
341  ? false
342  : ++std::get<I>(this->is_) == std::get<I>(this->ends_)
343  ? false
344  : res = true)
345  ...
346  };
347  return res;
348  }
349 
351  template <std::size_t... I>
353  {
354  return value_type{(std::get<I>(this->is_) == std::get<I>(this->ends_)
355  ? std::get<I>(pad_)
356  : *std::get<I>(this->is_))...};
357  }
358 
360  };
361 
364 
367 
369  {
370  return cbegin_(indices_t{});
371  }
372 
374  {
375  return cend_(indices_t{});
376  }
377 
379  {
380  return cbegin();
381  }
382 
384  {
385  return cend();
386  }
387 
389  {
390  return begin_(indices_t{});
391  }
392 
394  {
395  return end_(indices_t{});
396  }
397 
398  private:
399  template <std::size_t... I>
401  {
402  return {const_iterators_t{std::get<I>(sequences_).cbegin()...},
403  const_iterators_t{std::get<I>(sequences_).cend()...},
404  paddings_};
405  }
406 
407  template <std::size_t... I>
409  {
410  return {const_iterators_t{std::get<I>(sequences_).cend()...},
411  const_iterators_t{std::get<I>(sequences_).cend()...},
412  paddings_};
413  }
414 
415 
416  template <std::size_t... I>
418  {
419  return {iterators_t{std::get<I>(sequences_).begin()...},
420  iterators_t{std::get<I>(sequences_).end()...},
421  paddings_};
422  }
423 
424  template <std::size_t... I>
426  {
427  return {iterators_t{std::get<I>(sequences_).end()...},
428  iterators_t{std::get<I>(sequences_).end()...},
429  paddings_};
430  }
431 
432  private:
435  };
436 
437  template <typename... Sequences>
438  zip_sequences<Sequences...>
439  zip(Sequences&&... seqs)
440  {
441  return {std::forward<Sequences>(seqs)...};
442  }
443 
444  template <typename... Sequences>
445  zip_sequences<Sequences...>
446  zip_tuple(const std::tuple<Sequences...>& seqs)
447  {
448  return {seqs};
449  }
450 
451  template <typename... Sequences>
452  zip_sequences_padded<zip_sequences<Sequences...>>
453  zip_with_padding(const std::tuple<typename Sequences::value_type...>& pad,
454  const Sequences&... seq)
455  {
456  return {pad, std::make_tuple(seq...)};
457  }
458 }
typename zip_sequences_t::sequences_t sequences_t
Type of the tuples of all the maps.
Definition: zip.hh:255
iterator begin()
Definition: zip.hh:198
typename zip_sequences_t::const_iterators_t const_iterators_t
Tuple of const_iterators.
Definition: zip.hh:268
zip_sequences_padded(const value_type &pad, S...sequences)
Definition: zip.hh:282
IteratorsType iterators_type
Underlying iterators.
Definition: zip.hh:63
std::tuple< Sequences... > sequences_t
Type of the tuple of all the maps.
Definition: zip.hh:16
zip_iterator(zip_iterator< OtherValue, OtherIterators > const &that, const padding_t &pad)
Definition: zip.hh:305
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:231
bool next_()
Move to the next position.
Definition: zip.hh:328
const_iterator end() const
Definition: zip.hh:383
typename zip_sequences_t::indices_t indices_t
Index sequence for our sequences.
Definition: zip.hh:262
value_type dereference_(seq< I... >) const
Tuple of values.
Definition: zip.hh:166
typename zip_sequences_t::template zip_iterator< ValueType, IteratorsType > super_t
Definition: zip.hh:293
const_iterator cbegin() const
Definition: zip.hh:178
zip_iterator & operator++()
Advance to next position.
Definition: zip.hh:316
iterator end_(seq< I... >)
Definition: zip.hh:425
zip_sequences_padded(const value_type &pad, const std::tuple< S... > &sequences)
Definition: zip.hh:276
const_iterator cend_(seq< I... >) const
Definition: zip.hh:217
const_iterator cbegin() const
Definition: zip.hh:368
const_iterator begin() const
Definition: zip.hh:188
zip_sequences(const sequences_t &sequences)
Definition: zip.hh:36
bool equal_(const zip_iterator< OtherValue, OtherIterators > &that, seq< I... >) const
Definition: zip.hh:155
Composite iterator.
Definition: zip.hh:55
bool equal(const zip_iterator< OtherValue, OtherIterators > &that) const
Definition: zip.hh:148
const_iterator cbegin_(seq< I... >) const
Definition: zip.hh:400
typename zip_sequences_t::value_type value_type
The type of the members.
Definition: zip.hh:265
value_type operator*() const
Definition: zip.hh:310
iterator begin_(seq< I... >)
Definition: zip.hh:225
zip_sequences< Sequences... > zip(Sequences &&...seqs)
Definition: zip.hh:439
vcsn::detail::index_sequence< I... > seq
Type of index sequences.
Definition: zip.hh:20
const_iterator end() const
Definition: zip.hh:193
return res
Definition: multiply.hh:398
auto tuple(const Auts &...as)
Build the (accessible part of the) tuple.
const_iterator cend() const
Definition: zip.hh:183
typename std::remove_reference< Seq >::type seq_t
The type of the underlying sequences, without reference.
Definition: zip.hh:30
iterator end_(seq< I... >)
Definition: zip.hh:232
const_iterator begin() const
Definition: zip.hh:378
std::tuple< typename seq_t< Sequences >::const_iterator... > const_iterators_t
Tuple of const_iterators.
Definition: zip.hh:46
value_type padding_t
Definition: zip.hh:273
sequences_t sequences_
The sequences we iterate upon.
Definition: zip.hh:239
Definition: a-star.hh:8
std::tuple< typename seq_t< Sequences >::iterator... > iterators_t
Tuple of iterators.
Definition: zip.hh:50
static constexpr size_t size
Number of sequences.
Definition: zip.hh:23
void advance(std::size_t n)
Definition: zip.hh:94
typename zip_sequences_t::iterators_t iterators_t
Tuple of iterators.
Definition: zip.hh:271
const_iterator cbegin_(seq< I... >) const
Definition: zip.hh:210
const iterators_type & iterators() const
Definition: zip.hh:105
iterator end()
Definition: zip.hh:203
iterator begin_(seq< I... >)
Definition: zip.hh:417
bool next_(seq< I... >)
Definition: zip.hh:132
zip_sequences(Sequences...sequences)
Definition: zip.hh:40
zip_iterator(const iterators_type &is, const iterators_type &ends, const padding_t &pad)
Definition: zip.hh:298
value_type operator*() const
Definition: zip.hh:100
zip_iterator(zip_iterator< OtherValue, OtherIterators > const &that)
Definition: zip.hh:71
const_iterator cend_(seq< I... >) const
Definition: zip.hh:408
void done_()
We have reached the end, move all the cursors to this end.
Definition: zip.hh:119
zip_iterator(const iterators_type &is, const iterators_type &ends)
Definition: zip.hh:65
bool next_()
Move to the next position.
Definition: zip.hh:126
std::tuple< typename seq_t< Sequences >::value_type... > value_type
The type of the members.
Definition: zip.hh:34
IteratorsType iterators_type
Underlying iterators.
Definition: zip.hh:296
const_iterator cend() const
Definition: zip.hh:373
iterators_type is_
The current position.
Definition: zip.hh:77
friend class boost::iterator_core_access
Definition: zip.hh:116
value_type dereference_(seq< I... >) const
Tuple of values.
Definition: zip.hh:352
zip_sequences_padded< zip_sequences< Sequences... > > zip_with_padding(const std::tuple< typename Sequences::value_type... > &pad, const Sequences &...seq)
Definition: zip.hh:453
ZipSequences zip_sequences_t
Type of the wrapped zip sequence.
Definition: zip.hh:252
zip_iterator & operator++()
Advance to next position.
Definition: zip.hh:82
iterators_type ends_
The ends.
Definition: zip.hh:79
const iterators_type & end() const
Definition: zip.hh:110
sequences_t sequences_
Definition: zip.hh:433
zip_sequences< Sequences... > zip_tuple(const std::tuple< Sequences... > &seqs)
Definition: zip.hh:446