-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreams.h
726 lines (568 loc) · 23.9 KB
/
Streams.h
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#ifndef RUST_STREAMS_H
#define RUST_STREAMS_H
#include<tuple>
#if defined _MSC_VER
#include "Optional/optional.hpp"
#define CONSTEXPR
# else
#include <experimental/optional>
#define CONSTEXPR constexpr
#endif
namespace streams {
template<typename T>
using Optional = std::experimental::optional<T>;
using std::experimental::nullopt;
namespace traits {
template<typename Type>
constexpr bool IsOptional() {
using T = typename Type::value_type;
return std::is_same<std::decay_t<Type>, Optional<T>>::value;
}
template<typename Extractor>
using ValueType = std::decay_t<decltype(*(std::declval<Extractor>().get()))>;
template<typename Extractor, typename Functor>
using ApplyOnValueType = decltype(std::declval<Functor>()(std::declval<decltype(*(std::declval<Extractor>().get()))>()));
}
template <typename DerivedStreamExtractor>
struct StreamExtractor {
auto get() noexcept(noexcept(std::declval<DerivedStreamExtractor>().get_impl())) {
return static_cast<DerivedStreamExtractor*>(this)->get_impl();
}
bool advance() noexcept(noexcept(std::declval<DerivedStreamExtractor>().advance_impl())) {
return static_cast<DerivedStreamExtractor*>(this)->advance_impl();
}
};
template <typename IteratorType>
struct SequenceStreamExtractor : StreamExtractor<SequenceStreamExtractor<IteratorType>> {
SequenceStreamExtractor(IteratorType&& b, IteratorType&& e)
: current(std::forward<IteratorType>(b)), next(std::forward<IteratorType>(b))
, begin(std::forward<IteratorType>(b)), end(std::forward<IteratorType>(e)) {}
IteratorType current;
IteratorType next;
const IteratorType begin;
const IteratorType end;
auto get_impl() noexcept {
return current;
}
bool advance_impl() {
if (next != end) {
current = next++;
return true;
} else {
return false;
}
}
};
template<typename ExtractorType>
struct SkipFirstStreamExtractor : StreamExtractor<SkipFirstStreamExtractor<ExtractorType>> {
SkipFirstStreamExtractor(ExtractorType extractor, size_t count) : source(extractor), skipCount(count) {}
ExtractorType source;
size_t skipCount;
auto get_impl() {
return source.get();
}
bool advance_impl() {
while (skipCount != 0) {
--skipCount;
if (!source.advance()) {
return false;
}
}
return source.advance();
}
};
template<typename ExtractorType, typename Predicate>
struct SkipWhileStreamExtractor : StreamExtractor<SkipWhileStreamExtractor<ExtractorType, Predicate>> {
SkipWhileStreamExtractor(ExtractorType extractor, Predicate&& predicate) : source(extractor), predicate(std::forward<Predicate>(predicate)) {}
ExtractorType source;
Predicate predicate;
bool skipping = true;
auto get_impl() {
return source.get();
}
bool advance_impl() {
if (skipping) {
while (skipping && source.advance()) {
skipping = predicate(*source.get());
}
return !skipping; // depleted stream : skipping == true
} else {
return source.advance();
}
}
};
template<typename ExtractorType>
struct TakeStreamExtractor : StreamExtractor<TakeStreamExtractor<ExtractorType>> {
TakeStreamExtractor(ExtractorType extractor, size_t count) : source(extractor), limit(count) {}
ExtractorType source;
size_t limit;
auto get_impl() {
return source.get();
}
bool advance_impl() {
if (limit != 0) {
--limit;
return source.advance();
}
return false;
}
};
template<typename ExtractorType, typename Predicate>
struct TakeWhileStreamExtractor : StreamExtractor<TakeWhileStreamExtractor<ExtractorType, Predicate>> {
TakeWhileStreamExtractor(ExtractorType extractor, Predicate&& predicate) : source(extractor), predicate(std::forward<Predicate>(predicate)) {}
ExtractorType source;
Predicate predicate;
bool taking = true;
auto get_impl() {
return source.get();
}
bool advance_impl() {
taking &= taking && source.advance() && predicate(*source.get());
return taking;
}
};
template<typename ExtractorType, typename Predicate>
struct FilterStreamExtractor : StreamExtractor<FilterStreamExtractor<ExtractorType, Predicate>> {
FilterStreamExtractor(ExtractorType extractor, Predicate&& p) : source(extractor), predicate(std::forward<Predicate>(p)) {}
ExtractorType source;
Predicate predicate;
auto get_impl() {
return source.get();
}
bool advance_impl() {
if (!source.advance()) {
return false;
}
auto elementPtr = source.get();
while (!predicate(*elementPtr)) {
if (source.advance()) {
elementPtr = source.get();
} else {
return false;
}
}
return true;
}
};
template<typename ExtractorType, typename Transform>
struct FilterMapStreamExtractor : StreamExtractor<FilterMapStreamExtractor<ExtractorType, Transform>> {
FilterMapStreamExtractor(ExtractorType extractor, Transform&& t) : source(extractor), transform(std::forward<Transform>(t)) {}
ExtractorType source;
Transform transform;
traits::ValueType<ExtractorType> storage {};
static_assert(traits::IsOptional<decltype(std::declval<Transform>()(*source.get()))>(), "Transform functor should return Optional<T> type");
auto get_impl() {
return &storage;
}
bool advance_impl() {
while (true) {
if (!source.advance()) {
return false;
}
auto e = transform(*source.get());
if (e) {
storage = *e;
return true;
}
}
}
};
template<typename ExtractorType, typename Transform>
struct MapStreamExtractor : StreamExtractor<MapStreamExtractor<ExtractorType, Transform>> {
MapStreamExtractor(ExtractorType sourceExtractor, Transform&& transform) : source(sourceExtractor), transformer(std::forward<Transform>(transform)) {}
ExtractorType source;
Transform transformer;
traits::ApplyOnValueType<ExtractorType, Transform> value {};
auto get_impl() {
value = transformer(*source.get());
return &value;
}
bool advance_impl() {
return source.advance();
}
};
template<typename ExtractorType, typename Transform>
struct FlatMapStreamExtractor : StreamExtractor<FlatMapStreamExtractor<ExtractorType, Transform>> {
FlatMapStreamExtractor(ExtractorType sourceExtractor, Transform&& transform) : source(sourceExtractor), transformer(std::forward<Transform>(transform)) {}
ExtractorType source;
Transform transformer;
traits::ApplyOnValueType<ExtractorType, Transform> innerCollection{};
using SequenceStreamExtractorType = SequenceStreamExtractor<decltype(std::begin(innerCollection))>;
SequenceStreamExtractorType sequence{ std::begin(innerCollection), std::end(innerCollection) };
auto get_impl() {
return sequence.get();
}
bool advance_impl() {
if (!sequence.advance()) {
if (source.advance()) {
innerCollection = transformer(*source.get());
sequence.~SequenceStreamExtractorType();
new(&sequence) SequenceStreamExtractor<decltype(std::begin(innerCollection))> { std::begin(innerCollection), std::end(innerCollection) };
return advance_impl();
}
else {
return false;
}
}
else {
return true;
}
}
};
template<typename ExtractorType, typename Inspector>
struct InspectStreamExtractor : StreamExtractor<InspectStreamExtractor<ExtractorType, Inspector>> {
InspectStreamExtractor(ExtractorType extractor, Inspector&& inspector) : source(extractor), inspector(std::forward<Inspector>(inspector)) {}
ExtractorType source;
Inspector inspector;
auto get_impl() {
return source.get();
}
bool advance_impl() {
if (source.advance()) {
inspector(*source.get());
return true;
}
return false;
}
};
template<typename ExtractorType, typename Inspector>
struct SpyStreamExtractor : StreamExtractor<SpyStreamExtractor<ExtractorType, Inspector>> {
SpyStreamExtractor(ExtractorType extractor, Inspector&& inspector) : source(extractor), inspector(std::forward<Inspector>(inspector)) {}
ExtractorType source;
Inspector inspector;
auto get_impl() {
auto value = source.get();
inspector(*value);
return value;
}
bool advance_impl() {
return source.advance();
}
};
template<typename T>
struct Enumerated {
size_t i;
std::decay_t<T> v;
Enumerated& operator = (const Enumerated&) = default;
};
template<typename T>
bool operator == (const Enumerated<T>& lhs, const Enumerated<T>& rhs) {
return lhs.i == rhs.i && lhs.v == rhs.v;
}
template<typename ExtractorType>
struct EnumerateStreamExtractor : StreamExtractor<EnumerateStreamExtractor<ExtractorType>> {
EnumerateStreamExtractor(ExtractorType extractor, size_t counter = 0) : source(extractor), counter(counter){}
ExtractorType source;
size_t counter;
Enumerated<traits::ValueType<ExtractorType>> value {counter, {}};
auto get_impl() {
value = {counter - 1, *source.get()};
return &value;
}
bool advance_impl() {
++counter;
return source.advance();
}
};
template<typename ExtractorType>
struct EnumerateTupleStreamExtractor : StreamExtractor<EnumerateTupleStreamExtractor<ExtractorType>> {
EnumerateTupleStreamExtractor(ExtractorType extractor, size_t counter = 0) : source(extractor), counter(counter) {}
ExtractorType source;
size_t counter;
std::tuple<size_t, traits::ValueType<ExtractorType>> value {counter, {}};
auto get_impl() {
value = std::make_tuple(counter - 1, *source.get());
return &value;
}
bool advance_impl() {
++counter;
return source.advance();
}
};
template<typename ExtractorType, typename ExtractorOtherType>
struct ChainStreamExtractor : StreamExtractor<ChainStreamExtractor<ExtractorType, ExtractorOtherType>> {
ChainStreamExtractor(ExtractorType extractor, ExtractorOtherType other) : first(extractor), next(other){}
ExtractorType first;
ExtractorOtherType next;
bool firstHaveElements = true;
auto get_impl() {
if (firstHaveElements) {
return first.get();
} else {
return next.get();
}
}
bool advance_impl() {
if (firstHaveElements && (firstHaveElements = first.advance())) {
return true;
}
return next.advance();
}
};
template<typename ExtractorType, typename ExtractorOtherType>
struct ZipStreamExtractor : StreamExtractor<ZipStreamExtractor<ExtractorType, ExtractorOtherType>> {
ZipStreamExtractor(ExtractorType extractor, ExtractorOtherType other) : left(extractor), right(other) {}
ExtractorType left;
ExtractorOtherType right;
std::tuple<traits::ValueType<ExtractorType>, traits::ValueType<ExtractorOtherType>> value {};
auto get_impl() {
value = std::make_tuple(*left.get(), *right.get());
return &value;
}
bool advance_impl() {
return left.advance() && right.advance();
}
};
template<typename ExtractorType>
struct PurifyStreamExtractor : StreamExtractor<PurifyStreamExtractor<ExtractorType>> {
PurifyStreamExtractor(ExtractorType extractor) : source(extractor), value() {}
ExtractorType source;
using source_optional_type = traits::ValueType<ExtractorType>;
static_assert(traits::IsOptional<source_optional_type>(), "Expected Optional<T> as a source");
using value_type = std::remove_const_t<typename source_optional_type::value_type>;
value_type value;
auto get_impl() {
value = **source.get();
return &value;
}
bool advance_impl() {
while (source.advance()) {
if (*source.get() != nullopt) {
return true;
}
}
return false;
}
};
template<typename ExtractorType>
struct BaseStreamInterface {
ExtractorType extractor;
using value_type = std::remove_reference_t<decltype(*extractor.get())>;
CONSTEXPR BaseStreamInterface(ExtractorType e) : extractor(e) {}
// Intermediate Operations
template<typename Transform>
auto map(Transform&& transform) {
using Extractor = MapStreamExtractor<decltype(extractor), Transform>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Transform>(transform)));
}
// expects that std::begin and std::end can be called on the result of transform
template<typename Transform>
auto flatMap(Transform&& transform) {
using Extractor = FlatMapStreamExtractor<decltype(extractor), Transform>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Transform>(transform)));
}
// add flatten level
auto flatten() {
const auto flat = [](auto&& e) { return e; };
using Extractor = FlatMapStreamExtractor<decltype(extractor), decltype(flat)>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::move(flat)));
}
template<typename Predicate>
auto filter(Predicate&& predicate) {
using Extractor = FilterStreamExtractor<decltype(extractor), Predicate>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Predicate>(predicate)));
}
template<typename Transform>
auto filterMap(Transform&& transform) {
using Extractor = FilterMapStreamExtractor<decltype(extractor), Transform>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Transform>(transform)));
}
auto skip(size_t count) {
using Extractor = SkipFirstStreamExtractor<decltype(extractor)>;
return BaseStreamInterface<Extractor>(Extractor(extractor, count));
}
template<typename Predicate>
auto skipWhile(Predicate&& predicate) {
using Extractor = SkipWhileStreamExtractor<decltype(extractor), Predicate>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Predicate>(predicate)));
}
auto take(size_t count) {
using Extractor = TakeStreamExtractor<decltype(extractor)>;
return BaseStreamInterface<Extractor>(Extractor(extractor, count));
}
template<typename Predicate>
auto takeWhile(Predicate&& predicate) {
using Extractor = TakeWhileStreamExtractor<decltype(extractor), Predicate>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Predicate>(predicate)));
}
template<typename Inspector>
auto inspect(Inspector&& inspector) {
using Extractor = InspectStreamExtractor<decltype(extractor), Inspector>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Inspector>(inspector)));
}
template<typename Inspector>
auto spy(Inspector&& inspector) {
using Extractor = SpyStreamExtractor<decltype(extractor), Inspector>;
return BaseStreamInterface<Extractor>(Extractor(extractor, std::forward<Inspector>(inspector)));
}
auto enumerate(size_t from = 0) {
using Extractor = EnumerateStreamExtractor<decltype(extractor)>;
return BaseStreamInterface<Extractor>(Extractor(extractor, from));
}
auto enumerateTup(size_t from = 0) {
using Extractor = EnumerateTupleStreamExtractor<decltype(extractor)>;
return BaseStreamInterface<Extractor>(Extractor(extractor, from));
}
template <template<typename> class StreamOther, typename OtherExtractor>
auto chain(StreamOther<OtherExtractor> other) {
using Extractor = ChainStreamExtractor<decltype(extractor), OtherExtractor>;
return BaseStreamInterface<Extractor>(Extractor(extractor, other.extractor));
}
template <template<typename> class StreamOther, typename OtherExtractor>
auto zip(StreamOther<OtherExtractor> other) {
using Extractor = ZipStreamExtractor<decltype(extractor), OtherExtractor>;
return BaseStreamInterface<Extractor>(Extractor(extractor, other.extractor));
}
auto purify() {
static_assert(traits::IsOptional<value_type>(), "Purify should be called on a stream of Optional<T> values");
using Extractor = PurifyStreamExtractor<decltype(extractor)>;
return BaseStreamInterface<Extractor>(Extractor(extractor));
}
// Non-Terminal
Optional<value_type> next() {
if (extractor.advance()) {
return{ *extractor.get() };
}
return{};
}
Optional<value_type> nth(size_t n) {
while (n && extractor.advance()) {
--n;
}
return next();
}
// Terminal Operations
Optional<value_type> last() {
if (!extractor.advance()) {
return nullopt;
} else {
auto ptr = extractor.get();
while (extractor.advance()) {
ptr = extractor.get();
}
return *ptr;
}
}
template<typename Callable>
void forEach(Callable&& callable) {
while (extractor.advance()) {
callable(*extractor.get());
}
}
size_t count() {
size_t counter = 0;
while (extractor.advance()) {
++counter;
}
return counter;
}
template<typename Predicate>
bool any(Predicate&& predicate) {
while (extractor.advance()) {
if (predicate(*extractor.get())) {
return true;
}
}
return false;
}
template<typename Predicate>
bool all(Predicate&& predicate) {
while (extractor.advance()) {
if (!predicate(*extractor.get())) {
return false;
}
}
return true;
}
template<typename Comparator = std::less<std::remove_const_t<value_type>>>
Optional<std::remove_const_t<value_type>> min(Comparator cmp = {}) {
Optional<std::remove_const_t<value_type>> value {};
while (extractor.advance()) {
auto v = extractor.get();
if (!value || cmp(*v, *value)) { // nullopt is the least
value = *v;
}
}
return value;
}
template<typename Comparator = std::greater<std::remove_const_t<value_type>>>
Optional<std::remove_const_t<value_type>> max(Comparator cmp = {}) {
return min(cmp);
}
template<typename Predicate>
Optional<std::remove_const_t<value_type>> find(Predicate&& predicate) {
while (extractor.advance()) {
auto e = extractor.get();
if (predicate(*e)) {
return *e;
}
}
return nullopt;
}
template<typename Predicate>
Optional<size_t> position(Predicate&& predicate) {
size_t counter = 0;
while (extractor.advance()) {
++counter;
if (predicate(*extractor.get())) {
return counter;
}
}
return nullopt;
}
template<typename Accumulator, typename Fold>
Accumulator fold(Accumulator a, Fold&& fold) {
while (extractor.advance()) {
a = fold(a, *extractor.get());
}
return a;
}
template <template<class...> class Container = std::vector, typename Element = std::remove_const_t<value_type>>
auto collect() {
Container<Element> container;
while (extractor.advance()) {
container.push_back(*extractor.get());
}
return container;
}
template <typename Predicate, template<class...> class Container = std::vector, typename Element = std::remove_const_t<value_type>>
auto partition(Predicate&& predicate) {
std::pair<Container<Element>, Container<Element>> pair;
while (extractor.advance()) {
auto e = extractor.get();
if (predicate(*e)) {
pair.first.push_back(*e);
} else {
pair.second.push_back(*e);
}
}
return pair;
}
};
template<typename Container>
auto from(const Container& container) {
using Extractor = SequenceStreamExtractor<decltype(std::begin(container))>;
return BaseStreamInterface<Extractor>(Extractor(std::begin(container), std::end(container)));
}
template<typename Container>
auto from(const Container&& container) = delete; // currently disastrous
inline namespace generators {
struct CounterGenerator : StreamExtractor<CounterGenerator> {
constexpr CounterGenerator(size_t from = 0) : current(from - 1) {}
size_t current;
auto get_impl() noexcept {
return ¤t;
}
bool advance_impl() noexcept {
current++;
return true;
}
};
} // namespace generators
struct generate {
static CONSTEXPR auto counter(size_t from = 0) {
return BaseStreamInterface<CounterGenerator>(CounterGenerator(from));
}
}; // struct generate
} // namespace streams
#endif // !RUST_STREAMS_H