Awali
Another Weighted Automata library
zip.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_ZIP_HH
18 # define AWALI_MISC_ZIP_HH
19 
20 #include <awali/sttc/misc/raise.hh> // pass
21 #include <awali/common/tuple.hh>
22 
23 namespace awali {
24  namespace sttc {
25  namespace internal {
26 
27  template <typename... Sequences>
29  {
31  using sequences_t = std::tuple<Sequences...>;
32 
34  template <std::size_t... I>
36 
38  static constexpr size_t size = sizeof...(Sequences);
39 
41  using indices_t = awali::internal::make_index_sequence<sizeof...(Sequences)>;
42 
44  template <typename Seq>
45  using seq_t = typename std::remove_reference<Seq>::type;
46 
48  using value_type
49  = std::tuple<typename seq_t<Sequences>::value_type...>;
50 
51  zip_sequences(const sequences_t& sequences)
52  : sequences_(sequences)
53  {}
54 
55  zip_sequences(Sequences... sequences)
56  : sequences_(sequences...)
57  {}
58 
61  = std::tuple<typename seq_t<Sequences>::const_iterator...>;
62 
65  = std::tuple<typename seq_t<Sequences>::iterator...>;
66 
68  template <typename ValueType,
69  typename IteratorsType>
70  struct zip_iterator
71  {
73  using iterators_type = IteratorsType;
74 
75  zip_iterator(const iterators_type& is, const iterators_type& ends)
76  : is_{is}
77  , ends_{ends}
78  {}
79 
80  template <typename OtherValue, typename OtherIterators>
82  : is_{that.is_}
83  , ends_{that.ends_}
84  {}
85 
90 
93  {
94  if (!next_())
95  done_();
96  return *this;
97  }
98 
100  {
101  return dereference_(indices_t{});
102  }
103 
104  private:
106  void done_()
107  {
108  is_ = ends_;
109  }
110 
113  bool next_()
114  {
115  return next_(indices_t{});
116  }
117 
118  template <std::size_t... I>
119  bool next_(seq<I...>)
120  {
121  bool res = true;
122  using swallow = int[];
123  (void) swallow
124  {
125  res
126  && (++std::get<I>(is_) == std::get<I>(ends_)
127  ? res = false
128  : true)
129  ...
130  };
131  return res;
132  }
133 
134  template <typename OtherValue, typename OtherIterators>
135  bool equal(const zip_iterator<OtherValue, OtherIterators>& that) const
136  {
137  return equal_(that, indices_t{});
138  }
139 
140  template <typename OtherValue, typename OtherIterators,
141  std::size_t... I>
142  bool equal_(const zip_iterator<OtherValue, OtherIterators>& that,
143  seq<I...>) const
144  {
145  for (auto n: {(std::get<I>(is_) == std::get<I>(that.is_))...})
146  if (!n)
147  return false;
148  return true;
149  }
150 
152  template <std::size_t... I>
153  value_type dereference_(seq<I...>) const
154  {
155  return value_type{(*std::get<I>(is_))...};
156  }
157  };
158 
161 
164 
166  {
167  return cbegin_(indices_t{});
168  }
169 
171  {
172  return cend_(indices_t{});
173  }
174 
176  {
177  return cbegin();
178  }
179 
181  {
182  return cend();
183  }
184 
186  {
187  return begin_(indices_t{});
188  }
189 
191  {
192  return end_(indices_t{});
193  }
194 
195  private:
196  template <std::size_t... I>
197  const_iterator cbegin_(seq<I...>) const
198  {
199  return {const_iterators_t{std::get<I>(sequences_).cbegin()...},
200  const_iterators_t{std::get<I>(sequences_).cend()...}};
201  }
202 
203  template <std::size_t... I>
204  const_iterator cend_(seq<I...>) const
205  {
206  return {const_iterators_t{std::get<I>(sequences_).cend()...},
207  const_iterators_t{std::get<I>(sequences_).cend()...}};
208  }
209 
210 
211  template <std::size_t... I>
212  iterator begin_(seq<I...>)
213  {
214  return {iterators_t{std::get<I>(sequences_).begin()...},
215  iterators_t{std::get<I>(sequences_).end()...}};
216  }
217 
218  template <std::size_t... I>
219  iterator end_(seq<I...>)
220  {
221  return {iterators_t{std::get<I>(sequences_).end()...},
222  iterators_t{std::get<I>(sequences_).end()...}};
223  }
224 
226  sequences_t sequences_;
227  };
228 
229  template <typename... Sequences>
230  zip_sequences<Sequences...>
231  zip(Sequences&&... seqs)
232  {
233  return {std::forward<Sequences>(seqs)...};
234  }
235 
236  template <typename... Sequences>
237  zip_sequences<Sequences...>
238  zip_tuple(const std::tuple<Sequences...>& seqs)
239  {
240  return {seqs};
241  }
242  }
243  }
244 }//end of ns awali::stc
245 
246 #endif // !AWALI_MISC_ZIP_HH
Definition: tuple.hh:43
zip_sequences< Sequences... > zip(Sequences &&... seqs)
Definition: zip.hh:231
zip_sequences< Sequences... > zip_tuple(const std::tuple< Sequences... > &seqs)
Definition: zip.hh:238
Main namespace of Awali.
Definition: ato.hh:22
Composite iterator.
Definition: zip.hh:71
iterators_type is_
The current position.
Definition: zip.hh:87
zip_iterator & operator++()
Advance to next position.
Definition: zip.hh:92
zip_iterator(zip_iterator< OtherValue, OtherIterators > const &that)
Definition: zip.hh:81
zip_iterator(const iterators_type &is, const iterators_type &ends)
Definition: zip.hh:75
value_type operator*() const
Definition: zip.hh:99
iterators_type ends_
The ends.
Definition: zip.hh:89
IteratorsType iterators_type
Underlying iterators.
Definition: zip.hh:73
std::tuple< typename seq_t< Sequences >::const_iterator... > const_iterators_t
Tuple of const_iterators.
Definition: zip.hh:61
zip_sequences(const sequences_t &sequences)
Definition: zip.hh:51
const_iterator begin() const
Definition: zip.hh:175
const_iterator cend() const
Definition: zip.hh:170
iterator begin()
Definition: zip.hh:185
std::tuple< typename seq_t< Sequences >::value_type... > value_type
The type of the members.
Definition: zip.hh:49
const_iterator end() const
Definition: zip.hh:180
zip_sequences(Sequences... sequences)
Definition: zip.hh:55
iterator end()
Definition: zip.hh:190
typename std::remove_reference< Seq >::type seq_t
The type of the underlying sequences, without reference.
Definition: zip.hh:45
awali::internal::make_index_sequence< sizeof...(Sequences)> indices_t
Index sequence for our sequences.
Definition: zip.hh:41
const_iterator cbegin() const
Definition: zip.hh:165
std::tuple< Sequences... > sequences_t
Type of the tuple of all the maps.
Definition: zip.hh:31
std::tuple< typename seq_t< Sequences >::iterator... > iterators_t
Tuple of iterators.
Definition: zip.hh:65
zip_iterator< const value_type, const_iterators_t > const_iterator
Const iterator.
Definition: zip.hh:163
zip_iterator< value_type, iterators_t > iterator
Mutable iterator.
Definition: zip.hh:160
static constexpr size_t size
Number of sequences.
Definition: zip.hh:38