-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorange.hh
1877 lines (1599 loc) · 65.9 KB
/
orange.hh
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
/*
* orange - yet another range library
*
* By Aaron McDaid - [email protected]
*
*
* In the code below, I'm trying to organize it so that it can be
* read from top to bottom and is understandable.
*
* Where possible, I put some tests into a 'testing_namespace'. You might
* find it useful to just search for that string in this file and read
* the tests.
*
*
* Brief description, and overview of this code
* ============================================
*
* ( This documentation includes some stuff that isn't implemented. We
* should implement more! )
*
* vector<int> v {2,3,5,7};
*
* // print every value
* v |foreach| [](auto x) { std::cout << x << '\n'; };
*
* // print the square of each value
* v |mapr| [](auto x) { return x*x; }
* |foreach| [](auto y) { std:: cout "x^2=" << y << '\n';};
*
* // filter to only the odd ones, then print them:
* v |filter| [](auto x) { return x%2 == 1; }
* |foreach| [](auto x) { std::cout << x << '\n'; };
*
* ( say 'mapr' instead of 'map' simply to avoid clashing with 'std::map')
*
* Many different types can be considered as 'range types'. The obvious
* example is a pair of iterators, but there are many others too.
* A 'vector' is not itself a range; but it is trivially convertable
* to a range.
*
* A 'range' is typically a very lightweight object, like a pointer, that
* can be copied easily. It doesn't usually "own" the underlying data,
* but this library supports ownership where appropriate.
*
* A range will support some subset of these actions:
* - empty ::: No more input is available to read
* - front_val ::: Read the current value - repeated calls will return
* the same value (unless the underlying container has
* been modified by some other part of the system.
* - advance ::: skip the current item and move to the next
*
* - full ::: if an output range can no longer be written to
* - front_ref ::: return a reference to the current item. Repeated
* calls will return a reference to the same object.
* - push ::: write a value to an output range and advance. This
* is useful when treating the standard output as an
* output range. It's not possible to define 'front_ref'
* on such a range as we can't meaningful write to the
* "same place" in the output repeatedly. Once we write
* to the stream, our next write must be to the following
* 'position' in the output range.
* - pull ::: return the current value and also advance. As if
* running front_val and then advance. Useful when a
* range doesn't allow repeating read
*
* Via traits (see below), you can specify, for your own types, how these
* actions are to be performed on your objects.
* These names are all available in the orange:: namespace. They will use
* the underlying traits actions where they are provided and, in some cases,
* this library can synethesize extra functions where they are not explicit
* in your trait; for example, we can synthesize 'orange::pull' from 'front_val'
* and 'advance' if your trait does not contain 'advance'
*
* This becomes useful when you want to combine a range and a function,
* and create a new range which exposes range where the function has been
* applied to each element of the underlying range.
*
* ==
*
* A 'range type', R, here is a type for which the type traits<R> exists.
* More precisely, traits<R> can also be default constructed. The traits
* object has no state, its purpose is simply to record the type-specific
* details. For example, an input range must be able to support the 'orange::empty'
* function which tells us if more input is available. For a pair of iterators,
* this means testing the two iterators to see if they are equal. For a file
* input stream, we test the stream for end-of-file.
*
* (I'll try to document the functions in the order they appear below in
* the code)
*
* is_range_v ::: constexpr-bool function to test if a given
* type R has a suitable traits<R>
*
* The code then has the traits definition for a std::pair of iterators.
* Traits for other types are specified later in this code, but I brought
* std::pair to the top as it's simple and helps me to explain this system
*
* template<typename I>
* struct traits<std:: pair<I,I>>
*
* For now, this just means providing 'empty', 'advance' and 'front_val'
* In future, some functions for trait a pair as an output range should
* be added, such as 'full' and 'front_ref'.
*
* Next, the functions in 'orange::' are defined, relying on the operations
* provided in the traits object. For example, this defines 'orange::front_val':
*
* template<typename R>
* auto front_val (R const &r)
* ->decltype(traits<R>::front_val(r)) {
* return traits<R>::front_val(r); }
*
* Another overload of 'orange::front_val' could be provided to synthesize
* front_val where the traits has 'front_ref', but not 'front_val'.
*
*/
#include<utility>
#include<functional>
#include<algorithm> // for std::min
#include<tuple>
#include<vector>
#include<limits>
#include<memory>
/* SFINAE_ENABLE_IF_CHECK
* ======================
* Sorry for the macro, but this macro is just too useful! It has two
* extra 'features' over and above a straightforward enable_if.
*
* Here is a simple conventional use of enable_if in a function template:
*
* template<typename T
* , std::enable_if_t< std::is_lvalue_reference<T>{} >* =nullptr
* >
* bool i_am_an_lvalue(T&&)
* { return true; }
*
* and I suggest this instead:
*
* template<typename T
* , SFINAE_ENABLE_IF_CHECK( std::is_lvalue_reference<T>{} )
* >
* bool i_am_an_lvalue(T&&)
* { return true; }
*
* for these two reasons:
* 1) two overloads with very similar signatures can clash with each other,
* even when we know only one will satisfy the condition, causing an error.
* This macro solves this by building the line number (__LINE__) into the
* pointer type.
* 2) If the condition is not deduced, then the conventional method will give
* an error. For example, if it's a template parameter in an outer struct.
* This macro solves this by separating the condition into two template
* parameters (hence the comma in the macro, to separate the args).
* Hence, the previous code expands to the following, which works because
* the boolean is merely given a default value, which is then tested:
*
* template<typename T
* bool hidden_test_expression_for_enable_if = ( std::is_lvalue_reference<T>{} )
* , std::enable_if_t<hidden_test_expression_for_enable_if, std:: integral_constant<size_t, __LINE__> >* =nullptr
* >
* bool i_am_an_lvalue(T&&)
* { return true; }
*/
#define SFINAE_ENABLE_IF_CHECK(...) \
typename ... \
, bool hidden_test_expression_for_enable_if = (__VA_ARGS__) \
, std::enable_if_t<hidden_test_expression_for_enable_if, std:: integral_constant<size_t, __LINE__> >* =nullptr
/*
* orange_utils
*
* I define 'is_invokable_v' in this namespace as it's range specific and might be useful elsewhere.
*/
namespace orange_utils {
/* priority_tag
* ============
* 'priority_tag' is very useful to specify priority
* among overloads that would otherwise be ambiguous.
* https://stackoverflow.com/questions/43470741/how-does-eric-nieblers-implementation-of-stdis-function-work
*/
template<int i>
struct priority_tag;
template<int i>
struct priority_tag : public priority_tag<i-1> {};
template<>
struct priority_tag<0> {};
/* void_t
* ======
* https://stackoverflow.com/questions/27687389/how-does-void-t-work
*/
template< typename ... >
struct voider_t { using type = void; };
template< typename ... Ts> using void_t = typename voider_t<Ts...> :: type;
/* is_invokable_v
* ==============
* is_invokable_v<F, Args...> tells us if the function object F
* can be called with arguments of types Args...
*/
namespace impl__is_invokable {
template<typename F, typename ... Args>
constexpr auto
is_invokable_one_overload(orange_utils::priority_tag<2>)
-> decltype( std::declval<F>()(std::declval<Args>()...), true )
{ return true; }
template<typename F, typename ... Args>
constexpr auto
is_invokable_one_overload(orange_utils::priority_tag<1>)
-> decltype( false )
{ return false; }
template<typename F, typename ... Args>
constexpr bool
is_invokable_v =
is_invokable_one_overload<F, Args...>(orange_utils::priority_tag<9>{});
}
using impl__is_invokable:: is_invokable_v; // to 'export' this to the orange_utils namespace
template<typename ... Ts>
constexpr
void ignore(Ts && ...) {}
/* testing_namespace
* =================
* Throughout this file, I'll put tests, using static_assert, into this
* namespace. Reading the tests might help you to understand more of
* this code.
*/
namespace testing_namespace {
/*
* To make a tester which checks if a give type has a '.size()' method, we define a lambda with the
* relevant expression 'x.size()'. And also, we test if addition, (x+x), is defined.
*/
auto checker_for__has_size_method = [](auto&&x)->decltype(void( x.size() )){};
auto checker_for__has_addition = [](auto&&x)->decltype(void( x + x )){};
template<typename Arg>
constexpr bool has_size_method = orange_utils:: is_invokable_v<decltype(checker_for__has_size_method), Arg >;
template<typename Arg>
constexpr bool has_addition = orange_utils:: is_invokable_v<decltype(checker_for__has_addition), Arg >;
static_assert( has_size_method< std::vector<int> > ,"");
static_assert(!has_size_method< int > ,"");
static_assert( has_size_method< std::vector<int> > ,"");
static_assert(!has_size_method< int > ,"");
}
template <class F, std::size_t... I>
constexpr decltype(auto) apply_indices(F&& f, std::index_sequence<I...>)
{
return std::forward<F>(f)
(std::integral_constant<size_t,I>{}...);
}
template<typename ... Ts>
constexpr
std::tuple<Ts...> // values and l-refs, but not r-ref
mk_tuple(Ts && ... ts) {
return std::tuple<Ts...>{ std::forward<Ts>(ts)... };
}
template<typename T>
struct remove_RVALUE_reference
{ using type = T; };
template<typename T>
struct remove_RVALUE_reference<T&&>
{ using type = T; };
template<typename T>
auto non_rref(T&& t)
-> typename remove_RVALUE_reference<T>::type
{ return t; }
}
namespace orange {
/* traits<R>
* =========
* If 'R' is a range type, then this traits class tells us
* how to use it; how to test if it's empty, for example.
* With a pair of iterators, we test for emptiness by testing
* if the two iterators equal to each other. With a file input
* stream, we would test for emptiness by testing for .eof().
*/
template<typename R, typename = void> // second template arg is to allow 'void_t' https://stackoverflow.com/questions/27687389/how-does-void-t-work
struct traits;
/* lookup_traits<R>
* ================
* We don't look up 'traits' directly. We go through 'lookup_traits'
* instead, as it drops 'const' and drops references.
*/
template<typename R
, typename R_decayed = std::decay_t<R>
, decltype( traits< R_decayed> {} ) * = nullptr >
struct lookup_traits : public traits<R_decayed> {};
/* checker_for__is_range is_range_v
* ===================== ==========
* is_range_v<R> tests if lookup_traits<R> is defined.
* This is how we define is a type is a range type or not.
*/
auto checker_for__is_range=[](auto&&x)->decltype(void( lookup_traits< decltype(x)>{} )){};
template<typename T > constexpr bool
is_range_v = orange_utils:: is_invokable_v<decltype(checker_for__is_range), T>;
/* has_trait_{empty,advance,front,pull}
* ==================================================
* In order to 'synthesize' the user-facing functions ( orange::front, orange::empty, and so on )
* for a range type R, we need a convenient way to check which functions are provided in the trait<R>.
* These are the 'has_trait_*' functions defined here:
*/
auto checker_for__has_trait_empty = [](auto&&r)->decltype(void( lookup_traits<decltype(r)>::empty (r) )){};
auto checker_for__has_trait_advance = [](auto&&r)->decltype(void( lookup_traits<decltype(r)>::advance (r) )){};
auto checker_for__has_trait_front = [](auto&&r)->decltype(void( lookup_traits<decltype(r)>::front (r) )){};
auto checker_for__has_trait_pull = [](auto&&r)->decltype(void( lookup_traits<decltype(r)>::pull (r) )){};
template<typename R> constexpr bool
has_trait_empty = orange_utils:: is_invokable_v<decltype(checker_for__has_trait_empty), R>;
template<typename R> constexpr bool
has_trait_advance = orange_utils:: is_invokable_v<decltype(checker_for__has_trait_advance), R>;
template<typename R> constexpr bool
has_trait_front = orange_utils:: is_invokable_v<decltype(checker_for__has_trait_front ), R>;
template<typename R> constexpr bool
has_trait_pull = orange_utils:: is_invokable_v<decltype(checker_for__has_trait_pull), R>;
/*
* Users will never call the functions in the trait object directly.
* Instead, we synthesize all the functions, where possible, such
* as orange:empty, orange::front, orange::advance.
*
* This design allows us to synthesize extra functions. For example,
* if a trait has 'front' and 'advance', but not 'pull', then we
* are still able to synthesize 'orange::pull' using the first two.
* This allows each trait to focus on the smallest subset of
* necessary behaviour.
*/
// just one overload for 'empty'
template<typename R>
auto constexpr
empty (R &r)
->decltype(lookup_traits<R>::empty(r))
{ return lookup_traits<R>::empty(r); }
// one overload for 'advance'
template<typename R>
auto constexpr
advance (R &r)
->decltype(lookup_traits<R>::advance(r))
{ return lookup_traits<R>::advance(r); }
// 'front'
template<typename R>
auto constexpr
front (R &r)
->decltype(lookup_traits<R>::front(r))
{ return lookup_traits<R>::front(r); }
/* Next, we see 'begin' and 'end', which are useful
* for working with range-based for.
*
* TODO: synthesize a suitable pair of iterators
* for range types that don't specify a begin and
* end of their own.
*/
// one overload for 'begin'
template<typename R>
auto constexpr
begin (R &r)
->decltype(lookup_traits<R>::begin (r))
{ return lookup_traits<R>::begin (r); }
// one overload for 'end'
template<typename R>
auto constexpr
end (R &r)
->decltype(lookup_traits<R>::end (r))
{ return lookup_traits<R>::end (r); }
/* Two overloads for 'pull'.
* 1. has 'pull' in its trait
* 2. doesn't have 'pull' but does have 'front' and 'advance'
*/
// pull, via trait_pull
template<typename R
, SFINAE_ENABLE_IF_CHECK( has_trait_pull<R&> )
>
auto constexpr
pull (R &r)
{ return lookup_traits<R>::pull (r); }
// pull, via front
template<typename R
, SFINAE_ENABLE_IF_CHECK(( !has_trait_pull <R&> && has_trait_front<R&> && has_trait_advance<R&>
&& !std::is_same<void, decltype( lookup_traits<R>::front(std::declval<R&>()) )>{}
))
>
auto constexpr // TODO: proper as_value here, to convert nested refs (inside tuples) to values
pull (R &r)
{
auto copy = lookup_traits<R>::front(r);
lookup_traits<R>::advance(r);
return copy;
}
// void version of pull, via front
template<typename R
, SFINAE_ENABLE_IF_CHECK( !has_trait_pull <R&> && has_trait_front<R&> && has_trait_advance<R&>
&& std::is_same<void, decltype( lookup_traits<R>::front(std::declval<R&>()) )>{}
)
>
auto constexpr
pull (R &r)
-> void
{
lookup_traits<R>::front(r);
lookup_traits<R>::advance(r);
}
}
/*
* Everything above is very general. It's relevant for all range
* types. Next, we get more specific, by defining some traits
* and the methods.
*
* The functions in the trait are static, and capture the range
* as R&, where R is a deduced template parameter.
*/
namespace orange {
// Let's start with the simplest example - a std::pair of iterators
template<typename I, typename J>
struct traits<std:: pair<I,J>> {
template<typename R> static constexpr
bool
empty (R & r) { return r.first == r.second ;}
template<typename R> static
void
advance (R & r) { ++ r.first ;}
template<typename R> static constexpr
decltype(auto)
front (R & r) { return * r.first ;}
};
namespace testing_namespace {
static_assert(is_range_v< std::pair< std::vector<int>::iterator, std::vector<int>::iterator> >, "");
static_assert(is_range_v< std::pair<int*, int*> >, "");
static_assert( has_trait_empty < std::pair<int*, int*> > , "");
static_assert( has_trait_front < std::pair<int*, int*> > , "");
static_assert(!has_trait_front < std::vector<int> > , "");
}
/*
* orange_traits_are_static_here
* =============================
* Writing separate traits classes for every type might be annoying. If
* you have control over your own type, you may choose to place the
* definitions of the functions directly in your class. To do so, you need
* to create a typedef in your class called 'orange_traits_are_static_here'
* with type 'orange ::orange_traits_are_static_here', and then provide
* the *static* methods you want (named 'orange_empty', 'orange_front',
* and so on). To see an example of this, just scroll down to the
* 'mapping_range' template below.
*
* Immediately following this comment is the traits class which tests
* for the appropriate typedef and knows how to forward all such calls to
* the appropriate function.
*
*/
struct orange_traits_are_static_here{};
template<typename T>
struct traits < T
, orange_utils:: void_t< std:: enable_if_t<
std:: is_same<
typename T::orange_traits_are_static_here
, orange ::orange_traits_are_static_here
>{}
> >> {
template<typename R> static constexpr auto
empty (R & r)
->decltype(R:: orange_empty (r))
{ return R:: orange_empty (r); }
template<typename R> static constexpr auto
advance (R & r)
->decltype(R:: orange_advance (r))
{ return R:: orange_advance (r); }
template<typename R> static constexpr auto
front (R & r)
->decltype(R:: orange_front (r))
{ return R:: orange_front (r); }
template<typename R> static constexpr auto
pull (R & r)
->decltype(R:: orange_pull (r))
{ return R:: orange_pull (r); }
template<typename R> static constexpr auto
begin (R & r)
->decltype(R:: orange_begin (r))
{ return R:: orange_begin (r); }
template<typename R> static constexpr auto
end (R & r)
->decltype(R:: orange_end (r))
{ return R:: orange_end (r); }
};
}
namespace orange {
/*
* 'pair_of_values', so that we can range between a pair of numbers. See
* the 'ints' function below.
*
* This is related to 'iter_is_own_value', which is what we get if we call 'begin'
* and 'end' on a 'pair_of_values'.
*/
namespace impl {
template<typename I>
struct iter_is_own_value {
I m_i;
bool operator!= (iter_is_own_value const & other) const { return m_i != other.m_i; }
void operator++ () { ++m_i; }
I operator* () const { return m_i; }
};
}
template<typename T>
struct pair_of_values
{
T m_begin;
T m_end;
using orange_traits_are_static_here = orange:: orange_traits_are_static_here;
template<typename R> static constexpr
bool orange_empty (R & r) { return r.m_begin == r.m_end ;}
template<typename R> static constexpr
T orange_front (R & r) { return r.m_begin; }
template<typename R> static constexpr
void orange_advance (R & r) { ++ r.m_begin; }
template<typename R> static constexpr
auto orange_begin (R & r) { return impl:: iter_is_own_value<T>{r.m_begin};}
template<typename R> static constexpr
auto orange_end (R & r) { return impl:: iter_is_own_value<T>{r.m_end };}
};
struct intsFrom0_t
{
constexpr
pair_of_values<int>
operator() (int u) const
{ return {0,u}; }
constexpr intsFrom0_t(){}
} constexpr intsFrom0;
inline
constexpr
pair_of_values<int> ints(int u) { return {0,u}; }
inline
constexpr
pair_of_values<int> ints(int l, int u) { return {l,u}; }
inline
constexpr
pair_of_values<int> ints() { return {0,std::numeric_limits<int>::max()}; }
template<typename T>
struct replicate_t
{
int64_t m_n;
T m_t;
static_assert(!std::is_reference<T>{} ,"");
using orange_traits_are_static_here = orange:: orange_traits_are_static_here;
template<typename R> static constexpr auto
orange_empty (R &r) ->bool { return r.m_n <= 0;}
template<typename R> static constexpr auto
orange_advance (R &r) ->void { --r.m_n; }
template<typename R> static constexpr auto
orange_front (R &r) ->T { return r.m_t; }
};
template<typename T>
auto constexpr
replicate(int64_t n, T t)
{ return replicate_t<T>{n,std::move(t)}; }
template<typename T>
auto constexpr
repeat (T t)
{ return replicate_t<T>{std::numeric_limits<int64_t>::max(),std::move(t)}; }
/*
* Next, a 'pair_of_iterators' type in the orange:: namespace. The main (only?)
* reason for this (as opposed to an std::pair of iterators) is to allow
* 'begin' and 'end' to be defined appropriately, allowing for(auto x : r) to
* work. This is the class used when applying thing like '|'
* It's in the 'orange::' namespace, so 'orange::begin' and 'orange::end'
* can be found by ADL.
*/
template<typename B, typename E>
struct pair_of_iterators : public std::pair<B,E>
{
static_assert(!std::is_reference<B>{}, "");
static_assert(!std::is_reference<E>{}, "");
using std:: pair<B,E> :: pair; // to inherit the constructors
};
/*
* as_range
* Converts a non-range to a range, where appropriate. The obvious examples
* are a container such as 'std::vector' or 'std::list'. as_range can also
* be called with two iterators.
*/
// already a range? Just return as is
template<typename T
, SFINAE_ENABLE_IF_CHECK( is_range_v<T> )
>
constexpr auto
as_range(T &&t)
->decltype(std::forward<T>(t))
{ return std::forward<T>(t); }
// a container with begin and end. The typical use of 'as_range'
template <typename T>
auto constexpr
as_range(T &v)
-> pair_of_iterators< decltype(v.begin()) , decltype(v.end ()) >
{
static_assert(!is_range_v<T> ,"");
return {v.begin(),v.end()};
}
// an array
template <typename T, std:: size_t N>
auto constexpr
as_range(T (&v)[N])
-> pair_of_iterators< T*, T* >
{ return {std::begin(v),std::end(v)}; }
template<typename T, size_t N>
struct owning_range_for_ye_olde_C_array {
T m_array[N];
size_t m_offset;
// don't allow this to be copied
owning_range_for_ye_olde_C_array (owning_range_for_ye_olde_C_array const &) = delete;
owning_range_for_ye_olde_C_array & operator= (owning_range_for_ye_olde_C_array const &) = delete;
// ... but allow moving
constexpr
owning_range_for_ye_olde_C_array (owning_range_for_ye_olde_C_array &&) = default;
constexpr
owning_range_for_ye_olde_C_array & operator= (owning_range_for_ye_olde_C_array &&) = default;
using orange_traits_are_static_here = orange:: orange_traits_are_static_here;
template<typename M> static constexpr auto
orange_empty (M &m) ->bool { return m.m_offset >= N;}
template<typename M> static constexpr auto
orange_advance (M &m) ->void { ++m.m_offset; }
template<typename M> static constexpr auto
orange_front (M &m) ->decltype(auto) { return m.m_array[m.m_offset]; }
};
// an rvalue array. We need a helper function first though:
template <typename T, std:: size_t N, std:: size_t ... Indices>
auto constexpr
as_range_helper_for_rvalue_plain_C_arrays ( T (&&a)[N]
, std::index_sequence<Indices...>
)
-> owning_range_for_ye_olde_C_array<T,N>
{ return {{ std::move(a[Indices]) ... },0}; }
// for rvalue-array, we copy the array via the helper above and then we
// can store it in a special type, 'owning_range_for_ye_olde_C_array',
// that knows how to place nice inside 'constexpr'.
template <typename T, std:: size_t N>
auto constexpr
as_range(T (&&a)[N])
-> owning_range_for_ye_olde_C_array<T,N>
{
return as_range_helper_for_rvalue_plain_C_arrays(std::move(a), std::make_index_sequence<N>());
}
// two iterators as arguments
template <typename T>
auto constexpr
as_range(T b, T e)
->decltype(pair_of_iterators< decltype(b) , decltype(e) > {b,e})
{ return {b,e}; }
template<typename B, typename E>
struct traits<pair_of_iterators<B,E>> {
template<typename R> static constexpr
bool
empty (R & r) { return r.first == r.second ;}
template<typename R> static constexpr
void
advance (R & r) { ++ r.first ;}
template<typename R> static constexpr
decltype(auto)
front (R & r) { return * std::forward<R>(r) .first ;}
template<typename R> static constexpr
auto begin (R & r) { return r.first; }
template<typename R> static constexpr
auto end (R & r) { return r.second; }
};
template<typename C>
struct owning_range { // non-copyable
static_assert(!std::is_reference<C>{} ,"");
static_assert(!is_range_v<C> ,"");
using R = decltype( as_range(std::declval<C&>()) );
static_assert(!std::is_reference<R>{} ,"");
static_assert( is_range_v<R> ,"");
C m_c;
R m_r;
constexpr
owning_range (C && c) // takes only an rvalue reference, *not* a universal reference
: m_c(std::move(c)) , m_r( as_range(m_c) )
{}
// don't allow this to be copied
owning_range (owning_range const &) = delete;
owning_range & operator= (owning_range const &) = delete;
// ... but allow moving
constexpr
owning_range (owning_range &&) = default;
constexpr
owning_range & operator= (owning_range &&) = default;
using orange_traits_are_static_here = orange:: orange_traits_are_static_here;
template<typename M> static constexpr auto
orange_empty (M &m) ->bool
{ return orange:: empty(m.m_r);}
template<typename M> static constexpr auto
orange_advance (M &m) ->void
{ orange::advance( m.m_r ) ; }
template<typename M> static constexpr auto
orange_front (M &m) ->decltype(auto)
{ return orange::front ( m.m_r ) ;}
};
// as_range, for rvalues that aren't ranges. In this case, we wrap them
// up on owning_range. It's non-copyable, that's how it maintains the
// semantics of ranges.
template<typename T
, SFINAE_ENABLE_IF_CHECK( !std::is_reference<T>{} && !is_range_v<T> )
>
auto constexpr
as_range(T &&t) // enable_if is used here, to ensure it is only an rvalue
{
return owning_range<T>{ std::forward<T>(t) };
}
/*
* Above, all the basic underlying technology for a range has
* been defined. Now, the 'user-facing' code must be implemented,
* allowing |mapr| and |filter| and so on.
*
* v |foreach| [](auto x){ std::cout << x << '\n'; }
*
* The above works because we overload the '|' operator. 'foreach'
* is an object of an empty tag type. Therefore v|foreach is
* a valid expression which doesn't do much except capture
* a copy of the range object
* Then, via another overload of | , we apply the lambda.
* So, the above can be read as
*
* (v | foreach) | [](auto x){ std::cout << x << '\n'; }
*/
template<typename Tag_type>
struct tagger_t {
constexpr tagger_t() {} // clang-3.8.0 insists on a user-provided default constructor
};
struct filter_tag_t {}; constexpr tagger_t<filter_tag_t > filter;
struct map_tag_t {}; constexpr tagger_t<map_tag_t > map_range;
constexpr tagger_t<map_tag_t > mapr;
struct foreach_tag_t {}; constexpr tagger_t<foreach_tag_t > foreach;
struct collect_tag_t{constexpr collect_tag_t(){}};
constexpr collect_tag_t collect; // no need for 'tagger_t', this directly runs
struct discard_collect_tag_t{constexpr discard_collect_tag_t(){}};
constexpr discard_collect_tag_t discard_collect; // no need for 'tagger_t', this directly runs
struct accumulate_tag_t{constexpr accumulate_tag_t(){}};
constexpr accumulate_tag_t accumulate; // no need for 'tagger_t', this directly runs
struct concat_tag_t{constexpr concat_tag_t(){}};
constexpr concat_tag_t concat; // no need for 'tagger_t', this directly runs
struct memoize_tag_t{constexpr memoize_tag_t(){}};
constexpr memoize_tag_t memoize; // no need for 'tagger_t', this directly runs
template<size_t max_size>
struct collect_at_most_tag_t{constexpr collect_at_most_tag_t(){}};
template<size_t max_size>
auto constexpr collect_at_most = collect_at_most_tag_t<max_size>{}; // no need for 'tagger_t', this directly runs
// the type to capture the value, i.e. for the left-hand '|'
// of (x|operation|func)
template<typename R, typename Tag_type>
struct forward_this_with_a_tag {
R m_r;
static_assert(!std:: is_reference<R>{}, "");
static_assert( is_range_v< R >, "");
};
/*
* The actual overloads of '|' are here.
*/
template<typename R, typename Tag_type
, SFINAE_ENABLE_IF_CHECK( is_range_v<R> ) // if 'r' is a range
>
auto constexpr
operator| (R r, tagger_t<Tag_type>) {
static_assert( is_range_v<R> ,"");
return forward_this_with_a_tag<R, Tag_type> { std::move(r) };
}
template<typename R, typename Tag_type
, typename Rnonref = std::remove_reference_t<R>
, SFINAE_ENABLE_IF_CHECK( !is_range_v<Rnonref> ) // if 'nr' is a not a range
>
auto constexpr
operator| (R && nr, tagger_t<Tag_type> tag)
->decltype(as_range(std::forward<R>(nr)) | tag)
{
return as_range(std::forward<R>(nr)) | tag;
}
/*
* all_true
*/
constexpr bool
all_true(void) {return true;}
template<typename ...Ts>
constexpr bool
all_true(bool b, Ts && ... ts) {
if(b)
return all_true(std::forward<Ts>(ts)...);
else
return false;
}
/*
* vector_with_max_size
* This will be useful in 'collect_at_most'. It's like std::vector, but we
* have to have a maximum size in order to make it constexpr-friendly.
* I wouldn't expect this to be used in real code, it's just something
* convenient for use in compile-time testing.
*/
template< typename T
, size_t N >
struct vector_with_max_size // somewhat similar to std::vector, but fully constexpr
{
static_assert(!std::is_reference<T>{} ,"");
T m_data[N];
size_t m_current_size;
constexpr vector_with_max_size() : m_data{}, m_current_size(0) {}
template<typename ... U>
constexpr vector_with_max_size(U ... u) : m_data{u...}, m_current_size(sizeof...(u))
{ static_assert( all_true(std::is_same<T, U>{}...) ,""); }
auto constexpr
begin()
-> T *
{ return &m_data[0]; }
auto constexpr
end()
-> T *
{ return &m_data[m_current_size]; }
auto constexpr
begin() const
-> T const *
{ return &m_data[0]; }
auto constexpr
end() const
-> T const *
{ return &m_data[m_current_size]; }
};
template< typename T, typename ... Us>
auto constexpr
make_compact_vector_with_max_size(T t, Us ... us)
-> vector_with_max_size<T, 1+sizeof...(us)>
{
(void)t;
return {t,us...};
}
template< typename VectorLikeType, typename T , size_t N >
auto constexpr
operator==( vector_with_max_size<T,N> const & answer, VectorLikeType const & expected)
-> decltype( void(expected.size()), void(expected.at(0)) , bool{} )
// the return type is simply 'bool'. The above is a bit of SFINAE to ensure
// the VectorLikeType value has methods for .size() and .at(i).
{
if(answer.m_current_size != expected.size())
return false;
for(size_t i = 0; i < answer.m_current_size; ++i)
if(answer.m_data[i] != expected.at(i))
return false;
return true;
}
template< typename Tl , size_t Nl
, typename Tr , size_t Nr >
auto constexpr
operator==( vector_with_max_size<Tl,Nl> const & l, vector_with_max_size<Tr,Nr> const & r )
-> bool
{
if(l.m_current_size != r.m_current_size)
return false;
for(size_t i = 0; i < l.m_current_size; ++i)
if(l.m_data[i] != r.m_data[i])
return false;
return true;
}
template< typename T
, size_t N
, typename StreamType
, typename = decltype( std::declval<StreamType>() << "" ) // SFINAE if StreamType can't accept <<
>
StreamType & operator<< (StreamType &o, vector_with_max_size<T,N> const &v)
{
(void)v;