-
Notifications
You must be signed in to change notification settings - Fork 0
/
enhance.hpp
1326 lines (1056 loc) · 38.4 KB
/
enhance.hpp
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
/*
* Enhance v0.1
*
* ----------------------------------------------------------
* Copyright (c) 2016 Johannes Gerer.
*
* Distributed under the MIT License. (See accompanying file
* LICENSE.txt)
*
*/
#ifndef ENHANCE_INCLUDED
#define ENHANCE_INCLUDED
#include <functional>
#include <type_traits>
#include <ostream>
#include <iostream>
using std::cout;
using std::endl;
#ifndef FORCE_INLINE
#ifdef _MSC_FULL_VER
#define FORCE_INLINE __forceinline
#else // _MSC_FULL_VER
#define FORCE_INLINE inline __attribute__((always_inline))
#endif // _MSC_FULL_VER
#endif // FORCE_INLINE
#if defined(_MSC_VER) && _MSC_VER < 1800
#error "This version of Visual C++ does not support `template aliases` used by Enhance`. Either use Visual C++ 2013 or later or contact the maintainer of `Enhance`, who will be happy to backport to your version."
#endif
//todo assigment mit allen accessors testen
//todo std::swap specialization
//todo std::less specialization? (oder reicht normale instanz die den
//< operator nutzt?
//todo rvalue in constructor of Combiner
//todo base class access?
namespace enhance {
struct Nothing{};
//#################### 1 the `access` function ############################
/*
The `access` function is used to build generic
`Combiner::singleStep` functions used derive e.g. generic
comparison operators. They always takes two arguments:
`Accessor` and `Target`.
The accessor is used to access some value (like a data member or
the return value of a member function) from a target.
*/
//`access` for plain data-members
template<class Accessor,class Target>
FORCE_INLINE Accessor&
access(Accessor( Target::*a), Target& x)
{
return x.*a;
}
//`access` for const plain data-members
template<class Accessor,class Target>
FORCE_INLINE const Accessor&
access(const Accessor(Target::*a), const Target& x)
{
return x.*a;
}
/* `access` for member functions
*/
template<class Accessor,class Target>
FORCE_INLINE Accessor
access(Accessor(Target::*a)(), Target& x)
{
return (x.*a)();
}
/* `access` for member function of constant objects
member functions need to be `const`, for example:
class A{
int version;
int getVersion() const{
return version; }
};
*/
template<class Accessor,class Target>
FORCE_INLINE Accessor
access(Accessor(Target::*a)() const, const Target& x)
{
return (x.*a)();
}
//`access` for operators (such as lambda expressions, functions,
//or classes with ()-operators) acting on the target
template<class Accessor,class Target>
FORCE_INLINE auto
access(Accessor a, Target& x)
-> decltype(a(x)){
return a(x);
}
//#################### 2 Accessor Wrappers ############################
/*
Wrapper functions, that modify accessors. (E.g. to derefence
pointer-Accessors)
*/
//#################### 2.1 Dereference Wrapper ############################
/** The `dereference` wrapper modifies the Accessor such, that its
result will be dereferenced. Usefull for e.g. for (smart)
pointers
usage:
class A{...
int* p;
}
dereference(&A::p)
*/
template<class Accessor>
struct Dereference{
Accessor m;
Dereference(Accessor m):m(m){};
template<class Target>
auto operator()(Target& d) const
->decltype(*access(m, d)){
//there are no pointers to references, so reference qualifiers
//are not important here
return *access(m, d);
}
};
// factory function for template argument deduction
template<class Accessor>
Dereference<Accessor> dereference(Accessor a){
return Dereference<Accessor>(a);
}
//#################### 2.2 const_cast Wrapper ############################
/** The `constCast` wrapper modifies the Accessor such, that its
result will be not be const. This MIGHT be usefull for
e.g. for serialization of const members.
usage:
class A{...
const int p;
}
constCast(&A::p)
*/
template<class Accessor>
struct ConstCast{
Accessor m;
ConstCast(Accessor m):m(m){};
template<class Target>
auto operator()(Target& d) const
-> typename std::remove_const<
typename std::remove_reference<decltype((access(m,d)))>::type>::type&
{
return const_cast<typename std::remove_const<
typename std::remove_reference<decltype((access(m,d)))>::type>::type&>
(access(m, d));
}
};
// factory function for template argument deduction
template<class Accessor>
ConstCast<Accessor> constCast(Accessor a){
return ConstCast<Accessor>(a);
}
//#################### 2.3 Range Wrapper ############################
/** The `range` wrapper defined below function takes two accessors
`begin` and `end`. All elements between `begin` and
(excluding) `end` will be accessed in order.
usage:
class Vector{ ...
double* data;
size_t size;
}
range(&Vector::data,
[&](const Vector& v){return v.data + v.size;})
*/
template<class A, class B>
struct Range {
A a;
B b;
Range(A a,B b):a(a),b(b){};
};
// factory function for template argument deduction
template<class A, class B>
FORCE_INLINE Range<A,B> range(A a, B b){
return Range<A,B>(a, b);
}
//#################### 2.4 static (compile time) Range Wrapper ############################
template<int begin, int end, class B>
struct FromTo {
B m;
FromTo(B m) : m(m) {}
};
//helper for default end==-1
template<int endP, class T>
struct endHelper{
static const int value = endP > -1 ? endP :
std::tuple_size<typename std::remove_reference<T>::type >::value;
};
// factory function for template argument deduction
template<int begin=0, int end=-1, class B>
FromTo<begin, end, B> range(B b){
return FromTo<begin, end, B>(b);
}
//#################### 2.5 begin, end, container Wrapper ############################
/*
container, Automatically iterates from std::begin to std::end
*/
template<class Accessor>
struct Begin{
Accessor m;
Begin(Accessor m):m(m){};
template<class Target>
auto operator()(Target& d) const
-> decltype(std::begin(access(m, d)))
{
return std::begin(access(m, d));
}
};
template<class Accessor>
struct End{
Accessor m;
End(Accessor m):m(m){};
template<class Target>
auto operator()(Target& d) const
-> decltype(std::end(access(m, d)))
{
return std::end(access(m, d));
}
};
// factory functions for template argument deduction
template<class Accessor>
Begin<Accessor> begin(Accessor a){
return Begin<Accessor>(a);
}
template<class Accessor>
End<Accessor> end(Accessor a){
return End<Accessor>(a);
}
// factory function for template argument deduction
template<class Accessor>
FORCE_INLINE auto container(Accessor a)
-> decltype(range(begin(a),end(a)))
{
return range(begin(a),end(a));
}
//#################### 3 Combiners ############################
/*
A `Combiner` iteratively applies a given operator a list of
values derived from a `Target` (using `Accessors`, see above)
and combines the results to a single `Result`.
This functionality needs to be supplied by the derived class in
`Derived::singleStep`. The iteration stops, either if there are
no Accessors left or if `singleStep` returns `true`.
The list of accessors can be given in two ways:
1) They can be passed directly to the variadic ()-operator:
SomeClass target;
Combiner comb(target);
comb(&SomeClass::someMember,&SomeClass::anotherMember);
2) The call of the ()-operator can be put directly in the target
class, more precisely `Target::enhance`, which is called by
`Combiner::callEnhanced()` or less verbose in the conversion
operator converting the `Combiner` to `Result`.
`enhance` has to have the following signature:
void Target::enhance(Derived&) const
When deriving from different Combinors, several identical
enhance members functions can be combined using a template. E.g:
class SomeClass{...
template<class T> void enhance(T& t) const{
t(&SomeClass::someMember,&SomeClass::anotherMember);
}
}
*/
template<class Derived, class Target, class Result>
class Combiner{
protected:
Target& target;
Result result;
public:
//ctor
Combiner(Target& target, Result&& result)
:target(target), result(std::forward<Result>(result)){
static_cast<Derived&>(*this).initialize();
}
//call the target's `enhance` member, which should call the
//()-operator of the derived class passed to it.
FORCE_INLINE Result callEnhance(){
target.enhance(static_cast<Derived&>(*this));
return result;
}
// a conversion operator converting to Result. same
// functionality as `callEnhance` but less verbose,
FORCE_INLINE operator Result (){
callEnhance();
return result;
}
// the trivial version of the ()-operator
FORCE_INLINE Result operator()(){
static_cast<Derived&>(*this).finalize();
return result;
}
//Check for variadic tempalte support in MS Visual C++
#if !defined(_MSC_VER) || _MSC_VER >= 1800
template<class Accessor, class ... Rest>
FORCE_INLINE Result operator()(Accessor a, Rest ... rest)
{
if(!applySingleStep(a))
operator()(rest ...);
return result;
}
#else
template<class A1>
FORCE_INLINE Result operator()(A1 a1)
{ if(!applySingleStep(a1)) operator()(); return result; }
template<class A1,class A2>
FORCE_INLINE Result operator()(A1 a1, A2 a2)
{ if(!applySingleStep(a1)) operator()(a2); return result; }
template<class A1,class A2,class A3>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3)
{ if(!applySingleStep(a1)) operator()(a2,a3); return result; }
template<class A1,class A2,class A3,class A4>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4); return result; }
template<class A1,class A2,class A3,class A4,class A5>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4,A5 a5)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4,a5); return result; }
template<class A1,class A2,class A3,class A4,class A5,class A6>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4,A5 a5,A6 a6)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4,a5,a6); return result; }
template<class A1,class A2,class A3,class A4,class A5,class A6,class A7>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4,a5,a6,a7); return result; }
template<class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4,a5,a6,a7,a8); return result; }
template<class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8,class A9>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8,A9 a9)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4,a5,a6,a7,a8,a9); return result; }
template<class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8,class A9,class A10>
FORCE_INLINE Result operator()(A1 a1, A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8,A9 a9,A10 a10)
{ if(!applySingleStep(a1)) operator()(a2,a3,a4,a5,a6,a7,a8,a9,a10); return result; }
#endif
typedef Result result_t;
void initialize() const{
}
void beforeStep() const{
}
void finalize() const{
}
private:
template<class Accessor>
FORCE_INLINE bool applySingleStep(Accessor a)
{
if(static_cast<Derived&>(*this).singleStep(a)){
operator()();
return true;
}else
return false;
}
};
//#################### 3.1 Unary Combiner ############################
/*
A Combiner for 'unary' operators, i.e. operators, that act on
one target. Example: Hash functions
`Operator` is required to have the following static members:
typedef Result Operator::result_t
static Result Operator::init()
static bool Operator::apply(Result&, Target&)
The iterative application of `Operator::apply` will stop, if it
returns `true`.
*/
template<class Operator, class Target, class Derived=std::false_type>
struct UnaryCombiner :
public Combiner<typename std::conditional<std::is_same<Derived, std::false_type>::value,
UnaryCombiner<Operator, Target>, Derived>::type,
Target,
typename Operator::result_t> {
typedef typename Operator::result_t result_t;
typedef typename std::conditional<std::is_same<Derived, std::false_type>::value,
UnaryCombiner<Operator, Target>, Derived>::type
derived_t;
FORCE_INLINE UnaryCombiner(Target& target)
: UnaryCombiner::Combiner(target, Operator::init(target)){};
FORCE_INLINE UnaryCombiner(Target& target, result_t&& result)
: UnaryCombiner::Combiner(target, std::forward<result_t>(result)){};
template<class Accessor>
FORCE_INLINE bool singleStep(Accessor ac) {
static_cast<derived_t&>(*this).beforeStep();
return Operator::apply
(this->result, access(ac,this->target));
}
struct Functor{
typename UnaryCombiner::result_t operator()
(Target& target) const{
return UnaryCombiner(target);
}
};
//specialization for `Range` accessors
template<class A,class B>
FORCE_INLINE bool singleStep(Range<A,B> ac) {
// cout<<"ss1"<<endl;
//copy the begin iterator
auto b = access(ac.a,this->target);
//reference to the end iterator
auto&& e = access(ac.b,this->target);
for(; b<e; ++b){
static_cast<derived_t&>(*this).beforeStep();
if(Operator::apply(this->result, *b))
return true;
}
return false;
}
//Compile Time Range specialization.
template<int begin, int end, class Accessor>
FORCE_INLINE bool singleStep(FromTo<begin, end, Accessor> a)
{
auto& ref = access(a.m, this->target);
return recurseRange<begin,endHelper<end,decltype(ref)>::value >(ref);
}
private:
template<int begin, int end, class B>
FORCE_INLINE typename std::enable_if<begin < end, bool>::type
recurseRange(B& o)
{
static_cast<derived_t&>(*this).beforeStep();
if(Operator::apply(this->result, std::get<begin>(o)))
return true;
return recurseRange<begin+1,end>(o);
}
// Base case
template<int begin, int end, class B>
FORCE_INLINE typename std::enable_if<begin >= end, bool>::type
recurseRange(B& o)
{
return false;
}
};
//#################### 3.2 Binary Combiner ############################
/*
A Combiner for 'binary' operators, i.e. operators, that act on
two targets. Example: Comparison operators
`Operator` is required to have the following typed and static
members:
typedef Result Operator::result_t
static Result Operator::init()
static bool Operator::apply(Result&, Target&, Target&)
The iterative application of `Operator::apply` will stop, if it
returns `true`.
*/
template<class Operator, class Target, class Target2 = Target,
class Derived = std::false_type>
struct BinaryCombiner :
Combiner<typename std::conditional<std::is_same<Derived, std::false_type>::value,
BinaryCombiner<Operator, Target, Target2>,
Derived>::type,
Target,
typename Operator::result_t> {
typedef typename Operator::result_t result_t;
typedef typename std::conditional<std::is_same<Derived, std::false_type>::value,
BinaryCombiner<Operator, Target, Target2>,
Derived>::type
derived_t;
Target2& target2;
FORCE_INLINE BinaryCombiner(Target& target, Target2& target2)
: BinaryCombiner::Combiner(target, Operator::init(target, target2))
, target2(target2){};
FORCE_INLINE BinaryCombiner(Target& target, Target2& target2,
result_t&& result)
: BinaryCombiner::Combiner(target, std::forward<result_t>(result))
, target2(target2){};
template<class Accessor>
FORCE_INLINE bool singleStep(Accessor ac) {
static_cast<derived_t&>(*this).beforeStep();
return Operator::apply
(this->result, access(ac,this->target),
access(ac,this->target2));
}
struct Functor{
typename BinaryCombiner::result_t operator()
(Target& target, Target2& target2){
return BinaryCombiner(target, target2);
}
};
//specialization for `Range` accessors
template<class A,class B>
FORCE_INLINE bool singleStep(Range<A,B> ac) {
//copy the begin iterators
auto b_x = access(ac.a,this->target);
auto b_y = access(ac.a,this->target2);
//reference to the end iterators
auto&& e_x = access(ac.b,this->target);
auto&& e_y = access(ac.b,this->target2);
//require range of equal lengths (alternative implementations
// are possible, esp. for operator==, where differing length
// simply should result in the value `false`.)
assert(e_x-b_x==e_y-b_y);
for(; b_x<e_x; ++b_x, ++b_y){
static_cast<derived_t&>(*this).beforeStep();
if(Operator::apply(this->result, *b_x, *b_y))
return true;
}
return false;
}
//Compile Time Range specialization.
template<int begin, int end, class Accessor>
FORCE_INLINE bool singleStep(FromTo<begin, end, Accessor> a)
{
auto& ref = access(a.m, this->target);
auto& ref2 = access(a.m, this->target2);
return recurseRange<begin,endHelper<end,decltype(ref)>::value >(ref, ref2);
}
private:
template<int begin, int end, class B, class C>
FORCE_INLINE typename std::enable_if<begin < end, bool>::type
recurseRange(B& x, C& y)
{
static_cast<derived_t&>(*this).beforeStep();
if(Operator::apply(this->result, std::get<begin>(x), std::get<begin>(y)))
return true;
return recurseRange<begin+1,end>(x,y);
}
// Base case
template<int begin, int end, class B, class C>
FORCE_INLINE typename std::enable_if<begin >= end, bool>::type
recurseRange(B&, C&)
{
return false;
}
};
//#################### 4 Modules ############################
//#################### 4.1 Comparison Operators ############################
/*
This adopts the terminology of std::tuple.
From http://en.cppreference.com/w/cpp/utility/tuple/operator_cmp:
Point-wise comparison: Compares every pair of components.
Lexicographic comparison: Returns the result of the underlying
comparison operator applied to the first pair of components that
is not `==`.
All comparison operators are short-circuited; they do not access
tuple elements beyond what is necessary to determine the result
of the comparison.
Specilize e.g. using Comparison, ComparisonPW, ComparisonLEX
*/
template<class Base>
struct ComparisonOp : Base {
typedef bool result_t;
template<class A, class B>
static bool init(A&, B&){ return true; }
};
/* Template for point-wise comparison
*/
template<template<class> class Operator>
struct PointwiseComparisonOp {
template<class Value>
static bool apply(bool& r, Value&& a, Value&& b){
Operator<Value> op;
if(op(std::forward<Value>(a),std::forward<Value>(b)))
return false;
r = false;
return true;
}
};
/* Template for lexicographic comparison
*/
template<template<class> class Operator>
struct LexicographicalComparisonOp {
template<class Value>
static bool apply(bool& r, Value&& a, Value&& b){
Operator<Value> op;
if(a == b) return false;
r = op(std::forward<Value>(a),std::forward<Value>(b));
return true;
}
};
// Combiner aliases for the different operator types and single operators
template<class Operator, class Target>
using Comparison = BinaryCombiner<
ComparisonOp<Operator>, const Target>;
template<template<class> class Operator, class Target>
using ComparisonPW = Comparison<
PointwiseComparisonOp<Operator>, Target>;
template<template<class> class Operator, class Target>
using ComparisonLEX = Comparison<
LexicographicalComparisonOp<Operator>, Target>;
template<class Target>
using Equal = ComparisonPW<std::equal_to, Target>;
template<class Target>
using Unequal = ComparisonPW<std::not_equal_to, Target>;
template<class Target>
using Less = ComparisonLEX<std::less, Target>;
template<class Target>
using Greater = ComparisonLEX<std::greater, Target>;
template<class Target>
using LessEqual = ComparisonLEX<std::less_equal, Target>;
template<class Target>
using GreaterEqual = ComparisonLEX<std::greater_equal, Target>;
template<class Target>
using LessPW = ComparisonPW<std::less, Target>;
template<class Target>
using GreaterPW = ComparisonPW<std::greater, Target>;
template<class Target>
using LessEqualPW = ComparisonPW<std::less_equal, Target>;
template<class Target>
using GreaterEqualPW = ComparisonPW<std::greater_equal, Target>;
// factory functions for template argument deduction:
template<class Target>
Equal<const Target> equal(const Target& x,const Target& y){
return Equal<const Target>(x,y);
}
template<class Target>
Unequal<const Target> unequal(const Target& x,const Target& y){
return Unequal<const Target>(x,y);
}
template<class Target>
Less<const Target> less(const Target& x,const Target& y){
return Less<const Target>(x,y);
}
template<class Target>
Greater<const Target> greater(const Target& x,const Target& y){
return Greater<const Target>(x,y);
}
template<class Target>
LessEqualPW<const Target> lessEqualPW(const Target& x,const Target& y){
return LessEqualPW<const Target>(x,y);
}
template<class Target>
GreaterEqualPW<const Target> greaterEqualPW(const Target& x,const Target& y){
return GreaterEqualPW<const Target>(x,y);
}
template<class Target>
LessPW<const Target> lessPW(const Target& x,const Target& y){
return LessPW<const Target>(x,y);
}
template<class Target>
GreaterPW<const Target> greaterPW(const Target& x,const Target& y){
return GreaterPW<const Target>(x,y);
}
// base classes for operator inheritance
template<class Derived>
struct GreaterEqualPWComparable {
bool operator>=(const Derived& y) const{
return greaterEqualPW(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct GreaterEqualComparable {
bool operator>=(const Derived& y) const{
return greaterEqual(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct LessEqualPWComparable {
bool operator<=(const Derived& y) const{
return lessEqualPW(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct LessEqualComparable {
bool operator<=(const Derived& y) const{
return lessEqual(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct GreaterPWComparable {
bool operator>(const Derived& y) const{
return greaterPW(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct GreaterComparable {
bool operator>(const Derived& y) const{
return greater(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct LessPWComparable {
bool operator<(const Derived& y) const{
return lessPW(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct LessComparable {
bool operator<(const Derived& y) const{
return less(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct EqualComparable {
bool operator==(const Derived& y) const{
return equal(static_cast<const Derived&>(*this), y);
}
};
template<class Derived>
struct UnequalComparable {
bool operator!=(const Derived& y) const{
return unequal(static_cast<const Derived&>(*this), y);
}
};
//#################### 4.2 Arithmetic Operators ############################
//#################### 4.2.1 componentwise addition and subtraction ############################
template<class Base>
struct ArithmeticComponentwiseOp : Base {
template<class A, class B>
static Nothing init(A&, B&){ return Nothing(); }
};
struct AdditionOp {
typedef Nothing result_t;
template<class Value, class Value2>
static bool apply(Nothing& r, Value& a, Value2&& b){
a += std::forward<Value2>(b);
return false;
}
};
struct SubtractionOp {
typedef Nothing result_t;
template<class Value, class Value2>
static bool apply(Nothing& r, Value& a, Value2&& b){
a -= std::forward<Value2>(b);
return false;
}
};
// Combiner aliases
template<class Op, class Target>
using ArithmeticComponentwise = BinaryCombiner<
ArithmeticComponentwiseOp<Op>, Target, const Target>;
template<class Target>
using Addition = ArithmeticComponentwise<AdditionOp, Target>;
template<class Target>
using Subtraction = ArithmeticComponentwise<SubtractionOp, Target>;
// factory functions for template argument deduction:
template<class Target>
Subtraction<Target> subtraction(Target& target1, const Target& target2)
{
return Subtraction<Target>(target1, target2);
}
template<class Target>
Addition<Target> addition(Target& target1, const Target& target2)
{
return Addition<Target>(target1, target2);
}
// base classes for operator inheritance
template<class Derived>
struct Addible {
Derived& operator+=(Derived& y){
Derived& thisR(static_cast<Derived&>(*this));
addition(static_cast<Derived&>(*this), y).callEnhance();
return thisR;
}
};
template<class Derived>
struct Subtractable {
Derived& operator-=(Derived& y){
Derived& thisR(static_cast<Derived&>(*this));
subtraction(static_cast<Derived&>(*this), y).callEnhance();
return thisR;
}
};
//###### 4.2.2 scalar (dot-, or inner) product of two objects returning a scalar #############
template<class Scalar>
struct ScalarProductOp {
typedef Scalar result_t;
template<class A, class B>
static result_t init(A&, B&){return 0;}
template<class Value>
static bool apply(Scalar& r, Value&& a, Value&& b){
r += std::forward<Value>(a) * std::forward<Value>(b);
return false;
}
};
template<class Scalar, class Target>
using ScalarProduct = BinaryCombiner<ScalarProductOp<Scalar>, Target>;
template<class Scalar, class Target>
ScalarProduct<Scalar, Target> scalarProduct(Target& target1, Target& target2)
{
return ScalarProduct<Scalar, Target>(target1, target2);
}
template<class Scalar, class Derived>
struct WithScalarProduct {
Scalar operator*(Derived& y){
return scalarProduct<Scalar>(static_cast<Derived&>(*this), y);
}
};
//#################### 4.2.3 multiply/divide all fields by a scalar ############################
template<class Base>
struct ArithmeticScalarOp : Base {
};
template<class Scalar>
struct ScalarMultiplyOp {
typedef Scalar result_t;
template<class Value>
static bool apply(Scalar& r, Value& a){
a *= r;
return false;
}
};
template<class Scalar>
struct ScalarDivideOp {
typedef Scalar result_t;
template<class Value>
static bool apply(Scalar& r, Value& a){
a /= r;
return false;
}
};
template<class Op, class Target>
using ArithmeticScalar = UnaryCombiner<
ArithmeticScalarOp<Op>, Target>;
template<class Scalar, class Target>
using ScalarMultiply = ArithmeticScalar<