forked from jscheiny/Streams
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTerminators.h
More file actions
360 lines (310 loc) · 10.9 KB
/
Copy pathStreamTerminators.h
File metadata and controls
360 lines (310 loc) · 10.9 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef SCHEINERMAN_STREAM_STREAM_TERMINATORS_H
#define SCHEINERMAN_STREAM_STREAM_TERMINATORS_H
namespace stream {
namespace op {
#define CLASS_SPECIALIZATIONS(operation) \
template<typename R, typename C> auto operation (R (C::*member)()) \
{ return operation (std::mem_fn(member)); } \
template<typename R, typename C> auto operation (R (C::*member)() const) \
{ return operation (std::mem_fn(member)); }
template<typename Function>
inline auto for_each(Function&& function) {
return make_terminator("stream::op::for_each", [=](auto&& stream) mutable {
auto& source = stream.getSource();
while(source->advance()) {
function(std::move(*source->get()));
}
return function;
});
}
CLASS_SPECIALIZATIONS(for_each);
inline auto count() {
return make_terminator("stream::op::count", [=](auto&& stream) {
auto& source = stream.getSource();
size_t count = 0;
while(source->advance()) {
std::move(*source->get());
count++;
}
return count;
});
}
template<typename U, typename Accumulator>
inline auto identity_reduce(const U& identity, Accumulator&& accumulator) {
return make_terminator("stream::op::identity_reduce", [=](auto&& stream) mutable {
auto& source = stream.getSource();
U result = identity;
while(source->advance()) {
result = accumulator(std::move(result), std::move(*source->get()));
}
return result;
});
}
template<typename Accumulator>
inline auto reduce(Accumulator&& accumulator) {
return make_terminator("stream::op::reduce", [=](auto&& stream) mutable {
auto& source = stream.getSource();
if(source->advance()) {
auto reduction = identity_reduce(std::move(*source->get()),
std::forward<Accumulator>(accumulator));
return stream | reduction;
} else {
throw EmptyStreamException("stream::op::reduce");
}
});
}
template<typename IdentityFn, typename Accumulator>
inline auto reduce(IdentityFn&& identityFn, Accumulator&& accumulator) {
return make_terminator("stream::op::reduce", [=](auto&& stream) mutable {
auto& source = stream.getSource();
if(source->advance()) {
auto reduction = identity_reduce(identityFn(std::move(*source->get())),
std::forward<Accumulator>(accumulator));
return stream | reduction;
} else {
throw EmptyStreamException("stream::op::reduce");
}
});
}
inline auto first() {
return make_terminator("stream::op::first", [=](auto&& stream) {
auto& source = stream.getSource();
if(source->advance()) {
return std::move(*source->get());
} else {
throw EmptyStreamException("stream::op::first");
}
});
}
inline auto last() {
return reduce([](auto&& first, auto&& second) { return second; })
.rename("stream::op::last");
}
inline auto nth(size_t index) {
return (skip(index) | first()).rename("stream::op::nth");
}
inline auto sum() {
return reduce(std::plus<void>())
.rename("stream::op::sum");
}
template<typename T>
inline auto sum(const T& identity) {
return identity_reduce(identity, std::plus<T>())
.rename("stream::op::sum");
}
inline auto product() {
return reduce(std::multiplies<void>())
.rename("stream::op::product");
}
template<typename T>
inline auto product(const T& identity) {
return identity_reduce(identity, std::multiplies<T>())
.rename("stream::op::product");
}
template<typename Less = std::less<void>>
inline auto max(Less&& less = Less()) {
return reduce([=](const auto& a, const auto& b) {
return less(a, b) ? b : a;
}).rename("stream::op::max");
}
template<typename Function, typename Less = std::less<void>>
inline auto max_by(Function&& function, Less&& less = Less()) {
/* Pair is (max, max_elem) */
auto to_pair = [function](auto&& x) { return std::make_pair(function(x), x); };
auto next = [function, less](auto&& accumulated, auto&& value) {
auto fvalue = function(std::move(value));
if(less(accumulated.first, fvalue)) {
return std::make_pair(std::move(fvalue), std::move(value));
}
return accumulated;
};
return reduce(to_pair, next)
.then([](auto&& accumulated) { return accumulated.second; })
.rename("stream::op::max_by");
}
template<typename Less = std::less<void>>
inline auto min(Less&& less = Less()) {
return reduce([=](const auto& a, const auto& b) {
return less(a, b) ? a : b;
}).rename("stream::op::min");
}
template<typename Function, typename Less = std::less<void>>
inline auto min_by(Function&& function, Less&& less = Less()) {
/* Pair is (min, min_elem) */
auto to_pair = [function](auto&& x) { return std::make_pair(function(x), x); };
auto next = [function, less](auto&& accumulated, auto&& value) {
auto fvalue = function(std::move(value));
if(less(fvalue, accumulated.first)) {
return std::make_pair(std::move(fvalue), std::move(value));
}
return accumulated;
};
return reduce(to_pair, next)
.then([](auto&& accumulated) { return accumulated.second; })
.rename("stream::op::min_by");
}
template<typename Less = std::less<void>>
inline auto minmax(Less&& less = Less()) {
auto to_pair = [](auto&& x) { return std::make_pair(x, x); };
auto next_minmax = [less](auto&& accumulated, auto&& value) {
using T = std::remove_reference_t<decltype(value)>;
if(less(value, accumulated.first)) {
return std::pair<T,T>(std::move(value), std::move(accumulated.second));
}
if(less(accumulated.second, value)) {
return std::pair<T,T>(std::move(accumulated.first), std::move(value));
}
return accumulated;
};
return reduce(to_pair, next_minmax).rename("stream::op::minmax");
}
template<typename Function, typename Less = std::less<void>>
inline auto minmax_by(Function&& function, Less&& less = Less()) {
/* Pair is ((min, min_elem), (max, max_elem)) */
auto to_pair = [function](auto&& x) {
auto fx = function(x);
return std::make_pair(std::make_pair(fx, x), std::make_pair(fx, x));
};
auto next = [function, less](auto&& accumulated, auto&& value) {
auto fvalue = function(std::move(value));
auto min = accumulated.first;
if(less(fvalue, min.first)) {
min = std::make_pair(std::move(fvalue), std::move(value));
}
auto max = accumulated.second;
if(less(max.first, fvalue)) {
max = std::make_pair(std::move(fvalue), std::move(value));
}
return std::make_pair(min, max);
};
return reduce(to_pair, next)
.then([](auto&& accumulated) { return std::make_pair(accumulated.first.second, accumulated.second.second); })
.rename("stream::op::minmax_by");
}
template<typename Predicate>
inline auto any(Predicate&& predicate) {
return make_terminator("stream::op::any", [=](auto&& stream) mutable {
auto& source = stream.getSource();
while(source->advance()) {
if(predicate(*source->get())) {
return true;
}
}
return false;
});
}
inline auto any() {
return any([](bool b){ return b; });
}
CLASS_SPECIALIZATIONS(any);
template<typename Predicate>
inline auto all(Predicate&& predicate) {
return make_terminator("stream::op::all", [=](auto&& stream) mutable {
auto& source = stream.getSource();
while(source->advance()) {
if(!predicate(*source->get())) {
return false;
}
}
return true;
});
}
inline auto all() {
return all([](bool b){ return b; });
}
CLASS_SPECIALIZATIONS(all);
template<typename Predicate>
inline auto none(Predicate&& predicate) {
return any(std::forward<Predicate>(predicate))
.then([](bool x) { return !x; })
.rename("stream::op::none");
}
inline auto none() {
return none([](bool b){ return b; });
}
CLASS_SPECIALIZATIONS(none);
template<typename Predicate>
inline auto not_all(Predicate&& predicate) {
return all(std::forward<Predicate>(predicate))
.then([](bool x) { return !x; })
.rename("stream::op::not_all");
}
inline auto not_all() {
return not_all([](bool b){ return b; });
}
CLASS_SPECIALIZATIONS(not_all);
template<typename OutputIterator>
inline auto copy_to(OutputIterator out) {
return make_terminator("stream::op::copy_to", [=](auto&& stream) mutable {
using T = StreamType<decltype(stream)>;
auto& source = stream.getSource();
while(source->advance()) {
*out = *source->get();
++out;
}
return out;
});
}
template<typename OutputIterator>
inline auto move_to(OutputIterator out) {
return make_terminator("stream::op::move_to", [=](auto&& stream) mutable {
using T = StreamType<decltype(stream)>;
auto& source = stream.getSource();
while(source->advance()) {
*out = std::move(*source->get());
++out;
}
return out;
});
}
inline auto print_to(std::ostream& os, const char* delimiter = " ") {
return make_terminator("stream::op::print_to", [&os, delimiter](auto&& stream) -> std::ostream& {
using T = StreamType<decltype(stream)>;
stream | copy_to(std::ostream_iterator<T>(os, delimiter));
return os;
});
}
inline auto random_sample(size_t size) {
return make_terminator("stream::op::random_sample", [=](auto&& stream) {
using T = StreamType<decltype(stream)>;
auto& source = stream.getSource();
std::vector<T> results;
for(int i = 0; i < size; i++) {
if(source->advance()) {
results.push_back(std::move(*source->get()));
} else {
return results;
}
}
auto seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
auto random_index = [&generator](int upper) {
return std::uniform_int_distribution<int>(0, upper - 1)(generator);
};
size_t seen = size;
while(source->advance()) {
seen++;
int index = random_index(seen);
if(index < size) {
results[index] = std::move(*source->get());
} else {
source->get();
}
}
return results;
});
}
inline auto random_element() {
return random_sample(1)
.then([](auto&& vec) {
if(vec.empty()) {
throw EmptyStreamException("stream::op::random_element");
}
return vec[0];
})
.rename("stream::op::element");
}
#undef CLASS_SPECIALIZATIONS
} /* namespace op */
} /* namespace stream */
#endif