File: ContainerChain.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 (1336 lines) | stat: -rw-r--r-- 49,390 bytes parent folder | download
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
/* 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.h"
#include "polymake/ContainerUnion.h"
#include <array>

namespace pm {
namespace chains {

template <typename Indexes, typename Operation>
class Function;

template <size_t... Index, typename Operation>
class Function<std::index_sequence<Index...>, Operation> {
   using fpointer = typename Operation::fpointer;
   static constexpr size_t length = sizeof...(Index);

   static const fpointer table[length];
public:
   static fpointer get(int i) { return table[i]; }
};

template <size_t... Index, typename Operation>
const typename Function<std::index_sequence<Index...>, Operation>::fpointer
Function<std::index_sequence<Index...>, Operation>::table[] = { &Operation::template execute<Index>... };

template <typename IteratorList>
class Operations {
public:
   using it_tuple = typename mlist2tuple<IteratorList>::type;
   using reference = typename union_iterator_traits<IteratorList>::reference;
   using pointer = typename union_iterator_traits<IteratorList>::pointer;

   struct star {
      using fpointer = reference (*)(const it_tuple&);
      template <size_t i>
      static reference execute(const it_tuple& its)
      {
         return *std::get<i>(its);
      }
   };
   struct arrow {
      using fpointer = pointer (*)(const it_tuple&);
      template <size_t i>
      static pointer execute(const it_tuple& its)
      {
         return std::get<i>(its).operator->();
      }
   };
   struct incr {
      using fpointer = bool (*)(it_tuple&);
      template <size_t i>
      static bool execute(it_tuple& its)
      {
         return (++std::get<i>(its)).at_end();
      }
   };
   struct eq {
      using fpointer = bool (*)(const it_tuple&, const it_tuple&);
      template <size_t i>
      static bool execute(const it_tuple& its, const it_tuple& other)
      {
         return std::get<i>(its) == std::get<i>(other);
      }
   };
   struct at_end {
      using fpointer = bool (*)(const it_tuple&);
      template <size_t i>
      static bool execute(const it_tuple& its)
      {
         return std::get<i>(its).at_end();
      }
   };
   struct index {
      using fpointer = Int (*)(const it_tuple&);
      template <size_t i>
      static Int execute(const it_tuple& its)
      {
         return std::get<i>(its).index();
      }
   };
};

template <typename IteratorList,
          bool is_homogeneous=(mlist_length<typename mlist_remove_duplicates<IteratorList>::type>::value == 1)>
class iterator_store {
public:
   using iterator_t = typename mlist_head<IteratorList>::type;
   static constexpr size_t N = mlist_length<IteratorList>::value;
   using it_tuple = std::array<iterator_t, N>;
   it_tuple its;

   using reference = typename iterator_traits<iterator_t>::reference;
   using pointer = typename iterator_traits<iterator_t>::pointer;
   using value_type = typename iterator_traits<iterator_t>::value_type;

   template <typename... Iterator, typename=std::enable_if_t<sizeof...(Iterator)==N>>
   explicit iterator_store(Iterator&&... it)
      : its{ { std::forward<Iterator>(it)... } }
   {}

   iterator_store() = default;
   iterator_store(const iterator_store&) = default;
   iterator_store(iterator_store&&) = default;
   iterator_store& operator= (const iterator_store&) = default;

   template <typename IteratorList2, typename=std::enable_if_t<std::is_constructible<iterator_t, const typename mlist_head<IteratorList2>::type&>::value>>
   iterator_store(const iterator_store<IteratorList2, is_homogeneous>& other)
      : iterator_store(other, std::make_index_sequence<N>()) {}

   template <typename IteratorList2, size_t... Index>
   iterator_store(const iterator_store<IteratorList2, is_homogeneous>& other, std::index_sequence<Index...>)
      : its{ other.its[Index]... }
   {}

   template <typename IteratorList2, typename=std::enable_if_t<std::is_assignable<iterator_t&, const typename mlist_head<IteratorList2>::type&>::value>>
   iterator_store& operator= (const iterator_store<IteratorList2, is_homogeneous>& other)
   {
      std::copy(other.its.begin(), other.its.end(), its.begin());
      return *this;
   }

   it_tuple& get_it_tuple() { return its; }
   const it_tuple& get_it_tuple() const { return its; }

   reference star(int i) const
   {
      return *its[i];
   }

   pointer arrow(int i) const
   {
      return its[i].operator->();
   }

   Int index(int i) const
   {
      return its[i].index();
   }

   bool incr(int i)
   {
      return (++its[i]).at_end();
   }

   bool eq(int i, const iterator_store& other) const
   {
      return its[i] == other.its[i];
   }

   bool at_end(int i) const
   {
      return its[i].at_end();
   }

   void rewind()
   {
      for (iterator_t& it : its) it.rewind();
   }
};


template <typename IteratorList>
class iterator_store<IteratorList, false> {
public:
   using it_tuple = typename mlist2tuple<IteratorList>::type;
   it_tuple its;
   static constexpr size_t N = mlist_length<IteratorList>::value;

   using ops = Operations<IteratorList>;
   template <typename Operation>
   using functions = Function<std::make_index_sequence<N>, Operation>;

   using reference = typename union_iterator_traits<IteratorList>::reference;
   using pointer = typename union_iterator_traits<IteratorList>::pointer;
   using value_type = typename union_iterator_traits<IteratorList>::value_type;

   template <typename... Iterator, typename=std::enable_if_t<sizeof...(Iterator)==N>>
   explicit iterator_store(Iterator&&... it)
      : its(std::forward<Iterator>(it)...)
   {}

   iterator_store() = default;
   iterator_store(const iterator_store&) = default;
   iterator_store(iterator_store&&) = default;
   iterator_store& operator= (const iterator_store&) = default;

   template <typename IteratorList2, typename=std::enable_if_t<std::is_constructible<it_tuple, const typename mlist2tuple<IteratorList2>::type&>::value>>
   iterator_store(const iterator_store<IteratorList2, false>& other)
      : its(other.its) {}

   template <typename IteratorList2, typename=std::enable_if_t<std::is_assignable<it_tuple&, const typename mlist2tuple<IteratorList2>::type&>::value>>
   iterator_store& operator= (const iterator_store<IteratorList2, false>& other)
   {
      its = other.its;
      return *this;
   }

   it_tuple& get_it_tuple() { return its; }
   const it_tuple& get_it_tuple() const { return its; }

   reference star(int i) const
   {
      return functions<typename ops::star>::get(i)(its);
   }

   pointer arrow(int i) const
   {
      return functions<typename ops::arrow>::get(i)(its);
   }

   Int index(int i) const
   {
      return functions<typename ops::index>::get(i)(its);
   }

   bool incr(int i)
   {
      return functions<typename ops::incr>::get(i)(its);
   }

   bool eq(int i, const iterator_store& other) const
   {
      return functions<typename ops::eq>::get(i)(its, other.its);
   }

   bool at_end(int i) const
   {
      return functions<typename ops::at_end>::get(i)(its);
   }

   void rewind()
   {
      foreach_in_tuple(its, [](auto& it) -> void { it.rewind(); });
   }
};

template <typename T>
struct get_iterator {
   using type = typename iterator_traits<T>::iterator;
};
template <typename T>
struct get_const_iterator {
   using type = typename iterator_traits<T>::const_iterator;
};

}

template <typename IteratorList, bool is_indexed>
class iterator_chain
   : protected chains::iterator_store<IteratorList> {
   using base_t = chains::iterator_store<IteratorList>;
   template <typename, bool> friend class iterator_chain;
protected:
   int leg;

   static constexpr size_t n_off = is_indexed ? mlist_length<IteratorList>::value : 0;
   using offsets_t = std::array<Int, n_off>;
   offsets_t index_offsets;

   void valid_position()
   {
      while (!at_end() && base_t::at_end(leg)) ++leg;
   }
public:
   using iterator_category = forward_iterator_tag;
   using typename base_t::value_type;
   using typename base_t::reference;
   using typename base_t::pointer;
   using difference_type = ptrdiff_t;

   using iterator = iterator_chain<typename mlist_transform_unary<IteratorList, chains::get_iterator>::type, is_indexed>;
   using const_iterator = iterator_chain<typename mlist_transform_unary<IteratorList, chains::get_const_iterator>::type, is_indexed>;

   iterator_chain()
      : leg(mlist_length<IteratorList>::value) {}

   template <typename... Iterator>
   iterator_chain(int leg_arg, offsets_t&& offsets_arg, Iterator&&... it)
      : base_t(std::forward<Iterator>(it)...)
      , leg(leg_arg)
      , index_offsets(offsets_arg)
   {
      valid_position();
   }

   template <typename... Iterator>
   iterator_chain(int leg_arg, std::nullptr_t, Iterator&&... it)
      : base_t(std::forward<Iterator>(it)...)
      , leg(leg_arg)
   {
      valid_position();
   }

   iterator_chain(const iterator& other)
      : base_t(other)
      , leg(other.leg)
      , index_offsets(other.index_offsets) {}

   iterator_chain& operator= (const iterator& other)
   {
      base_t::operator=(other);
      leg=other.leg;
      index_offsets=other.index_offsets;
      return *this;
   }

   typename base_t::reference operator* () const
   {
      return base_t::star(leg);
   }

   typename base_t::pointer operator-> () const
   {
      return base_t::arrow(leg);
   }

   iterator_chain& operator++ ()
   {
      if (base_t::incr(leg)) {
         ++leg;
         valid_position();
      }
      return *this;
   }
   const iterator_chain operator++ (int) { iterator_chain copy=*this; operator++(); return copy; }

   bool operator== (const iterator_chain& other) const
   {
      return leg==other.leg && (at_end() || base_t::eq(leg, other));
   }

   bool operator!= (const iterator_chain& other) const
   {
      return !operator==(other);
   }

   bool at_end() const
   {
      return leg == mlist_length<IteratorList>::value;
   }

   void rewind()
   {
      static_assert(check_iterator_feature<iterator_chain, rewindable>::value, "iterator is not rewindable");
      base_t::rewind();
      leg=0;
      valid_position();
   }

   Int index() const
   {
      static_assert(is_indexed, "iterator is not indexed");
      return base_t::index(leg) + index_offsets[leg];
   }

   using typename base_t::it_tuple;
   int get_leg() const { return leg; }
   const it_tuple& get_it_tuple() const { return base_t::get_it_tuple(); }
};

template <typename IteratorList, bool is_indexed, typename Feature>
struct check_iterator_feature<iterator_chain<IteratorList, is_indexed>, Feature>
   : mlist_and<typename mlist_transform_binary<IteratorList, mrepeat<Feature>, check_iterator_feature>::type> {};

template <typename IteratorList, bool is_indexed>
struct check_iterator_feature<iterator_chain<IteratorList, is_indexed>, indexed>
   : bool_constant<is_indexed> {};

template <typename IteratorList, bool is_indexed>
struct check_iterator_feature<iterator_chain<IteratorList, is_indexed>, contractable>
   : std::false_type {};


template <typename ContainerList, bool enforce_const=false>
struct chain_const_helper {
   static constexpr bool chain_is_const = enforce_const || mlist_or<typename mlist_transform_unary<ContainerList, is_effectively_const>::type>::value;
   using type = std::conditional_t<chain_is_const, typename mlist_transform_unary<ContainerList, add_const>::type, ContainerList>;
};

template <typename Top, typename Params>
class container_chain_typebase : public manip_container_top<Top, Params> {
   using base_t = manip_container_top<Top, Params>;
public:
   using container_ref_list = typename mtagged_list_extract<Params, ContainerRefTag>::type;
   using container_list = typename mlist_transform_unary<container_ref_list, deref>::type;

   static constexpr bool
      is_const       = chain_const_helper<container_ref_list>::chain_is_const,
      is_sparse      = mlist_or<typename mlist_transform_binary<container_list, mrepeat<sparse>, check_container_feature>::type>::value,
      all_sparse_compatible = mlist_and<typename mlist_transform_binary<container_list, mrepeat<sparse_compatible>, check_container_feature>::type>::value,
      is_pure_sparse = mlist_and<typename mlist_transform_binary<container_list, mrepeat<pure_sparse>, check_container_feature>::type>::value;

   using can_enforce_features = dense;
   // either some containers are sparse (then the resulting iterator_chain is automatically indexed),
   // or the whole chain should be made indexed (it is cheaper)
   using cannot_enforce_features = mlist<indexed, provide_construction<rewindable, false>, provide_construction<end_sensitive, false> >;

   using needed_features = typename mix_features<
      typename base_t::expected_features,
      std::conditional_t<is_sparse,
                         std::conditional_t<mlist_contains<typename base_t::expected_features, dense>::value,
                                            mlist<indexed, end_sensitive>, sparse_compatible>,
                         end_sensitive>>::type;

   static constexpr bool needs_indexed = all_sparse_compatible || mlist_contains<needed_features, indexed, absorbing_feature>::value;

   template <typename T>
   using get_category = typename container_traits<T>::category;

   using iterator_list = typename mlist_transform_binary<container_ref_list, mrepeat<needed_features>, extract_iterator_with_features>::type;
   using const_iterator_list = typename mlist_transform_binary<container_ref_list, mrepeat<needed_features>, extract_const_iterator_with_features>::type;

   using iterator = iterator_chain<std::conditional_t<is_const, const_iterator_list, iterator_list>, needs_indexed>;
   using const_iterator = iterator_chain<const_iterator_list, needs_indexed>;

   using reference = typename iterator::reference;
   using const_reference = typename const_iterator::reference;
   using value_type = typename iterator::value_type;

   using container_category = typename least_derived_class<typename mlist_concat<bidirectional_iterator_tag,
                                 typename mlist_transform_unary<container_list, extract_category>::type>::type>::type;

   static constexpr size_t chain_length = mlist_length<container_list>::value;

   static constexpr auto tuple_indexes()
   {
      return std::make_index_sequence<chain_length>();
   }

   static constexpr auto reverse_tuple_indexes()
   {
      return make_reverse_index_sequence<chain_length>();
   }

   using typename base_t::manip_top_type;

   template <typename Iterator, typename Creator, size_t... Index, typename OffsetsArg>
   Iterator make_iterator(int leg_arg, const Creator& cr, std::index_sequence<Index...>, OffsetsArg&& offsets_arg)
   {
      using me_t = std::conditional_t<is_const, const manip_top_type&, manip_top_type&>;
      me_t& me = this->manip_top();
      return Iterator(leg_arg, std::forward<OffsetsArg>(offsets_arg), cr(me.get_container(size_constant<Index>()))...);
   }

   template <typename Iterator, typename Creator, size_t... Index, typename OffsetsArg>
   Iterator make_iterator(int leg_arg, const Creator& cr, std::index_sequence<Index...>, OffsetsArg&& offsets_arg) const
   {
      const auto& me = this->manip_top();
      return Iterator(leg_arg, std::forward<OffsetsArg>(offsets_arg), cr(me.get_container(size_constant<Index>()))...);
   }

   static constexpr auto make_begin()
   {
      return [](auto&& c) { return ensure(c, needed_features()).begin(); };
   }
   static constexpr auto make_end()
   {
      return [](auto&& c) { return ensure(c, needed_features()).end(); };
   }
   static constexpr auto make_rbegin()
   {
      return [](auto&& c) { return ensure(c, needed_features()).rbegin(); };
   }
   static constexpr auto make_rend()
   {
      return [](auto&& c) { return ensure(c, needed_features()).rend(); };
   }

   template <size_t... Index>
   std::array<Int, chain_length> make_index_offsets(std::true_type, bool is_reverse, std::index_sequence<Index...>) const
   {
      Int off = 0;
      const auto summator = [&off](Int i) ->Int { Int sum = off; off += i; return sum; };

      std::array<Int, chain_length> offsets{ { summator(get_dim(this->manip_top().get_container(size_constant<Index>())))... } };
      if (is_reverse) std::reverse(offsets.begin(), offsets.end());
      return offsets;
   }

   template <typename... Args>
   static constexpr std::nullptr_t make_index_offsets(std::false_type, Args&&... args)
   {
      return nullptr;
   }

   auto make_index_offsets(bool is_reverse) const
   {
      return make_index_offsets(bool_constant<needs_indexed>(), is_reverse, tuple_indexes());
   }
};

template <typename Top, typename Params=typename Top::manipulator_params,
          typename Category = typename container_chain_typebase<Top, Params>::container_category>
class container_chain_impl
   : public container_chain_typebase<Top, Params> {
   using base_t = container_chain_typebase<Top, Params>;
private:
   static constexpr Int size_(size_constant<0>)
   {
      return 0;
   }
   static constexpr Int dim_(size_constant<0>)
   {
      return 0;
   }
   static constexpr bool empty_(size_constant<0>)
   {
      return true;
   }
   template <size_t i>
   Int size_(size_constant<i>) const
   {
      return this->manip_top().get_container(size_constant<i-1>()).size() + size_(size_constant<i-1>());
   }
   template <size_t i>
   Int dim_(size_constant<i>) const
   {
      return get_dim(this->manip_top().get_container(size_constant<i-1>())) + dim_(size_constant<i-1>());
   }
   template <size_t i>
   bool empty_(size_constant<i>) const
   {
      return this->manip_top().get_container(size_constant<i-1>()).empty() && empty_(size_constant<i-1>());
   }

public:
   typedef container_chain_impl<Top, Params> manipulator_impl;
   typedef Params manipulator_params;
   using typename base_t::iterator;
   using typename base_t::const_iterator;

   template <typename FeatureCollector>
   struct rebind_feature_collector {
      typedef container_chain_impl<FeatureCollector, Params> type;
   };

   iterator begin()
   {
      return base_t::template make_iterator<iterator>(0, base_t::make_begin(), base_t::tuple_indexes(), base_t::make_index_offsets(false));
   }
   iterator end()
   {
      return base_t::template make_iterator<iterator>(base_t::chain_length, base_t::make_end(), base_t::tuple_indexes(), nullptr);
   }
   const_iterator begin() const
   {
      return base_t::template make_iterator<const_iterator>(0, base_t::make_begin(), base_t::tuple_indexes(), base_t::make_index_offsets(false));
   }
   const_iterator end() const
   {
      return base_t::template make_iterator<const_iterator>(base_t::chain_length, base_t::make_end(), base_t::tuple_indexes(), nullptr);
   }

   Int size() const
   {
      return size_(size_constant<base_t::chain_length>());
   }
   Int dim() const
   {
      return dim_(size_constant<base_t::chain_length>());
   }
   bool empty() const
   {
      return empty_(size_constant<base_t::chain_length>());
   }
};

template <typename Top, typename Params>
class container_chain_impl<Top, Params, forward_iterator_tag>
   : public container_chain_impl<Top, Params, input_iterator_tag> {
   using base_t = container_chain_impl<Top, Params, input_iterator_tag>;
public:
   using typename base_t::reference;
   using typename base_t::const_reference;
private:
   reference front_(size_constant<base_t::chain_length-1>)
   {
      return this->manip_top().get_container(size_constant<base_t::chain_length-1>()).front();
   }
   const_reference front_(size_constant<base_t::chain_length-1>) const
   {
      return this->manip_top().get_container(size_constant<base_t::chain_length-1>()).front();
   }
   template <size_t i>
   reference front_(size_constant<i>)
   {
      auto& c = this->manip_top().get_container(size_constant<i>());
      if (!c.empty()) return c.front();
      return front_(size_constant<i+1>());
   }
   template <size_t i>
   const_reference front_(size_constant<i>) const
   {
      const auto& c = this->manip_top().get_container(size_constant<i>());
      if (!c.empty()) return c.front();
      return front_(size_constant<i+1>());
   }
public:
   reference front()
   {
      return front_(size_constant<0>());
   }
   const_reference front() const
   {
      return front_(size_constant<0>());
   }
};

template <typename Top, typename Params>
class container_chain_impl<Top, Params, bidirectional_iterator_tag>
   : public container_chain_impl<Top, Params, forward_iterator_tag> {
   using base_t = container_chain_impl<Top, Params, forward_iterator_tag>;
public:
   using reverse_container_list = typename mlist_reverse<typename base_t::container_ref_list>::type;
   using reverse_iterator_list = typename mlist_transform_binary<reverse_container_list,
                                   mrepeat<typename base_t::needed_features>, extract_reverse_iterator_with_features>::type;
   using const_reverse_iterator_list = typename mlist_transform_binary<reverse_container_list,
                                   mrepeat<typename base_t::needed_features>, extract_const_reverse_iterator_with_features>::type;
   using reverse_iterator = iterator_chain<std::conditional_t<base_t::is_const, const_reverse_iterator_list, reverse_iterator_list>, base_t::needs_indexed>;
   using const_reverse_iterator = iterator_chain<const_reverse_iterator_list, base_t::needs_indexed>;
   using typename base_t::reference;
   using typename base_t::const_reference;
private:
   reference back_(size_constant<0>)
   {
      return this->manip_top().get_container(size_constant<0>()).back();
   }
   const_reference back_(size_constant<0>) const
   {
      return this->manip_top().get_container(size_constant<0>()).back();
   }
   template <size_t i>
   reference back_(size_constant<i>)
   {
      auto& c = this->manip_top().get_container(size_constant<i>());
      if (!c.empty()) return c.back();
      return back_(size_constant<i-1>());
   }
   template <size_t i>
   const_reference back_(size_constant<i>) const
   {
      const auto& c = this->manip_top().get_container(size_constant<i>());
      if (!c.empty()) return c.back();
      return back_(size_constant<i-1>());
   }
public:
   reverse_iterator rbegin()
   {
      return base_t::template make_iterator<reverse_iterator>(0, base_t::make_rbegin(), base_t::reverse_tuple_indexes(), base_t::make_index_offsets(true));
   }
   reverse_iterator rend()
   {
      return base_t::template make_iterator<reverse_iterator>(base_t::chain_length, base_t::make_rend(), base_t::reverse_tuple_indexes(), nullptr);
   }
   const_reverse_iterator rbegin() const
   {
      return base_t::template make_iterator<const_reverse_iterator>(0, base_t::make_rbegin(), base_t::reverse_tuple_indexes(), base_t::make_index_offsets(true));
   }
   const_reverse_iterator rend() const
   {
      return base_t::template make_iterator<const_reverse_iterator>(base_t::chain_length, base_t::make_rend(), base_t::reverse_tuple_indexes(), nullptr);
   }

   reference back()
   {
      return back_(size_constant<base_t::chain_length-1>());
   }
   const_reference back() const
   {
      return back_(size_constant<base_t::chain_length-1>());
   }
};

template <template <typename...> class Owner, typename... TailParams>
struct chain_arg_helper {
   template <typename T>
   using recognize = is_instance_of<pure_type_t<T>, Owner>;

   template <typename T, typename=void>
   struct extract_impl {
      using type = T;
   };
   template <typename T>
   struct extract_impl<T, std::enable_if_t<recognize<T>::value &&
            std::is_same<typename mlist_tail<typename recognize<T>::params>::type, mlist<TailParams...>>::value>> {
      using aliases = typename mlist_transform_unary<typename mget_template_parameter<pure_type_t<T>, 0>::type, make_alias>::type;
      using type = typename mlist_transform_binary<aliases, mrepeat<T>, inherit_reference>::type;
   };

   template <typename T>
   using extract = extract_impl<T>;

   template <typename ContainerList, typename ArgList>
   static constexpr bool allow(ContainerList, ArgList)
   {
      using flattened_args = typename mlist_flatten<typename mlist_transform_unary<ArgList, extract>::type>::type;
      using alias_list = typename mlist_transform_unary<ContainerList, make_alias>::type;
      return allow_args(alias_list(), flattened_args());
   }

   template <typename AliasList, typename ArgList>
   static constexpr std::enable_if_t<mlist_length<AliasList>::value == mlist_length<ArgList>::value, bool>
   allow_args(AliasList, ArgList)
   {
      return mlist_and< typename mlist_transform_binary<AliasList, ArgList, is_direct_constructible>::type >::value;
   }

   template <typename AliasList, typename ArgList>
   static constexpr std::enable_if_t<mlist_length<AliasList>::value != mlist_length<ArgList>::value, bool>
   allow_args(AliasList, ArgList)
   {
      return false;
   }

   template <typename T>
   using count = int_constant<mlist_length<typename extract_impl<T>::type>::value>;

   template <typename Count, typename CountList>
   using add_count = mlist_concat<Count, typename mlist_transform_binary<CountList, mrepeat<Count>, madd>::type>;

   template <typename ArgList, bool no_tuples>
   struct matcher {
      using type = std::true_type;
   };

   template <typename ArgList>
   struct matcher<ArgList, false> {
      using type = typename mlist_wrap<typename mlist_fold_transform<ArgList, count, add_count>::type>::type;
   };

   template <int length, typename ArgList>
   static constexpr auto match(ArgList)
   {
      return typename matcher<ArgList, (length==mlist_length<ArgList>::value)>::type();
   }
};

template <typename Elements>
class alias_tuple {
protected:
   using alias_list = typename mlist_transform_unary<Elements, make_alias>::type;
   using alias_store = typename mlist2tuple<alias_list>::type;

   alias_store aliases;

   // TODO: =delete
   alias_tuple() = default;

   alias_tuple(alias_tuple&&) = default;
   alias_tuple(const alias_tuple&) = default;

   template <template <typename...> class Owner, typename... TailParams, typename... Args>
   alias_tuple(chain_arg_helper<Owner, TailParams...> helper, Args&&... args)
      : alias_tuple(helper.template match<mlist_length<Elements>::value>(mlist<Args...>()), std::forward<Args>(args)...) {}

private:
   template <typename... Args>
   alias_tuple(std::true_type, Args&&... args)
      : aliases(std::forward<Args>(args)...) {}

   template <typename... Counts, typename... Args>
   alias_tuple(mlist<Counts...>, Args&&... args)
      : alias_tuple(typename index_sequence_for<Elements>::type(),
                    mlist<int_constant<0>, Counts...>(),
                    std::forward_as_tuple(std::forward<Args>(args)...)) {}

   template <size_t... Index, typename Counts, typename... Args>
   alias_tuple(std::index_sequence<Index...>, Counts, std::tuple<Args...>&& args)
      : aliases(pick_arg<Index, Counts>(args)...) {}

   template <int i, typename Counts, typename... Args>
   static constexpr decltype(auto) pick_arg(std::tuple<Args...>& args)
   {
      using arg_finder = mlist_find_if<Counts, mis_greater, int_constant<i>>;
      constexpr int offset = mlist_at<Counts, arg_finder::pos-1>::type::value;
      constexpr bool arg_is_tuple = arg_finder::match::value > offset+1;
      return pick_arg(std::get<arg_finder::pos-1>(std::move(args)), int_constant<(arg_is_tuple ? i-offset : -1)>());
   }

   template <typename Arg>
   static constexpr decltype(auto) pick_arg(Arg&& arg, int_constant<-1>)
   {
      return std::forward<Arg>(arg);
   }

   template <typename Arg, int i>
   static constexpr decltype(auto) pick_arg(Arg&& arg, int_constant<i>)
   {
      return std::forward<Arg>(arg).get_alias(size_constant<i>());
   }

public:
   template <size_t i>
   decltype(auto) get_alias(size_constant<i>) &
   {
      return std::get<i>(aliases);
   }
   template <size_t i>
   decltype(auto) get_alias(size_constant<i>) const &
   {
      return std::get<i>(aliases);
   }
   template <size_t i>
   decltype(auto) get_alias(size_constant<i>) &&
   {
      return std::move(std::get<i>(aliases));
   }
   template <size_t i>
   decltype(auto) get_container(size_constant<i>)
   {
      return *get_alias(size_constant<i>());
   }
   template <size_t i>
   decltype(auto) get_container(size_constant<i>) const
   {
      return *get_alias(size_constant<i>());
   }

   template <typename T>
   using get_lazy = bool_constant<object_traits<typename deref<T>::type>::is_lazy>;

   static constexpr bool
      is_lazy = mlist_or<typename mlist_transform_unary<Elements, get_lazy>::type>::value,
      is_always_const = mlist_or<typename mlist_transform_unary<Elements, is_effectively_const>::type>::value;
};

template <template <typename...> class Owner, bool enforce_const=false, typename... TailParams>
struct chain_compose {
   template <typename T>
   using recognize = is_instance_of<pure_type_t<T>, Owner>;

   template <typename T, typename=void>
   struct extract_impl {
      using type = T;
   };
   template <typename T>
   struct extract_impl<T, std::enable_if_t<recognize<T>::value &&
            std::is_same<typename mlist_tail<typename recognize<T>::params>::type, mlist<TailParams...>>::value>> {
      using type = typename mlist_head<typename recognize<T>::params>::type;
   };
   template <typename T>
   using extract = extract_impl<T>;

   template <typename... T>
   using component_list = typename mlist_flatten<typename mlist_transform_unary<typename mlist_wrap<T...>::type, extract>::type>::type;

   template <typename... T>
   using list = typename chain_const_helper<component_list<T...>, enforce_const>::type;

   template <typename... T>
   using with = Owner<list<T...>, TailParams...>;
};

template <typename ContainerList>
class ContainerChain
   : public alias_tuple<ContainerList>
   , public container_chain_impl< ContainerChain<ContainerList>,
                                  mlist< ContainerRefTag<ContainerList> > > {
   using arg_helper = chain_arg_helper<pm::ContainerChain>;
protected:
   using alias_tuple<ContainerList>::alias_tuple;
public:
   template <typename... Args, typename=std::enable_if_t<arg_helper::allow(ContainerList(), mlist<Args...>())>>
   explicit ContainerChain(Args&&... args)
      : alias_tuple<ContainerList>(arg_helper(), std::forward<Args>(args)...) {}
};

template <typename ContainerList>
struct spec_object_traits< ContainerChain<ContainerList> >
   : spec_object_traits<is_container> {
   static constexpr bool
      is_temporary = true,
      is_lazy = alias_tuple<ContainerList>::is_lazy,
      is_always_const = alias_tuple<ContainerList>::is_always_const;
};

template <typename ContainerList>
struct check_container_feature<ContainerChain<ContainerList>, sparse>
   : mlist_or<typename mlist_transform_binary<ContainerList, mrepeat<sparse>, check_container_ref_feature>::type> {};

template <typename ContainerList>
struct check_container_feature<ContainerChain<ContainerList>, pure_sparse>
   : mlist_and<typename mlist_transform_binary<ContainerList, mrepeat<pure_sparse>, check_container_ref_feature>::type> {};

template <typename... Container>
auto concatenate(Container&&... c)
{
   return typename chain_compose<ContainerChain>::template with<Container...>(std::forward<Container>(c)...);
}

template <typename IteratorList, typename Operation>
class tuple_transform_iterator
   : protected chains::iterator_store<IteratorList> {
   using base_t = chains::iterator_store<IteratorList>;
   using typename base_t::it_tuple;
protected:
   Operation op;

   template <size_t... Index>
   static decltype(auto) apply_op(const Operation& op, const it_tuple& t, std::index_sequence<Index...>)
   {
      return op(*(std::get<Index>(t))...);
   }

public:
   using iterator_category = typename union_iterator_traits<IteratorList>::iterator_category;
   using difference_type = ptrdiff_t;

   using iterator = tuple_transform_iterator<typename mlist_transform_unary<IteratorList, chains::get_iterator>::type, Operation>;
   using const_iterator = tuple_transform_iterator<typename mlist_transform_unary<IteratorList, chains::get_const_iterator>::type, Operation>;

   tuple_transform_iterator() = default;

   template <typename... Iterator, typename=std::enable_if_t<std::is_constructible<base_t, Iterator...>::value>>
   explicit tuple_transform_iterator(Iterator&&... args)
      : base_t(std::forward<Iterator>(args)...) {}

   template <typename... Iterator, typename=std::enable_if_t<std::is_constructible<base_t, Iterator...>::value>>
   tuple_transform_iterator(const Operation& op_arg, Iterator&&... args)
      : base_t(std::forward<Iterator>(args)...)
      , op(op_arg) {}

   tuple_transform_iterator(const iterator& other)
      : base_t(other) {}

   tuple_transform_iterator& operator= (const iterator& other)
   {
      base_t::operator=(other);
      return *this;
   }

   using reference = decltype(apply_op(std::declval<const Operation&>(), std::declval<const it_tuple&>(), typename index_sequence_for<IteratorList>::type()));
   using pointer = typename arrow_helper<reference>::pointer;
   using value_type = pure_type_t<reference>;

   reference operator* () const
   {
      return apply_op(op, base_t::get_it_tuple(), typename index_sequence_for<IteratorList>::type());
   }
   pointer operator-> () const
   {
      return arrow_helper<reference>::get(*this);
   }

   tuple_transform_iterator& operator++ ()
   {
      foreach_in_tuple(base_t::its, [](auto& it) ->void { ++it; });
      return *this;
   }
   tuple_transform_iterator operator++ (int) { tuple_transform_iterator copy(*this); operator++(); return copy; }

   tuple_transform_iterator& operator-- ()
   {
      static_assert(is_derived_from<iterator_category, std::bidirectional_iterator_tag>::value,
                    "decrement is not supported by all involved iterators");
      foreach_in_tuple(base_t::get_it_tuple(), [](auto& it) ->void { --it; });
      return *this;
   }
   tuple_transform_iterator operator-- (int) { tuple_transform_iterator copy(*this); operator--(); return copy; }

   tuple_transform_iterator& operator+= (ptrdiff_t i)
   {
      static_assert(std::is_same<iterator_category, std::random_access_iterator_tag>::value,
                    "random access is not supported by all involved iterators");
      foreach_in_tuple(base_t::get_it_tuple(), [i](auto& it) ->void { it+=i; });
      return *this;
   }
   tuple_transform_iterator& operator-= (ptrdiff_t i)
   {
      static_assert(std::is_same<iterator_category, std::random_access_iterator_tag>::value,
                    "random access is not supported by all involved iterators");
      foreach_in_tuple(base_t::get_it_tuple(), [i](auto& it) ->void { it-=i; });
      return *this;
   }

   tuple_transform_iterator operator+ (ptrdiff_t i) const
   {
      tuple_transform_iterator copy(*this);  return copy+=i;
   }
   tuple_transform_iterator operator- (ptrdiff_t i) const
   {
      tuple_transform_iterator copy(*this);  return copy-=i;
   }
   friend tuple_transform_iterator operator+ (ptrdiff_t i, const tuple_transform_iterator& me)
   {
      return me+i;
   }
   ptrdiff_t operator- (const tuple_transform_iterator& other) const
   {
      constexpr int i = mlist_find_if<IteratorList, can_subtract_iterators>::pos;
      static_assert(i>=0, "no random access");
      return std::get<i>(base_t::get_it_tuple()) - std::get<i>(other.get_it_tuple());
   }

   bool at_end() const
   {
      constexpr int i = mlist_find_if<IteratorList, check_iterator_feature, end_sensitive>::pos;
      return std::get<i>(base_t::get_it_tuple()).at_end();
   }

   Int index() const
   {
      constexpr int i = mlist_find_if<IteratorList, check_iterator_feature, indexed>::pos;
      return std::get<i>(base_t::get_it_tuple()).index();
   }

   bool operator== (const tuple_transform_iterator& other) const
   {
      constexpr int i = mlist_find_if<IteratorList, mnegate_binary<check_iterator_feature>::template func, unlimited>::pos;
      return std::get<i>(base_t::get_it_tuple()) == std::get<i>(other.get_it_tuple());
   }
   bool operator!= (const tuple_transform_iterator& other) const
   {
      return !operator==(other);
   }
};

template <typename IteratorList, typename Operation, typename Feature>
struct check_iterator_feature< tuple_transform_iterator<IteratorList, Operation>, Feature>
   : mlist_or< typename mlist_transform_binary<IteratorList, mrepeat<Feature>, check_iterator_feature>::type > {};

template <typename IteratorList, typename Operation>
struct check_iterator_feature< tuple_transform_iterator<IteratorList, Operation>, unlimited>
   : mlist_and< typename mlist_transform_binary<IteratorList, mrepeat<unlimited>, check_iterator_feature>::type > {};

template <typename Top, typename Params>
class modified_container_tuple_typebase : public manip_container_top<Top, Params> {
   using base_t = manip_container_top<Top, Params>;
public:
   using container_ref_list = typename mtagged_list_extract<Params, ContainerRefTag>::type;
   using container_list = typename mlist_transform_unary<container_ref_list, deref>::type;
   using operation = typename mtagged_list_extract<Params, OperationTag>::type;
   using raw_iterator_list = typename mlist_transform_unary<container_ref_list, extract_iterator>::type;

   static constexpr int tuple_size = mlist_length<container_list>::value;
   static constexpr auto tuple_indexes()
   {
      return std::make_index_sequence<tuple_size>();
   }

   using usual_or_features = mlist<end_sensitive, indexed>;
   using typename base_t::expected_features;
   using or_features = typename mlist_match_all<expected_features, usual_or_features, equivalent_features>::type;
   using and_features = typename mlist_match_all<expected_features, usual_or_features, equivalent_features>::complement;
   using missing_or_features = typename mlist_match_all<raw_iterator_list, or_features, check_iterator_feature>::complement2;

   static constexpr size_t normal_it_pos = mlist_find_if<raw_iterator_list, mnegate_binary<check_iterator_feature>::template func, unlimited>::pos;
   using needed_feature_list = typename mlist_concat< typename mreplicate< ExpectedFeaturesTag<and_features>, normal_it_pos>::type,
                                                      ExpectedFeaturesTag< typename mix_features<and_features, missing_or_features>::type >,
                                                      typename mreplicate< ExpectedFeaturesTag<and_features>, tuple_size-normal_it_pos-1>::type >::type;

   using iterator_list = typename mlist_transform_binary<container_ref_list, needed_feature_list, extract_iterator_with_features>::type;
   using const_iterator_list = typename mlist_transform_binary<container_ref_list, needed_feature_list, extract_const_iterator_with_features>::type;

   using iterator = tuple_transform_iterator<iterator_list, operation>;
   using const_iterator = tuple_transform_iterator<const_iterator_list, operation>;
   using container_category = typename least_derived_class< typename mlist_transform_unary<container_list, extract_category>::type >::type;
   using reference = typename iterator::reference;
   using const_reference = typename const_iterator::reference;
   using value_type = typename iterator::value_type;
};

template <typename Top, typename Params>
class reverse_modified_container_tuple_typebase {
   using base_t = modified_container_tuple_typebase<Top, Params>;
public:
   using reverse_iterator_list =
      typename mlist_transform_binary<typename base_t::container_ref_list, typename base_t::needed_feature_list,
                                      extract_reverse_iterator_with_features>::type;
   using const_reverse_iterator_list =
      typename mlist_transform_binary<typename base_t::container_ref_list, typename base_t::needed_feature_list,
                                      extract_const_reverse_iterator_with_features>::type;

   using reverse_iterator = tuple_transform_iterator<reverse_iterator_list, typename base_t::operation>;
   using const_reverse_iterator = tuple_transform_iterator<const_reverse_iterator_list, typename base_t::operation>;
};

template <typename Top, typename Params=typename Top::manipulator_params,
          typename Category=typename modified_container_tuple_typebase<Top, Params>::container_category>
class modified_container_tuple_impl
   : public modified_container_tuple_typebase<Top, Params> {
   using base_t = modified_container_tuple_typebase<Top, Params>;
public:
   typedef modified_container_tuple_impl<Top, Params> manipulator_impl;
   typedef Params manipulator_params;
   using typename base_t::iterator;
   using typename base_t::const_iterator;

   template <typename FeatureCollector>
   struct rebind_feature_collector {
      typedef modified_container_tuple_impl<FeatureCollector, Params> type;
   };

   iterator begin()
   {
      return make_begin(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }
   iterator end()
   {
      return make_end(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }
   const_iterator begin() const
   {
      return make_begin(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }
   const_iterator end() const
   {
      return make_end(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }

   Int size() const
   {
      return this->manip_top().get_container(size_constant<base_t::normal_it_pos>()).size();
   }
   Int dim() const
   {
      return get_dim(this->manip_top().get_container(size_constant<base_t::normal_it_pos>()));
   }
   bool empty() const
   {
      return this->manip_top().get_container(size_constant<base_t::normal_it_pos>()).empty();
   }

   decltype(auto) front()
   {
      return make_front(base_t::tuple_indexes());
   }
   decltype(auto) front() const
   {
      return make_front(base_t::tuple_indexes());
   }

private:
   template <size_t... Index, typename... Features>
   iterator make_begin(std::index_sequence<Index...>, mlist<Features...>)
   {
      return iterator(this->manip_top().get_operation(),
                      ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).begin()...);
   }

   template <size_t... Index, typename... Features>
   iterator make_end(std::index_sequence<Index...>, mlist<Features...>)
   {
      return iterator(this->manip_top().get_operation(),
                      ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).end()...);
   }

   template <size_t... Index, typename... Features>
   const_iterator make_begin(std::index_sequence<Index...>, mlist<Features...>) const
   {
      return const_iterator(this->manip_top().get_operation(),
                            ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).begin()...);
   }

   template <size_t... Index, typename... Features>
   const_iterator make_end(std::index_sequence<Index...>, mlist<Features...>) const
   {
      return const_iterator(this->manip_top().get_operation(),
                            ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).end()...);
   }

   template <size_t... Index>
   decltype(auto) make_front(std::index_sequence<Index...>)
   {
      return this->manip_top().get_operation()( this->manip_top().get_container(size_constant<Index>()).front()... );
   }

   template <size_t... Index>
   decltype(auto) make_front(std::index_sequence<Index...>) const
   {
      return this->manip_top().get_operation()( this->manip_top().get_container(size_constant<Index>()).front()... );
   }
};

template <typename Top, typename Params>
class modified_container_tuple_impl<Top, Params, std::bidirectional_iterator_tag>
   : public modified_container_tuple_impl<Top, Params, std::forward_iterator_tag> {
   using base_t = modified_container_tuple_impl<Top, Params, std::forward_iterator_tag>;
   using rev_traits = reverse_modified_container_tuple_typebase<Top, Params>;
public:
   using reverse_iterator = typename rev_traits::reverse_iterator;
   using const_reverse_iterator = typename rev_traits::const_reverse_iterator;

   reverse_iterator rbegin()
   {
      return make_rbegin(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }
   reverse_iterator rend()
   {
      return make_rend(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }
   const_reverse_iterator rbegin() const
   {
      return make_rbegin(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }
   const_reverse_iterator rend() const
   {
      return make_rend(base_t::tuple_indexes(), typename base_t::needed_feature_list());
   }

   decltype(auto) back()
   {
      return make_back(base_t::tuple_indexes());
   }
   decltype(auto) back() const
   {
      return make_back(base_t::tuple_indexes());
   }

private:
   template <size_t... Index, typename... Features>
   reverse_iterator make_rbegin(std::index_sequence<Index...>, mlist<Features...>)
   {
      return reverse_iterator(this->manip_top().get_operation(),
                              ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).rbegin()...);
   }

   template <size_t... Index, typename... Features>
   reverse_iterator make_rend(std::index_sequence<Index...>, mlist<Features...>)
   {
      return reverse_iterator(this->manip_top().get_operation(),
                              ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).rend()...);
   }

   template <size_t... Index, typename... Features>
   const_reverse_iterator make_rbegin(std::index_sequence<Index...>, mlist<Features...>) const
   {
      return const_reverse_iterator(this->manip_top().get_operation(),
                                    ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).rbegin()...);
   }

   template <size_t... Index, typename... Features>
   const_reverse_iterator make_rend(std::index_sequence<Index...>, mlist<Features...>) const
   {
      return const_reverse_iterator(this->manip_top().get_operation(),
                                    ensure(this->manip_top().get_container(size_constant<Index>()), muntag_t<Features>()).rend()...);
   }

   template <size_t... Index>
   decltype(auto) make_back(std::index_sequence<Index...>)
   {
      return this->manip_top().get_operation()( this->manip_top().get_container(size_constant<Index>()).back()... );
   }

   template <size_t... Index>
   decltype(auto) make_back(std::index_sequence<Index...>) const
   {
      return this->manip_top().get_operation()( this->manip_top().get_container(size_constant<Index>()).back()... );
   }
};

template <typename Top, typename Params>
class modified_container_tuple_impl<Top, Params, std::random_access_iterator_tag>
   : public modified_container_tuple_impl<Top, Params, std::bidirectional_iterator_tag> {
   using base_t = modified_container_tuple_impl<Top, Params, std::bidirectional_iterator_tag>;
public:
   decltype(auto) operator[] (Int i)
   {
      return make_random(i, base_t::tuple_indexes());
   }
   decltype(auto) operator[] (Int i) const
   {
      return make_random(i, base_t::tuple_indexes());
   }

private:
   template <size_t... Index>
   decltype(auto) make_random(Int i, std::index_sequence<Index...>)
   {
      return this->manip_top().get_operation()( this->manip_top().get_container(size_constant<Index>())[i]... );
   }

   template <size_t... Index>
   decltype(auto) make_random(Int i, std::index_sequence<Index...>) const
   {
      return this->manip_top().get_operation()( this->manip_top().get_container(size_constant<Index>())[i]... );
   }
};

namespace operations {

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

template <typename LeftRef, typename RightRef>
struct concat : concat_impl<LeftRef, RightRef> {};

} // end namespace operations
} // end namespace pm

namespace polymake {

using pm::concatenate;

namespace operations {

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

// to be used in Rows/Cols of BlockMatrix, where no hidden chains can ocur among the arguments
template <template <typename> class Result>
struct concat_tuple {
   template <typename... Args>
   auto operator() (Args&&... args) const
   {
      using list = typename pm::chain_const_helper<mlist<Args...>>::type;
      return Result<list>(std::forward<Args>(args)...);
   }
};

} }


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