forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_header.hpp
More file actions
5007 lines (4066 loc) · 133 KB
/
single_header.hpp
File metadata and controls
5007 lines (4066 loc) · 133 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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ITERBASE_HPP_
#define ITERBASE_HPP_
// This file consists of utilities used for the generic nature of the
// iterable wrapper classes. As such, the contents of this file should be
// considered UNDOCUMENTED and is subject to change without warning. This
// also applies to the name of the file. No user code should include
// this file directly.
#include <cassert>
#include <cstddef>
#include <functional>
#include <iterator>
#include <optional>
#include <tuple>
#include <type_traits>
#include <utility>
// see gcc bug 87651
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87651
#ifdef __GNUC__
#define NO_GCC_FRIEND_ERROR __GNUC__ < 8
#else
#define NO_GCC_FRIEND_ERROR 1
#endif
namespace iter {
namespace impl {
namespace get_iters {
// begin() for C arrays
template <typename T, std::size_t N>
T* get_begin_impl(T (&array)[N], int) {
return array;
}
// Prefer member begin().
template <typename T, typename I = decltype(std::declval<T&>().begin())>
I get_begin_impl(T& r, int) {
return r.begin();
}
// Use ADL otherwises.
template <typename T, typename I = decltype(begin(std::declval<T&>()))>
I get_begin_impl(T& r, long) {
return begin(r);
}
template <typename T>
auto get_begin(T& t) -> decltype(get_begin_impl(std::declval<T&>(), 42)) {
return get_begin_impl(t, 42);
}
// end() for C arrays
template <typename T, std::size_t N>
T* get_end_impl(T (&array)[N], int) {
return array + N;
}
// Prefer member end().
template <typename T, typename I = decltype(std::declval<T&>().end())>
I get_end_impl(T& r, int) {
return r.end();
}
// Use ADL otherwise.
template <typename T, typename I = decltype(end(std::declval<T&>()))>
I get_end_impl(T& r, long) {
return end(r);
}
template <typename T>
auto get_end(T& t) -> decltype(get_end_impl(std::declval<T&>(), 42)) {
return get_end_impl(t, 42);
}
}
using get_iters::get_begin;
using get_iters::get_end;
template <typename T>
struct type_is {
using type = T;
};
template <typename T>
using AsConst = decltype(std::as_const(std::declval<T&>()));
// iterator_type<C> is the type of C's iterator
// TODO: See bug
// https://developercommunity.visualstudio.com/content/problem/252157/sfinae-error-depends-on-name-of-template-parameter.html
// for why we use T instead of Container. Should be
// changed back to Container when that bug is fixed in
// MSVC.
template <typename T>
using iterator_type = decltype(get_begin(std::declval<T&>()));
// iterator_type<C> is the type of C's iterator
template <typename Container>
using const_iterator_type = decltype(
get_begin(std::declval<const std::remove_reference_t<Container>&>()));
// iterator_deref<C> is the type obtained by dereferencing an iterator
// to an object of type C
template <typename Container>
using iterator_deref = decltype(*std::declval<iterator_type<Container>&>());
// const_iteator_deref is the type obtained through dereferencing
// a const iterator& (note: not a const_iterator). ie: the result
// of Container::iterator::operator*() const
template <typename Container>
using const_iterator_deref =
decltype(*std::declval<const iterator_type<Container>&>());
// the type of dereferencing a const_iterator
template <typename Container>
using const_iterator_type_deref =
decltype(*std::declval<const_iterator_type<Container>&>());
template <typename Container>
using iterator_traits_deref =
std::remove_reference_t<iterator_deref<Container>>;
template <typename T, typename = void>
struct IsIterable : std::false_type {};
// Assuming that if a type works with begin, it is an iterable.
template <typename T>
struct IsIterable<T, std::void_t<iterator_type<T>>> : std::true_type {};
template <typename T>
constexpr bool is_iterable = IsIterable<T>::value;
namespace detail {
template <typename T, typename = void>
struct ArrowHelper {
using type = void;
void operator()(T&) const noexcept {}
};
template <typename T>
struct ArrowHelper<T*, void> {
using type = T*;
constexpr type operator()(T* t) const noexcept {
return t;
}
};
template <typename T>
struct ArrowHelper<T,
std::void_t<decltype(std::declval<T&>().operator->())>> {
using type = decltype(std::declval<T&>().operator->());
type operator()(T& t) const {
return t.operator->();
}
};
template <typename T>
using arrow = typename detail::ArrowHelper<T>::type;
}
// type of C::iterator::operator->, also works with pointers
// void if the iterator has no operator->
template <typename C>
using iterator_arrow = detail::arrow<iterator_type<C>>;
// applys the -> operator to an object, if the object is a pointer,
// it returns the pointer
template <typename T>
detail::arrow<T> apply_arrow(T& t) {
return detail::ArrowHelper<T>{}(t);
}
// For iterators that have an operator* which returns a value
// they can return this type from their operator-> instead, which will
// wrap an object and allow it to be used with arrow
template <typename T>
class ArrowProxy {
private:
using TPlain = typename std::remove_reference<T>::type;
T obj;
public:
constexpr ArrowProxy(T&& in_obj) : obj(std::forward<T>(in_obj)) {}
TPlain* operator->() {
return &obj;
}
};
template <typename, typename = void>
struct is_random_access_iter : std::false_type {};
template <typename T>
struct is_random_access_iter<T,
std::enable_if_t<
std::is_same<typename std::iterator_traits<T>::iterator_category,
std::random_access_iterator_tag>::value>> : std::true_type {};
template <typename T>
using has_random_access_iter = is_random_access_iter<iterator_type<T>>;
// because std::advance assumes a lot and is actually smart, I need a dumb
// version that will work with most things
template <typename InputIt, typename Distance = std::size_t>
void dumb_advance_unsafe(InputIt& iter, Distance distance) {
for (Distance i(0); i < distance; ++i) {
++iter;
}
}
template <typename Iter, typename EndIter, typename Distance>
void dumb_advance_impl(
Iter& iter, const EndIter& end, Distance distance, std::false_type) {
for (Distance i(0); i < distance && iter != end; ++i) {
++iter;
}
}
template <typename Iter, typename EndIter, typename Distance>
void dumb_advance_impl(
Iter& iter, const EndIter& end, Distance distance, std::true_type) {
if (static_cast<Distance>(end - iter) < distance) {
iter = end;
} else {
iter += distance;
}
}
// iter will not be incremented past end
template <typename Iter, typename EndIter, typename Distance = std::size_t>
void dumb_advance(Iter& iter, const EndIter& end, Distance distance) {
dumb_advance_impl(iter, end, distance, is_random_access_iter<Iter>{});
}
template <typename ForwardIt, typename Distance = std::size_t>
ForwardIt dumb_next(ForwardIt it, Distance distance = 1) {
dumb_advance_unsafe(it, distance);
return it;
}
template <typename ForwardIt, typename Distance = std::size_t>
ForwardIt dumb_next(
ForwardIt it, const ForwardIt& end, Distance distance = 1) {
dumb_advance(it, end, distance);
return it;
}
template <typename Container, typename Distance = std::size_t>
Distance dumb_size(Container&& container) {
Distance d{0};
auto end_it = get_end(container);
for (auto it = get_begin(container); it != end_it; ++it) {
++d;
}
return d;
}
template <typename... Ts>
struct are_same : std::true_type {};
template <typename T, typename U, typename... Ts>
struct are_same<T, U, Ts...>
: std::integral_constant<bool,
std::is_same<T, U>::value && are_same<T, Ts...>::value> {};
// DerefHolder holds the value gotten from an iterator dereference
// if the iterate dereferences to an lvalue references, a pointer to the
// element is stored
// if it does not, a value is stored instead
// get() returns a reference to the held item
// get_ptr() returns a pointer to the held item
// reset() replaces the currently held item
template <typename T>
class DerefHolder {
private:
static_assert(!std::is_lvalue_reference<T>::value,
"Non-lvalue-ref specialization used for lvalue ref type");
// it could still be an rvalue reference
using TPlain = std::remove_reference_t<T>;
std::optional<TPlain> item_p_;
public:
using reference = TPlain&;
using pointer = TPlain*;
static constexpr bool stores_value = true;
DerefHolder() = default;
reference get() {
assert(item_p_.has_value());
return *item_p_;
}
pointer get_ptr() {
assert(item_p_.has_value());
return &item_p_.value();
}
void reset(T&& item) {
item_p_.emplace(std::move(item));
}
explicit operator bool() const {
return static_cast<bool>(item_p_);
}
};
// Specialization for when T is an lvalue ref
template <typename T>
class DerefHolder<T&> {
public:
using reference = T&;
using pointer = T*;
private:
pointer item_p_{};
public:
static constexpr bool stores_value = false;
DerefHolder() = default;
reference get() {
assert(item_p_);
return *item_p_;
}
pointer get_ptr() {
assert(item_p_);
return item_p_;
}
void reset(reference item) {
item_p_ = &item;
}
explicit operator bool() const {
return item_p_ != nullptr;
}
};
// allows f(x) to be 'called' as x | f
// let the record show I dislike adding yet another syntactical mess to
// this clown car of a language.
template <typename ItTool>
struct Pipeable {
template <typename T>
friend decltype(auto) operator|(T&& x, const Pipeable& p) {
return static_cast<const ItTool&>(p)(std::forward<T>(x));
}
};
// Pipeable Callable generator, where ItImpl is templated on the first
// argument to the call.
template <template <typename> class ItImpl>
struct IterToolFn : Pipeable<IterToolFn<ItImpl>> {
template <typename T, typename... Ts>
ItImpl<T> operator()(T&& t, Ts... ts) const {
return {std::forward<T>(t), std::move(ts)...};
}
};
// Pipeable callable which allows binding of the first argument
// f(a, b) is the same as b | f(a)
template <typename F>
struct PipeableAndBindFirst : Pipeable<F> {
protected:
template <typename T>
struct FnPartial : Pipeable<FnPartial<T>> {
mutable T stored_arg;
constexpr FnPartial(T in_t) : stored_arg(in_t) {}
template <typename Container>
auto operator()(Container&& container) const {
return F{}(stored_arg, std::forward<Container>(container));
}
};
public:
template <typename T, typename = std::enable_if_t<!is_iterable<T>>>
FnPartial<std::decay_t<T>> operator()(T&& t) const {
return {std::forward<T>(t)};
}
};
// This is a complicated class to generate a callable that can work:
// (1) with just a single (iterable) passed, and DefaultT substituted
// (2) with an iterable and a callable
// (3) with just a callable, to have the iterable passed later via pipe
template <template <typename, typename> class ItImpl, typename DefaultT>
struct IterToolFnOptionalBindFirst
: PipeableAndBindFirst<IterToolFnOptionalBindFirst<ItImpl, DefaultT>> {
private:
using Base =
PipeableAndBindFirst<IterToolFnOptionalBindFirst<ItImpl, DefaultT>>;
protected:
template <typename Container>
auto operator()(Container&& container, std::false_type) const {
return static_cast<const Base&>(*this)(
std::forward<Container>(container));
}
template <typename Container>
auto operator()(Container&& container, std::true_type) const {
return (*this)(DefaultT{}, std::forward<Container>(container));
}
public:
template <typename T>
auto operator()(T&& t) const {
return (*this)(std::forward<T>(t), IsIterable<T>{});
}
template <typename T, typename Container,
typename = std::enable_if_t<is_iterable<Container>>>
ItImpl<T, Container> operator()(T func, Container&& container) const {
return {std::move(func), std::forward<Container>(container)};
}
};
template <template <typename, typename> class ItImpl, typename DefaultT>
struct IterToolFnOptionalBindSecond
: Pipeable<IterToolFnOptionalBindSecond<ItImpl, DefaultT>> {
private:
// T is whatever is being held for later use
template <typename T>
struct FnPartial : Pipeable<FnPartial<T>> {
mutable T stored_arg;
constexpr FnPartial(T in_t) : stored_arg(in_t) {}
template <typename Container>
auto operator()(Container&& container) const {
return IterToolFnOptionalBindSecond{}(
std::forward<Container>(container), stored_arg);
}
};
public:
template <typename Container, typename T>
ItImpl<Container, T> operator()(Container&& container, T func) const {
return {std::forward<Container>(container), std::move(func)};
}
template <typename T, typename = std::enable_if_t<!is_iterable<T>>>
FnPartial<std::decay_t<T>> operator()(T&& func) const {
return {std::forward<T>(func)};
}
template <typename Container,
typename = std::enable_if_t<is_iterable<Container>>>
auto operator()(Container&& container) const {
return (*this)(std::forward<Container>(container), DefaultT{});
}
};
template <template <typename> class ItImpl>
struct IterToolFnBindSizeTSecond { // NOTE not pipeable
private:
using Size = std::size_t;
struct FnPartial : Pipeable<FnPartial> {
Size sz{};
constexpr FnPartial(Size in_sz) : sz{in_sz} {}
template <typename Container>
auto operator()(Container&& container) const {
return IterToolFnBindSizeTSecond{}(
std::forward<Container>(container), sz);
}
};
public:
FnPartial operator()(Size sz) const {
return {sz};
}
template <typename Container,
typename = std::enable_if_t<is_iterable<Container>>>
ItImpl<Container> operator()(Container&& container, Size sz) const {
return {std::forward<Container>(container), sz};
}
};
}
}
#endif
#ifndef ITERTOOLS_ITERATOR_WRAPPER_HPP_
#define ITERTOOLS_ITERATOR_WRAPPER_HPP_
#include <variant>
namespace iter {
namespace impl {
// iterator_end_type<C> is the type of C's end iterator
template <typename Container>
using iterator_end_type = decltype(get_end(std::declval<Container&>()));
template <typename SubIter, typename SubEnd>
class IteratorWrapperImpl;
// If begin and end return the same type, type will be
// iterator_type<Container>
// If begin and end return different types, type will be IteratorWrapperImpl
template <typename Container, bool same_types>
struct IteratorWrapperImplType;
template <typename Container>
struct IteratorWrapperImplType<Container, true>
: type_is<iterator_type<Container>> {};
template <typename Container>
struct IteratorWrapperImplType<Container, false>
: type_is<IteratorWrapperImpl<iterator_type<Container>,
iterator_end_type<Container>>> {};
template <typename Container>
using IteratorWrapper = typename IteratorWrapperImplType<Container,
std::is_same_v<impl::iterator_type<Container>,
impl::iterator_end_type<Container>>>::type;
}
}
template <typename SubIter, typename SubEnd>
class iter::impl::IteratorWrapperImpl {
private:
static_assert(!std::is_same_v<SubIter, SubEnd>);
SubIter& sub_iter() {
auto* sub = std::get_if<SubIter>(&sub_iter_or_end_);
assert(sub);
return *sub;
}
const SubIter& sub_iter() const {
auto* sub = std::get_if<SubIter>(&sub_iter_or_end_);
assert(sub);
return *sub;
}
std::variant<SubIter, SubEnd> sub_iter_or_end_;
public:
IteratorWrapperImpl() : IteratorWrapperImpl(SubIter{}) {}
IteratorWrapperImpl(SubIter&& it) : sub_iter_or_end_{std::move(it)} {}
IteratorWrapperImpl(SubEnd&& it) : sub_iter_or_end_(std::move(it)) {}
IteratorWrapperImpl& operator++() {
++sub_iter();
return *this;
}
decltype(auto) operator*() {
return *sub_iter();
}
decltype(auto) operator*() const {
return *sub_iter();
}
decltype(auto) operator-> () {
return apply_arrow(sub_iter());
}
decltype(auto) operator-> () const {
return apply_arrow(sub_iter());
}
bool operator!=(const IteratorWrapperImpl& other) const {
constexpr static struct : std::not_equal_to<void> {
// specially compare Ends because rangev3 sentinels are not equality
// comparable
bool operator()(const SubEnd&, const SubEnd&) const {
return false;
}
using std::not_equal_to<void>::operator();
} not_equal;
return std::visit(not_equal, sub_iter_or_end_, other.sub_iter_or_end_);
}
bool operator==(const IteratorWrapperImpl& other) const {
return !(*this != other);
}
};
#endif
#ifndef ITERTOOLS_ITER_TUPLES_HPP_
#define ITERTOOLS_ITER_TUPLES_HPP_
namespace iter {
namespace impl {
namespace detail {
template <typename... Ts>
std::tuple<iterator_deref<Ts>...> iterator_tuple_deref_helper(
const std::tuple<Ts...>&);
template <typename... Ts>
std::tuple<IteratorWrapper<Ts>...> iterator_tuple_type_helper(
const std::tuple<Ts...>&);
template <typename... Ts>
std::tuple<iterator_deref<const std::remove_reference_t<Ts>>...>
const_iterator_tuple_deref_helper(const std::tuple<Ts...>&);
template <typename... Ts>
std::tuple<IteratorWrapper<const std::remove_reference_t<Ts>>...>
const_iterator_tuple_type_helper(const std::tuple<Ts...>&);
}
// Given a tuple template argument, evaluates to a tuple of iterators
// for the template argument's contained types.
template <typename TupleType>
using iterator_tuple_type =
decltype(detail::iterator_tuple_type_helper(std::declval<TupleType>()));
template <typename TupleType>
using const_iterator_tuple_type = decltype(
detail::const_iterator_tuple_type_helper(std::declval<TupleType>()));
// Given a tuple template argument, evaluates to a tuple of
// what the iterators for the template argument's contained types
// dereference to
template <typename TupleType>
using iterator_deref_tuple = decltype(
detail::iterator_tuple_deref_helper(std::declval<TupleType>()));
template <typename TupleType>
using const_iterator_deref_tuple = decltype(
detail::const_iterator_tuple_deref_helper(std::declval<TupleType>()));
// function absorbing all arguments passed to it. used when
// applying a function to a parameter pack but not passing the evaluated
// results anywhere
template <typename... Ts>
void absorb(Ts&&...) {}
}
}
#endif
#ifndef ITERATOR_ITERATOR_HPP_
#define ITERATOR_ITERATOR_HPP_
// IterIterWrapper and IteratorIterator provide a means to have a container
// of iterators act like a container of the pointed to objects. This is useful
// for combinatorics and similar itertools which need to keep track of
// more than one element at a time.
// an IterIterWrapper<some_collection_type<collection<T>::iterator>>
// behave like some_collection<T> when iterated over or indexed
namespace iter {
namespace impl {
template <typename T, typename = void>
struct HasConstDeref : std::false_type {};
template <typename T>
struct HasConstDeref<T, std::void_t<decltype(*std::declval<const T&>())>>
: std::true_type {};
template <typename TopIter>
class IteratorIterator {
template <typename> friend class IteratorIterator;
using Diff = std::ptrdiff_t;
static_assert(
std::is_same<
typename std::iterator_traits<TopIter>::iterator_category,
std::random_access_iterator_tag>::value,
"IteratorIterator only works with random access iterators");
private:
TopIter sub_iter;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = std::remove_reference_t<decltype(**std::declval<TopIter>())>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
IteratorIterator() = default;
IteratorIterator(const TopIter& it) : sub_iter{it} {}
const TopIter& get() const {
return sub_iter;
}
template <typename T>
bool operator==(const IteratorIterator<T>& other) const {
return !(*this != other);
}
template <typename T>
bool operator!=(const IteratorIterator<T>& other) const {
return this->sub_iter != other.sub_iter;
}
IteratorIterator& operator++() {
++this->sub_iter;
return *this;
}
IteratorIterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
IteratorIterator& operator--() {
--this->sub_iter;
return *this;
}
IteratorIterator operator--(int) {
auto ret = *this;
--*this;
return ret;
}
auto operator*() const -> decltype(**sub_iter) {
return **this->sub_iter;
}
auto operator-> () const -> decltype(*sub_iter) {
return *this->sub_iter;
}
IteratorIterator& operator+=(Diff n) {
this->sub_iter += n;
return *this;
}
IteratorIterator operator+(Diff n) const {
auto it = *this;
it += n;
return it;
}
friend IteratorIterator operator+(Diff n, IteratorIterator it) {
it += n;
return it;
}
IteratorIterator& operator-=(Diff n) {
this->sub_iter -= n;
return *this;
}
IteratorIterator operator-(Diff n) const {
auto it = *this;
it -= n;
return it;
}
Diff operator-(const IteratorIterator& rhs) const {
return this->sub_iter - rhs.sub_iter;
}
auto operator[](Diff idx) const -> decltype(*sub_iter[idx]) {
return *sub_iter[idx];
}
bool operator<(const IteratorIterator& rhs) const {
return this->sub_iter < rhs.sub_iter;
}
bool operator>(const IteratorIterator& rhs) const {
return this->sub_iter > rhs.sub_iter;
}
bool operator<=(const IteratorIterator& rhs) const {
return this->sub_iter <= rhs.sub_iter;
}
bool operator>=(const IteratorIterator& rhs) const {
return this->sub_iter >= rhs.sub_iter;
}
};
template <typename Container>
class IterIterWrapper {
private:
Container container;
using contained_iter = typename Container::value_type;
using size_type = typename Container::size_type;
using iterator = IteratorIterator<typename Container::iterator>;
using const_iterator =
IteratorIterator<typename Container::const_iterator>;
using reverse_iterator =
IteratorIterator<typename Container::reverse_iterator>;
using const_reverse_iterator =
IteratorIterator<typename Container::const_reverse_iterator>;
template <typename U = Container, typename = void>
struct ConstAtTypeOrVoid : type_is<void> {};
template <typename U>
struct ConstAtTypeOrVoid<U,
std::void_t<decltype(*std::declval<const U&>().at(0))>>
: type_is<decltype(*std::declval<const U&>().at(0))> {};
using const_at_type_or_void_t = typename ConstAtTypeOrVoid<>::type;
template <typename U = Container, typename = void>
struct ConstIndexTypeOrVoid : type_is<void> {};
template <typename U>
struct ConstIndexTypeOrVoid<U,
std::void_t<decltype(*std::declval<const U&>()[0])>>
: type_is<decltype(*std::declval<const U&>()[0])> {};
using const_index_type_or_void_t = typename ConstIndexTypeOrVoid<>::type;
public:
IterIterWrapper() = default;
explicit IterIterWrapper(size_type sz) : container(sz) {}
IterIterWrapper(size_type sz, const contained_iter& val)
: container(sz, val) {}
auto at(size_type pos) -> decltype(*container.at(pos)) {
return *container.at(pos);
}
auto at(size_type pos) const -> const_at_type_or_void_t {
return *container.at(pos);
}
auto operator[](size_type pos) noexcept(noexcept(*container[pos]))
-> decltype(*container[pos]) {
return *container[pos];
}
auto operator[](size_type pos) const noexcept(noexcept(*container[pos]))
-> const_index_type_or_void_t {
return *container[pos];
}
bool empty() const noexcept {
return container.empty();
}
size_type size() const noexcept {
return container.size();
}
iterator begin() noexcept {
return {container.begin()};
}
iterator end() noexcept {
return {container.end()};
}
const_iterator begin() const noexcept {
return {container.begin()};
}
const_iterator end() const noexcept {
return {container.end()};
}
const_iterator cbegin() const noexcept {
return {container.cbegin()};
}
const_iterator cend() const noexcept {
return {container.cend()};
}
reverse_iterator rbegin() noexcept {
return {container.rbegin()};
}
reverse_iterator rend() noexcept {
return {container.rend()};
}
const_reverse_iterator rbegin() const noexcept {
return {container.rbegin()};
}
const_reverse_iterator rend() const noexcept {
return {container.rend()};
}
const_reverse_iterator crbegin() const noexcept {
return {container.rbegin()};
}
const_reverse_iterator crend() const noexcept {
return {container.rend()};
}
// get() exposes the underlying container. this allows the
// itertools to manipulate the iterators in the container
// and should not be depended on anywhere else.
Container& get() noexcept {
return container;
}
const Container& get() const noexcept {
return container;
}
};
}
}
#endif
#ifndef ITER_FILTER_H_
#define ITER_FILTER_H_
#include <initializer_list>
namespace iter {
namespace impl {
template <typename FilterFunc, typename Container>
class Filtered;
struct BoolTester {
template <typename T>
constexpr bool operator()(const T& item_) const {
return bool(item_);
}
};
using FilterFn = IterToolFnOptionalBindFirst<Filtered, BoolTester>;
}
constexpr impl::FilterFn filter{};
}
template <typename FilterFunc, typename Container>
class iter::impl::Filtered {
private:
Container container_;
mutable FilterFunc filter_func_;
friend FilterFn;
protected:
// Value constructor for use only in the filter function
Filtered(FilterFunc filter_func, Container&& container)
: container_(std::forward<Container>(container)),
filter_func_(filter_func) {}
public:
Filtered(Filtered&&) = default;
template <typename ContainerT>
class Iterator {
private:
template <typename>
friend class Iterator;
using Holder = DerefHolder<iterator_deref<ContainerT>>;
mutable IteratorWrapper<ContainerT> sub_iter_;
IteratorWrapper<ContainerT> sub_end_;
mutable Holder item_;
FilterFunc* filter_func_;
// All of these are marked const because the sub_iter_ is lazily
// initialized. The morality of this is questionable.
void inc_sub_iter() const {
++sub_iter_;
if (sub_iter_ != sub_end_) {
item_.reset(*sub_iter_);
}
}
// increment until the iterator points to is true on the
// predicate. Called by constructor and operator++
void skip_failures() const {
while (
sub_iter_ != sub_end_ && !std::invoke(*filter_func_, item_.get())) {
inc_sub_iter();
}
}
void init_if_first_use() const {
if (!item_ && sub_iter_ != sub_end_) {
item_.reset(*sub_iter_);
skip_failures();
}
}
public:
using iterator_category = std::input_iterator_tag;
using value_type = iterator_traits_deref<ContainerT>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator(IteratorWrapper<ContainerT>&& sub_iter,
IteratorWrapper<ContainerT>&& sub_end, FilterFunc& filter_func)
: sub_iter_{std::move(sub_iter)},
sub_end_{std::move(sub_end)},
filter_func_(&filter_func) {}