This file is indexed.

/usr/include/polymake/IndexedSubset.h is in libpolymake-dev-common 3.2r2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
/* Copyright (c) 1997-2018
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.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.
--------------------------------------------------------------------------------
*/

#ifndef POLYMAKE_INDEXED_SUBSET_H
#define POLYMAKE_INDEXED_SUBSET_H

#include "polymake/GenericSet.h"
#include "polymake/internal/Wary.h"

namespace pm {

template <typename Iterator1, typename Iterator2, bool TUseIndex1, bool TRenumber, bool TReversed=false>
class indexed_selector
   : public Iterator1 {
public:
   typedef Iterator1 first_type;
   typedef Iterator2 second_type;

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

   // 0 - general case
   // 1 - sequence::iterator
   // 2 - series::iterator
   static const int step_kind= TUseIndex1 ? 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;
   typedef int_constant<step_kind> step_discr;
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), TReversed ? 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), TReversed ? 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 TReversed ? 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(TReversed ? -i : i, renumber, bool_constant<check_iterator_feature<Iterator1, contractable>::value>());
   }
public:
   typedef typename least_derived_class<typename iterator_traits<Iterator1>::iterator_category,
                                        typename iterator_traits<Iterator2>::iterator_category>::type
      iterator_category;
   typedef typename iterator_traits<Iterator2>::difference_type difference_type;
   typedef indexed_selector<typename iterator_traits<Iterator1>::iterator, Iterator2, TUseIndex1, TRenumber, TReversed> iterator;
   typedef indexed_selector<typename iterator_traits<Iterator1>::const_iterator, Iterator2, TUseIndex1, TRenumber, TReversed> const_iterator;

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

   template <typename SourceIterator1, typename SourceIterator2,
             typename suitable1=typename suitable_arg_for_iterator<SourceIterator1, Iterator1>::type,
             typename suitable2=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), !TRenumber);
   }

   template <typename SourceIterator1, typename SourceIterator2,
             typename suitable1=typename suitable_arg_for_iterator<SourceIterator1, Iterator1>::type,
             typename suitable2=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, !TRenumber);
   }

   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>
   typename std::enable_if<is_derived_from_any<Other, iterator, const_iterator>::value, bool>::type
   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>
   typename std::enable_if<is_derived_from_any<Other, iterator, const_iterator>::value, bool>::type
   operator== (const Other& it) const
   {
      return second==it.second;
   }
   template <typename Other>
   typename std::enable_if<is_derived_from_any<Other, iterator, const_iterator>::value, bool>::type
   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<TRenumber>());
   }

   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(TRenumber && renumber, distance_front, distance_back);
      contract1(*second-pos, !TRenumber && renumber);
   }
};

template <typename Iterator1, typename Iterator2, bool TUseIndex1, bool TRenumber, bool TReversed, typename Feature>
struct check_iterator_feature<indexed_selector<Iterator1, Iterator2, TUseIndex1, TRenumber, TReversed>, Feature> {
   typedef cons<end_sensitive, contractable> via_second;
   static const bool value= (list_contains<via_second, Feature>::value ||
			     check_iterator_feature<Iterator1, Feature>::value) &&
			    check_iterator_feature<Iterator2, Feature>::value;
};

template <typename Iterator1, typename Iterator2, bool TUseIndex1, bool TRenumber, bool TReversed>
struct check_iterator_feature<indexed_selector<Iterator1, Iterator2, TUseIndex1, TRenumber, TReversed>, indexed> {
   static const bool value= !TRenumber || check_iterator_feature<Iterator2, indexed>::value;
};

template <bool TRenumber, bool TReverse>
struct sparse_indexed_selector_coupler {
   typedef typename std::conditional<TReverse, reverse_zipper<set_intersection_zipper>, set_intersection_zipper>::type
      Controller;
   template <typename Iterator1, typename Iterator2, typename ExpectedFeatures>
   struct defs {
      typedef iterator_zipper<Iterator1, Iterator2, operations::cmp, Controller, true, false> iterator;
      typedef ExpectedFeatures needed_features1;  // is already sparse
      typedef typename mix_features<needed_features1,
				    typename concat_if<TRenumber, provide_construction<indexed>, end_sensitive>::type
                                   >::type
         needed_features2;
   };
};

template <bool TRenumber, bool TReverse>
struct reverse_coupler< sparse_indexed_selector_coupler<TRenumber, TReverse> > {
   typedef sparse_indexed_selector_coupler<TRenumber, !TReverse> type;
};

template <typename Top, typename TParams> class indexed_subset_typebase;

template <typename Top, typename Helper> class indexed_subset_complement_top;

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>
      : list_contains< list( operations::apply2< BuildUnaryIt<operations::index2element> >,
			     pair< nothing, operations::apply2< BuildUnaryIt<operations::index2element> > >,
			     pair< operations::apply2< BuildUnaryIt<operations::index2element> >,
			           operations::apply2< BuildUnaryIt<operations::index2element> > >),
		       typename deref<ContainerRef>::type::operation > {};

   template <typename ContainerRef,
	     bool TRenumber=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 TRenumber,
             bool TIsComplement=is_derived_from_instance_of<typename deref<ContainerRef2>::type, Complement>::value>
   struct index_helper {
      typedef std::true_type is_complement;
      static const kind
         value = check_container_feature<typename deref<ContainerRef1>::type, pm::sparse>::value ||
                 index2element<ContainerRef1, TRenumber>::value
                 ? sparse : generic;
      static const bool random=false;
      static const bool use_index1=TRenumber && check_container_feature<typename deref<ContainerRef1>::type, sparse_compatible>::value;
      typedef operations::mul<sequence,ContainerRef2> operation;
      typedef typename deref<typename operation::result_type>::type container;
      typedef const container container_ref;
   };

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

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

   template <typename Top, typename Helper, typename _is_complement=typename Helper::is_complement>
   struct index_container_helper {
      typedef Top top_type;
   };

   template <typename Top, typename Helper>
   struct index_container_helper<Top, Helper, std::true_type> {
      typedef indexed_subset_complement_top<Top, Helper> top_type;
   };
}

template <typename Top, typename Helper>
class indexed_subset_complement_top : public Top {
public:
   const typename Helper::container get_container2() const
   {
      typename Helper::operation op;
      return op(sequence(0, get_dim(this->get_container1())), static_cast<const Top*>(this)->get_container2());
   }
protected:
   indexed_subset_complement_top();
   ~indexed_subset_complement_top();
};

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

template <typename Top, typename TParams>
class indexed_subset_typebase : public manip_container_top<Top, TParams> {
public:
   typedef typename mtagged_list_extract<TParams, Container1Tag>::type container1_ref;
   typedef typename mtagged_list_extract<TParams, Container2Tag>::type container2_ref;
   static constexpr bool renumber=tagged_list_extract_integral<TParams, RenumberTag>(false);

   typedef typename deref<container1_ref>::minus_ref container1;
   typedef typename deref<container2_ref>::minus_ref container2;
   typedef subset_classifier::index_helper<container1_ref, container2_ref, renumber> index_helper;
   typedef subset_classifier::index_container_helper<typename manip_container_top<Top, TParams>::manip_top_type, index_helper>
      index_container_helper;
   typedef typename index_helper::container index_container;
   typedef typename index_helper::container_ref index_container_ref;
   typedef container1 data_container;
   typedef typename temp_ref<container1_ref>::type data_container_ref;
   typedef typename container_traits<container1>::reference reference;
   typedef typename container_traits<container1>::const_reference const_reference;
   typedef typename container_traits<container1>::value_type value_type;
   typedef typename least_derived_class<typename container_traits<data_container>::category,
                                        typename container_traits<index_container>::category>::type
      container_category;
   typedef typename mselect<std::enable_if< mtagged_list_extract<TParams, OperationTag>::is_specified,
                                            typename mtagged_list_extract<TParams, 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
      operation;

   typedef typename concat_if<renumber, indexed, cons<end_sensitive, rewindable> >::type must_enforce_features;
   typedef typename manip_container_top<Top, TParams>::expected_features expected_features;

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

   static const subset_classifier::kind suggested_kind=
      subset_classifier::kind(std::is_same<typename mtagged_list_extract<TParams, HintTag>::type, sparse>::value
                              ? subset_classifier::sparse : index_helper::value);
public:
   static const 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:
   typedef typename list_search_all<expected_features,
				    typename concat_if<renumber, indexed, end_sensitive>::type,
				    std::is_same>::negative
      expected_features1;

   typedef typename std::conditional<rewind_required,
                                     typename list_replace<expected_features1, rewindable, provide_construction<rewindable,false> >::type,
                                     expected_features1>::type
      needed_features1;

   typedef typename std::conditional<index_required,
                                     typename mix_features<expected_features,
                                                           cons<provide_construction<indexed>, end_sensitive> >::type,
                                     typename mix_features<typename list_search_all<expected_features, indexed, absorbing_feature>::negative,
                                                           end_sensitive >::type >::type
      needed_features2;

   static bool const use_index1=index_helper::use_index1;

   int size_impl(std::false_type) const
   {
      return this->manip_top().get_container2().size();
   }
   int size_impl(std::true_type) const
   {
      return this->manip_top().get_container1().empty() ? 0 : this->manip_top().get_container1().size() - this->manip_top().get_container2().base().size();
   }
   bool empty_impl(std::false_type) const
   {
      return this->manip_top().get_container2().empty();
   }
   bool empty_impl(std::true_type) const
   {
      return size_impl(std::true_type())==0;
   }
public:
   int size() const { return size_impl(typename index_helper::is_complement()); }
   int max_size() const { return size(); }
   bool empty() const { return empty_impl(typename index_helper::is_complement()); }

   const typename index_container_helper::top_type& index_top() const
   {
      return static_cast<const typename index_container_helper::top_type&>(this->manip_top());
   }
};

template <typename Top, typename TParams,
          subset_classifier::kind TKind=indexed_subset_typebase<Top, TParams>::kind,
          typename Category=typename indexed_subset_typebase<Top, TParams>::container_category>
class indexed_subset_elem_access
   : public indexed_subset_typebase<Top, TParams> {
   typedef indexed_subset_typebase<Top, TParams> base_t;
public:
   typedef indexed_selector<typename ensure_features<typename base_t::container1, 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>
      iterator;
   typedef indexed_selector<typename ensure_features<typename base_t::container1, 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>
      const_iterator;

   iterator begin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      return iterator(ensure(c1, (typename base_t::needed_features1*)0).begin(),
		      ensure(this->index_top().get_container2(), (typename base_t::needed_features2*)0).begin(),
		      true, 0);
   }
   iterator end()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      typename base_t::index_container_ref indices=this->index_top().get_container2();
      return iterator(ensure(c1, (typename base_t::needed_features1*)0).end(),
		      ensure(indices, (typename base_t::needed_features2*)0).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*)0).begin(),
			    ensure(this->index_top().get_container2(), (typename base_t::needed_features2*)0).begin(),
			    true, 0);
   }
   const_iterator end() const
   {
      const typename base_t::container1& c1=this->manip_top().get_container1();
      typename base_t::index_container_ref indices=this->index_top().get_container2();
      return const_iterator(ensure(c1, (typename base_t::needed_features1*)0).end(),
			    ensure(indices, (typename base_t::needed_features2*)0).end(),
			    !iterator_traits<typename iterator::first_type>::is_bidirectional || indices.empty() ? 0 : indices.back()-int(c1.size()));
   }
};

template <typename Top, typename TParams, subset_classifier::kind TKind>
class indexed_subset_elem_access<Top, TParams, TKind, forward_iterator_tag>
   : public indexed_subset_elem_access<Top, TParams, TKind, input_iterator_tag> {
   typedef indexed_subset_typebase<Top, TParams> base_t;
public:
   typename base_t::reference front() { return *(this->begin()); }
   typename base_t::const_reference front() const { return *(this->begin()); }
};

template <typename Top, typename TParams, subset_classifier::kind TKind>
class indexed_subset_rev_elem_access
   : public indexed_subset_elem_access<Top, TParams, TKind, forward_iterator_tag> {
   typedef indexed_subset_elem_access<Top, TParams, TKind, forward_iterator_tag> base_t;
public:
   typedef indexed_selector<typename ensure_features<typename base_t::container1, 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>
      reverse_iterator;
   typedef indexed_selector<typename ensure_features<typename base_t::container1, 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>
      const_reverse_iterator;

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

template <typename Top, typename TParams, subset_classifier::kind TKind>
class indexed_subset_elem_access<Top, TParams, TKind, bidirectional_iterator_tag>
   : public indexed_subset_rev_elem_access<Top, TParams, TKind> {
   typedef indexed_subset_rev_elem_access<Top, TParams, TKind> base_t;
public:
   typename base_t::reference back() { return *(this->rbegin()); }
   typename base_t::reference back() const { return *(this->rbegin()); }
};

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

   iterator begin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      iterator b=ensure(c1, (needed_features1*)0).begin();
      std::advance(b, this->manip_top().get_container2().front());
      return b;
   }
   iterator end()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      if (iterator_traits<iterator>::is_bidirectional) {
	 iterator e=ensure(c1, (needed_features1*)0).end();
	 std::advance(e, this->manip_top().get_container2().back()+1-int(c1.size()));
	 return e;
      } else {
	 iterator b=ensure(c1, (needed_features1*)0).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*)0).begin();
      std::advance(b, this->manip_top().get_container2().front());
      return b;
   }
   const_iterator end() const
   {
      const typename base_t::container1& c1=this->manip_top().get_container1();
      if (iterator_traits<const_iterator>::is_bidirectional) {
	 const_iterator e=ensure(c1, (needed_features1*)0).end();
	 std::advance(e, this->manip_top().get_container2().back()+1-int(c1.size()));
	 return e;
      } else {
	 const_iterator b=ensure(c1, (needed_features1*)0).begin();
	 std::advance(b, this->manip_top().get_container2().back()+1);
	 return b;
      }
   }
};

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

   reverse_iterator rbegin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      reverse_iterator rb=ensure(c1, (typename base_t::needed_features1*)0).rbegin();
      std::advance(rb, int(c1.size())-1 - this->manip_top().get_container2().back());
      return rb;
   }
   reverse_iterator rend()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      if (iterator_traits<reverse_iterator>::is_bidirectional) {
	 reverse_iterator re=ensure(c1, (typename base_t::needed_features1*)0).rend();
	 std::advance(re, -this->manip_top().get_container2().front());
	 return re;
      } else {
	 reverse_iterator rb=ensure(c1, (typename base_t::needed_features1*)0).rbegin();
	 std::advance(rb, int(c1.size()) - this->manip_top().get_container2().front());
	 return rb;
      }
   }
   const_reverse_iterator rbegin() const
   {
      const typename base_t::container1& c1=this->manip_top().get_container1();
      const_reverse_iterator rb=ensure(c1, (typename base_t::needed_features1*)0).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*)0).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*)0).rbegin();
	 std::advance(rb, int(this->manip_top().get_container1().size()) - this->manip_top().get_container2().front());
	 return rb;
      }
   }
};

template <typename Top, typename TParams>
class indexed_subset_elem_access<Top, TParams, subset_classifier::contiguous, input_iterator_tag>
   : public indexed_subset_typebase<Top, TParams> {
   typedef indexed_subset_typebase<Top, TParams> base_t;
protected:
   typedef typename concat_if<list_search<typename base_t::expected_features, indexed, absorbing_feature>::value,
			      provide_construction<indexed,false>,
	   typename concat_if<list_search<typename base_t::expected_features, rewindable, absorbing_feature>::value,
			      provide_construction<rewindable,false>,
			      void>::type >::type
      enforce_features1;
   typedef typename mix_features<typename base_t::expected_features, enforce_features1>::type needed_features1;
public:
   typedef typename ensure_features<typename base_t::container1, needed_features1>::iterator iterator;
   typedef typename ensure_features<typename base_t::container1, needed_features1>::const_iterator const_iterator;

   iterator begin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      iterator b=ensure(c1, (needed_features1*)0).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*)0).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 TParams>
class indexed_subset_rev_elem_access<Top, TParams, subset_classifier::contiguous>
   : public indexed_subset_elem_access<Top, TParams, subset_classifier::contiguous, forward_iterator_tag> {
   typedef indexed_subset_elem_access<Top, TParams, subset_classifier::contiguous, forward_iterator_tag> base_t;
public:
   typedef typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::reverse_iterator reverse_iterator;
   typedef typename ensure_features<typename base_t::container1, typename base_t::needed_features1>::const_reverse_iterator const_reverse_iterator;

   reverse_iterator rbegin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      const typename base_t::container2& indices=this->manip_top().get_container2();
      reverse_iterator rb=ensure(c1, (typename base_t::needed_features1*)0).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 typename base_t::container1& c1=this->manip_top().get_container1();
      const typename base_t::container2& indices=this->manip_top().get_container2();
      const_reverse_iterator rb=ensure(c1, (typename base_t::needed_features1*)0).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 TParams>
class indexed_subset_elem_access<Top, TParams, subset_classifier::range, input_iterator_tag>
   : public indexed_subset_typebase<Top, TParams> {
   typedef indexed_subset_typebase<Top, TParams> base_t;
protected:
   typedef typename concat_if<list_search<typename base_t::expected_features, indexed, absorbing_feature>::value,
			      provide_construction<indexed, false>,
	   typename concat_if<list_search<typename base_t::expected_features, rewindable, absorbing_feature>::value,
			      provide_construction<rewindable, false>,
			      provide_construction<end_sensitive, false> >::type >::type
      enforce_features1;
   typedef typename mix_features<typename base_t::expected_features, enforce_features1>::type needed_features1;
public:
   typedef typename ensure_features<typename base_t::container1, needed_features1>::iterator iterator;
   typedef typename ensure_features<typename base_t::container1, needed_features1>::const_iterator const_iterator;

   iterator begin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      const typename base_t::container2& indices=this->manip_top().get_container2();
      iterator b=ensure(c1, (needed_features1*)0).begin();
      b.contract(base_t::renumber, indices.front(), int(c1.size())-1-indices.back());
      return b;
   }
   iterator end()
   {
      return begin()+this->size();
   }
   const_iterator begin() const
   {
      const typename base_t::container1& c1=this->manip_top().get_container1();
      const typename base_t::container2& indices=this->manip_top().get_container2();
      const_iterator b=ensure(c1, (needed_features1*)0).begin();
      b.contract(base_t::renumber, indices.front(), int(c1.size())-1-indices.back());
      return b;
   }
   const_iterator end() const
   {
      return begin()+this->size();
   }
};

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

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

template <typename Top, typename TParams, subset_classifier::kind TKind>
class indexed_subset_elem_access<Top, TParams, TKind, random_access_iterator_tag>
   : public indexed_subset_elem_access<Top, TParams, TKind, bidirectional_iterator_tag> {
   typedef indexed_subset_elem_access<Top, TParams, TKind, bidirectional_iterator_tag> base_t;
public:
   typename base_t::reference front()
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().front() ];
   }
   typename base_t::const_reference front() const
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().front() ];
   }
   typename base_t::reference back()
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().back() ];
   }
   typename base_t::const_reference back() const
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2().back() ];
   }
   typename base_t::reference operator[] (int i)
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2()[i] ];
   }
   typename base_t::const_reference operator[] (int i) const
   {
      return this->manip_top().get_container1()[ this->manip_top().get_container2()[i] ];
   }
};

template <typename Top, typename TParams>
class indexed_subset_elem_access<Top, TParams, subset_classifier::sparse, forward_iterator_tag>
   : public indexed_subset_typebase<Top, TParams> {
   typedef indexed_subset_typebase<Top, TParams> base_t;
protected:
   typedef typename mtagged_list_extract<TParams, IteratorCouplerTag, sparse_indexed_selector_coupler<base_t::renumber, false> >::type
      it_coupler;
   typedef 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
      needed_features1;
   typedef 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
      needed_features2;

   typedef 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
      iterator_pair;
   typedef 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
      const_iterator_pair;

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

   iterator begin()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      return iterator(ensure(c1, (needed_features1*)0).begin(),
		      ensure(this->index_top().get_container2(), (needed_features2*)0).begin());
   }
   iterator end()
   {
      typename base_t::data_container_ref c1=this->manip_top().get_container1();
      return iterator(ensure(c1, (needed_features1*)0).end(),
		      ensure(this->index_top().get_container2(), (needed_features2*)0).end());
   }
   const_iterator begin() const
   {
      return const_iterator(ensure(this->manip_top().get_container1(), (needed_features1*)0).begin(),
			    ensure(this->index_top().get_container2(), (needed_features2*)0).begin());
   }
   const_iterator end() const
   {
      return const_iterator(ensure(this->manip_top().get_container1(), (needed_features1*)0).end(),
			    ensure(this->index_top().get_container2(), (needed_features2*)0).end());
   }

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

   typename base_t::reference front() { return *begin(); }
   typename base_t::const_reference front() const { return *begin(); }
};

template <typename Top, typename TParams>
class indexed_subset_elem_access<Top, TParams, subset_classifier::sparse, bidirectional_iterator_tag>
   : public indexed_subset_elem_access<Top, TParams, subset_classifier::sparse, forward_iterator_tag> {
   typedef indexed_subset_elem_access<Top, TParams, subset_classifier::sparse, forward_iterator_tag> base_t;
protected:
   typedef typename reverse_coupler<typename base_t::it_coupler>::type rev_coupler;
public:
   typedef 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
      reverse_iterator_pair;
   typedef 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
      const_reverse_iterator_pair;
   typedef typename subset_classifier::iterator_helper<reverse_iterator_pair, typename base_t::operation>::iterator
      reverse_iterator;
   typedef typename subset_classifier::iterator_helper<const_reverse_iterator_pair, typename base_t::operation>::iterator
      const_reverse_iterator;

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

   typename base_t::reference back() { return *rbegin(); }
   typename base_t::const_reference back() const { return *rbegin(); }
};

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

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

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

template <typename ContainerRef1, typename ContainerRef2, typename TParams,
          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 TParams>
class IndexedSubset
   : public container_pair_base<ContainerRef1, ContainerRef2>,
     public indexed_subset_impl< IndexedSubset<ContainerRef1, ContainerRef2, TParams>,
				 typename mlist_concat< Container1Tag<ContainerRef1>, Container2Tag<ContainerRef2>, TParams >::type >,
     public generic_of_indexed_subset<ContainerRef1, ContainerRef2, TParams> {
   typedef container_pair_base<ContainerRef1, ContainerRef2> base_t;
public:
   IndexedSubset(typename base_t::first_arg_type src1_arg, typename base_t::second_arg_type src2_arg)
      : base_t(src1_arg, src2_arg) {}

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

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

template <typename ContainerRef1, typename ContainerRef2, typename TParams>
struct spec_object_traits< IndexedSubset<ContainerRef1, ContainerRef2, TParams> >
   : spec_object_traits<is_container> {
   static const bool is_temporary    = true,
                     is_lazy         = object_traits<typename deref<ContainerRef1>::type>::is_lazy,
                     is_always_const = effectively_const<ContainerRef1>::value;
};

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

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

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

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

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

template <typename IndexSet> inline
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 Container, typename IndexSet> inline
IndexedSubset<unwary_t<Container>&, const unwary_t<typename Concrete<IndexSet>::type>&>
select(Container& c, const IndexSet& indices)
{
   if (POLYMAKE_DEBUG || !Unwary<Container>::value || !Unwary<typename Concrete<IndexSet>::type>::value) {
      if (!set_within_range(indices, get_dim(unwary(c))))
	 throw std::runtime_error("select - indices out of range");
   }
   return IndexedSubset<unwary_t<Container>&, const unwary_t<typename Concrete<IndexSet>::type>&>
                       (unwary(c), unwary(concrete(indices)));
}

template <typename Container, typename IndexSet> inline
IndexedSubset<const unwary_t<Container>&, const unwary_t<typename Concrete<IndexSet>::type>&>
select(const Container& c, const IndexSet& indices)
{
   if (POLYMAKE_DEBUG || !Unwary<Container>::value || !Unwary<typename Concrete<IndexSet>::type>::value) {
      if (!set_within_range(indices, get_dim(unwary(c))))
	 throw std::runtime_error("select - indices out of range");
   }
   return IndexedSubset<const unwary_t<Container>&, const unwary_t<typename Concrete<IndexSet>::type>&>
                       (unwary(c), unwary(concrete(indices)));
}

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

template <typename ContainerRef1, typename ContainerRef2, typename TParams,
          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, TParams>, typename deref<ContainerRef1>::type>::type {};

template <typename ContainerRef1, typename ContainerRef2, typename TParams>
struct IndexedSlice_impl {
   typedef indexed_subset_impl< IndexedSlice<ContainerRef1, ContainerRef2, TParams>,
				typename mlist_concat< Container1Tag<ContainerRef1>, Container2Tag<ContainerRef2>,
                                                       RenumberTag<std::true_type>, TParams >::type >
      type;
};

template <typename ContainerRef1, typename ContainerRef2, typename TParams,
	  bool TImmutable=effectively_const<ContainerRef1>::value,
	  bool TSparse=check_container_ref_feature<ContainerRef1, sparse>::value,
	  typename tag=typename object_traits<typename deref<ContainerRef1>::type>::generic_tag,
	  bool TBidirectional=is_derived_from<typename IndexedSlice_impl<ContainerRef1, ContainerRef2, TParams>::type::container_category,
                                              bidirectional_iterator_tag>::value>
class IndexedSlice_mod {};

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

   IndexedSlice(typename base_t::first_arg_type src1_arg, typename base_t::second_arg_type src2_arg)
      : base_t(src1_arg,src2_arg) {}

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

protected:
   int dim_impl(std::false_type) const { return this->get_container2().size(); }
   int dim_impl(std::true_type) const { return get_dim(this->get_container1()) - this->get_container2().base().size(); }
public:
   int dim() const
   {
      return dim_impl(is_derived_from_instance_of<typename deref<ContainerRef2>::type, Complement>());
   }

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

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

// set, forward category
template <typename ContainerRef1, typename ContainerRef2, typename TParams>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, false, is_set, false> {
   typedef IndexedSlice<ContainerRef1, ContainerRef2, TParams> master;
protected:
   typedef typename IndexedSlice_impl<ContainerRef1, ContainerRef2, TParams>::type impl_t;
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.index_top().get_container2(), (typename master::needed_features2*)0).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.index_top().get_container2(), (typename master::needed_features2*)0).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 TParams>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, false, is_set, true>
   : public IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, false, is_set, false> {
   typedef IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, false, is_set, false> base_t;
   typedef IndexedSlice<ContainerRef1, ContainerRef2, TParams> master;
protected:
   typedef typename base_t::impl_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.index_top().get_container2(), (typename master::needed_features2*)0).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 TParams, typename TTag>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, true, TTag, false>
   : public IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, false, is_set> {
   typedef IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, false, is_set> base_t;
   typedef IndexedSlice<ContainerRef1, ContainerRef2, TParams> master;
protected:
   typedef typename base_t::impl_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.index_top().get_container2(), (typename master::needed_features2*)0).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 TParams, typename TTag>
class IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, true, TTag, true>
   : public IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, true, TTag, false> {
   typedef IndexedSlice_mod<ContainerRef1, ContainerRef2, TParams, false, true, TTag, false> base_t;
   typedef IndexedSlice<ContainerRef1, ContainerRef2, TParams> master;
protected:
   typedef typename base_t::impl_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 TParams>
struct spec_object_traits< IndexedSlice<ContainerRef1, ContainerRef2, TParams> >
   : spec_object_traits<is_container> {
   static const bool is_temporary    = true,
                     is_lazy         = object_traits<typename deref<ContainerRef1>::type>::is_lazy,
                     is_always_const = effectively_const<ContainerRef1>::value;
};

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

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

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

} // end namespace pm

namespace polymake {
using pm::select;
using pm::IndexedSubset;
using pm::IndexedSlice;
}

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

#endif // POLYMAKE_INDEXED_SUBSET_H

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