-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.hpp
295 lines (243 loc) · 9.25 KB
/
zip.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef _UTIL_ZIP_HPP_
#define _UTIL_ZIP_HPP_
/* A zip implementation for use in ranged for loops introduced in
* C++11.
*
* usage:
* std::array<int, 10> int_array; // Assume the containers have been
* std::vector<std::string> string_array; // initialized and filled
* for(auto it : zip(int_array, string_array)) {
* int x;
* std::string s;
* std::tie(x, s) = *it;
*
* int& x_bis = std::get<O>(*it);
* // ... anything with x and s
* }
*/
#include <tuple>
#include <type_traits>
namespace util {
/* This structure creates a parameter pack type for a late use.
* The type is accessible through the #type attribute.
*/
// Dummy structure to define the template squeletton
template<template<class...> class pack_template, class A, class actual_pack>
struct tuple_concat_left {
};
// The actual specialization that is used
template<template<class...> class pack_template, class A, class... pack_content>
struct tuple_concat_left<pack_template, A, pack_template<pack_content...> > {
using type = pack_template<A, pack_content...>;
};
/**********************************************************/
/* This structure creates a std::tuple of iterator types from a variadic
* template parameter pack.
*/
// Recursive definition : while you have a non-empty list, prepend the new
// iterator type to the iterator list.
template<template<class> class type_extractor, class A, class... B>
struct type_extractor_tuple {
using type = typename tuple_concat_left<
std::tuple,
typename type_extractor<A>::type,
typename type_extractor_tuple<type_extractor, B...>::type
>::type;
};
// Recursion end
template<template<class> class type_extractor, class A>
struct type_extractor_tuple<type_extractor, A> {
using type = std::tuple<typename type_extractor<A>::type>;
};
/**********************************************************/
/// Get the iterator from a class
template<class A>
struct iterator_extractor {
using type = typename std::conditional<
std::is_const<
typename std::remove_reference<A>::type
>::value,
typename std::remove_reference<A>::type::const_iterator,
typename std::remove_reference<A>::type::iterator
>::type;
};
template<class... A>
using iterator_type_tuple = type_extractor_tuple<iterator_extractor, A...>;
/**********************************************************/
/// Get the reference from a class
template<class A>
struct reference_extractor {
using type = typename std::conditional<
std::is_const<typename std::remove_reference<A>::type>::value,
typename std::remove_reference<A>::type::const_reference,
typename std::remove_reference<A>::type::reference
>::type;
};
template<class... A>
using reference_type_tuple = type_extractor_tuple<reference_extractor, A...>;
/**********************************************************/
/** Tuple zipped lists iterator
*
* This iterator is meant to be used in C++(11) for-ranged loops. It combines
* several iterators in one.
*
* \tparam Args... A list of iterable classes. These classes must define the following types:
* - iterator
* - reference
* The classe iterators must also define the following members:
* - default constuctor
* - operator=(const it& other)
* - operator++() //prefix
* - operator*() //dereference
* - operator!=(const it& other)
*/
template<class... Args>
class zip_iterator: public iterator_type_tuple<Args...>::type {
public:
using reference_tuple = typename reference_type_tuple<Args...>::type;
constexpr static std::size_t len = sizeof...(Args);
/// Prefix increment operator, moves the iterator to the next objects
zip_iterator& operator++() {
return (*this += 1);
}
zip_iterator& operator--() {
operator--<0,Args...>();
return *this;
}
zip_iterator& operator+=(const std::size_t& n) {
operator+=<0, Args...>(n);
return *this;
}
zip_iterator& operator-=(const std::size_t& n) {
for(std::size_t i = 0; i < n; ++i) {
--(*this);
}
return *this;
}
friend zip_iterator operator-(zip_iterator it, const std::size_t& n) {
return (it -= n);
}
friend zip_iterator operator+(zip_iterator it, const std::size_t& n) {
return (it += n);
}
/// Dereference operator, gets the pointed objects
reference_tuple operator*() {
return this->operator*<0, Args...>();
}
bool operator==(const zip_iterator<Args...> & other) const {
return this->operator==<0, Args...>(other);
}
/// Difference test operator
bool operator!=(const zip_iterator<Args...> & other) const {
return ! this->operator==<0,Args...>(other);
}
private:
template<int Idx, class A, class... B>
void operator+=(const std::size_t& n) {
operator+=<Idx+1,B...>(n);
std::get<Idx>(*this) = std::get<Idx>(*this) + n;
}
template<int>
void operator+=(const std::size_t&) {}
template<int Idx, class A, class... B>
void operator--() {
operator--<Idx+1,B...>();
--std::get<Idx>(*this);
}
template<int>
void operator--() {}
template<int, class A, class... B>
typename reference_type_tuple<A, B...>::type operator*() {
std::tuple<typename reference_extractor<A>::type> temp(*(std::get<len - sizeof...(B)-1>(*this)));
return std::tuple_cat(temp, this->operator*<1, B...>());
}
template<int>
std::tuple<> operator*() const {
return std::tuple<>();
}
template<int Idx, class A, class... B>
bool operator!=(const zip_iterator<Args...> & other) const {
return std::get<Idx>(*this) != std::get<Idx>(other)
// zip_iterators are different if all their iterators are different
&& this->operator!=<Idx+1,B...>(other);
}
template<int>
bool operator!=(const zip_iterator<Args...> & ) const {
return true;
}
template<int Idx, class A, class... B>
bool operator==(const zip_iterator<Args...> & other) const {
return std::get<Idx>(*this) == std::get<Idx>(other)
// zip_iterators are equal if one pair of their iterators is equal
|| this->operator==<Idx+1,B...>(other);
}
template<int>
bool operator==(const zip_iterator<Args...> & ) const {
return false;
}
};
/** Dummy container for ranged loop iteration
*
* This class wraps several containers and offers the required methods to be
* used with the ranged for loop syntax introduced by C++11.
*
* \tparam Args... The types of the wrapped containers.
*/
template <class... Args>
struct zip_impl {
/// The zip iterator type
using iterator = zip_iterator<Args...>;
/// The number of containers wrapped inside
constexpr static std::size_t len = sizeof...(Args);
/// An array of references to the actual containers
std::tuple<Args&&...> containers;
/*zip_impl() {}*/
zip_impl(Args&&... args) : containers(std::forward<Args>(args)...) {
}
iterator begin() {
iterator begin_it;
begin<1,Args...>(begin_it);
return begin_it;
}
template<int,class A, class... B>
void begin(iterator& begin_it) {
begin<1,B...>(begin_it);
std::get<len - sizeof...(B)-1>(begin_it) = std::get<len - sizeof...(B)-1>(containers).begin();
}
template<int>
void begin(iterator&) {}
iterator end() {
iterator end_it;
end<1,Args...>(end_it);
return end_it;
}
template<int,class A, class... B>
void end(iterator& end_it) {
end<1,B...>(end_it);
std::get<len - sizeof...(B)-1>(end_it) = std::get<len - sizeof...(B)-1>(containers).end();
}
template<int>
void end(iterator&) {}
};
/* A zip wrapper function for ranged iteration.
*
* This function returns a dummy container that represents all of its arguments.
*
* usage:
* std::array<int, 10> int_array; // Assume the containers have been
* std::vector<std::string> string_array; // initialized and filled
* for(auto it : zip(int_array, string_array)) {
* int x;
* std::string s;
* std::tie(x, s) = *it;
*
* int& x_bis = std::get<O>(*it);
* // ... anything with x and s
* }
*/
template<typename... T>
auto zip(T&&... containers) -> zip_impl<T...> {
return zip_impl<T...>(std::forward<T>(containers)...);
}
}
#endif