File: operations.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (1539 lines) | stat: -rw-r--r-- 63,635 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#pragma once

#include "polymake/internal/operations_basic_defs.h"
#include "polymake/internal/iterators.h"

namespace pm {
namespace operations {

template <typename Operation>
class is_partially_defined {
   typedef typename Operation::first_argument_type first_orig_argument_type;
   typedef typename Operation::second_argument_type second_orig_argument_type;
   typedef typename std::conditional<std::is_same<first_orig_argument_type, void>::value, void*, first_orig_argument_type>::type
      first_argument_type;
   typedef typename std::conditional<std::is_same<second_orig_argument_type, void>::value, void*, second_orig_argument_type>::type
      second_argument_type;
   struct helper {
      template <typename Result>
      static std::true_type Test(const Result&);
      static std::false_type Test(helper*);
   };
   struct catch_first {
      catch_first(typename function_argument<first_argument_type>::const_type) {}
   };
   struct catch_second {
      catch_second(typename function_argument<second_argument_type>::const_type) {}
   };
   struct mix_in : public Operation {
      using Operation::operator();
      helper* operator() (partial_left, catch_first, catch_second) const;
      helper* operator() (partial_right, catch_first, catch_second) const;
   };
public:
   using type = typename mlist_or< decltype(helper::Test( std::declval<mix_in&>()(partial_left(), std::declval<first_argument_type&>(), std::declval<second_argument_type&>()))),
                                   decltype(helper::Test( std::declval<mix_in&>()(partial_right(), std::declval<first_argument_type&>(), std::declval<second_argument_type&>())))
                >::type;
   static constexpr bool value = type::value;
};

template <typename Operation, typename Container1, typename Container2>
struct is_partially_defined_for
   : is_partially_defined<typename binary_op_builder<Operation, typename Container1::const_iterator,
                                                                typename Container2::const_iterator>::operation> {};

template <typename OpRef,
          typename Discr=typename object_traits<typename deref<OpRef>::type>::generic_tag>
struct neg_impl;

template <typename LeftRef, class RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct add_impl;

template <typename LeftRef, typename RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct sub_impl;

template <typename LeftRef, typename RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct mul_impl;

template <typename LeftRef, typename RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct div_impl;

template <typename LeftRef, typename RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct divexact_impl;

template <typename LeftRef, typename RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct tensor_impl;

template <typename OpRef>
struct neg_impl<OpRef, is_scalar>
   : neg_scalar<typename deref<OpRef>::type, typename deref<OpRef>::type> {};

template <typename LeftRef, typename RightRef>
struct add_impl<LeftRef, RightRef, cons<is_scalar, is_scalar> >
   : add_scalar<typename deref<LeftRef>::type, typename deref<RightRef>::type,
                typename add_result<typename deref<LeftRef>::type, typename deref<RightRef>::type>::type> {};

template <typename LeftRef, typename RightRef>
struct sub_impl<LeftRef, RightRef, cons<is_scalar, is_scalar> >
   : sub_scalar<typename deref<LeftRef>::type, typename deref<RightRef>::type,
                typename sub_result<typename deref<LeftRef>::type, typename deref<RightRef>::type>::type> {};

template <typename LeftRef, typename RightRef>
struct mul_impl<LeftRef, RightRef, cons<is_scalar, is_scalar> >
   : mul_scalar<typename deref<LeftRef>::type, typename deref<RightRef>::type,
                typename mul_result<typename deref<LeftRef>::type, typename deref<RightRef>::type>::type> {};

template <typename LeftRef, typename RightRef>
struct div_impl<LeftRef, RightRef, cons<is_scalar, is_scalar> >
   : div_scalar<typename deref<LeftRef>::type, typename deref<RightRef>::type,
                typename div_result<typename deref<LeftRef>::type, typename deref<RightRef>::type>::type> {};

template <typename LeftRef, typename RightRef>
struct divexact_impl<LeftRef, RightRef, cons<is_scalar, is_scalar> >
   : divexact_scalar<typename deref<LeftRef>::type, typename deref<RightRef>::type,
                     typename div_result<typename deref<LeftRef>::type, typename deref<RightRef>::type>::type> {};

template <typename OpRef,
          typename Discr=typename object_traits<typename deref<OpRef>::type>::generic_tag>
struct square_impl {
   typedef OpRef argument_type;
   typedef decltype(sqr(std::declval<OpRef>())) result_type;

   result_type operator() (argument_type a) const
   {
      return sqr(a);
   }
};

template <typename Char, typename Traits, typename Alloc>
struct add_impl<const std::basic_string<Char, Traits, Alloc>&, const std::basic_string<Char, Traits, Alloc>&, cons<is_opaque, is_opaque> > :
   add_scalar<std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc> > {};

template <typename Char, typename Traits, typename Alloc>
struct add_impl<std::basic_string<Char, Traits, Alloc>&, const std::basic_string<Char, Traits, Alloc>&, cons<is_opaque, is_opaque> > :
   add_scalar<std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc> > {};

template <typename Char, typename Traits, typename Alloc>
struct add_impl<const std::basic_string<Char, Traits, Alloc>&, std::basic_string<Char, Traits, Alloc>&, cons<is_opaque, is_opaque> > :
   add_scalar<std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc> > {};

template <typename Char, typename Traits, typename Alloc>
struct add_impl<std::basic_string<Char, Traits, Alloc>&, std::basic_string<Char, Traits, Alloc>&, cons<is_opaque, is_opaque> > :
   add_scalar<std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc>, std::basic_string<Char, Traits, Alloc> > {};

template <typename Char, typename Traits, typename Alloc>
struct add_impl<const std::basic_string<Char, Traits, Alloc>&, const Char*, cons<is_opaque, is_not_object> > :
   add_scalar<std::basic_string<Char, Traits, Alloc>, const Char*, std::basic_string<Char, Traits, Alloc> > {};

template <typename Char, typename Traits, typename Alloc>
struct add_impl<std::basic_string<Char, Traits, Alloc>&, const Char*, cons<is_opaque, is_not_object> > :
   add_scalar<std::basic_string<Char, Traits, Alloc>, const Char*, std::basic_string<Char, Traits, Alloc> > {};

template <typename OpRef>
struct neg : neg_impl<OpRef> {};
template <typename LeftRef, typename RightRef>
struct add : add_impl<LeftRef,RightRef> {};
template <typename LeftRef, typename RightRef>
struct sub : sub_impl<LeftRef,RightRef> {};
template <typename LeftRef, typename RightRef>
struct mul : mul_impl<LeftRef,RightRef> {};
template <typename LeftRef, typename RightRef>
struct div : div_impl<LeftRef,RightRef> {};
template <typename LeftRef, typename RightRef>
struct divexact : divexact_impl<LeftRef,RightRef> {};
template <typename OpRef>
struct square : square_impl<OpRef> {};
template <typename LeftRef, typename RightRef>
struct tensor : tensor_impl<LeftRef,RightRef> {};

// TODO: replace with a generic swap_operands
template <typename LeftRef, typename RightRef>
struct mul_from_left : mul_impl<RightRef, LeftRef> {
   typedef RightRef first_argument_type;
   typedef LeftRef second_argument_type;

   template <typename L, typename R>
   decltype(auto) operator() (L&& l, R&& r) const
   {
      return r*l;
   }

   template <typename L, typename R>
   void assign(L&& l, R&& r) const
   {
      l = r*l;
   }
};

template <typename LeftRef, typename RightRef,
          typename Discr=typename isomorphic_types<typename deref<LeftRef>::type, typename deref<RightRef>::type>::discriminant>
struct bitwise_xor_impl;

template <typename LeftRef, typename RightRef>
struct bitwise_xor : bitwise_xor_impl<LeftRef,RightRef> {};

struct unary_noop {
   typedef void argument_type;
   typedef void result_type;
   template <typename Any>
   result_type operator() (const Any&) const {}
};

struct binary_noop {
   typedef void first_argument_type;
   typedef void second_argument_type;
   typedef void result_type;
   template <typename Any1, typename Any2>
   result_type operator() (const Any1&, const Any2&) const {}
};

template <typename T> class composite_clear;

template <typename OpRef>
class clear {
public:
   typedef OpRef argument_type;
   typedef typename deref<OpRef>::type value_type;
   typedef typename std::conditional<object_traits<value_type>::allow_static, typename attrib<OpRef>::plus_const_ref, value_type>::type result_type;

   result_type operator() () const
   {
      return default_instance(bool_constant<object_traits<value_type>::allow_static>());
   }

   void operator() (typename lvalue_arg<OpRef>::type x) const
   {
      do_clear(x, typename object_traits<value_type>::model());
   }

private:
   static
   result_type default_instance(std::true_type)
   {
      static const value_type dflt = value_type();
      return dflt;
   }

   static
   result_type default_instance(std::false_type)
   {
      return value_type();
   }

   template <typename Model>
   void do_clear(typename lvalue_arg<OpRef>::type x, Model) const
   {
      x=operator()();
   }

   void do_clear(typename lvalue_arg<OpRef>::type x, is_scalar) const
   {
      x=object_traits<value_type>::zero();
   }

   void do_clear(typename lvalue_arg<OpRef>::type, nothing) const {}

   void do_clear(typename lvalue_arg<OpRef>::type x, is_container) const
   {
      x.clear();
   }

   void do_clear(typename lvalue_arg<OpRef>::type x, is_composite) const
   {
      composite_clear<typename object_traits<value_type>::elements> cc;
      object_traits<value_type>::visit_elements(x, cc);
   }
};

template <> class composite_clear<void> {};

template <typename T>
class composite_clear : public composite_clear<typename list_tail<T>::type> {
public:
   typedef typename list_head<T>::type element_type;
   typedef typename deref<element_type>::type value_type;

   composite_clear<typename list_tail<T>::type>&
   operator<< (typename attrib<element_type>::plus_ref elem)
   {
      operations::clear<value_type> clr;
      clr(elem);
      return *this;
   }
};

template <typename Cref, typename Operation>
struct fix1 : Operation {
   typedef Operation super;
   typedef Cref stored_type;
   stored_type c;

   typedef typename Operation::second_argument_type argument_type;
   typedef typename Operation::result_type result_type;

   fix1() {}

   fix1(typename function_argument<Cref>::type c_arg, const Operation& op_arg=Operation())
      : super(op_arg), c(c_arg) {}

   result_type operator() (typename function_argument<argument_type>::type b) const
   {
      return super::operator()(c,b);
   }
};

template <typename Cref, typename Operation>
struct fix2 : Operation {
   typedef Operation super;
   typedef Cref stored_type;
   stored_type c;

   typedef typename Operation::first_argument_type argument_type;
   typedef typename Operation::result_type result_type;

   fix2() {}

   fix2(typename function_argument<Cref>::type c_arg, const Operation& op_arg=Operation())
      : super(op_arg), c(c_arg) {}

   result_type operator() (typename function_argument<argument_type>::type a) const
   {
      return super::operator()(a,c);
   }

   void assign(typename lvalue_arg<argument_type>::type a) const
   {
      super::assign(a,c);
   }
};

// Compositions of two operations

// OuterUnary(InnerUnary(x))
template <typename InnerOperation, typename OuterOperation>
struct composed11 {
   InnerOperation inner;
   OuterOperation outer;

   typedef typename InnerOperation::argument_type argument_type;
   typedef typename OuterOperation::result_type result_type;

   composed11(const InnerOperation& inner_arg=InnerOperation(), const OuterOperation& outer_arg=OuterOperation())
      : inner(inner_arg), outer(outer_arg) {}
   composed11(const OuterOperation& outer_arg)
      : outer(outer_arg) {}

   result_type operator() (typename function_argument<argument_type>::type a) const
   {
      return outer(inner(a));
   }

   void assign(typename lvalue_arg<argument_type>::type a) const
   {
      inner.assign(a); outer.assign(a);
   }
};

// OuterUnary(InnerBinary(x,y))
template <typename InnerOperation, typename OuterOperation, bool is_partial=is_partially_defined<InnerOperation>::value>
struct composed21 {
   InnerOperation inner;
   OuterOperation outer;

   typedef typename InnerOperation::first_argument_type first_argument_type;
   typedef typename InnerOperation::second_argument_type second_argument_type;
   typedef typename OuterOperation::result_type result_type;

   composed21(const InnerOperation& inner_arg=InnerOperation(), const OuterOperation& outer_arg=OuterOperation())
      : inner(inner_arg), outer(outer_arg) {}
   composed21(const OuterOperation& outer_arg)
      : outer(outer_arg) {}

   result_type operator() (typename function_argument<first_argument_type>::type a,
                           typename function_argument<second_argument_type>::type b) const
   {
      return outer(inner(a,b));
   }

   void assign(typename lvalue_arg<first_argument_type>::type a,
               typename function_argument<second_argument_type>::type b) const
   {
      inner.assign(a,b); outer.assign(a);
   }
};

template <typename InnerOperation, typename OuterOperation>
struct composed21<InnerOperation, OuterOperation, true>
   : public composed21<InnerOperation, OuterOperation, false> {
   typedef composed21<InnerOperation, OuterOperation, false> _super;
public:
   composed21(const InnerOperation& inner_arg=InnerOperation(), const OuterOperation& outer_arg=OuterOperation())
      : _super(inner_arg,outer_arg) {}
   composed21(const OuterOperation& outer_arg)
      : _super(outer_arg) {}

   template <typename Iterator2>
   typename _super::result_type
   operator() (partial_left, typename function_argument<typename _super::first_argument_type>::type a,
               const Iterator2& it2) const
   {
      return outer(inner(partial_left(), a, it2));
   }
   template <typename Iterator1>
   typename _super::result_type
   operator() (partial_right, const Iterator1& it1,
               typename function_argument<typename _super::second_argument_type>::type b) const
   {
      return outer(inner(partial_right(), it1, b));
   }
   using _super::operator();
};

template <typename InnerOperation2, typename OuterOperation>
struct composed12_is_partially_defined : is_partially_defined<OuterOperation> {};

template <typename OuterOperation>
struct composed12_is_partially_defined<OuterOperation, void> : is_partially_defined<OuterOperation> {};

// OuterBinary(InnerUnary1(x), InnerUnary2(y))
template <typename InnerOperation1, typename InnerOperation2, typename OuterOperation,
          bool is_partial=composed12_is_partially_defined<InnerOperation2,OuterOperation>::value>
struct composed12 {
   InnerOperation1 inner1;
   InnerOperation2 inner2;
   OuterOperation outer;

   typedef typename InnerOperation1::argument_type first_argument_type;
   typedef typename InnerOperation2::argument_type second_argument_type;
   typedef typename OuterOperation::result_type result_type;

   composed12(const InnerOperation1& inner_arg1=InnerOperation1(),
              const InnerOperation2& inner_arg2=InnerOperation2(),
              const OuterOperation& outer_arg=OuterOperation())
      : inner1(inner_arg1), inner2(inner_arg2), outer(outer_arg) {}
   composed12(const InnerOperation1& inner_arg1, const OuterOperation& outer_arg)
      : inner1(inner_arg1), outer(outer_arg) {}
   composed12(const InnerOperation2& inner_arg2, const OuterOperation& outer_arg)
      : inner2(inner_arg2), outer(outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : outer(outer_arg) {}

   result_type operator() (typename function_argument<first_argument_type>::type a,
                           typename function_argument<second_argument_type>::type b) const
   {
      return outer(inner1(a), inner2(b));
   }

   void assign(typename lvalue_arg<first_argument_type>::type a,
               typename function_argument<second_argument_type>::type b) const
   {
      inner1.assign(a); outer.assign(a, inner2(b));
   }
};

template <typename InnerOperation1, typename InnerOperation2, typename OuterOperation>
struct composed12<InnerOperation1, InnerOperation2, OuterOperation, true>
   : public composed12<InnerOperation1, InnerOperation2, OuterOperation, false> {
   typedef composed12<InnerOperation1, InnerOperation2, OuterOperation, false> _super;
public:
   composed12(const InnerOperation1& inner_arg1=InnerOperation1(),
              const InnerOperation2& inner_arg2=InnerOperation2(),
              const OuterOperation& outer_arg=OuterOperation())
      : _super(inner_arg1,inner_arg2,outer_arg) {}
   composed12(const InnerOperation1& inner_arg1, const OuterOperation& outer_arg)
      : _super(inner_arg1,outer_arg) {}
   composed12(const InnerOperation2& inner_arg2, const OuterOperation& outer_arg)
      : _super(inner_arg2,outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : _super(outer_arg) {}

   template <typename Iterator2>
   typename _super::result_type
   operator() (partial_left, typename function_argument<typename _super::first_argument_type>::type a,
               const Iterator2& it2) const
   {
      return outer(partial_left(), inner1(a), it2);
   }
   template <typename Iterator1>
   typename _super::result_type
   operator() (partial_right, const Iterator1& it1,
               typename function_argument<typename _super::second_argument_type>::type b) const
   {
      return outer(partial_right(), it1, inner2(b));
   }
   using _super::operator();
};

// OuterBinary(InnerUnary(x), y)
template <typename InnerOperation1, typename OuterOperation>
struct composed12<InnerOperation1, void, OuterOperation, false> {
   InnerOperation1 inner1;
   OuterOperation outer;

   typedef typename InnerOperation1::argument_type first_argument_type;
   typedef typename OuterOperation::second_argument_type second_argument_type;
   typedef typename OuterOperation::result_type result_type;

   composed12(const InnerOperation1& inner_arg1=InnerOperation1(),
              const OuterOperation& outer_arg=OuterOperation())
      : inner1(inner_arg1), outer(outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : outer(outer_arg) {}

   result_type operator() (typename function_argument<first_argument_type>::type a,
                           typename function_argument<second_argument_type>::type b) const
   {
      return outer(inner1(a), b);
   }

   void assign(typename lvalue_arg<first_argument_type>::type a,
               typename function_argument<second_argument_type>::type b) const
   {
      inner1.assign(a); outer.assign(a, b);
   }
};

template <typename InnerOperation1, typename OuterOperation>
struct composed12<InnerOperation1, void, OuterOperation, true>
   : public composed12<InnerOperation1, void, OuterOperation, false> {
   typedef composed12<InnerOperation1, void, OuterOperation, false> _super;
public:
   composed12(const InnerOperation1& inner_arg1=InnerOperation1(),
              const OuterOperation& outer_arg=OuterOperation())
      : _super(inner_arg1,outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : _super(outer_arg) {}

   template <typename Iterator2>
   typename _super::result_type
   operator() (partial_left, typename function_argument<typename _super::first_argument_type>::type a,
               const Iterator2& it2) const
   {
      return outer(partial_left(), inner1(a), it2);
   }
   template <typename Iterator1>
   typename _super::result_type
   operator() (partial_right, const Iterator1& it1,
               typename function_argument<typename _super::second_argument_type>::type b) const
   {
      return outer(partial_right(), it1, b);
   }
   using _super::operator();
};

// OuterBinary(x, InnerUnary(y))
template <typename InnerOperation2, typename OuterOperation>
struct composed12<void, InnerOperation2, OuterOperation, false> {
   InnerOperation2 inner2;
   OuterOperation outer;

   typedef typename OuterOperation::first_argument_type first_argument_type;
   typedef typename InnerOperation2::argument_type second_argument_type;
   typedef typename OuterOperation::result_type result_type;

   composed12(const InnerOperation2& inner_arg2=InnerOperation2(),
              const OuterOperation& outer_arg=OuterOperation())
      : inner2(inner_arg2), outer(outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : outer(outer_arg) {}

   result_type operator() (typename function_argument<first_argument_type>::type a,
                           typename function_argument<second_argument_type>::type b) const
   {
      return outer(a, inner2(b));
   }

   void assign(typename lvalue_arg<first_argument_type>::type a,
               typename function_argument<second_argument_type>::type b) const
   {
      outer.assign(a, inner2(b));
   }
};

template <typename InnerOperation2, typename OuterOperation>
struct composed12<void, InnerOperation2, OuterOperation, true>
   : public composed12<void, InnerOperation2, OuterOperation, false> {
   typedef composed12<void, InnerOperation2, OuterOperation, false> _super;
public:
   composed12(const InnerOperation2& inner_arg2=InnerOperation2(),
              const OuterOperation& outer_arg=OuterOperation())
      : _super(inner_arg2,outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : _super(outer_arg) {}

   template <typename Iterator2>
   typename _super::result_type
   operator() (partial_left, typename function_argument<typename _super::first_argument_type>::type a,
               const Iterator2& it2) const
   {
      return outer(partial_left(), a, it2);
   }

   template <typename Iterator1>
   typename _super::result_type
   operator() (partial_right, const Iterator1& it1,
               typename function_argument<typename _super::second_argument_type>::type b) const
   {
      return outer(partial_right(), it1, inner2(b));
   }
   using _super::operator();
};

// OuterBinary(InnerUnary(x), InnerUnary(y))
template <typename InnerOperation, typename OuterOperation>
struct composed12<InnerOperation, OuterOperation, void, false> {
   InnerOperation inner;
   OuterOperation outer;

   typedef typename InnerOperation::argument_type first_argument_type;
   typedef typename InnerOperation::argument_type second_argument_type;
   typedef typename OuterOperation::result_type result_type;

   composed12(const InnerOperation& inner_arg=InnerOperation(),
              const OuterOperation& outer_arg=OuterOperation())
      : inner(inner_arg), outer(outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : outer(outer_arg) {}

   result_type operator() (typename function_argument<first_argument_type>::type a,
                           typename function_argument<second_argument_type>::type b) const
   {
      return outer(inner(a), inner(b));
   }

   void assign(typename lvalue_arg<first_argument_type>::type a,
               typename function_argument<second_argument_type>::type b) const
   {
      inner.assign(a); outer.assign(a, inner(b));
   }
};

template <typename InnerOperation, typename OuterOperation>
struct composed12<InnerOperation, OuterOperation, void, true>
   : public composed12<InnerOperation, OuterOperation, void, false> {
   typedef composed12<InnerOperation, OuterOperation, void, false> _super;
public:
   composed12(const InnerOperation& inner_arg=InnerOperation(),
              const OuterOperation& outer_arg=OuterOperation())
      : _super(inner_arg,outer_arg) {}
   composed12(const OuterOperation& outer_arg)
      : _super(outer_arg) {}

   template <typename Iterator2>
   typename _super::result_type
   operator() (partial_left, typename function_argument<typename _super::first_argument_type>::type a,
               const Iterator2& it2) const
   {
      return outer(partial_left(), inner(a), it2);
   }

   template <typename Iterator1>
   typename _super::result_type
   operator() (partial_right, const Iterator1& it1,
               typename function_argument<typename _super::second_argument_type>::type b) const
   {
      return outer(partial_right(), it1, inner(b));
   }
   using _super::operator();
};

template <typename UnaryOperation, typename Right=void>
struct apply1 : UnaryOperation {
   typedef typename UnaryOperation::argument_type first_argument_type;
   typedef Right second_argument_type;

   apply1(const UnaryOperation& op_arg=UnaryOperation()) : UnaryOperation(op_arg) {}

   typename UnaryOperation::result_type
   operator() (typename function_argument<first_argument_type>::type x, typename attrib<Right>::plus_const_ref) const
   {
      return UnaryOperation::operator()(x);
   }
};

template <typename UnaryOperation>
struct apply1<UnaryOperation,void> : incomplete {};

template <typename UnaryOperation, typename Left=void>
struct apply2 : UnaryOperation {
   typedef Left first_argument_type;
   typedef typename UnaryOperation::argument_type second_argument_type;

   apply2(const UnaryOperation& op_arg=UnaryOperation()) : UnaryOperation(op_arg) {}

   typename UnaryOperation::result_type
   operator() (typename attrib<Left>::plus_const_ref, typename function_argument<second_argument_type>::type x) const
   {
      return UnaryOperation::operator()(x);
   }
};

template <typename UnaryOperation>
struct apply2<UnaryOperation,void> : incomplete {};

template <typename Iterator>
class random_access {
protected:
   Iterator start;
public:
   typedef typename iterator_traits<Iterator>::difference_type argument_type;
   typedef typename iterator_traits<Iterator>::reference result_type;

   random_access(const Iterator& start_arg=Iterator())
      : start(start_arg) {}

   result_type operator() (argument_type i) const
   {
      return start[i];
   }
};

template <typename Operation, typename Iterator>
class unary_indirect : public Operation {
protected:
   Iterator start;
public:
   typedef typename iterator_traits<Iterator>::difference_type argument_type;

   unary_indirect(const Iterator& start_arg=Iterator())
      : start(start_arg) {}

   typename Operation::result_type operator() (argument_type i) const
   {
      return Operation::operator()(start[i]);
   }
};

template <typename Operation, typename Iterator1, typename Iterator2=Iterator1>
class binary_indirect : public Operation {
protected:
   Iterator1 start1;
   Iterator2 start2;
public:
   typedef typename iterator_traits<Iterator1>::difference_type first_argument_type;
   typedef typename iterator_traits<Iterator2>::difference_type second_argument_type;

   binary_indirect(const Iterator1& start1_arg)
      : start1(start1_arg), start2(start1_arg) {}
   binary_indirect(const Iterator1& start1_arg, const Iterator2& start2_arg)
      : start1(start1_arg), start2(start2_arg) {}

   typename Operation::result_type operator() (first_argument_type i1, second_argument_type i2) const
   {
      return Operation::operator()(start1[i1], start2[i2]);
   }
   // for equal_range & Co.
   typename Operation::result_type operator() (first_argument_type i1,
                                               typename function_argument<typename Operation::second_argument_type>::type x2) const
   {
      return Operation::operator()(start1[i1], x2);
   }
   typename Operation::result_type operator() (typename function_argument<typename Operation::first_argument_type>::type x1,
                                               second_argument_type i2) const
   {
      return Operation::operator()(x1, start2[i2]);
   }
};

template <typename Class, typename Member, Member Class::*Ptr, typename ObjRef=void>
class member {
public:
   typedef ObjRef argument_type;
   typedef typename inherit_ref<Member,ObjRef>::type result_type;
   result_type operator() (argument_type obj) const { return obj.*Ptr; }
};

template <typename Class, typename Member, Member Class::*Ptr>
class member<Class, Member, Ptr, void> : incomplete {};

template <typename Cref, typename M>
class var_member {
public:
   typedef Cref argument_type;
   typedef typename deref<Cref>::type C;
   typedef typename inherit_ref<M,Cref>::type result_type;

   var_member(M C::* ptr_arg) : ptr(ptr_arg) {}

   result_type operator() (typename function_argument<Cref>::type c) const { return c.*ptr; }
protected:
   M C::* ptr;
};

template <typename Left, typename Right>
struct swap_op {
   typedef Left first_argument_type;
   typedef Right second_argument_type;
   typedef const swap_op& result_type;

   result_type operator() (typename function_argument<Left>::type a, typename function_argument<Right>::type b) const
   {
      std::swap(a,b);
      return *this;
   }
};

template <typename TRef>
struct move {
   typedef TRef argument_type;
   typedef std::add_lvalue_reference_t<pure_type_t<TRef>> unconst_type;
   typedef std::add_rvalue_reference_t<pure_type_t<TRef>> result_type;

   result_type operator() (argument_type x) const { return static_cast<result_type>(const_cast<unconst_type>(x)); }
};

}

template <typename Iterator>
struct operation_cross_const_helper< operations::random_access<Iterator> > {
   typedef operations::random_access<typename iterator_traits<Iterator>::iterator> operation;
   typedef operations::random_access<typename iterator_traits<Iterator>::const_iterator> const_operation;
};

// Automatic construction of composed operations

template <typename InnerOperation, typename OuterOperation, typename Iterator, typename Reference>
struct unary_op_builder< operations::composed11<InnerOperation, OuterOperation>,
                         Iterator, Reference> {
   typedef operations::composed11<InnerOperation, OuterOperation> Operation;
   typedef unary_op_builder<InnerOperation, Iterator, Reference> inner_builder;
   typedef typename inner_builder::operation inner_operation;
   typedef typename inner_operation::result_type inner_result;
   typedef unary_op_builder<OuterOperation, typename deref<inner_result>::type, inner_result> outer_builder;
   typedef typename outer_builder::operation outer_operation;
   typedef operations::composed11<inner_operation, outer_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation, typename AltOuterOperation>
   static operation create(const operations::composed11<AltInnerOperation, AltOuterOperation>& op)
   {
      return operation(inner_builder::create(op.inner), outer_builder::create(op.outer));
   }
};

template <typename InnerOperation, typename OuterOperation, bool is_partial,
          typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::composed21<InnerOperation, OuterOperation, is_partial>,
                          Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::composed21<InnerOperation, OuterOperation, is_partial> Operation;
   typedef binary_op_builder<InnerOperation, Iterator1, Iterator2, Reference1, Reference2> inner_builder;
   typedef typename inner_builder::operation inner_operation;
   typedef typename inner_operation::result_type inner_result;
   typedef unary_op_builder<OuterOperation, typename deref<inner_result>::type, inner_result> outer_builder;
   typedef typename outer_builder::operation outer_operation;
   typedef operations::composed21<inner_operation, outer_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation, typename AltOuterOperation>
   static operation create(const operations::composed21<AltInnerOperation, AltOuterOperation, is_partial>& op)
   {
      return operation(inner_builder::create(op.inner), outer_builder::create(op.outer));
   }
};
                         
template <typename InnerOperation1, typename InnerOperation2, typename OuterOperation, bool is_partial,
          typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::composed12<InnerOperation1, InnerOperation2, OuterOperation, is_partial>,
                          Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::composed12<InnerOperation1, InnerOperation2, OuterOperation, is_partial> Operation;
   typedef unary_op_builder<InnerOperation1, Iterator1, Reference1> inner_builder1;
   typedef unary_op_builder<InnerOperation2, Iterator2, Reference2> inner_builder2;
   typedef typename inner_builder1::operation inner_operation1;
   typedef typename inner_builder2::operation inner_operation2;
   typedef typename inner_operation1::result_type inner_result1;
   typedef typename inner_operation2::result_type inner_result2;
   typedef binary_op_builder<OuterOperation, typename deref<inner_result1>::type, typename deref<inner_result2>::type,
                             inner_result1, inner_result2>
      outer_builder;
   typedef typename outer_builder::operation outer_operation;
   typedef operations::composed12<inner_operation1, inner_operation2, outer_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation1, typename AltInnerOperation2, typename AltOuterOperation>
   static operation create(const operations::composed12<AltInnerOperation1, AltInnerOperation2, AltOuterOperation, is_partial>& op)
   {
      return operation(inner_builder1::create(op.inner1), inner_builder2::create(op.inner2), outer_builder::create(op.outer));
   }
};

template <typename InnerOperation1, typename OuterOperation, bool is_partial,
          typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::composed12<InnerOperation1, void, OuterOperation, is_partial>,
                          Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::composed12<InnerOperation1, void, OuterOperation, is_partial> Operation;
   typedef unary_op_builder<InnerOperation1, Iterator1, Reference1> inner_builder1;
   typedef typename inner_builder1::operation inner_operation1;
   typedef typename inner_operation1::result_type inner_result1;
   typedef binary_op_builder<OuterOperation, typename deref<inner_result1>::type, Iterator2,
                             inner_result1, Reference2>
      outer_builder;
   typedef typename outer_builder::operation outer_operation;
   typedef operations::composed12<inner_operation1, void, outer_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation1, typename AltOuterOperation>
   static operation create(const operations::composed12<AltInnerOperation1, void, AltOuterOperation, is_partial>& op)
   {
      return operation(inner_builder1::create(op.inner1), outer_builder::create(op.outer));
   }
};
                         
template <typename InnerOperation2, typename OuterOperation, bool is_partial,
          typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::composed12<void, InnerOperation2, OuterOperation, is_partial>,
                          Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::composed12<void, InnerOperation2, OuterOperation, is_partial> Operation;
   typedef unary_op_builder<InnerOperation2, Iterator2, Reference2> inner_builder2;
   typedef typename inner_builder2::operation inner_operation2;
   typedef typename inner_operation2::result_type inner_result2;
   typedef binary_op_builder<OuterOperation, Iterator1, typename deref<inner_result2>::type,
                             Reference1, inner_result2>
      outer_builder;
   typedef typename outer_builder::operation outer_operation;
   typedef operations::composed12<void, inner_operation2, outer_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation2, typename AltOuterOperation>
   static operation create(const operations::composed12<void, AltInnerOperation2, AltOuterOperation, is_partial>& op)
   {
      return operation(inner_builder2::create(op.inner2), outer_builder::create(op.outer));
   }
};

template <typename InnerOperation, typename OuterOperation, bool is_partial,
          typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::composed12<InnerOperation, OuterOperation, void, is_partial>,
                          Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::composed12<InnerOperation, OuterOperation, void, is_partial> Operation;
   typedef unary_op_builder<InnerOperation, Iterator1, Reference1> inner_builder1;
   typedef unary_op_builder<InnerOperation, Iterator2, Reference2> inner_builder2;
   typedef typename inner_builder1::operation inner_operation1;
   typedef typename inner_builder2::operation inner_operation2;
   typedef typename inner_operation1::result_type inner_result1;
   typedef typename inner_operation2::result_type inner_result2;
   typedef binary_op_builder<OuterOperation, typename deref<inner_result1>::type, typename deref<inner_result2>::type,
                             inner_result1, inner_result2>
      outer_builder;
   typedef typename outer_builder::operation outer_operation;

   static const bool ident12=std::is_same<inner_operation1, inner_operation2>::value;
   typedef typename std::conditional< ident12,
                                      operations::composed12<inner_operation1, outer_operation, void>,
                                      operations::composed12<inner_operation1, inner_operation2, outer_operation> >::type
      operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation, typename AltOuterOperation>
   static operation create_impl(const operations::composed12<AltInnerOperation, AltOuterOperation, void, is_partial>& op, std::false_type)
   {
      return operation(inner_builder1::create(op.inner), inner_builder2::create(op.inner), outer_builder::create(op.outer));
   }

   template <typename AltInnerOperation, typename AltOuterOperation>
   static operation create_impl(const operations::composed12<AltInnerOperation, AltOuterOperation, void, is_partial>& op, std::true_type)
   {
      return operation(inner_builder1::create(op.inner), outer_builder::create(op.outer));
   }

   template <typename AltInnerOperation, typename AltOuterOperation>
   static operation create(const operations::composed12<AltInnerOperation, AltOuterOperation, void, is_partial>& op)
   {
      return create_impl(op, bool_constant<ident12>());
   }
};

template <typename Cref, typename InnerOperation, typename Iterator, typename Reference>
struct unary_op_builder< operations::fix1<Cref, InnerOperation>, Iterator, Reference> {
   typedef operations::fix1<Cref, InnerOperation> Operation;
   typedef binary_op_builder<InnerOperation, void, Iterator, typename attrib<typename Operation::stored_type>::plus_const_ref, Reference> inner_builder;
   typedef typename inner_builder::operation inner_operation;
   typedef operations::fix1<Cref, inner_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation>
   static operation create(const operations::fix1<Cref, AltInnerOperation>& op)
   {
      return operation(op.c, inner_builder::create(op));
   }
};

template <typename Cref, typename InnerOperation, typename Iterator, typename Reference>
struct unary_op_builder< operations::fix2<Cref, InnerOperation>, Iterator, Reference> {
   typedef operations::fix2<Cref, InnerOperation> Operation;
   typedef binary_op_builder<InnerOperation, Iterator, void, Reference, typename attrib<typename Operation::stored_type>::plus_const_ref> inner_builder;
   typedef typename inner_builder::operation inner_operation;
   typedef operations::fix2<Cref, inner_operation> operation;

   static const operation& create(const operation& op) { return op; }

   template <typename AltInnerOperation>
   static operation create(const operations::fix2<Cref, AltInnerOperation>& op)
   {
      return operation(op.c, inner_builder::create(op));
   }
};

template <typename Class, typename Member, Member Class::* Ptr, typename Iterator, typename Reference>
struct unary_op_builder< operations::member<Class,Member,Ptr,void>, Iterator, Reference>
   : empty_op_builder< operations::member<Class,Member,Ptr,Reference> > {};

template <typename UnaryOperation, typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder<operations::apply1<UnaryOperation>, Iterator1, Iterator2, Reference1, Reference2>
   : empty_op_builder< operations::apply1<typename unary_op_builder<UnaryOperation,Iterator1,Reference1>::operation, Iterator2> > {};

template <typename UnaryOperation, typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder<operations::apply2<UnaryOperation>, Iterator1, Iterator2, Reference1, Reference2>
   : empty_op_builder< operations::apply2<typename unary_op_builder<UnaryOperation,Iterator2,Reference2>::operation, Iterator1> > {};

template <template <typename> class Operation, typename Iterator> inline
operations::unary_indirect< Operation<typename iterator_traits<Iterator>::reference>, Iterator >
construct_indirect_operation(const Iterator& src)
{
   return src;
}

template <typename Iterator, template <typename> class Operation> inline
operations::unary_indirect< Operation<typename iterator_traits<Iterator>::reference>, Iterator >
construct_indirect_operation(const Iterator& src, const BuildUnary<Operation>&)
{
   return src;
}

template <template <typename,typename> class Operation, typename Iterator> inline
operations::binary_indirect< Operation<typename iterator_traits<Iterator>::reference,
                                       typename iterator_traits<Iterator>::reference>, Iterator >
construct_indirect_operation(const Iterator& src)
{
   return src;
}

template <typename Iterator, template <typename,typename> class Operation> inline
operations::binary_indirect< Operation<typename iterator_traits<Iterator>::reference,
                                       typename iterator_traits<Iterator>::reference>, Iterator >
construct_indirect_operation(const Iterator& src, const BuildBinary<Operation>&)
{
   return src;
}

template <template <typename,typename> class Operation, typename Iterator1, typename Iterator2> inline
operations::binary_indirect< Operation<typename iterator_traits<Iterator1>::reference,
                                       typename iterator_traits<Iterator2>::reference>, Iterator1, Iterator2 >
construct_indirect_operation(const Iterator1& src1, const Iterator2& src2)
{
   return operations::binary_indirect< Operation<typename iterator_traits<Iterator1>::reference,
                                                 typename iterator_traits<Iterator2>::reference>, Iterator1, Iterator2 >
                                     (src1,src2);
}

template <typename Iterator1, typename Iterator2, template <typename,typename> class Operation>
operations::binary_indirect< Operation<typename iterator_traits<Iterator1>::reference,
                                       typename iterator_traits<Iterator2>::reference>, Iterator1, Iterator2 >
construct_indirect_operation(const Iterator1& src1, const Iterator2& src2, const BuildBinary<Operation>&)
{
   return operations::binary_indirect< Operation<typename iterator_traits<Iterator1>::reference,
                                                 typename iterator_traits<Iterator2>::reference>, Iterator1, Iterator2 >
                                     (src1,src2);
}

template <typename Iterator, typename Operation>
void perform_assign(Iterator&& dst, const Operation& op_arg)
{
   typedef unary_op_builder<Operation, pure_type_t<Iterator>> opb;
   const typename opb::operation& op=opb::create(op_arg);
   for (; !dst.at_end(); ++dst)
      op.assign(*dst);
}

template <typename Iterator1, typename Iterator2, typename Operation>
void perform_assign(Iterator1&& dst, Iterator2&& src2, const Operation& op_arg,
                    std::enable_if_t<check_iterator_feature<pure_type_t<Iterator1>, end_sensitive>::value, void**> =nullptr)
{
   typedef binary_op_builder<Operation, pure_type_t<Iterator1>, pure_type_t<Iterator2>> opb;
   const typename opb::operation& op=opb::create(op_arg);
   for (; !dst.at_end(); ++dst, ++src2)
      op.assign(*dst, *src2);
}

template <typename Iterator1, typename Iterator2, typename Operation>
void perform_assign(Iterator1&& dst, Iterator2&& src2, const Operation& op_arg,
                    std::enable_if_t<!check_iterator_feature<pure_type_t<Iterator1>, end_sensitive>::value &&
                                     check_iterator_feature<pure_type_t<Iterator2>, end_sensitive>::value, void**> =nullptr)
{
   typedef binary_op_builder<Operation, pure_type_t<Iterator1>, pure_type_t<Iterator2>> opb;
   const typename opb::operation& op=opb::create(op_arg);
   for (; !src2.at_end(); ++dst, ++src2)
      op.assign(*dst, *src2);
}

template <typename Iterator, typename Operation, typename Object,
          typename=std::enable_if_t<!is_effectively_const<Object>::value>>
void accumulate_in(Iterator&& src, const Operation& op_arg, Object&& x)
{
   typedef binary_op_builder<Operation, const pure_type_t<Object>*, pure_type_t<Iterator>> opb;
   const typename opb::operation& op=opb::create(op_arg);
   for (; !src.at_end(); ++src) op.assign(x, *src);
}

template <typename Container, typename Operation>
auto accumulate(const Container& c, const Operation& op_arg)
{
   using elem_t = typename object_traits<typename Container::value_type>::persistent_type;
   if (c.empty()) return elem_t{};
   auto src = entire_range(c);
   elem_t x{*src};
   accumulate_in(++src, op_arg, x);
   return x;
}

template <typename Container>
auto average(const Container& c)
{
   return accumulate(c, BuildBinary<operations::add>()) / static_cast<matching_primitive_number_t<Container>>(c.size());
}

namespace operations {

template <template <typename> class Result, typename ArgRef=void>
struct construct_unary {
   typedef ArgRef argument_type;
   typedef Result<ArgRef> result_type;

   template <typename X>
   result_type operator() (X&& x) const
   {
      return result_type(std::forward<X>(x));
   }
};

template <template <typename> class Result>
struct construct_unary<Result, void> : incomplete {};

template <template <typename,typename> class Result, typename Second, typename ArgRef=void>
struct construct_unary2 {
   typedef ArgRef argument_type;
   typedef Result<ArgRef, Second> result_type;

   template <typename X>
   result_type operator() (X&& x) const
   {
      return result_type(std::forward<X>(x));
   }
};

template <template <typename,typename> class Result, typename Second>
struct construct_unary2<Result, Second, void> : incomplete {};

template <template <typename,typename,typename> class Result, typename Second, typename Third, typename ArgRef=void>
struct construct_unary3 {
   typedef ArgRef argument_type;
   typedef Result<ArgRef, Second, Third> result_type;

   template <typename X>
   result_type operator() (X&& x) const
   {
      return result_type(std::forward<X>(x));
   }
};

template <template <typename,typename,typename> class Result, typename Second, typename Third>
struct construct_unary3<Result, Second, Third, void> : incomplete {};

template <template <typename> class Result, typename Second, typename ArgRef=void>
struct construct_unary_with_arg {
protected:
   Second second;
public:
   typedef ArgRef argument_type;
   typedef Result<ArgRef> result_type;

   construct_unary_with_arg() {}
   construct_unary_with_arg(const Second& second_arg) : second(second_arg) {}

   template <typename X>
   result_type operator() (X&& x) const
   {
      return result_type(std::forward<X>(x), second);
   }
};

template <template <typename> class Result, typename Second>
struct construct_unary_with_arg<Result, Second, void> : incomplete {
protected:
   Second second;
public:
   construct_unary_with_arg() {}
   construct_unary_with_arg(const Second& second_arg) : second(second_arg) {}

   operator const Second& () const { return second; }
};

template <template <typename,typename> class Result, typename Second, typename ArgRef=void>
struct construct_unary2_with_arg {
protected:
   Second second;
public:
   typedef ArgRef argument_type;
   typedef Result<ArgRef, Second> result_type;

   construct_unary2_with_arg() {}
   construct_unary2_with_arg(const Second& second_arg) : second(second_arg) {}

   template <typename X>
   result_type operator() (X&& x) const
   {
      return result_type(std::forward<X>(x), second);
   }
};

template <template <typename,typename> class Result, typename Second>
struct construct_unary2_with_arg<Result, Second, void> : incomplete {
protected:
   Second second;
public:
   construct_unary2_with_arg() {}
   construct_unary2_with_arg(const Second& second_arg) : second(second_arg) {}

   operator const Second& () const { return second; }
};

template <template <typename,typename> class Result, typename LeftRef=void, typename RightRef=void>
struct construct_binary {
   typedef LeftRef first_argument_type;
   typedef RightRef second_argument_type;
   typedef Result<LeftRef, RightRef> result_type;

   template <typename L, typename R>
   result_type operator() (L&& l, R&& r) const
   {
      return result_type(std::forward<L>(l), std::forward<R>(r));
   }
};

template <template <typename,typename> class Result>
struct construct_binary<Result, void, void> : incomplete {};

template <template <typename,typename,typename> class Result, typename Third, typename LeftRef=void, typename RightRef=void>
struct construct_binary2 {
   typedef LeftRef first_argument_type;
   typedef RightRef second_argument_type;
   typedef Result<LeftRef, RightRef, Third> result_type;

   template <typename L, typename R>
   result_type operator() (L&& l, R&& r) const
   {
      return result_type(std::forward<L>(l), std::forward<R>(r));
   }
};

template <template <typename,typename,typename> class Result, typename Third>
struct construct_binary2<Result, Third, void, void> : incomplete {};

template <template <typename,typename> class Result, typename Third, typename LeftRef=void, typename RightRef=void>
struct construct_binary_with_arg {
protected:
   Third third;
public:
   typedef LeftRef first_argument_type;
   typedef RightRef second_argument_type;
   typedef Result<LeftRef, RightRef> result_type;

   construct_binary_with_arg() {}
   construct_binary_with_arg(const Third& third_arg) : third(third_arg) {}

   template <typename L, typename R>
   result_type operator() (L&& l, R&& r) const
   {
      return result_type(std::forward<L>(l), std::forward<R>(r), third);
   }
};

template <template <typename,typename> class Result, typename Third>
struct construct_binary_with_arg<Result, Third, void, void> : incomplete {
protected:
   Third third;
public:
   construct_binary_with_arg() {}
   construct_binary_with_arg(const Third& third_arg) : third(third_arg) {}

   operator const Third& () const { return third; }
};

template <template <typename,typename,typename> class Result, typename Third, typename LeftRef=void, typename RightRef=void>
struct construct_binary2_with_arg {
protected:
   Third third;
public:
   typedef LeftRef first_argument_type;
   typedef RightRef second_argument_type;
   typedef Result<LeftRef, RightRef, Third> result_type;

   construct_binary2_with_arg() {}
   construct_binary2_with_arg(const Third& third_arg) : third(third_arg) {}

   template <typename L, typename R>
   result_type operator() (L&& l, R&& r) const
   {
      return result_type(std::forward<L>(l), std::forward<R>(r), third);
   }
};

template <template <typename,typename,typename> class Result, typename Third>
struct construct_binary2_with_arg<Result, Third, void, void> : incomplete {
protected:
   Third third;
public:
   construct_binary2_with_arg() {}
   construct_binary2_with_arg(const Third& third_arg) : third(third_arg) {}

   operator const Third& () const { return third; }
};

template <typename IteratorRef>
struct index2element {
   typedef IteratorRef argument_type;
   typedef Int result_type;
   result_type operator() (argument_type it) const { return it.index(); }
};

template <typename IteratorRef>
struct dereference {
   typedef IteratorRef argument_type;
   typedef typename iterator_traits<typename deref<IteratorRef>::type>::reference ref;
   typedef typename std::conditional<attrib<IteratorRef>::is_const, typename attrib<ref>::plus_const, ref>::type result_type;
   result_type operator() (IteratorRef it) const { return *it; }
};

template <typename Ref>
struct identity {
   typedef Ref argument_type;
   typedef Ref result_type;
   Ref operator() (Ref x) const { return x; }
   void assign(typename lvalue_arg<Ref>::type) const {}
};

template <typename Ref>
struct ref2pointer {
   typedef Ref argument_type;
   typedef typename deref<Ref>::minus_ref* result_type;
   result_type operator() (Ref x) const { return &x; }
};

template <typename OrigRef, typename ApparentRef, bool _need_cache=!attrib<OrigRef>::is_reference>
struct reinterpret_impl {
   typedef OrigRef argument_type;
   typedef ApparentRef result_type;
   result_type operator() (OrigRef x) const
   {
      return reinterpret_cast<result_type>(x);
   }
};

template <typename Orig, typename ApparentRef>
struct reinterpret_impl<Orig, ApparentRef, true> {
   typedef Orig argument_type;
   typedef typename attrib<ApparentRef>::plus_ref result_type;
private:
   mutable op_value_cache<typename deref<Orig>::type> cache;
public:
   result_type operator() (argument_type x) const
   {
      cache=x;
      return reinterpret_cast<result_type>(cache.get());
   }
};

template <typename IteratorRef1, typename IteratorRef2>
struct dereference2 {
   typedef IteratorRef1 first_argument_type;
   typedef IteratorRef2 second_argument_type;
   typedef typename iterator_traits<typename deref<IteratorRef2>::type>::reference result_type;

   result_type operator() (IteratorRef1, IteratorRef2 it2) const { return *it2; }
};

template <typename Apparent> struct reinterpret : incomplete {};
template <template <typename> class Masquerade> struct masquerade : incomplete {};
template <template <typename,typename> class Masquerade, typename Second> struct masquerade2 : incomplete {};
template <template <typename,typename,typename> class Masquerade, typename Second, typename Third> struct masquerade3 : incomplete {};

} // end namespace operations

template <template <typename> class Result, typename Iterator, typename Reference>
struct unary_op_builder< operations::construct_unary<Result>, Iterator, Reference>
   : empty_op_builder< operations::construct_unary<Result,Reference> > {};

template <template <typename,typename> class Result, typename Second, typename Iterator, typename Reference>
struct unary_op_builder< operations::construct_unary2<Result,Second>, Iterator, Reference>
   : empty_op_builder< operations::construct_unary2<Result,Second,Reference> > {};

template <template <typename,typename,typename> class Result, typename Second, typename Third, typename Iterator, typename Reference>
struct unary_op_builder< operations::construct_unary3<Result,Second,Third>, Iterator, Reference>
   : empty_op_builder< operations::construct_unary3<Result,Second,Third,Reference> > {};

template <template <typename> class Result, typename Second, typename Iterator, typename Reference>
struct unary_op_builder< operations::construct_unary_with_arg<Result,Second>, Iterator, Reference> {
   typedef operations::construct_unary_with_arg<Result,Second,Reference> operation;

   static operation create(const Second& arg) { return operation(arg); }
};

template <template <typename,typename> class Result, typename Second, typename Iterator, typename Reference>
struct unary_op_builder< operations::construct_unary2_with_arg<Result,Second>, Iterator, Reference> {
   typedef operations::construct_unary2_with_arg<Result,Second,Reference> operation;

   static operation create(const Second& arg) { return operation(arg); }
};

template <template <typename,typename> class Result, typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::construct_binary<Result>, Iterator1, Iterator2, Reference1, Reference2>
   : empty_op_builder< operations::construct_binary<Result, Reference1, Reference2> > {};

template <template <typename,typename,typename> class Result, typename Third, typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::construct_binary2<Result,Third>, Iterator1, Iterator2, Reference1, Reference2>
   : empty_op_builder< operations::construct_binary2<Result, Third, Reference1, Reference2> > {};

template <template <typename,typename> class Result, typename Third, typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::construct_binary_with_arg<Result,Third>, Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::construct_binary_with_arg<Result, Third, Reference1, Reference2> operation;

   static operation create(const Third& arg) { return operation(arg); }
};

template <template <typename,typename,typename> class Result, typename Third, typename Iterator1, typename Iterator2, typename Reference1, typename Reference2>
struct binary_op_builder< operations::construct_binary2_with_arg<Result,Third>, Iterator1, Iterator2, Reference1, Reference2> {
   typedef operations::construct_binary2_with_arg<Result, Third, Reference1, Reference2> operation;

   static operation create(const Third& arg) { return operation(arg); }
};

template <typename Apparent, typename Iterator, typename Reference>
struct unary_op_builder<operations::reinterpret<Apparent>, Iterator, Reference>
   : empty_op_builder< operations::reinterpret_impl<Reference, typename inherit_ref<Apparent,Reference>::type> > {};

template <template <typename> class Masquerade, typename Iterator, typename Reference>
struct unary_op_builder<operations::masquerade<Masquerade>, Iterator, Reference>
   : empty_op_builder< operations::reinterpret_impl<Reference, typename masquerade<Masquerade,Reference>::type> > {};

template <template <typename,typename> class Masquerade, typename Second, typename Iterator, typename Reference>
struct unary_op_builder<operations::masquerade2<Masquerade,Second>, Iterator, Reference>
   : empty_op_builder< operations::reinterpret_impl<Reference, typename masquerade2<Masquerade,Reference,Second>::type> > {};

template <template <typename,typename,typename> class Masquerade, typename Second, typename Third, typename Iterator, typename Reference>
struct unary_op_builder<operations::masquerade3<Masquerade,Second,Third>, Iterator, Reference>
  : empty_op_builder< operations::reinterpret_impl<Reference, typename masquerade3<Masquerade,Reference,Second,Third>::type> > {};

template <typename Iterator>
inline
Iterator&& enforce_movable_values(Iterator&& it,
                                  typename std::enable_if<!std::is_lvalue_reference<typename iterator_traits<Iterator>::reference>::value>::type** = nullptr)
{
   return std::forward<Iterator>(it);
}

template <typename Iterator>
inline
unary_transform_iterator<pointer2iterator_t<Iterator>, BuildUnary<operations::move>>
enforce_movable_values(Iterator&& it,
                       typename std::enable_if<std::is_lvalue_reference<typename iterator_traits<Iterator>::reference>::value>::type** = nullptr)
{
   return pointer2iterator(std::forward<Iterator>(it));
}

} // end namespace pm

namespace polymake {
   using pm::construct_indirect_operation;
   using pm::perform_assign;
   using pm::accumulate;
   using pm::accumulate_in;
   using pm::average;
   using pm::enforce_movable_values;

   namespace operations {
      typedef BuildUnary<pm::operations::neg> neg;
      typedef BuildBinary<pm::operations::add> add;
      typedef BuildBinary<pm::operations::sub> sub;
      typedef BuildBinary<pm::operations::mul> mul;
      typedef BuildBinary<pm::operations::div> div;
      typedef BuildBinary<pm::operations::divexact> divexact;
      typedef BuildUnary<pm::operations::square> square;
      typedef BuildBinary<pm::operations::tensor> tensor;

      typedef BuildBinary<pm::operations::bitwise_xor> bitwise_xor;

      typedef BuildUnary<pm::operations::clear> clear;

      using pm::operations::fix1;
      using pm::operations::fix2;
      using pm::operations::composed11;
      using pm::operations::composed12;
      using pm::operations::composed21;
      using pm::operations::member;
      typedef BuildBinary<pm::operations::swap_op> swap_op;

      using pm::operations::construct_unary;
      using pm::operations::construct_unary2;
      using pm::operations::construct_unary3;
      using pm::operations::construct_binary;
      using pm::operations::construct_binary2;

      typedef BuildUnary<pm::operations::move> move;

      /// these come from pair, but Build*ary is not defined there
      typedef BuildBinary<pm::operations::pair_maker> pair_maker;
      typedef BuildUnary<pm::operations::take_first> take_first;
      typedef BuildUnary<pm::operations::take_second> take_second;
   }
}

#include "polymake/internal/extend_algo.h"

#ifdef __clang__

// FIXME: remove this hack when all composed operations are expelled in favor of lambdas
namespace std {

template <typename InnerOperation, typename OuterOperation>
struct is_default_constructible<pm::operations::composed11<InnerOperation, OuterOperation>>
   : polymake::mlist_and<is_default_constructible<InnerOperation>, is_default_constructible<OuterOperation>> {};

}

#endif // __clang__


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: