-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFunctional.h
More file actions
972 lines (800 loc) · 25.5 KB
/
Functional.h
File metadata and controls
972 lines (800 loc) · 25.5 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
#include "Common.h"
#pragma once
namespace pure {
/* Forwarder<F>(x...) = f(x...) */
template< class F > struct Forwarder {
using function = Decay<F>;
function f = F();
template< class ...G >
constexpr Forwarder( G&& ...g ) : f( std::forward<G>(g)... ) { }
template< class ...X >
constexpr auto operator() ( X&& ...x )
-> decltype( f(declval<X>()...) )
{
return f( std::forward<X>(x)... );
}
};
/* Make<T>(x...) = T(x...) */
template< class T > struct Make {
template< class ...X >
constexpr T operator () ( X&& ...x ) {
return T( forward<X>(x)... );
}
};
template< class T > struct Initialize {
template< class ...X >
constexpr T operator () ( X&& ...x ) {
return T{ forward<X>(x)... };
}
};
template< template<class...> class T > struct InitializeT {
template< class X, class ...Y >
constexpr T<X> operator () ( X&& x, Y&& ...y ) {
return T<X>{ forward<X>(x), forward<Y>(y)... };
}
};
template< template<class...> class T > struct TieT {
template< class ...X >
constexpr T<X&...> operator () ( X& ...x ) {
return T<X&...>( x... );
}
};
/* MakeT<T>(x) = T<X>(x) */
template< template<class...> class T > struct MakeT {
template< class ...X, class R = T< Decay<X>... > >
constexpr R operator () ( X&& ...x ) {
return R( forward<X>(x)... );
}
};
/* ForwardT<T>(X&& x) = T<X>(x) */
template< template<class...> class T > struct ForwardT {
template< class ...X, class R = T< X... > >
constexpr R operator () ( X&& ...x ) {
return R( forward<X>(x)... );
}
};
/* Flip f : g(x,y) = f(y,x) */
template< class F > struct Flip {
F f;
template< class _F >
constexpr Flip( _F&& f ) : f(forward<_F>(f)) { }
template< class X, class Y, class ...Z >
constexpr decltype( f(declval<Y>(),declval<X>(),declval<Z>()...) )
operator() ( X&& x, Y&& y, Z&& ...z ) {
return f( forward<Y>(y), forward<X>(x), forward<Z>(z)... );
}
};
constexpr auto flip = MakeT<Flip>();
/*
* Partial application.
* g(y) = f( x, y )
* h() = f( x, y )
* partial( f, x ) = g
* partial( f, x, y ) = h
*/
template< class F, class X >
struct Part {
F f = F();
X x;
template< class _F, class _X >
constexpr Part( _F&& f, _X&& x )
: f(forward<_F>(f)), x(forward<_X>(x))
{
}
constexpr Part( X x )
: x(move(x))
{
}
/*
* The return type of F only gets deduced based on the number of arguments
* supplied. Part otherwise has no idea whether f takes 1 or 10 xs.
*/
template< class ... Xs >
auto operator () ( Xs&& ...xs )
-> decltype( f(x,declval<Xs>()...) )
{
return f( x, forward<Xs>(xs)... );
}
template< class ... Xs >
constexpr auto operator () ( Xs&& ...xs )
-> decltype( f(x,declval<Xs>()...) )
{
return f( x, forward<Xs>(xs)... );
}
};
/*
* Reverse partial application.
* g(z) = f( x, y, z )
* rpartial( f, y, z ) -> g(x)
*/
template< class F, class X > struct RPart {
F f = F();
X x;
template< class _F, class _X >
constexpr RPart( _F&& f, _X&& x )
: f( forward<_F>(f) ), x( forward<_X>(x) ) { }
constexpr RPart( X x )
: x(move(x))
{
}
template< class ...Y >
constexpr decltype( f(declval<Y>()..., x) )
operator() ( Y&& ...y ) {
return f( forward<Y>(y)..., x );
}
};
/*
* Given any binary function, f(x,y):
* Let f(x) represent a partial application.
* Left f.with(y) represent a right-oriented application.
*/
template< class D > struct Binary {
template< class X >
constexpr Part<D,X> operator () ( X x ) {
return Part<D,X>( D(), move(x) );
}
template< class X >
constexpr RPart<D,X> with( X x ) {
return RPart<D,X>( D(), move(x) );
}
};
/*
* chainl(b,x...) -- Apply b to x... from left to right.
* chainl(+,1,2,3) = (1+2) + 3
*/
constexpr struct Chainl : Binary<Chainl> {
using Binary<Chainl>::operator();
template< class F, class X >
constexpr X operator () ( const F&, X&& x ) {
return forward<X>(x);
}
template< class F, class X, class Y, class ...Z >
constexpr auto operator () ( F&& b, X&& x, Y&& y, Z&& ...z)
-> decltype(
(*this)( b, b(declval<X>(),declval<Y>()), declval<Z>()... )
)
{
return (*this)(
b,
b( forward<X>(x), forward<Y>(y) ),
forward<Z>(z)...
);
}
} chainl{};
/*
* chainr(b,x...) -- Apply b to x... from right to left.
* chainr(+,1,2,3) = 1 + (2+3)
*/
constexpr struct Chainr : Binary<Chainr> {
using Binary<Chainr>::operator();
template< class F, class X >
constexpr X operator () ( const F&, X&& x ) {
return forward<X>(x);
}
template< class F, class X, class Y, class ...Z >
constexpr auto operator () ( const F& b, X&& x, Y&& y, Z&& ...z)
-> decltype(
b( (*this)(b,declval<Y>(),declval<Z>()...), declval<X>() )
)
{
return b (
// Reverse the argument order.
(*this)( b, forward<Y>(y), forward<Z>(z)... ),
// The first gets applied last.
forward<X>(x)
);
}
} chainr{};
/* ConstructBinary<T>(x,y) = T<X,Y>(x,y) */
template< template<class...> class T >
struct MakeBinaryT : Binary<MakeBinaryT<T>> {
using Binary<MakeBinaryT<T>>::operator();
template< class X, class Y, class R = T< Decay<X>, Decay<Y> > >
constexpr R operator () ( X&& x, Y&& y ) {
return R( forward<X>(x), forward<Y>(y) );
}
};
/* ForwardBinary<T>(x,y) = T<X&&,Y&&>(x,y) */
template< template<class...> class T >
struct ForwardBinaryT : Binary<ForwardBinaryT<T>> {
using Binary<ForwardBinaryT<T>>::operator();
template< class X, class Y, class R = T<X,Y> >
constexpr R operator () ( X&& x, Y&& y ) {
return R( forward<X>(x), forward<Y>(y) );
}
};
/*
* Left Associativity
* Given a binary function, f(x,y):
* Let f(x,y,z,h) = f( f( f(x,y) ,z ), h ) -- Chaining
*/
template< class F > struct Chainable : Binary<F> {
using Binary<F>::operator();
template< class X, class Y >
using R = typename std::result_of< F(X,Y) >::type;
// Three arguments: unroll.
template< class X, class Y, class Z >
constexpr auto operator () ( X&& x, Y&& y, Z&& z )
-> R< R<X,Y>, Z >
{
return F()(
F()( std::forward<X>(x), std::forward<Y>(y) ),
std::forward<Z>(z)
);
}
template< class X, class Y, class ...Z >
using Unroll = Result <
Chainable<F>, R<X,Y>, Z...
>;
// Any more? recurse.
template< class X, class Y, class Z, class H, class ...J >
constexpr auto operator () ( X&& x, Y&& y, Z&& z, H&& h, J&& ...j )
-> Unroll<X,Y,Z,H,J...>
{
// Notice how (*this) always gets applied at LEAST three arguments.
return (*this)(
F()( std::forward<X>(x), std::forward<Y>(y) ),
std::forward<Z>(z), std::forward<H>(h), std::forward<J>(j)...
);
}
};
template< class F > struct ReverseChainable : Binary<F> {
using Self = ReverseChainable<F>;
using Binary<F>::operator();
template< class X, class Y >
using R = typename std::result_of< F(X,Y) >::type;
// Three arguments: unroll.
template< class X, class Y, class Z >
constexpr auto operator () ( X&& x, Y&& y, Z&& z )
-> R< R<X,Z>, Y >
{
return F()(
F()( std::forward<X>(x), std::forward<Z>(z) ),
std::forward<Y>(y)
);
}
// Any more? recurse.
template< class X, class Y, class Z, class H, class ...J >
constexpr auto operator () ( X&& x, Y&& y, Z&& z, H&& h, J&& ...j )
-> decltype(
F() (
(*this)( forward<X>(x), forward<Z>(z),
forward<H>(h), forward<J>(j)... ),
forward<Y>(y)
)
)
{
// Notice how (*this) always gets applied at LEAST three arguments.
return F() (
(*this)( forward<X>(x), forward<Z>(z),
forward<H>(h), forward<J>(j)... ),
forward<Y>(y)
);
}
};
template< template<class...> class T >
struct MakeChainableT : Chainable<MakeT<T>> {
using Chainable<MakeT<T>>::operator();
template< class X, class Y, class R = T< Decay<X>, Decay<Y> > >
constexpr R operator () ( X&& x, Y&& y ) {
return R( forward<X>(x), forward<Y>(y) );
}
};
template< template<class...> class T >
struct MakeReverseChainableT : ReverseChainable<MakeT<T>> {
using ReverseChainable<MakeT<T>>::operator();
template< class X, class Y, class R = T< Decay<X>, Decay<Y> > >
constexpr R operator () ( X&& x, Y&& y ) {
return R( forward<X>(x), forward<Y>(y) );
}
};
template< template<class...> class T >
struct ForwardChainableT : Chainable<ForwardT<T>> {
using Chainable<ForwardT<T>>::operator();
template< class X, class Y, class R = T<X,Y> >
constexpr R operator () ( X&& x, Y&& y ) {
return R( forward<X>(x), forward<Y>(y) );
}
};
template< template<class...> class T >
struct ForwardReverseChainableT : ReverseChainable<ForwardT<T>> {
using ReverseChainable<ForwardT<T>>::operator();
template< class X, class Y, class R = T<X,Y> >
constexpr R operator () ( X&& x, Y&& y ) {
return R( forward<X>(x), forward<Y>(y) );
}
};
// Define And early as a helper for Transitive.
struct And : Chainable<And> {
using Chainable<And>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() && declval<Y>() )
{
return forward<X>(x) && forward<Y>(y);
}
};
/*
* Transitivity:
* Given a transitive function, f(x,y,z), f(x,y) and f(y,z) implies f(x,z).
* Let "and" be some function that folds the return of f.
*/
template< class F, class Fold=And > struct Transitive : Binary<F> {
using Binary<F>::operator();
template< class X, class Y, class Z >
constexpr auto operator () ( X&& x, Y&& y, Z&& z )
-> Result<F,X,Y>
{
return Fold() (
F()( forward<X>(x), forward<Y>(y) ),
F()( forward<Y>(y), forward<Z>(z) )
);
}
template< class X, class Y, class Z, class A, class ...B >
constexpr auto operator () ( X&& x, Y&& y, Z&& z, A&& a, B&& ...b )
-> Result<F,X,Y>
{
return Fold() ( F()( forward<X>(x), forward<Y>(y) ),
F()( forward<Y>(y), forward<Z>(z),
forward<A>(a), forward<B>(b)... ) );
}
};
/*
* PARTIAL APPLICATION
* Some languages implement partial application through closures, which hold
* references to the function's arguments. But they also often use reference
* counting. We must consider the scope of the variables we want to apply. If
* we apply references and then return the applied function, its references
* will dangle.
*
* See:
* upward funarg problem: http://en.wikipedia.org/wiki/Upward_funarg_problem
*/
/*
* closure http://en.wikipedia.org/wiki/Closure_%28computer_science%29
* Here, closure forwards the arguments, which may be references or rvalues--it
* does not matter. A regular closure works for passing functions down.
*
* Thinking as closures as open (having references to variables outside of
* itself), let's refer to a closet as closed. It contains a function and its
* arguments (or environment).
*/
constexpr auto closure = ForwardChainableT<Part>();
constexpr auto closet = MakeChainableT<Part>();
constexpr auto rclosure = ForwardReverseChainableT<RPart>();
constexpr auto rcloset = MakeReverseChainableT<RPart>();
template< class F, class ...X >
using Closure = decltype( closure(declval<F>(), declval<X>()...) );
template< class F, class ...X >
using RClosure = decltype( rclosure(declval<F>(), declval<X>()...) );
template< class F, class ...X >
using Closet = decltype( closet(declval<F>(), declval<X>()...) );
template< class F, class ...X >
using RCloset = decltype( rcloset(declval<F>(), declval<X>()...) );
/*
* Given f(x,y...) and fx(y...)
* enclose f = fx
*/
template< class F > struct Enclosure {
F f;
template< class _F >
constexpr Enclosure( _F&& f ) : f(forward<_F>(f)) { }
template< class ...X >
using Result = decltype( closure(f,declval<X>()...) );
template< class ...X >
constexpr Result<X...> operator() ( X&& ...x ) {
return closure( f, forward<X>(x)... );
}
};
constexpr auto enclosure = MakeT<Enclosure>();
/*
* Composition.
* Given f(x,y...) and g(z)
* The composition of f and g, f . g, equals h(z,y...)
* h(z,y...) = f( g(z), y... )
*
*/
template< class F, class G > struct Composition {
F f = F();
G g = G();
constexpr Composition() { }
constexpr Composition( F f, G g)
: f(move(f)), g(move(g)) { }
template< class X, class ...Y >
constexpr decltype( f(g(declval<X>()), declval<Y>()...) )
operator () ( X&& x, Y&& ...y ) {
return f( g( forward<X>(x) ), forward<Y>(y)... );
}
};
constexpr auto compose = MakeChainableT<Composition>();
/* A const composition for when g is a constant function. */
template< class F, class G > struct CComposition {
F f = F();
G g = G();
constexpr CComposition() { }
template< class _F, class _G >
constexpr CComposition( _F&& f, _G&& g )
: f(forward<_F>(f)), g(forward<_G>(g)) { }
template< class ...Y >
constexpr decltype( f(g(), declval<Y>()...) )
operator() ( Y&& ...y ) {
return f( g(), forward<Y>(y)... );
}
};
constexpr auto ccompose = MakeChainableT<CComposition>();
/* N-ary composition assumes a unary f and N-ary g. */
template< class F, class G > struct NComposition {
F f = F();
G g = G();
constexpr NComposition() { }
template< class _F, class _G >
constexpr NComposition( _F&& f, _G&& g )
: f(forward<_F>(f)), g(forward<_G>(g)) { }
template< class ...X >
constexpr decltype( f(g(declval<X>()...)) )
operator() ( X&& ...x ) {
return f( g( forward<X>(x)... ) );
}
};
constexpr auto ncompose = MakeChainableT<NComposition>();
/*
* Binary composition
* (compose2 http://www.sgi.com/tech/stl/binary_compose.html)
* Given g(x)=y, h(x)=z, and f(y,z), let bf(x) = f( g(x), h(x) )
* This implementation diverges from compose2 in that it allows g and h to be
* n-ary.
*/
template< class F, class G, class H >
struct BComposition
{
F f = F();
G g = G();
H h = H();
constexpr BComposition() {}
template< class _F, class _G, class _H >
constexpr BComposition( _F&& f, _G&& g, _H&& h )
: f(forward<_F>(f)), g(forward<_G>(g)), h(forward<_H>(h)) { }
template< class ...X >
constexpr decltype( f(g(declval<X>()...), h(declval<X>()...)) )
operator() ( X&& ...x ) {
return f( g( forward<X>(x)... ), h( forward<X>(x)... ) );
}
};
constexpr auto bcompose = MakeT<BComposition>();
constexpr auto returnPair = MakeChainableT<std::pair>();
template< size_t N, class P >
using Nth = decltype( std::get<N>(declval<P>()) );
template< class F, class G > struct FanComposition {
F f = F();
G g = G();
constexpr FanComposition() {}
constexpr FanComposition( F f, G g )
: f( std::move(f) ), g( std::move(g) )
{
}
template< class X >
using resultF = Result<F,X>;
template< class X >
using resultG = Result<G,X>;
template< class X >
using result = std::pair< resultF<X>, resultG<X> >;
template< class X, class R = result<X> >
constexpr R operator () ( const X& x ) {
return R{ f(x), g(x) };
}
};
constexpr auto fanCompose = MakeChainableT<FanComposition>();
template< class X > constexpr X inc( X x ) { return ++x; }
template< class X > constexpr X dec( X x ) { return --x; }
constexpr struct Add : Chainable<Add> {
using Chainable<Add>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() + declval<Y>() )
{
return forward<X>(x) + forward<Y>(y);
}
} add{};
constexpr struct AddEq : Chainable<AddEq> {
using Chainable<AddEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() += declval<Y>() )
{
return forward<X>(x) += forward<Y>(y);
}
} addEq{};
constexpr struct Sub : Chainable<Sub> {
using Chainable<Sub>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() - declval<Y>() )
{
return forward<X>(x) - forward<Y>(y);
}
} sub{};
constexpr struct SubEq : Chainable<SubEq> {
using Chainable<SubEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() -= declval<Y>() )
{
return forward<X>(x) -= forward<Y>(y);
}
} subEq{};
constexpr struct Mult : Chainable<Mult> {
using Chainable<Mult>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() * declval<Y>() )
{
return forward<X>(x) * forward<Y>(y);
}
} mult{};
constexpr struct MultEq : Chainable<MultEq> {
using Chainable<MultEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() *= declval<Y>() )
{
return forward<X>(x) *= forward<Y>(y);
}
} multEq{};
constexpr struct Div : Chainable<Div> {
using Chainable<Div>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() / declval<Y>() )
{
return forward<X>(x) / forward<Y>(y);
}
} div{};
constexpr struct DivEq : Chainable<DivEq> {
using Chainable<DivEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() /= declval<Y>() )
{
return forward<X>(x) /= forward<Y>(y);
}
} divEq{};
constexpr struct Mod : Chainable<Mod> {
using Chainable<Mod>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() % declval<Y>() )
{
return forward<X>(x) % forward<Y>(y);
}
} mod{};
constexpr struct ModEq : Chainable<ModEq> {
using Chainable<ModEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() %= declval<Y>() )
{
return forward<X>(x) %= forward<Y>(y);
}
} modEq{};
constexpr struct LShift : Chainable<LShift> {
using Chainable<LShift>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() << declval<Y>() )
{
return forward<X>(x) << forward<Y>(y);
}
} lshift{};
constexpr struct LShiftEq : Chainable<LShiftEq> {
using Chainable<LShiftEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() <<= declval<Y>() )
{
return forward<X>(x) <<= forward<Y>(y);
}
} lshiftEq{};
constexpr struct RShift : Chainable<RShift> {
using Chainable<RShift>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() >> declval<Y>() )
{
return forward<X>(x) >> forward<Y>(y);
}
} rshift{};
constexpr struct RShiftEq : Chainable<RShiftEq> {
using Chainable<RShiftEq>::operator();
template< class X, class Y >
constexpr auto operator() ( X&& x, Y&& y )
-> decltype( declval<X>() >>= declval<Y>() )
{
return forward<X>(x) >>= forward<Y>(y);
}
} rshiftEq{};
// TODO: notEq(x,y,z) should be equivalent to x!=y and y!=z and x!=z.
constexpr struct NotEq : Binary<NotEq> {
using Binary<NotEq>::operator();
template< class X, class Y >
constexpr bool operator() ( X&& x, Y&& y ) {
return forward<X>(x) != forward<Y>(y);
}
} notEq{};
constexpr struct Eq : Transitive<Eq> {
using Transitive<Eq,And>::operator();
template< class X, class Y >
constexpr bool operator() ( X&& x, Y&& y ) {
return forward<X>(x) == forward<Y>(y);
}
} eq{};
struct Or : Chainable<Or> {
using Chainable<Or>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() || declval<Y>() )
{
return forward<X>(x) || forward<Y>(y);
}
};
constexpr struct BitOr : Chainable<BitOr> {
using Chainable<BitOr>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() | declval<Y>() )
{
return forward<X>(x) | forward<Y>(y);
}
} bitOr{};
constexpr struct BitOrEq : Chainable<BitOrEq> {
using Chainable<BitOrEq>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() |= declval<Y>() )
{
return forward<X>(x) |= forward<Y>(y);
}
} bitOrEq{};
constexpr struct XOr : Chainable<XOr> {
using Chainable<XOr>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() ^ declval<Y>() )
{
return forward<X>(x) ^ forward<Y>(y);
}
} xOr{};
constexpr struct XOrEq : Chainable<XOrEq> {
using Chainable<XOrEq>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() ^= declval<Y>() )
{
return forward<X>(x) ^= forward<Y>(y);
}
} xOrEq{};
constexpr struct BitAnd : Chainable<BitAnd> {
using Chainable<BitAnd>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() & declval<Y>() )
{
return forward<X>(x) & forward<Y>(y);
}
} bitAnd{};
constexpr struct BitAndEq : Chainable<BitAndEq> {
using Chainable<BitAndEq>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() &= declval<Y>() )
{
return forward<X>(x) &= forward<Y>(y);
}
} bitAndEq{};
constexpr struct Less : Transitive<Less> {
using Transitive<Less,And>::operator();
template< class X, class Y >
constexpr bool operator() ( X&& x, Y&& y ) {
return forward<X>(x) < forward<Y>(y);
}
} less{};
constexpr struct LessEq : Transitive<LessEq> {
using Transitive<LessEq,And>::operator();
template< class X, class Y >
constexpr bool operator() ( X&& x, Y&& y ) {
return forward<X>(x) <= forward<Y>(y);
}
} lessEq{};
constexpr struct Greater : Transitive<Greater> {
using Transitive<Greater,And>::operator();
template< class X, class Y >
constexpr bool operator() ( X&& x, Y&& y ) {
return forward<X>(x) > forward<Y>(y);
}
} greater{};
constexpr struct GreaterEq : Transitive<GreaterEq> {
using Transitive<GreaterEq,And>::operator();
template< class X, class Y >
constexpr bool operator() ( X&& x, Y&& y ) {
return forward<X>(x) >= forward<Y>(y);
}
} greaterEq{};
constexpr struct BinaryNot {
template< class B >
constexpr bool operator() ( B&& b ) {
return not (bool)forward<B>(b);
}
} binaryNot{};
constexpr auto fnot = ncompose( binaryNot );
constexpr auto divisorOf = compose( fnot, mod );
constexpr auto divisibleBy = compose( fnot, rcloset(mod) );
constexpr auto multipleOf = divisibleBy;
constexpr struct Max : Chainable<Max> {
using Chainable<Max>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() + declval<Y>() )
{
static_assert( std::is_integral<X>::value, "Non-integral x!" );
static_assert( std::is_integral<Y>::value, "Non-integral y!" );
return x > y ? x : y;
}
template< class X >
constexpr const X& operator() ( const X& a, const X& b ) {
return a > b ? a : b;
}
template< class X >
X& operator() ( X& a, X& b ) const {
return a > b ? a : b;
}
} max{};
constexpr struct Min : Chainable<Min> {
using Chainable<Min>::operator();
template< class X, class Y >
constexpr auto operator () ( X&& x, Y&& y )
-> decltype( declval<X>() + declval<Y>() )
{
static_assert( std::is_integral<X>::value, "Non-integral x!" );
static_assert( std::is_integral<Y>::value, "Non-integral y!" );
return x > y ? x : y;
}
template< class X >
constexpr const X& operator() ( const X& a, const X& b ) {
return a < b ? a : b;
}
template< class X >
X& operator() ( X& a, X& b ) const {
return a < b ? a : b;
}
} min{};
template< bool b >
struct If {
template< class X, class Y >
constexpr X operator () ( X&& x, const Y& ) {
return forward<X>(x);
}
};
template<>
struct If<false> {
template< class X, class Y >
constexpr Y operator () ( const X&, Y&& y ) {
return forward<Y>(y);
}
};
template< template<class...> class P/*redicate*/, class T, class F >
struct SelectF {
T t = T();
F f = F();
constexpr SelectF( T t, F f ) : t(move(t)), f(move(f)) { }
template< class ...X, class Fn = If< P<Decay<X>...>::value > >
constexpr auto operator () ( X&& ...x )
-> decltype( Fn()(t,f)( forward<X>(x)... ) )
{
return Fn()(t,f)( forward<X>(x)... );
}
};
template< template<class...> class P/*redicate*/, class T, class F,
class S = SelectF<P,T,F> >
constexpr S selectF( T t, F f ) {
return S( move(t), move(f) );
}
} // namespace pure.