-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipit.h
More file actions
297 lines (200 loc) · 7.42 KB
/
zipit.h
File metadata and controls
297 lines (200 loc) · 7.42 KB
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
296
297
#ifndef __ZIPIT_H__
#define __ZIPIT_H__
#include <utility>
#include <iterator>
#include <optional>
#include <tuple>
namespace zipit {
template<typename ItFirst, typename ItSecond>
class Zip_iterator {
public:
using elm_type = std::pair< typename std::iterator_traits<ItFirst>::value_type, typename std::iterator_traits<ItSecond>::value_type >;
using difference_type = long;
using value_type = elm_type;
using pointer = elm_type*;
using reference = elm_type&;
using iterator_category = std::forward_iterator_tag;
Zip_iterator(ItFirst first, ItSecond second) : first_(first), second_(second) {
}
Zip_iterator(const Zip_iterator &arg) : first_(arg.first_), second_(arg.second_) {
}
Zip_iterator& operator++() {
++first_;
++second_;
return *this;
}
Zip_iterator operator++(int) {
Zip_iterator retval = *this;
++(*this);
return retval;
}
bool operator==(const Zip_iterator other) const {
return first_ == other.first_ || second_ == other.second_;
}
bool operator!=(const Zip_iterator other) const {
return !(*this == other);
}
value_type operator*() {
return elm_type( *first_, *second_ );
}
private:
ItFirst first_;
ItSecond second_;
};
template<typename ItFirst, typename ItSecond>
Zip_iterator<ItFirst,ItSecond> make(ItFirst first, ItSecond second) {
return Zip_iterator(first, second);
}
template<typename ...Type>
class Zip_iterator_tuple {
public:
using self_type = Zip_iterator_tuple<Type...>;
using elm_type = std::tuple< typename std::iterator_traits<Type>::value_type... >;
using difference_type = long;
using value_type = elm_type;
using pointer = elm_type*;
using reference = elm_type&;
using iterator_category = std::forward_iterator_tag;
Zip_iterator_tuple(Type... it) : iterators_(it...) {
}
Zip_iterator_tuple& operator++() {
inc_helper<0>();
return *this;
}
Zip_iterator_tuple operator++(int) {
Zip_iterator_tuple retval = *this;
++(*this);
return retval;
}
bool operator==(const self_type other) const {
return cmp_helper<0>(other);
}
bool operator!=(const self_type other) const {
return !(*this == other);
}
value_type operator*() {
value_type rval;
make_rval<0>(rval);
return rval;
}
private:
std::tuple<Type...> iterators_;
template<size_t FieldIndex>
void inc_helper() {
++std::get<FieldIndex>(iterators_);
if constexpr (FieldIndex < std::tuple_size<elm_type>::value - 1 ) {
inc_helper<FieldIndex + 1>();
}
}
template<size_t FieldIndex>
bool cmp_helper(const self_type &other) const {
if (std::get<FieldIndex>(iterators_) == std::get<FieldIndex>(other.iterators_)) {
return true;
}
if constexpr (FieldIndex < std::tuple_size<elm_type>::value - 1) {
return cmp_helper<FieldIndex + 1>(other);
}
return false;
}
template<size_t FieldIndex>
void make_rval(value_type &rval) {
std::get<FieldIndex>(rval) = *std::get<FieldIndex>(iterators_);
if constexpr (FieldIndex < std::tuple_size<elm_type>::value - 1) {
make_rval<FieldIndex + 1>(rval);
}
}
};
template<typename ...Ty>
Zip_iterator_tuple<Ty...> maken(Ty ...it) {
return Zip_iterator_tuple(it...);
}
template<typename InputItFirst, typename InputItSecond, typename Function>
void for_each_zip(InputItFirst begin_first, InputItFirst end_first, InputItSecond begin_sec, InputItSecond end_sec, Function func) {
using OptFirstArg = typename std::iterator_traits<InputItFirst>::value_type;
using OptSecondArg = typename std::iterator_traits<InputItSecond>::value_type;
using OptArgPair = std::pair< OptFirstArg, OptSecondArg >;
while( begin_first != end_first && begin_sec != end_sec) {
OptArgPair arg(*begin_first, *begin_sec);
func( arg );
++begin_first;
++begin_sec;
}
}
template<typename InputItFirst, typename InputItSecond, typename Function>
void for_each_zip_ref_func(InputItFirst begin_first, InputItFirst end_first, InputItSecond begin_sec, InputItSecond end_sec, Function &func) {
using OptFirstArg = typename std::iterator_traits<InputItFirst>::value_type;
using OptSecondArg = typename std::iterator_traits<InputItSecond>::value_type;
using OptArgPair = std::pair< OptFirstArg, OptSecondArg >;
while( begin_first != end_first && begin_sec != end_sec) {
OptArgPair arg(*begin_first, *begin_sec);
func( arg );
++begin_first;
++begin_sec;
}
}
template<typename InputItFirst, typename InputItSecond, typename Function>
void for_each_zip_longest(InputItFirst begin_first, InputItFirst end_first, InputItSecond begin_sec, InputItSecond end_sec, Function func) {
using OptFirstArg = std::optional< typename std::iterator_traits<InputItFirst>::value_type >;
using OptSecondArg = std::optional< typename std::iterator_traits<InputItSecond>::value_type >;
using OptArgPair = std::pair< OptFirstArg, OptSecondArg >;
while( begin_first != end_first && begin_sec != end_sec) {
OptFirstArg first_arg;
first_arg.emplace( *begin_first );
OptSecondArg second_arg;
second_arg.emplace( *begin_sec );
OptArgPair val( first_arg, second_arg );
func( val );
++begin_first;
++begin_sec;
}
while( begin_first != end_first ) {
OptFirstArg first_arg;
first_arg.emplace( *begin_first );
OptSecondArg second_arg;
OptArgPair val( first_arg, second_arg );
func( val );
++begin_first;
}
while( begin_sec != end_sec) {
OptFirstArg first_arg;
OptSecondArg second_arg;
second_arg.emplace( *begin_sec );
OptArgPair val( first_arg, second_arg );
func( val );
++begin_sec;
}
}
template<typename InputItFirst, typename InputItSecond, typename Function>
void for_each_zip_longest_ref_func(InputItFirst begin_first, InputItFirst end_first, InputItSecond begin_sec, InputItSecond end_sec, Function &func) {
using OptFirstArg = std::optional< typename std::iterator_traits<InputItFirst>::value_type >;
using OptSecondArg = std::optional< typename std::iterator_traits<InputItSecond>::value_type >;
using OptArgPair = std::pair< OptFirstArg, OptSecondArg >;
while( begin_first != end_first && begin_sec != end_sec) {
OptFirstArg first_arg;
first_arg.emplace( *begin_first );
OptSecondArg second_arg;
second_arg.emplace( *begin_sec );
OptArgPair val( first_arg, second_arg );
func( val );
++begin_first;
++begin_sec;
}
while( begin_first != end_first ) {
OptFirstArg first_arg;
first_arg.emplace( *begin_first );
OptSecondArg second_arg;
OptArgPair val( first_arg, second_arg );
func( val );
++begin_first;
}
while( begin_sec != end_sec) {
OptFirstArg first_arg;
OptSecondArg second_arg;
second_arg.emplace( *begin_sec );
OptArgPair val( first_arg, second_arg );
func( val );
++begin_sec;
}
}
} // eof namespace zipit
#endif