-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter.hpp
More file actions
314 lines (276 loc) · 8.85 KB
/
iter.hpp
File metadata and controls
314 lines (276 loc) · 8.85 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef PYTHON_ITER_HPP
#define PYTHON_ITER_HPP
#include "python.hpp"
#include <type_traits>
#include <vector>
#include <list>
#include <functional>
namespace python {
class iter {
class _iter{
list l;
public:
_iter(iter& it) : l(it.l) {}
var operator*() { return l[0]; }
_iter& operator++() { if (*this) l.del(0); return *this; }
operator bool() { return !l.empty(); }
};
list l;
public:
iter(list l) : l(l) {}
_iter begin() { return *this; }
bool end() { return false; }
};
/******************************************************************************
* python basic iterables
******************************************************************************/
template <typename T>
class range {
class range_iter {
T v, m_stop, m_step;
public:
range_iter(range &r) : v(r.m_start), m_stop(r.m_stop), m_step(r.m_step) {}
var operator*() { return v; }
range_iter &operator++() { return v += m_step, *this; }
operator bool() { return m_step > 0 ? v < m_stop : v > m_stop; }
};
T zero = 0, m_start = 0, m_stop = 0, m_step = 1;
public:
range(T start, T stop, T step = 1) : m_start(start), m_stop(stop), m_step(step) {}
range(T stop) : m_stop(stop) {}
range() = default;
range_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class zip {
class zip_iter {
std::vector<var> iters;
public:
zip_iter(zip& self) : iters(self.m_iters) {}
list operator*() {
list l;
for (auto &i:iters)
l.append(*i);
return l;
}
zip_iter &operator++() {
for (auto &i:iters) ++i;
return *this;
}
operator bool() {
for (auto i:iters)
if (!i) return false;
return true;
}
};
std::vector<var> m_iters;
public:
template <typename... Iters>
zip(Iters&&...i) : m_iters({std::forward<Iters>(i).begin()...}) {}
zip_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class map {
using func = std::function<var(var)>;
class map_iter {
func f;
var it;
public:
map_iter(map& self) : f(self.m_func), it(self.m_it.begin()) {
}
var operator*() { return f(*it); }
map_iter &operator++() { ++it; return *this; }
operator bool() { return it;}
};
func m_func;
var m_it;
public:
map(func f, var it) : m_it(it), m_func(f) {}
map_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class filter {
using func = std::function<bool(var)>;
class filter_iter {
func f;
var it;
public:
filter_iter(filter& self) : f(self.m_func), it(self.m_it) {}
var operator*() { return *it; }
filter_iter &operator++() { while (!f(*++it)); return *this; }
operator bool() { return it; }
};
func m_func;
var m_it;
public:
filter(func f, var it) : m_it(it.begin()), m_func(f) { while (!f(*m_it)) ++m_it; }
filter_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
/*----------------------------------------------------------------------------
* itertools: infinite iterables
*----------------------------------------------------------------------------*/
namespace itertools
{
class count {
class count_iter {
int v,step;
public:
count_iter(count& c) : v(c.m_start), step(c.m_step) {}
var operator*() { return v; }
count_iter &operator++() { return v+=step, *this; }
operator bool() { return true; }
};
int m_start, m_step;
public:
count(int start = 0, int step = 1) : m_start(start), m_step(step) {}
count_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class repeat {
class repeat_iter {
var v;
int count;
public:
repeat_iter(repeat& r) : v(r.m_v), count(r.m_count) {}
var operator*() { return v; }
repeat_iter &operator++() { return --count, *this; }
operator bool() { return count; }
};
var m_v;
int m_count;
public:
repeat(var v, int count = -1) : m_v(v), m_count(count) {}
repeat_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class cycle {
class cycle_iter {
var begin, it;
public:
cycle_iter(cycle& c) : begin(c.m_begin), it(begin) {}
var operator*() { return *it; }
cycle_iter &operator++() { ++it ? it : it = begin; return *this; }
operator bool() { return true; }
};
var m_begin;
public:
cycle(var i) : m_begin(i.begin()) {}
cycle_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
/*----------------------------------------------------------------------------
* itertools: finite iterables
*----------------------------------------------------------------------------*/
class accumulate {
class accumulate_iter {
var it, sum;
public:
accumulate_iter(accumulate& a) : it(a.m_it.begin()), sum(a.m_start) {}
var operator*() { return sum; }
accumulate_iter &operator++() { sum += *++it; return *this; }
operator bool() { return it; }
};
var m_it;
var m_start;
public:
accumulate(var i, var start = 0) : m_it(i), m_start(start) {}
accumulate_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class chain {
class chain_iter {
std::list<var> begins;
var it;
public:
chain_iter(chain& self) : begins(self.m_begins), it(begins.front()) {}
var operator*() { return *it; }
chain_iter &operator++() {
if (++it) {
return *this;
} else {
begins.pop_front();
if (!begins.empty())
it = begins.front();
return *this;
}
}
operator bool() {
return !begins.empty() || it;
}
};
std::list<var> m_begins;
public:
template <typename... Iters>
chain(Iters&&...i) : m_begins({std::forward<Iters>(i).begin()...}) {}
chain_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class compress {
class compress_iter {
var data, selectors;
public:
compress_iter(compress& c) : data(c.m_data), selectors(c.m_selectors) {
while (data && selectors && !*selectors) ++data, ++selectors;
}
var operator*() { return *data; }
compress_iter &operator++() {
while (++data && ++selectors && !*selectors);
return *this;
}
operator bool() { return data&&selectors; }
};
var m_data, m_selectors;
public:
compress(var d, var s) : m_data(d.begin()), m_selectors(s.begin()) {}
compress(var d, list s) : m_data(d.begin()), m_selectors(iter(s).begin()) {}
compress_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class dropwhile {
using func = std::function<bool(var)>;
class dropwhile_iter {
var it;
public:
dropwhile_iter(dropwhile& d) : it(d.m_it) {}
var operator*() { return *it; }
dropwhile_iter &operator++() { ++it; return *this; }
operator bool() { return it; }
};
var m_it;
public:
dropwhile(func f, var i) : m_it(i.begin()) { while (m_it && f(*m_it)) ++m_it; }
dropwhile_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class groupby {
using func = std::function<var(var)>;
var m_it;
public:
groupby(var it, func f=[](var v) { return v; }) {
list keys;
list res;
}
// groupby_iter begin() { return *this; }
bool end() { return false; } // this is just a placeholder
};
class filterfalse : public filter {
public: filterfalse(std::function<bool(var)> f, var i) : filter([f](var x) { return !f(x); }, i) {}
};
class islice;
class zip_longest;
/*----------------------------------------------------------------------------
* itertools: combinatoric iterables
*----------------------------------------------------------------------------*/
class product;
class permutations;
class combinations;
class combinations_with_replacement;
} // namespace itertools
class enumerate : public zip {
public:
template <typename I>
enumerate(I it,int start=0) : zip(itertools::count(0), it) {}
};
} // namespace python
#endif // PYTHON_ITER_HPP