File: IndexedSubset.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 (1327 lines) | stat: -rw-r--r-- 61,369 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
/* 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/GenericSet.h"
#include "polymake/internal/Wary.h"

namespace pm {

template <typename Iterator1, typename Iterator2, bool UseIndex1, bool Renumber, bool Reversed=false>
class indexed_selector
   : public Iterator1 {
public:
   using first_type = Iterator1;
   using second_type = Iterator2;

   Iterator2 second;
protected:
   typedef bool_constant<UseIndex1> pos_discr;

   // 0 - general case
   // 1 - sequence::iterator
   // 2 - series::iterator
   static constexpr int step_kind= UseIndex1 ? 0 :
                                   (is_derived_from<Iterator2, sequence::iterator>::value +
                                    is_derived_from<Iterator2, series::iterator>::value) *
                                   std::is_same<typename accompanying_iterator<Iterator2>::type, sequence::iterator>::value;
   using step_discr = int_constant<step_kind>;
private:
   Int get_pos1(std::false_type) const
   {
      return *second;
   }
   static
   Int get_pos1(std::false_type, Int expected)
   {
      return expected;
   }
   Int get_pos1(std::true_type, Int expected = 0) const
   {
      return static_cast<const first_type&>(*this).index();
   }
   void forw_impl(int_constant<0>)
   {
      const Int pos = get_pos1(pos_discr());
      ++second;
      if (!at_end())
         std::advance(static_cast<first_type&>(*this), Reversed ? pos-*second : *second-pos);
   }
   void forw_impl(int_constant<1>)
   {
      ++second;
      first_type::operator++();
   }
   void forw_impl(int_constant<2>)
   {
      ++second;
      if (!at_end())
         std::advance(static_cast<first_type&>(*this), second.step());
   }
   void back_impl(int_constant<0>)
   {
      if (second.at_end()) {
         --second;
      } else {
         const Int pos = get_pos1(pos_discr());
         --second;
         std::advance(static_cast<first_type&>(*this), Reversed ? pos-*second : *second-pos);
      }
   }
   void back_impl(int_constant<1>)
   {
      --second;
      first_type::operator--();
   }
   void back_impl(int_constant<2>)
   {
      if (second.at_end()) {
         --second;
      } else {
         --second;
         std::advance(static_cast<first_type&>(*this), -second.step());
      }
   }
   Int step_impl(Int i, int_constant<0>)
   {
      const Int pos = second.at_end() ? second[-1] : get_pos1(pos_discr());
      second += i;
      return Reversed ? pos-(second.at_end() ? second[-1] : *second) : (second.at_end() ? second[-1] : *second)-pos;
   }
   Int step_impl(Int i, int_constant<1>)
   {
      second += i;
      return i;
   }
   Int step_impl(Int i, int_constant<2>)
   {
      second += i;
      return second.step() * i;
   }

   void contract1(Int i, bool, std::false_type)
   {
      std::advance(static_cast<first_type&>(*this), i);
   }
   void contract1(Int i, bool renumber, std::true_type)
   {
      first_type::contract(renumber, i);
   }
   void contract1(Int i, bool renumber)
   {
      contract1(Reversed ? -i : i, renumber, bool_constant<check_iterator_feature<Iterator1, contractable>::value>());
   }
public:
   using iterator_category = typename least_derived_class<typename iterator_traits<Iterator1>::iterator_category,
                                                          typename iterator_traits<Iterator2>::iterator_category>::type;
   using difference_type = typename iterator_traits<Iterator2>::difference_type;
   using iterator = indexed_selector<typename iterator_traits<Iterator1>::iterator, Iterator2, UseIndex1, Renumber, Reversed>;
   using const_iterator = indexed_selector<typename iterator_traits<Iterator1>::const_iterator, Iterator2, UseIndex1, Renumber, Reversed>;

   indexed_selector() = default;

   indexed_selector(const iterator& it)
      : first_type(static_cast<const typename iterator::first_type&>(it))
      , second(it.second) {}

   template <typename SourceIterator1, typename SourceIterator2,
             typename = typename suitable_arg_for_iterator<SourceIterator1, Iterator1>::type,
             typename = typename suitable_arg_for_iterator<SourceIterator2, Iterator2>::type>
   indexed_selector(const SourceIterator1& first_arg, const SourceIterator2& second_arg, bool adjust=false, Int expected_pos1 = 0)
      : first_type(prepare_iterator_arg<Iterator1>(first_arg))
      , second(prepare_iterator_arg<Iterator2>(second_arg))
   {
      if (adjust && !at_end()) contract1(*second-get_pos1(pos_discr(), expected_pos1), !Renumber);
   }

   template <typename SourceIterator1, typename SourceIterator2,
             typename = typename suitable_arg_for_iterator<SourceIterator1, Iterator1>::type,
             typename = typename suitable_arg_for_iterator<SourceIterator2, Iterator2>::type>
   indexed_selector(const SourceIterator1& first_arg, const SourceIterator2& second_arg, Int offset)
      : first_type(prepare_iterator_arg<Iterator1>(first_arg))
      , second(prepare_iterator_arg<Iterator2>(second_arg))
   {
      if (offset) contract1(offset, !Renumber);
   }

   indexed_selector& operator++ ()
   {
      forw_impl(step_discr());
      return *this;
   }
   const indexed_selector operator++ (int) { indexed_selector copy=*this; operator++(); return copy; }

   indexed_selector& operator--()
   {
      static_assert(iterator_pair_traits<Iterator1, Iterator2>::is_bidirectional, "iterator is not bidirectional");
      back_impl(step_discr());
      return *this;
   }
   const indexed_selector operator-- (int) { indexed_selector copy=*this;  operator--();  return copy; }

   indexed_selector& operator+= (Int i)
   {
      static_assert(iterator_pair_traits<Iterator1, Iterator2>::is_random, "iterator is not random-access");
      static_cast<first_type&>(*this) += step_impl(i, step_discr());
      return *this;
   }
   indexed_selector& operator-= (Int i)
   {
      static_assert(iterator_pair_traits<Iterator1, Iterator2>::is_random, "iterator is not random-access");
      static_cast<first_type&>(*this) += step_impl(-i, step_discr());
      return *this;
   }

   indexed_selector operator+ (Int i) const { indexed_selector copy=*this; return copy+=i; }
   indexed_selector operator- (Int i) const { indexed_selector copy=*this; return copy-=i; }
   friend indexed_selector operator+ (Int i, const indexed_selector& it) { return it+i; }

   template <typename Other>
   std::enable_if_t<is_derived_from_any<Other, iterator, const_iterator>::value, bool>
   operator- (const Other& it) const
   {
      static_assert(iterator_traits<second_type>::is_random, "iterator is not random-access");
      return second - it.second;
   }

   typename first_type::reference operator[] (Int i) const
   {
      static_assert(iterator_pair_traits<Iterator1, Iterator2>::is_random, "iterator is not random-access");
      return static_cast<const first_type&>(*this)[ second[i] - (second.at_end() ? second[-1] : get_pos1(pos_discr())) ];
   }

   template <typename Other>
   std::enable_if_t<is_derived_from_any<Other, iterator, const_iterator>::value, bool>
   operator== (const Other& it) const
   {
      return second==it.second;
   }
   template <typename Other>
   std::enable_if_t<is_derived_from_any<Other, iterator, const_iterator>::value, bool>
   operator!= (const Other& it) const
   {
      return !operator==(it);
   }

   bool at_end() const
   {
      return second.at_end();
   }
private:
   Int index_impl(std::false_type) const { return *second; }
   Int index_impl(std::true_type) const { return second.index(); }
public:
   Int index() const
   {
      return index_impl(bool_constant<Renumber>());
   }

   void rewind()
   {
      static_assert(check_iterator_feature<Iterator1, rewindable>::value && check_iterator_feature<Iterator2, rewindable>::value,
                    "iterator is not rewindable");
      first_type::rewind();
      second.rewind();
   }

   void contract(bool renumber, Int distance_front, Int distance_back = 0)
   {
      static_assert(check_iterator_feature<Iterator2, contractable>::value, "iterator is not contractable");
      const Int pos = get_pos1(pos_discr());
      second.contract(Renumber && renumber, distance_front, distance_back);
      contract1(*second-pos, !Renumber && renumber);
   }
};

template <typename Iterator1, typename Iterator2, bool UseIndex1, bool Renumber, bool Reversed, typename Feature>
struct check_iterator_feature<indexed_selector<Iterator1, Iterator2, UseIndex1, Renumber, Reversed>, Feature>
   : bool_constant<((is_among<Feature, end_sensitive, contractable>::value ||
                     check_iterator_feature<Iterator1, Feature>::value) &&
                    check_iterator_feature<Iterator2, Feature>::value)> {};

template <typename Iterator1, typename Iterator2, bool UseIndex1, bool Renumber, bool Reversed>
struct check_iterator_feature<indexed_selector<Iterator1, Iterator2, UseIndex1, Renumber, Reversed>, indexed>
   : bool_constant<(!Renumber || check_iterator_feature<Iterator2, indexed>::value)> {};

template <bool Renumber, bool Reverse>
struct sparse_indexed_selector_coupler {
   using Controller = std::conditional_t<Reverse, reverse_zipper<set_intersection_zipper>, set_intersection_zipper>;
   template <typename Iterator1, typename Iterator2, typename ExpectedFeatures>
   struct defs {
      typedef iterator_zipper<Iterator1, Iterator2, operations::cmp, Controller, true, false> iterator;
      using needed_features1 = ExpectedFeatures;  // is already sparse
      using needed_features2 = typename mix_features<needed_features1,
                                 typename mlist_prepend_if<Renumber, provide_construction<indexed>, end_sensitive>::type>::type;
   };
};

template <bool Renumber, bool Reverse>
struct reverse_coupler< sparse_indexed_selector_coupler<Renumber, Reverse> > {
   using type = sparse_indexed_selector_coupler<Renumber, !Reverse>;
};

template <typename Top, typename Params> class indexed_subset_typebase;

namespace subset_classifier {
   enum kind { generic, sparse, plain, contiguous, range };

   template <typename ContainerRef,
             bool maybe = is_derived_from_instance_of<typename deref<ContainerRef>::type, modified_container_typebase>::value>
   struct detect_set_of_indices : std::false_type {};

   template <typename ContainerRef>
   struct detect_set_of_indices<ContainerRef, true>
      : std::is_same<typename deref<ContainerRef>::type::operation, BuildUnaryIt<operations::index2element> > {};

   template <typename ContainerRef,
             bool maybe = is_derived_from_instance_of<typename deref<ContainerRef>::type, indexed_subset_typebase>::value>
   struct detect_indexed_slice : std::false_type {};

   template <typename ContainerRef>
   struct detect_indexed_slice<ContainerRef, true>
      : is_among< typename deref<ContainerRef>::type::operation,
                  operations::apply2< BuildUnaryIt<operations::index2element> >,
                  pair< nothing, operations::apply2< BuildUnaryIt<operations::index2element> > >,
                  pair< operations::apply2< BuildUnaryIt<operations::index2element> >,
                        operations::apply2< BuildUnaryIt<operations::index2element> > >
                > {};

   template <typename ContainerRef,
             bool Renumber = true,
             typename TTag = typename object_traits<typename deref<ContainerRef>::type>::generic_tag>
   struct index2element : std::false_type {};

   template <typename ContainerRef>
   struct index2element<ContainerRef, true, is_set> {
      static const bool value = detect_set_of_indices<ContainerRef>::value || detect_indexed_slice<ContainerRef>::value;
   };

   template <typename ContainerRef1, typename ContainerRef2, bool Renumber>
   struct index_helper {
      static constexpr bool random = iterator_traits<typename container_traits<ContainerRef1>::iterator>::is_random;
      static constexpr bool use_index1 = Renumber && check_container_feature<typename deref<ContainerRef1>::type, sparse_compatible>::value;
      static constexpr kind
      value = check_container_feature<typename deref<ContainerRef1>::type, pm::sparse>::value ||
                 index2element<ContainerRef1, Renumber>::value
                 ? sparse :
                 std::is_same<typename container_traits<ContainerRef2>::iterator, sequence::iterator>::value && !use_index1
                 ? plain
                 : generic;
   };

   template <typename IteratorPair, typename Operation>
   struct iterator_helper {
      using iterator = binary_transform_iterator<IteratorPair, Operation>;
   };
   template <typename IteratorPair>
   struct iterator_helper<IteratorPair, void> {
      using iterator = IteratorPair;
   };
}

template <typename> class RenumberTag {};
template <typename> class HintTag {};

template <typename Top, typename Params>
class indexed_subset_typebase : public manip_container_top<Top, Params> {
public:
   using container1_ref_raw = typename extract_container_ref<Params, Container1RefTag, Container1Tag>::type;
   using container2_ref = typename extract_container_ref<Params, Container2RefTag, Container2Tag>::type;
   typedef effectively_const_t<container1_ref_raw> container1_ref;
   typedef typename deref<container1_ref>::minus_ref container1;
   typedef typename deref<container2_ref>::minus_ref container2;

   static constexpr bool renumber=tagged_list_extract_integral<Params, RenumberTag>(false);

   using index_helper = subset_classifier::index_helper<container1_ref, container2_ref, renumber>;

   using data_container = container1;
   using index_container = container2;
   using reference = typename container_traits<data_container>::reference;
   using const_reference = typename container_traits<data_container>::const_reference;
   using value_type = typename container_traits<data_container>::value_type;
   using container_category = typename least_derived_class<typename container_traits<data_container>::category,
                                                           typename container_traits<index_container>::category>::type;

   using operation = typename mselect<std::enable_if< mtagged_list_extract<Params, OperationTag>::is_specified,
                                                      typename mtagged_list_extract<Params, OperationTag>::type >,
                                      std::enable_if< subset_classifier::index2element<container1_ref, renumber>::value,
                                                      pair< operations::apply2<BuildUnaryIt<operations::index2element>>,
                                                            operations::apply2<BuildUnaryIt<operations::index2element>> > >,
                                      std::enable_if< renumber && index_helper::value == subset_classifier::sparse,
                                                      pair< nothing,
                                                            operations::apply2<BuildUnaryIt<operations::index2element>> > >,
                                      void>::type;

   using must_enforce_features = typename mlist_prepend_if<renumber, indexed, mlist<end_sensitive, rewindable>>::type;
   using expected_features = typename manip_container_top<Top, Params>::expected_features;

protected:
   static constexpr bool at_end_required = mlist_contains<expected_features, end_sensitive, absorbing_feature>::value,
                         rewind_required = mlist_contains<expected_features, rewindable, absorbing_feature>::value,
                          index_required = renumber && (mlist_contains<expected_features, indexed, absorbing_feature>::value ||
                                                        check_container_feature<data_container, sparse_compatible>::value);

   static constexpr subset_classifier::kind suggested_kind=
      subset_classifier::kind(std::is_same<typename mtagged_list_extract<Params, HintTag>::type, sparse>::value
                              ? subset_classifier::sparse : index_helper::value);
public:
   static constexpr subset_classifier::kind kind=subset_classifier::kind(
      suggested_kind == subset_classifier::plain
      ? ( at_end_required
          ? (index_helper::random
             ? subset_classifier::range : subset_classifier::generic) :
          index_required
          ? (index_helper::random
             ? subset_classifier::contiguous : subset_classifier::generic) :
          rewind_required
          ? subset_classifier::contiguous : subset_classifier::plain )
      : suggested_kind);
protected:
   using expected_features1 = typename mlist_difference<expected_features, typename mlist_prepend_if<renumber, indexed, end_sensitive>::type>::type;

   using needed_features1 = std::conditional_t<rewind_required,
                                               typename mlist_replace<expected_features1, rewindable, provide_construction<rewindable,false> >::type,
                                               expected_features1>;

   using needed_features2 = std::conditional_t<index_required,
                                               typename mix_features<expected_features,
                                                                     mlist<provide_construction<indexed>, end_sensitive> >::type,
                                               typename mix_features<typename mlist_match_all<expected_features, indexed, absorbing_feature>::complement,
                                                                     end_sensitive >::type >;

   static bool constexpr use_index1=index_helper::use_index1;
public:
   Int size() const
   {
      return this->manip_top().get_container2().size();
   }
   Int max_size() const
   {
      return size();
   }
   bool empty() const
   {
      return this->manip_top().get_container2().empty();
   }
};

template <typename Top, typename Params,
          subset_classifier::kind Kind=indexed_subset_typebase<Top, Params>::kind,
          typename Category=typename indexed_subset_typebase<Top, Params>::container_category>
class indexed_subset_elem_access
   : public indexed_subset_typebase<Top, Params> {
   using base_t = indexed_subset_typebase<Top, Params>;
public:
   using iterator = indexed_selector<typename ensure_features<typename base_t::data_container, typename base_t::needed_features1>::iterator,
                                     typename ensure_features<typename base_t::index_container, typename base_t::needed_features2>::const_iterator,
                                     base_t::use_index1, base_t::renumber>;
   using const_iterator = indexed_selector<typename ensure_features<typename base_t::data_container, typename base_t::needed_features1>::const_iterator,
                                           typename ensure_features<typename base_t::index_container, typename base_t::needed_features2>::const_iterator,
                                           base_t::use_index1, base_t::renumber>;

   iterator begin()
   {
      auto&& c1=this->manip_top().get_container1();
      return iterator(ensure(c1, typename base_t::needed_features1()).begin(),
                      ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).begin(),
                      true, 0);
   }
   iterator end()
   {
      auto&& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      return iterator(ensure(c1, typename base_t::needed_features1()).end(),
                      ensure(indices, typename base_t::needed_features2()).end(),
                      !iterator_traits<typename iterator::first_type>::is_bidirectional || indices.empty() ? 0 : indices.back() - Int(c1.size()));
   }
   const_iterator begin() const
   {
      return const_iterator(ensure(this->manip_top().get_container1(), typename base_t::needed_features1()).begin(),
                            ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).begin(),
                            true, 0);
   }
   const_iterator end() const
   {
      const auto& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      return const_iterator(ensure(c1, typename base_t::needed_features1()).end(),
                            ensure(indices, typename base_t::needed_features2()).end(),
                            !iterator_traits<typename iterator::first_type>::is_bidirectional || indices.empty() ? 0 : indices.back() - Int(c1.size()));
   }
};

template <typename Top, typename Params, subset_classifier::kind Kind>
class indexed_subset_elem_access<Top, Params, Kind, forward_iterator_tag>
   : public indexed_subset_elem_access<Top, Params, Kind, input_iterator_tag> {
public:
   decltype(auto) front() { return *(this->begin()); }
   decltype(auto) front() const { return *(this->begin()); }
};

template <typename Top, typename Params, subset_classifier::kind Kind>
class indexed_subset_rev_elem_access
   : public indexed_subset_elem_access<Top, Params, Kind, forward_iterator_tag> {
   using base_t = indexed_subset_elem_access<Top, Params, Kind, forward_iterator_tag>;
public:
   using reverse_iterator = indexed_selector<typename ensure_features<typename base_t::data_container, typename base_t::needed_features1>::reverse_iterator,
                                             typename ensure_features<typename base_t::index_container, typename base_t::needed_features2>::const_reverse_iterator,
                                             base_t::use_index1, base_t::renumber, true>;
   using const_reverse_iterator = indexed_selector<typename ensure_features<typename base_t::data_container, typename base_t::needed_features1>::const_reverse_iterator,
                                                   typename ensure_features<typename base_t::index_container, typename base_t::needed_features2>::const_reverse_iterator,
                                                   base_t::use_index1, base_t::renumber, true>;

   reverse_iterator rbegin()
   {
      auto&& c1 = this->manip_top().get_container1();
      return reverse_iterator(ensure(c1, typename base_t::needed_features1()).rbegin(),
                              ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).rbegin(),
                              true, Int(c1.size())-1);
   }
   reverse_iterator rend()
   {
      auto&& c1=this->manip_top().get_container1();
      const auto& indices=this->manip_top().get_container2();
      return reverse_iterator(ensure(c1, typename base_t::needed_features1()).rend(),
                              ensure(indices, typename base_t::needed_features2()).rend(),
                              !iterator_traits<typename reverse_iterator::first_type>::is_bidirectional || indices.empty() ? 0 : indices.front()+1);
   }
   const_reverse_iterator rbegin() const
   {
      const auto& c1 = this->manip_top().get_container1();
      return const_reverse_iterator(ensure(c1, typename base_t::needed_features1()).rbegin(),
                                    ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).rbegin(),
                                    true, Int(c1.size())-1);
   }
   const_reverse_iterator rend() const
   {
      const auto& indices=this->manip_top().get_container2();
      return const_reverse_iterator(ensure(this->manip_top().get_container1(), typename base_t::needed_features1()).rend(),
                                    ensure(indices, typename base_t::needed_features2()).rend(),
                                    !iterator_traits<typename reverse_iterator::first_type>::is_bidirectional || indices.empty() ? 0 : indices.front()+1);
   }
};

template <typename Top, typename Params, subset_classifier::kind Kind>
class indexed_subset_elem_access<Top, Params, Kind, bidirectional_iterator_tag>
   : public indexed_subset_rev_elem_access<Top, Params, Kind> {
public:
   decltype(auto) back() { return *(this->rbegin()); }
   decltype(auto) back() const { return *(this->rbegin()); }
};

template <typename Top, typename Params>
class indexed_subset_elem_access<Top, Params, subset_classifier::plain, input_iterator_tag>
   : public indexed_subset_typebase<Top, Params> {
   using base_t = indexed_subset_typebase<Top, Params>;
protected:
   using needed_features1 = typename base_t::expected_features;
public:
   using iterator = typename ensure_features<typename base_t::container1, needed_features1>::iterator;
   using const_iterator = typename ensure_features<typename base_t::container1, needed_features1>::const_iterator;

   iterator begin()
   {
      auto&& c1 = this->manip_top().get_container1();
      iterator b = ensure(c1, needed_features1()).begin();
      std::advance(b, this->manip_top().get_container2().front());
      return b;
   }
   iterator end()
   {
      auto&& c1 = this->manip_top().get_container1();
      if (iterator_traits<iterator>::is_bidirectional) {
         iterator e = ensure(c1, needed_features1()).end();
         std::advance(e, this->manip_top().get_container2().back()+1-Int(c1.size()));
         return e;
      } else {
         iterator b = ensure(c1, needed_features1()).begin();
         std::advance(b, this->manip_top().get_container2().back()+1);
         return b;
      }
   }
   const_iterator begin() const
   {
      const_iterator b=ensure(this->manip_top().get_container1(), needed_features1()).begin();
      std::advance(b, this->manip_top().get_container2().front());
      return b;
   }
   const_iterator end() const
   {
      const auto& c1 = this->manip_top().get_container1();
      if (iterator_traits<const_iterator>::is_bidirectional) {
         const_iterator e = ensure(c1, needed_features1()).end();
         std::advance(e, this->manip_top().get_container2().back()+1-Int(c1.size()));
         return e;
      } else {
         const_iterator b = ensure(c1, needed_features1()).begin();
         std::advance(b, this->manip_top().get_container2().back()+1);
         return b;
      }
   }
};

template <typename Top, typename Params>
class indexed_subset_rev_elem_access<Top, Params, subset_classifier::plain>
   : public indexed_subset_elem_access<Top, Params, subset_classifier::plain, forward_iterator_tag> {
   using base_t = indexed_subset_elem_access<Top, Params, subset_classifier::plain, forward_iterator_tag>;
public:
   using reverse_iterator = typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::reverse_iterator;
   using const_reverse_iterator = typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::const_reverse_iterator;

   reverse_iterator rbegin()
   {
      auto&& c1 = this->manip_top().get_container1();
      reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
      std::advance(rb, Int(c1.size())-1-this->manip_top().get_container2().back());
      return rb;
   }
   reverse_iterator rend()
   {
      auto&& c1 = this->manip_top().get_container1();
      if (iterator_traits<reverse_iterator>::is_bidirectional) {
         reverse_iterator re = ensure(c1, typename base_t::needed_features1()).rend();
         std::advance(re, -this->manip_top().get_container2().front());
         return re;
      } else {
         reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
         std::advance(rb, Int(c1.size()) - this->manip_top().get_container2().front());
         return rb;
      }
   }
   const_reverse_iterator rbegin() const
   {
      const auto& c1 = this->manip_top().get_container1();
      const_reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
      std::advance(rb, Int(c1.size())-1-this->manip_top().get_container2().back());
      return rb;
   }
   const_reverse_iterator rend() const
   {
      if (iterator_traits<reverse_iterator>::is_bidirectional) {
         const_reverse_iterator re = ensure(this->manip_top().get_container1(), typename base_t::needed_features1()).rend();
         std::advance(re, -this->manip_top().get_container2().front());
         return re;
      } else {
         const_reverse_iterator rb = ensure(this->manip_top().get_container1(), typename base_t::needed_features1()).rbegin();
         std::advance(rb, Int(this->manip_top().get_container1().size()) - this->manip_top().get_container2().front());
         return rb;
      }
   }
};

template <typename Top, typename Params>
class indexed_subset_elem_access<Top, Params, subset_classifier::contiguous, input_iterator_tag>
   : public indexed_subset_typebase<Top, Params> {
   using base_t = indexed_subset_typebase<Top, Params>;
protected:
   using enforce_features1 = typename mlist_prepend_if<mlist_contains<typename base_t::expected_features, indexed, absorbing_feature>::value,
                                                       provide_construction<indexed, false>,
                             typename mlist_prepend_if<mlist_contains<typename base_t::expected_features, rewindable, absorbing_feature>::value,
                                                       provide_construction<rewindable, false>,
                                                       mlist<>>::type >::type;
   using needed_features1 = typename mix_features<typename base_t::expected_features, enforce_features1>::type;
public:
   using iterator = typename ensure_features<typename base_t::container1, needed_features1>::iterator;
   using const_iterator = typename ensure_features<typename base_t::container1, needed_features1>::const_iterator;

   iterator begin()
   {
      auto&& c1=this->manip_top().get_container1();
      iterator b=ensure(c1, needed_features1()).begin();
      b.contract(base_t::renumber, this->manip_top().get_container2().front());
      return b;
   }
   iterator end()
   {
      iterator b=begin();
      std::advance(b, this->size());
      return b;
   }
   const_iterator begin() const
   {
      const_iterator b=ensure(this->manip_top().get_container1(), needed_features1()).begin();
      b.contract(base_t::renumber, this->manip_top().get_container2().front());
      return b;
   }
   const_iterator end() const
   {
      const_iterator b=begin();
      std::advance(b, this->size());
      return b;
   }
};

template <typename Top, typename Params>
class indexed_subset_rev_elem_access<Top, Params, subset_classifier::contiguous>
   : public indexed_subset_elem_access<Top, Params, subset_classifier::contiguous, forward_iterator_tag> {
   using base_t = indexed_subset_elem_access<Top, Params, subset_classifier::contiguous, forward_iterator_tag>;
public:
   using reverse_iterator = typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::reverse_iterator;
   using const_reverse_iterator = typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::const_reverse_iterator;

   reverse_iterator rbegin()
   {
      auto&& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
      rb.contract(base_t::renumber,  Int(c1.size())-1-indices.back(),  base_t::index_required ? indices.front() : 0);
      return rb;
   }
   reverse_iterator rend()
   {
      reverse_iterator rb = rbegin();
      std::advance(rb, this->size());
      return rb;
   }
   const_reverse_iterator rbegin() const
   {
      const auto& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      const_reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
      rb.contract(base_t::renumber,  Int(c1.size())-1-indices.back(),  base_t::index_required ? indices.front() : 0);
      return rb;
   }
   const_reverse_iterator rend() const
   {
      const_reverse_iterator rb=rbegin();
      std::advance(rb, this->size());
      return rb;
   }
};

template <typename Top, typename Params>
class indexed_subset_elem_access<Top, Params, subset_classifier::range, input_iterator_tag>
   : public indexed_subset_typebase<Top, Params> {
   using base_t = indexed_subset_typebase<Top, Params>;
protected:
   using enforce_features1 = typename mlist_prepend_if<mlist_contains<typename base_t::expected_features, indexed, absorbing_feature>::value,
                                                       provide_construction<indexed, false>,
                             typename mlist_prepend_if<mlist_contains<typename base_t::expected_features, rewindable, absorbing_feature>::value,
                                                       provide_construction<rewindable, false>,
                                                       provide_construction<end_sensitive, false> >::type >::type;
   using needed_features1 = typename mix_features<typename base_t::expected_features, enforce_features1>::type;
public:
   using iterator = typename ensure_features<typename base_t::container1, needed_features1>::iterator;
   using const_iterator = typename ensure_features<typename base_t::container1, needed_features1>::const_iterator;

   iterator begin()
   {
      auto&& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      iterator b=ensure(c1, needed_features1()).begin();
      b.contract(base_t::renumber, indices.front(), Int(c1.size())-1-indices.back());
      return b;
   }
   iterator end()
   {
      iterator b = begin();
      b += this->size();
      return b;
   }
   const_iterator begin() const
   {
      const auto& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      const_iterator b=ensure(c1, needed_features1()).begin();
      b.contract(base_t::renumber, indices.front(), Int(c1.size())-1-indices.back());
      return b;
   }
   const_iterator end() const
   {
      const_iterator b=begin();
      b += this->size();
      return b;
   }
};

template <typename Top, typename Params>
class indexed_subset_rev_elem_access<Top, Params, subset_classifier::range>
   : public indexed_subset_elem_access<Top, Params, subset_classifier::range, forward_iterator_tag> {
   using base_t = indexed_subset_elem_access<Top, Params, subset_classifier::range, forward_iterator_tag>;
public:
   using reverse_iterator = typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::reverse_iterator;
   using const_reverse_iterator = typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::const_reverse_iterator;

   reverse_iterator rbegin()
   {
      auto&& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
      rb.contract(base_t::renumber, Int(c1.size())-1-indices.back(), indices.front());
      return rb;
   }
   reverse_iterator rend()
   {
      reverse_iterator rb = rbegin();
      rb += this->size();
      return rb;
   }
   const_reverse_iterator rbegin() const
   {
      const auto& c1 = this->manip_top().get_container1();
      const auto& indices = this->manip_top().get_container2();
      const_reverse_iterator rb = ensure(c1, typename base_t::needed_features1()).rbegin();
      rb.contract(base_t::renumber, Int(c1.size())-1-indices.back(), indices.front());
      return rb;
   }
   const_reverse_iterator rend() const
   {
      const_reverse_iterator rb=rbegin();
      rb += this->size();
      return rb;
   }
};

template <typename Top, typename Params, subset_classifier::kind Kind>
class indexed_subset_elem_access<Top, Params, Kind, random_access_iterator_tag>
   : public indexed_subset_elem_access<Top, Params, Kind, bidirectional_iterator_tag> {
public:
   decltype(auto) front()
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().front() ];
   }
   decltype(auto) front() const
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().front() ];
   }
   decltype(auto) back()
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().back() ];
   }
   decltype(auto) back() const
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().back() ];
   }
   decltype(auto) operator[] (Int i)
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2()[i] ];
   }
   decltype(auto) operator[] (Int i) const
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2()[i] ];
   }
};

template <typename Top, typename Params>
class indexed_subset_elem_access<Top, Params, subset_classifier::sparse, forward_iterator_tag>
   : public indexed_subset_typebase<Top, Params> {
   using base_t = indexed_subset_typebase<Top, Params>;
protected:
   using it_coupler = typename mtagged_list_extract<Params, IteratorCouplerTag, sparse_indexed_selector_coupler<base_t::renumber, false> >::type;
   using needed_features1 = typename it_coupler::template defs<typename container_traits<typename base_t::data_container>::iterator,
                                                               typename container_traits<typename base_t::index_container>::const_iterator,
                                                               typename base_t::expected_features>::needed_features1;
   using needed_features2 = typename it_coupler::template defs<typename container_traits<typename base_t::data_container>::const_iterator,
                                                               typename container_traits<typename base_t::index_container>::const_iterator,
                                                               typename base_t::expected_features>::needed_features2;

   using iterator_pair = typename it_coupler::template defs<typename ensure_features<typename base_t::data_container, needed_features1>::iterator,
                                                            typename ensure_features<typename base_t::index_container, needed_features2>::const_iterator,
                                                            typename base_t::expected_features>::iterator;
   using const_iterator_pair = typename it_coupler::template defs<typename ensure_features<typename base_t::data_container, needed_features1>::const_iterator,
                                                                  typename ensure_features<typename base_t::index_container, needed_features2>::const_iterator,
                                                                  typename base_t::expected_features>::iterator;

public:
   using iterator = typename subset_classifier::iterator_helper<iterator_pair, typename base_t::operation>::iterator;
   using const_iterator = typename subset_classifier::iterator_helper<const_iterator_pair, typename base_t::operation>::iterator;

   iterator begin()
   {
      auto&& c1=this->manip_top().get_container1();
      return iterator(ensure(c1, needed_features1()).begin(),
                      ensure(this->manip_top().get_container2(), needed_features2()).begin());
   }
   iterator end()
   {
      auto&& c1=this->manip_top().get_container1();
      return iterator(ensure(c1, needed_features1()).end(),
                      ensure(this->manip_top().get_container2(), needed_features2()).end());
   }
   const_iterator begin() const
   {
      return const_iterator(ensure(this->manip_top().get_container1(), needed_features1()).begin(),
                            ensure(this->manip_top().get_container2(), needed_features2()).begin());
   }
   const_iterator end() const
   {
      return const_iterator(ensure(this->manip_top().get_container1(), needed_features1()).end(),
                            ensure(this->manip_top().get_container2(), needed_features2()).end());
   }

   Int size() const { return count_it(begin()); }
   bool empty() const { return begin().at_end(); }

   decltype(auto) front() { return *begin(); }
   decltype(auto) front() const { return *begin(); }
};

template <typename Top, typename Params>
class indexed_subset_elem_access<Top, Params, subset_classifier::sparse, bidirectional_iterator_tag>
   : public indexed_subset_elem_access<Top, Params, subset_classifier::sparse, forward_iterator_tag> {
   using base_t = indexed_subset_elem_access<Top, Params, subset_classifier::sparse, forward_iterator_tag>;
protected:
   using rev_coupler = typename reverse_coupler<typename base_t::it_coupler>::type;
public:
   using reverse_iterator_pair = typename rev_coupler::template defs<typename ensure_features<typename base_t::data_container, typename base_t::needed_features1>::reverse_iterator,
                                                                     typename ensure_features<typename base_t::index_container, typename base_t::needed_features2>::const_reverse_iterator,
                                                                     typename base_t::expected_features>::iterator;
   using const_reverse_iterator_pair = typename rev_coupler::template defs<typename ensure_features<typename base_t::data_container, typename base_t::needed_features1>::const_reverse_iterator,
                                                                           typename ensure_features<typename base_t::index_container, typename base_t::needed_features2>::const_reverse_iterator,
                                                                           typename base_t::expected_features>::iterator;
   using reverse_iterator = typename subset_classifier::iterator_helper<reverse_iterator_pair, typename base_t::operation>::iterator;
   using const_reverse_iterator = typename subset_classifier::iterator_helper<const_reverse_iterator_pair, typename base_t::operation>::iterator;

   reverse_iterator rbegin()
   {
      auto&& c1=this->manip_top().get_container1();
      return reverse_iterator(ensure(c1, typename base_t::needed_features1()).rbegin(),
                              ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).rbegin());
   }
   reverse_iterator rend()
   {
      auto&& c1=this->manip_top().get_container1();
      return reverse_iterator(ensure(c1, typename base_t::needed_features1()).rend(),
                              ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).rend());
   }
   const_reverse_iterator rbegin() const
   {
      return const_reverse_iterator(ensure(this->manip_top().get_container1(), typename base_t::needed_features1()).rbegin(),
                                    ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).rbegin());
   }
   const_reverse_iterator rend() const
   {
      return const_reverse_iterator(ensure(this->manip_top().get_container1(), typename base_t::needed_features1()).rend(),
                                    ensure(this->manip_top().get_container2(), typename base_t::needed_features2()).rend());
   }

   decltype(auto) back() { return *rbegin(); }
   decltype(auto) back() const { return *rbegin(); }
};

template <typename Top, typename Params=typename Top::manipulator_params>
class indexed_subset_impl
   : public indexed_subset_elem_access<Top, Params> {
public:
   typedef indexed_subset_impl<Top, Params> manipulator_impl;
   typedef Params manipulator_params;

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

template <typename ContainerRef1, typename ContainerRef2, typename Params=mlist<>> class IndexedSubset;

template <typename ContainerRef1, typename ContainerRef2, typename Params,
          typename Generic1=typename object_traits<typename deref<ContainerRef1>::type>::generic_type,
          typename Generic2=typename object_traits<typename deref<ContainerRef2>::type>::generic_type>
class generic_of_indexed_subset {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
class IndexedSubset
   : public container_pair_base<ContainerRef1, ContainerRef2>
   , public indexed_subset_impl< IndexedSubset<ContainerRef1, ContainerRef2, Params>,
                                 typename mlist_concat< Container1RefTag<ContainerRef1>, Container2RefTag<ContainerRef2>, Params >::type >
   , public generic_of_indexed_subset<ContainerRef1, ContainerRef2, Params> {
public:
   using container_pair_base<ContainerRef1, ContainerRef2>::container_pair_base;

   Int dim() const { return get_dim(this->get_container1()); }
};

template <typename ContainerRef1, typename ContainerRef2, typename Params,
          typename Set1, typename E, typename Comparator, typename Set2>
class generic_of_indexed_subset<ContainerRef1, ContainerRef2, Params,
                                GenericSet<Set1, E, Comparator>, GenericSet<Set2, Int, operations::cmp> >
   : public GenericSet<IndexedSubset<ContainerRef1, ContainerRef2, Params>, E, Comparator> {
public:
   decltype(auto) get_comparator() const
   {
      return this->manip_top().get_container1().get_comparator();
   }
};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct spec_object_traits< IndexedSubset<ContainerRef1, ContainerRef2, Params> >
   : spec_object_traits<is_container> {
   static constexpr bool
      is_temporary = true,
      is_lazy = object_traits<typename deref<ContainerRef1>::type>::is_lazy,
      is_always_const = is_effectively_const<ContainerRef1>::value || is_generic_set<ContainerRef1>::value;
};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct check_container_feature<IndexedSubset<ContainerRef1, ContainerRef2, Params>, sparse_compatible>
   : check_container_feature<typename deref<ContainerRef1>::type, sparse_compatible> {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct check_container_feature<IndexedSubset<ContainerRef1, ContainerRef2, Params>, sparse>
   : check_container_feature<typename deref<ContainerRef1>::type, sparse> {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct check_container_feature<IndexedSubset<ContainerRef1, ContainerRef2, Params>, pure_sparse>
   : check_container_feature<typename deref<ContainerRef1>::type, pure_sparse> {};

// for index sets of slices, minors, and similar contexts with known full range
class OpenRange
   : public sequence {
public:
   explicit OpenRange(Int start)
      : sequence(start, 0) {}

   sequence stretch_dim(Int d) const
   {
      // for empty containers, we ignore the start value,
      // because most of the time OpenRange is used instead of a complement ~[0]
      return d ? sequence(start_, d-start_) : sequence(0, 0);
   }
};

template <>
struct spec_object_traits< OpenRange >
   : spec_object_traits<sequence> {};

inline
OpenRange range_from(Int start)
{
   return OpenRange(start);
}

template <typename IndexSet>
std::enable_if_t<!check_container_feature<typename Concrete<IndexSet>::type, sparse_compatible>::value, bool>
set_within_range(const IndexSet& s, Int d)
{
   const auto& ss = unwary(concrete(s));
   return ss.empty() || (ss.front() >= 0 && ss.back() < d);
}

template <typename IndexSet>
std::enable_if_t<check_container_feature<typename Concrete<IndexSet>::type, sparse_compatible>::value, bool>
set_within_range(const IndexSet& s, Int d)
{
   return unwary(concrete(s)).dim() <= d;
}

template <typename IndexSet>
bool set_within_range(const Complement<IndexSet>& s, Int d)
{
   // as a special case we allow a complement-based slice or minor of an empty vector resp. matrix
   return d == 0 || set_within_range(s.base(), d);
}

template <typename IndexSetRef, typename=void>
struct final_index_set {
   using type = add_const_t<unwary_t<IndexSetRef>>;
};

template <typename IndexSet, typename GetDim>
decltype(auto) prepare_index_set(IndexSet&& indices, const GetDim&,
                                 std::enable_if_t<!is_instance_of<pure_type_t<unwary_t<IndexSet>>, Complement>::value &&
                                 !std::is_same<pure_type_t<unwary_t<IndexSet>>, OpenRange>::value, void**> =nullptr)
{
   return unwary(std::forward<IndexSet>(indices));
}

template <typename IndexSet, typename GetDim>
auto prepare_index_set(IndexSet&& indices, const GetDim& d,
                       std::enable_if_t<is_instance_of<pure_type_t<unwary_t<IndexSet>>, Complement>::value, void**> =nullptr)
{
   return pure_type_t<unwary_t<IndexSet>>(unwary(std::forward<IndexSet>(indices)), d());
}

template <typename IndexSetRef>
struct final_index_set<IndexSetRef, std::enable_if_t<std::is_same<pure_type_t<IndexSetRef>, OpenRange>::value>> {
   using type = const sequence;
};

template <typename GetDim>
sequence prepare_index_set(const OpenRange& indices, const GetDim& d)
{
   return indices.stretch_dim(d());
}

template <typename Container, typename IndexSet>
auto select(Container&& c, IndexSet&& indices)
{
   if (POLYMAKE_DEBUG || is_wary<Container>() || is_wary<IndexSet>()) {
      if (!set_within_range(indices, get_dim(unwary(c))))
         throw std::runtime_error("select - indices out of range");
   }
   using result_type = IndexedSubset<unwary_t<Container>, add_const_t<unwary_t<IndexSet>>>;
   return result_type(unwary(std::forward<Container>(c)),
                      prepare_index_set(std::forward<IndexSet>(indices), [&](){ return get_dim(unwary(c)); }));
}

template <typename ContainerRef1, typename ContainerRef2, typename Params=mlist<>>
class IndexedSlice;

template <typename ContainerRef1, typename ContainerRef2, typename Params,
          typename Generic1=typename object_traits<typename deref<ContainerRef1>::type>::generic_type,
          typename Generic2=typename object_traits<typename deref<ContainerRef2>::type>::generic_type>
class generic_of_indexed_slice
   : public inherit_generic< IndexedSlice<ContainerRef1, ContainerRef2, Params>, typename deref<ContainerRef1>::type>::type {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct IndexedSlice_impl {
   using type = indexed_subset_impl< IndexedSlice<ContainerRef1, ContainerRef2, Params>,
                                     typename mlist_concat< Container1RefTag<ContainerRef1>, Container2RefTag<ContainerRef2>,
                                                            RenumberTag<std::true_type>, Params >::type >;
};

template <typename ContainerRef1, typename ContainerRef2, typename Params,
          bool is_immutable=is_effectively_const<ContainerRef1>::value,
          bool is_sparse=check_container_ref_feature<ContainerRef1, sparse>::value,
          typename tag=typename object_traits<typename deref<ContainerRef1>::type>::generic_tag,
          bool is_bidir=is_derived_from<typename IndexedSlice_impl<ContainerRef1, ContainerRef2, Params>::type::container_category,
                                        bidirectional_iterator_tag>::value>
class IndexedSlice_mod {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
class IndexedSlice
   : public container_pair_base<ContainerRef1, ContainerRef2>
   , public IndexedSlice_impl<ContainerRef1, ContainerRef2, Params>::type
   , public IndexedSlice_mod<ContainerRef1, ContainerRef2, Params>
   , public generic_of_indexed_slice<ContainerRef1, ContainerRef2, Params> {
   using base_t = container_pair_base<ContainerRef1, ContainerRef2>;
public:
   using generic_mutable_type = typename inherit_generic<IndexedSlice, typename deref<ContainerRef1>::type>::type;

   using container_pair_base<ContainerRef1, ContainerRef2>::container_pair_base;

   IndexedSlice& operator= (const IndexedSlice& other) { return generic_mutable_type::operator=(other); }
   using generic_mutable_type::operator=;

public:
   Int dim() const
   {
      return this->get_container2().size();
   }

   template <typename, typename, typename, bool, bool, typename, bool> friend class IndexedSlice_mod;
};

template <typename ContainerRef1, typename ContainerRef2, typename Params,
          typename Set1, typename E, typename Comparator, typename Set2>
class generic_of_indexed_slice<ContainerRef1, ContainerRef2, Params,
                               GenericSet<Set1, E, Comparator>, GenericSet<Set2, Int, operations::cmp> >
   : public inherit_generic< IndexedSlice<ContainerRef1, ContainerRef2, Params>, typename deref<ContainerRef1>::type>::type {
public:
   decltype(auto) get_comparator() const
   {
      return this->top().get_container1().get_comparator();
   }
};

// set, forward category
template <typename ContainerRef1, typename ContainerRef2, typename Params>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, false, is_set, false> {
   using master = IndexedSlice<ContainerRef1, ContainerRef2, Params>;
protected:
   using impl_t = typename IndexedSlice_impl<ContainerRef1, ContainerRef2, Params>::type;
private:
   typename impl_t::iterator::second_type rewind_index_impl(const typename impl_t::iterator::second_type&, Int i, forward_iterator_tag)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::iterator::second_type iit=ensure(me.manip_top().get_container2(), typename master::needed_features2()).begin();
      while (iit.index() < i) ++iit;
      return iit;
   }

   typename impl_t::iterator::second_type rewind_index_impl(typename impl_t::iterator::second_type iit, Int i, bidirectional_iterator_tag)
   {
      if (iit.at_end()) --iit;
      std::advance(iit, i-iit.index());
      return iit;
   }
protected:
   typename impl_t::iterator::second_type rewind_index(const typename impl_t::iterator::second_type& iit, Int i)
   {
      return rewind_index_impl(iit, i, typename iterator_traits<typename impl_t::iterator::second_type>::iterator_category());
   }
public:
   void clear()
   {
      master& me = static_cast<master&>(*this);
      for (typename impl_t::iterator it = me.begin(); !it.at_end(); )
         me.get_container1().erase(it++);
   }

   typename impl_t::iterator insert(const typename impl_t::iterator& pos, Int i)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::iterator::second_type iit = rewind_index(pos.second, i);
      return typename impl_t::iterator(me.get_container1().insert(pos, *iit), iit);
   }

   typename impl_t::iterator insert(Int i)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::iterator::second_type iit = rewind_index(ensure(me.manip_top().get_container2(), typename master::needed_features2()).end(), i);
      return typename impl_t::iterator(me.get_container1().insert(*iit), iit);
   }

   void erase(const typename impl_t::iterator& pos)
   {
      master& me = static_cast<master&>(*this);
      me.get_container1().erase(pos);
   }
};

// set, bidirectional category
template <typename ContainerRef1, typename ContainerRef2, typename Params>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, false, is_set, true>
   : public IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, false, is_set, false> {
   using base_t = IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, false, is_set, false>;
   using master = IndexedSlice<ContainerRef1, ContainerRef2, Params>;
protected:
   using impl_t = typename base_t::impl_t;
private:
   typename impl_t::reverse_iterator::second_type rewind_index_impl(const typename impl_t::reverse_iterator::second_type&, Int i, forward_iterator_tag)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::reverse_iterator::second_type iit = ensure(me.manip_top().get_container2(), typename master::needed_features2()).rbegin();
      while (iit.index() > i) ++iit;
      return iit;
   }

   typename impl_t::reverse_iterator::second_type rewind_index_impl(typename impl_t::reverse_iterator::second_type iit, Int i, bidirectional_iterator_tag)
   {
      if (iit.at_end()) --iit;
      std::advance(iit, iit.index() - i);
      return iit;
   }
protected:
   typename impl_t::reverse_iterator::second_type rewind_index(const typename impl_t::reverse_iterator::second_type& iit, Int i)
   {
      return rewind_index_impl(iit, i, typename iterator_traits<typename impl_t::reverse_iterator::second_type>::iterator_category());
   }
   using base_t::rewind_index;
public:
   typename impl_t::reverse_iterator insert(const typename impl_t::reverse_iterator& pos, Int i)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::reverse_iterator::second_type iit = rewind_index(pos.second, i);
      return typename impl_t::reverse_iterator(me.get_container1().insert(pos, *iit), iit);
   }
   using base_t::insert;

   void erase(const typename impl_t::reverse_iterator& pos)
   {
      master& me=static_cast<master&>(*this);
      me.get_container1().erase(pos);
   }
   using base_t::erase;
};

// sparse vector, forward category
template <typename ContainerRef1, typename ContainerRef2, typename Params, typename Tag>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, true, Tag, false>
   : public IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, false, is_set> {
   using base_t = IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, false, is_set>;
   using master = IndexedSlice<ContainerRef1, ContainerRef2, Params>;
protected:
   using impl_t = typename base_t::impl_t;
public:
   template <typename Data>
   typename impl_t::iterator insert(const typename impl_t::iterator& pos, Int i, const Data& d)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::iterator::second_type iit = this->rewind_index(pos.second, i);
      return typename impl_t::iterator(me.get_container1().insert(pos, *iit, d), iit);
   }

   template <typename Data>
   typename impl_t::iterator insert(Int i, const Data& d)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::iterator::second_type iit = this->rewind_index(ensure(me.manip_top().get_container2(), typename master::needed_features2()).end(), i);
      return typename impl_t::iterator(me.get_container1().insert(*iit, d), iit);
   }

   using base_t::insert;
};

// sparse vector, bidirectional category
template <typename ContainerRef1, typename ContainerRef2, typename Params, typename Tag>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, true, Tag, true>
   : public IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, true, Tag, false> {
   using base_t = IndexedSlice_mod<ContainerRef1, ContainerRef2, Params, false, true, Tag, false>;
   using master = IndexedSlice<ContainerRef1, ContainerRef2, Params>;
protected:
   using impl_t = typename base_t::impl_t;
public:
   template <typename Data>
   typename impl_t::reverse_iterator insert(const typename impl_t::reverse_iterator& pos, Int i, const Data& d)
   {
      master& me = static_cast<master&>(*this);
      typename impl_t::reverse_iterator::second_type iit = this->rewind_index(pos.second, i);
      return typename impl_t::reverse_iterator(me.get_container1().insert(pos, *iit, d), iit);
   }
   using base_t::insert;
};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct spec_object_traits< IndexedSlice<ContainerRef1, ContainerRef2, Params> >
   : spec_object_traits<is_container> {
   static constexpr bool
      is_temporary = true,
      is_lazy = object_traits<typename deref<ContainerRef1>::type>::is_lazy,
      is_always_const = is_effectively_const<ContainerRef1>::value;
};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct check_container_feature<IndexedSlice<ContainerRef1, ContainerRef2, Params>, sparse_compatible>
   : check_container_ref_feature<ContainerRef1, sparse_compatible> {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct check_container_feature<IndexedSlice<ContainerRef1, ContainerRef2, Params>, sparse>
   : check_container_ref_feature<ContainerRef1, sparse> {};

template <typename ContainerRef1, typename ContainerRef2, typename Params>
struct check_container_feature<IndexedSlice<ContainerRef1, ContainerRef2, Params>, pure_sparse>
   : check_container_ref_feature<ContainerRef1, pure_sparse> {};

} // end namespace pm

namespace polymake {

using pm::range_from;
using pm::select;
using pm::IndexedSubset;
using pm::IndexedSlice;

}

namespace std {

// due to silly overloading rules
template <typename ContainerRef1, typename ContainerRef2, typename Params>
void swap(pm::IndexedSlice<ContainerRef1, ContainerRef2, Params>& s1,
          pm::IndexedSlice<ContainerRef1, ContainerRef2, Params>& s2) { s1.swap(s2); }

}


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