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


#include "polymake/internal/sparse2d_ruler.h"
#include "polymake/internal/chunk_allocator.h"
#include "polymake/IncidenceMatrix.h"
#include "polymake/EmbeddedList.h"
#include "polymake/vector"
#include "polymake/list"
#include <cassert>

namespace pm {

class FacetList;
template <> class Cols<FacetList>;

namespace fl_internal {
   class facet;
   class vertex_list;
   class lex_ordered_vertex_list;
   class superset_iterator;
   template <typename Iterator, bool check_range = true> class subset_iterator;
   class Facet; class Table;
   typedef Int facet_id_t;
}

template <>
struct spec_object_traits<fl_internal::facet>
   : spec_object_traits<is_container> {
   static const bool is_always_const=true;
   static const IO_separator_kind IO_separator=IO_sep_inherit;
};
template <>
struct spec_object_traits<fl_internal::Facet>
   : spec_object_traits<is_container> {
   static const bool is_always_const=true;
   typedef fl_internal::facet masquerade_for;
};

template <> struct spec_object_traits<fl_internal::vertex_list>
   : spec_object_traits<is_container> {};
template <> struct spec_object_traits<fl_internal::lex_ordered_vertex_list>
   : spec_object_traits<is_container> {};

namespace fl_internal {

// element of a facet
struct cell {
   cell* head_cell;  // apparent head cell contained in the facet structure

   // links to neighbor cells in the same facet; =head_cell for the lowest/highest vertex
   ptr_pair<cell> facet;   // preceding vertex;  

   // links to other facets containing the same vertex:
   // prev = facet added after this one - new facets are always pushed at the front of the column list
   // next = facet added before this one
   ptr_pair<cell> col;

   // links to neighbor siblings in the prefix tree: facets containing same vertices up to here and differing in the next vertex
   ptr_pair<cell> lex;

   Int vertex;       // vertex number = element of the facet

   cell(cell* head_arg, Int vertex_arg)
      : head_cell(head_arg)
      , vertex(vertex_arg) {}
};

// iterator along a list of cells
template <ptr_pair<cell> cell::* links, bool _rev=false>
class cell_iterator
   : public embedded_list_iterator<cell, links, true, _rev> {
public:
   typedef cell_iterator iterator;
   typedef cell_iterator const_iterator;

   typedef embedded_list_iterator<cell, links, true, _rev> super;

   cell_iterator() {}

   cell_iterator(const cell* cur_arg, const cell* head_arg)
      : super(cur_arg)
      , head(head_arg)
   {
      assert(links == &cell::facet || !head_arg);
   }

   explicit cell_iterator(const cell* cur_arg)
      : super(cur_arg)
      , head(links == &cell::facet ? cur_arg->head_cell : nullptr) {}

   iterator& operator++ ()
   {
      super::operator++();
      return *this;
   }
   iterator& operator-- ()
   {
      super::operator--();
      return *this;
   }
   const iterator operator++ (int) { iterator copy(*this); operator++(); return copy; }
   const iterator operator-- (int) { iterator copy(*this); operator--(); return copy; }

   bool at_end() const { return this->cur == head; }

   Int index() const
   {
      return links == &cell::facet ? this->cur->vertex : get_facet()->get_id();
   }

   const facet* get_facet() const;

protected:
   const cell* head;   // apparent head cell of the facet or nullptr for vertical traversals

   // move to another facet following the given link
   // @return false if no cell exists in the given direction
   bool down(ptr_pair<cell> cell::* vertical_links)
   {
      assert(links == &cell::facet && links != vertical_links);
      const cell* next = (this->cur->*vertical_links).next;
      if (!next) return false;
      this->cur = next;
      head = this->cur->head_cell;
      return true;
   }

   template <ptr_pair<cell> cell::*, bool> friend class cell_iterator;
   friend class facet;
   friend class Table;
};

class facet {
public:
   explicit facet(facet_id_t id_arg=facet_id_t(-1))
      : cells(&cell::facet)
      , size_(0)
      , id(id_arg) {}

   facet(const facet& l, chunk_allocator& al);

   void unlink_cells(chunk_allocator& al);
   cell* push_back(Int vertex, chunk_allocator& al);

   typedef cell_iterator<&cell::facet, false> iterator;
   typedef iterator const_iterator;
   typedef cell_iterator<&cell::facet, true> reverse_iterator;
   typedef reverse_iterator const_reverse_iterator;
   typedef cell value_type;
   typedef const cell& reference;
   typedef reference const_reference;

   iterator begin() const
   {
      return iterator(cells.next, head_cell());
   }
   iterator end() const
   {
      return iterator(head_cell(), head_cell());
   }
   reverse_iterator rbegin() const
   {
      return reverse_iterator(cells.prev, head_cell());
   }
   reverse_iterator rend() const
   {
      return reverse_iterator(head_cell(), head_cell());
   }

   Int size() const { return size_; }
   bool empty() const { return size_ == 0; }

protected:
   ptr_pair<facet> list_ptrs;  // embedded list of all facets in FacetList
   ptr_pair<cell> cells;       // vertices comprising this facet
   Int size_;
   facet_id_t id;

   const cell* head_cell() const
   {
      return reverse_cast(&cells, &cell::facet);
   }
   cell* head_cell()
   {
      return reverse_cast(&cells, &cell::facet);
   }

public:
   static const facet* from_head_cell(const cell* head)
   {
      return reverse_cast(&(head->facet), &facet::cells);
   }

   static const facet* from_cell(const cell* c)
   {
      return from_head_cell(c->head_cell);
   }

   facet_id_t get_id() const { return id; }

   struct id2index {
      typedef facet argument_type;
      typedef Int result_type;
      result_type operator() (const facet& f) const { return f.id; }
   };

   friend class Table;
   friend class superset_iterator;
   friend class fl_allocator;
private:
   // deleted
   facet(const facet&);
   void operator= (const facet&);
};


template <ptr_pair<cell> cell::* links, bool _rev>
const facet* cell_iterator<links, _rev>::get_facet() const
{
   return links == &cell::facet ? facet::from_head_cell(head) : facet::from_cell(this->cur);
}


class Facet
   : public modified_container_impl< Facet,
                                     mlist< HiddenTag<facet>,
                                            OperationTag< BuildUnaryIt<operations::index2element> > > >,
   public GenericSet<Facet, Int, operations::cmp> {
public:
   operations::cmp get_comparator() const { return operations::cmp(); }
   facet_id_t get_id() const { return this->hidden().get_id(); }
protected:
   Facet();
   ~Facet();
};

// Iterator over facets containing a given vertex.
// The facets are visited in reverse chronological order.
class col_order_iterator
   : public cell_iterator<&cell::col> {
   typedef cell_iterator<&cell::col> super;
public:
   typedef forward_iterator_tag iterator_category;
   typedef Facet value_type;
   typedef const Facet& reference;
   typedef const Facet* pointer;
   typedef col_order_iterator iterator;
   typedef iterator const_iterator;

   col_order_iterator(const cell* cur_arg = nullptr)
      : super(cur_arg, nullptr) {}

   reference operator* () const
   {
      return reinterpret_cast<reference>(*get_facet());
   }
   pointer operator-> () const { return &(operator*()); }

   iterator& operator++ () { super::operator++(); return *this; }
   const iterator operator++ (int) { iterator copy(*this); super::operator++(); return copy; }
};

//! Iterator over facets containing a given vertex as the lowest one.
//! Facets are visited in lexicographical order.
class lex_order_iterator {
protected:
   typedef cell_iterator<&cell::lex> cit;
   typedef std::list<cit> it_list;
   it_list Q;

public:
   typedef forward_iterator_tag iterator_category;
   typedef Facet value_type;
   typedef const Facet& reference;
   typedef const Facet* pointer;
   typedef ptrdiff_t difference_type;
   typedef lex_order_iterator iterator;
   typedef lex_order_iterator const_iterator;

   lex_order_iterator(const cell* cur_arg = nullptr);

   reference operator* () const
   {
      return reinterpret_cast<reference>(*Q.back().get_facet());
   }
   pointer operator-> () const { return &(operator*()); }

   iterator& operator++ ();
   const iterator operator++ (int) { iterator copy(*this); operator++(); return copy; }

   bool at_end() const { return Q.empty(); }

   bool operator== (const iterator& it) const { return Q==it.Q; }
   bool operator!= (const iterator& it) const { return !operator==(it); }

   Int index() const
   {
      return Q.back().index();
   }
private:
   void scan_facet(const cell* cur);
};


// Heads of lists of cells pertaining to a certain vertex
class vertex_list {
public:
   explicit vertex_list(Int vertex_)
      : vertex(vertex_)
      , first_col(nullptr)
      , first_lex(nullptr) {}

   vertex_list(const vertex_list& l);

   vertex_list(vertex_list&& l)
      : vertex_list(l.vertex, std::move(l)) {}

   vertex_list(Int vertex_, vertex_list&& l)
      : vertex(vertex_)
      , first_col(l.first_col)
      , first_lex(l.first_lex)
   {
      if (first_col)
      {
         first_col->col.prev = col_head_cell();
         l.first_col = nullptr;
      }
      if (first_lex)
      {
         first_lex->lex.prev = lex_head_cell();
         l.first_lex = nullptr;
      }
   }

private:
   // deleted
   void operator=(const vertex_list&);

public:
   typedef col_order_iterator iterator;
   typedef iterator const_iterator;

   typedef Facet value_type;
   typedef const Facet& reference;
   typedef reference const_reference;

   iterator begin() const
   {
      return iterator(first_col);
   }
   iterator end() const
   {
      return iterator();
   }
   reference front() const
   {
      return reinterpret_cast<reference>(*facet::from_cell(first_col));
   }
   Int size() const
   {
      return count_it(begin());
   }
   bool empty() const
   {
      return !first_col;
   }

protected:
   Int vertex;
   cell* first_col;
   cell* first_lex;

   cell* col_head_cell() const
   {
      return reverse_cast(reverse_cast(const_cast<cell**>(&first_col), &ptr_pair<cell>::next), &cell::col);
   }
   cell* lex_head_cell() const
   {
      return reverse_cast(reverse_cast(const_cast<cell**>(&first_lex), &ptr_pair<cell>::next), &cell::lex);
   }

   // helper class inserting cells of a new facet into all lists
   class inserter {
   private:
      // deleted
      inserter(const inserter&);
      void operator=(const inserter&);
   public:
      inserter()
         : first_old(nullptr)
         , last_old(nullptr)
         , first_new(nullptr)
         , last_new(nullptr) {}

      // @retval true if the fork point reached
      bool push(vertex_list& column, cell* newc);

      // the new facet ended up as a prefix of an existing one
      bool new_facet_ended();

   private:
      void finalize();

      // cells of an existing facet that equals the new one so far
      cell* first_old;   // target cell of the incoming arc of the prefix tree
      cell* last_old;    // highest vertex coinciding with the new facet
      // cells of the new facet
      cell* first_new;   // of the same vertex as first_old
      cell* last_new;    // of the same vertex as last_old, that is, the new cell consumed last
   };

   // insert the cell at front of the column list
   cell* push_front(cell* c)
   {
      if ((c->col.next=first_col) != nullptr)
         first_col->col.prev=c;
      c->col.prev=col_head_cell();
      first_col=c;
      return c;
   }

   friend class Table;
   template <typename,bool> friend class subset_iterator;
};

// masquerade for vertex_list, exposing the facets in lexicographical order
class lex_ordered_vertex_list
   : public vertex_list {
protected:
   lex_ordered_vertex_list() = delete;
   ~lex_ordered_vertex_list() = delete;
   lex_ordered_vertex_list(const lex_ordered_vertex_list&) = delete;
public:
   typedef lex_order_iterator iterator;
   typedef iterator const_iterator;
   typedef Facet value_type;
   typedef const Facet& reference;
   typedef reference const_reference;

   iterator begin() const
   {
      return iterator(first_lex);
   }
   iterator end() const
   {
      return iterator();
   }
   reference front() const
   {
      return reinterpret_cast<reference>(*facet::from_cell(first_lex));
   }
   Int size() const
   {
      return count_it(begin());
   }
   bool empty() const
   {
      return first_lex == nullptr;
   }
};

typedef sparse2d::ruler<vertex_list> col_ruler;

class superset_iterator {
public:
   typedef forward_iterator_tag iterator_category;
   typedef facet value_type;
   typedef const value_type& reference;
   typedef const value_type* pointer;
   typedef ptrdiff_t difference_type;
   typedef superset_iterator iterator;
   typedef iterator const_iterator;
protected:
   typedef cell_iterator<&cell::col> it_type;
   typedef std::list<it_type> it_list;

   it_list its;
   pointer cur;
   Int its_size;
   static const value_type empty_facet;
public:
   superset_iterator() {}

   template <typename TSet>
   superset_iterator(col_ruler::const_iterator columns, const GenericSet<TSet>& f, bool show_empty_facet)
   {
      its_size = iterator_traits<typename TSet::const_iterator>::is_bidirectional
                 ? f.top().size() : 0;
      for (auto v = entire(f.top());  !v.at_end();  ++v) {
         its.push_back(columns[*v].begin());
         if (!iterator_traits<typename TSet::const_iterator>::is_bidirectional)
            ++its_size;
      }
      if (its_size != 0)
         valid_position();
      else
         cur = show_empty_facet ? &empty_facet : nullptr;
   }

   reference operator* () const { return *cur; }
   pointer operator-> () const { return cur; }

   bool exact_match() const
   {
      return its_size == cur->size();
   }

   iterator& operator++ () { valid_position(); return *this; }
   const iterator operator++ (int) { iterator copy(*this);  operator++();  return copy; }

   bool at_end() const { return cur == nullptr; }

   bool operator== (const iterator& it) const { return cur == it.cur; }
   bool operator!= (const iterator& it) const { return !operator==(it); }

   Int index() const { return cur->get_id(); }
private:
   void valid_position();

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

template <typename TSet, bool check_range>
class subset_iterator {
public:
   using iterator_category = forward_iterator_tag;
   using value_type = facet;
   using reference = const value_type&;
   using pointer = const value_type*;
   using difference_type = ptrdiff_t;
   using iterator = subset_iterator;
   using const_iterator = iterator;
protected:
   using set_iterator = typename ensure_features<TSet, end_sensitive>::const_iterator;
   using it_pair = std::pair<facet::iterator, set_iterator>;
   using it_list = std::list<it_pair>;

   col_ruler::const_iterator columns;
   Int n_columns;
   set_iterator start;
   it_list Q;
   pointer cur;

public:
   subset_iterator() {}

   subset_iterator(col_ruler::const_iterator columns_arg, const GenericSet<TSet>& f)
      : columns(columns_arg)
      , start(entire(f.top()))
   {
      assert(!check_range);
      valid_position();
   }

   subset_iterator(col_ruler::const_iterator columns_arg, Int n_columns_arg, const GenericSet<TSet>& f)
      : columns(columns_arg)
      , n_columns(n_columns_arg)
      , start(entire(f.top()))
   {
      valid_position();
   }

   reference operator* () const { return *cur; }
   pointer operator-> () const { return cur; }

   iterator& operator++ () { valid_position(); return *this; }
   const iterator operator++ (int) { iterator copy(*this);  operator++();  return copy; }

   bool at_end() const { return !cur; }

   bool operator== (const iterator& it) const { return cur==it.cur; }
   bool operator!= (const iterator& it) const { return cur!=it.cur; }

   bool operator== (const superset_iterator& it) const { return cur==it.cur; }
   bool operator!= (const superset_iterator& it) const { return cur!=it.cur; }

   Int index() const { return cur->get_id(); }
private:
   void valid_position();
};

template <typename TSet, bool check_range>
void subset_iterator<TSet, check_range>::valid_position()
{
   for (;;) {
      while (!Q.empty()) {
         it_pair itp = Q.back();  Q.pop_back();
         bool match;
         do {
            if (itp.first->lex.next != nullptr) {
               Q.push_back(it_pair(facet::iterator(itp.first->lex.next), itp.second));
            }
            if ((++itp.first).at_end()) {
               cur = itp.first.get_facet();
               return;
            }
            const Int vertex_of_facet = itp.first->vertex;
            while ((match = !(++itp.second).at_end())) {
               const Int vertex_of_query = *itp.second;
               if (vertex_of_query >= vertex_of_facet) {
                  match = vertex_of_query == vertex_of_facet;
                  break;
               }
            }
         } while (match);
      }

      for (;;) {
         if (start.at_end() || check_range && *start >= n_columns) {
            cur = nullptr;
            return;
         }
         cell* first_lex = columns[*start].first_lex;
         if (first_lex != nullptr) {
            Q.push_back(it_pair(facet::iterator(first_lex), start));
            ++start;
            break;
         }
         ++start;
      }
   }
}

} // end namespace fl_internal;

template <ptr_pair<fl_internal::cell> fl_internal::cell::* links, bool _rev>
struct check_iterator_feature<fl_internal::cell_iterator<links, _rev>, end_sensitive> : std::true_type {};
template <ptr_pair<fl_internal::cell> fl_internal::cell::* links, bool _rev>
struct check_iterator_feature<fl_internal::cell_iterator<links, _rev>, indexed> : std::true_type {};

template <> struct check_iterator_feature<fl_internal::col_order_iterator, end_sensitive> : std::true_type {};
template <> struct check_iterator_feature<fl_internal::col_order_iterator, indexed> : std::true_type {};

template <> struct check_iterator_feature<fl_internal::lex_order_iterator, end_sensitive> : std::true_type {};
template <> struct check_iterator_feature<fl_internal::lex_order_iterator, indexed> : std::true_type {};

template <> struct check_iterator_feature<fl_internal::superset_iterator, end_sensitive> : std::true_type {};
template <> struct check_iterator_feature<fl_internal::superset_iterator, indexed> : std::true_type {};

template <typename TSet, bool check_range>
struct check_iterator_feature<fl_internal::subset_iterator<TSet, check_range>, end_sensitive> : std::true_type {};
template <typename TSet, bool check_range>
struct check_iterator_feature<fl_internal::subset_iterator<TSet, check_range>, indexed> : std::true_type {};

namespace fl_internal {

class Table {
public:
   typedef facet value_type;
   typedef const value_type& reference;
   typedef reference const_reference;
   typedef embedded_list_iterator<facet, &facet::list_ptrs, true, false> iterator;
   typedef embedded_list_iterator<facet, &facet::list_ptrs, true, true> reverse_iterator;
   typedef iterator const_iterator;
   typedef reverse_iterator const_reverse_iterator;

   explicit Table(size_t facet_size = sizeof(facet), Int n_vertices = 0);

   Table(const Table& c);

private:
   void operator= (const Table&) = delete;

public:
   ~Table() { col_ruler::destroy(columns); }

   //! make the table look empty
   void clear();

   //! remove all facets, keep the allocated number of vertices
   void clear_facets();

   Int n_vertices() const { return columns->size(); }

   iterator begin() const { return iterator(facets.next); }
   iterator end() const { return iterator(end_facet()); }
   reverse_iterator rbegin() const { return reverse_iterator(facets.prev); }
   reverse_iterator rend() const { return reverse_iterator(end_facet()); }

   template <typename IndexConsumer>
   void squeeze(const IndexConsumer& ic)
   {
      Int new_vertex = 0;
      for (auto column_it = columns->begin(), end = columns->end();  column_it != end;  ++column_it) {
         if (cell* c = column_it->first_col) {
            const Int old_vertex = column_it->vertex;
            if (old_vertex != new_vertex) {
               do
                  c->vertex = new_vertex;
               while ((c = c->col.next) != nullptr);
               new(&column_it[new_vertex - old_vertex]) vertex_list(new_vertex, std::move(*column_it));
            }
            ic(old_vertex, new_vertex);  ++new_vertex;
         }
      }
      if (new_vertex < n_vertices()) columns=col_ruler::resize(columns, new_vertex, false);

      if (next_id != size_) squeeze_ids();
   }

   template <typename Iterator>
   Table(size_t facet_size, Iterator src, std::false_type)
      : facet_alloc(facet_size)
      , cell_alloc(sizeof(cell))
      , facets(&facet::list_ptrs)
      , columns(col_ruler::construct(0))
      , size_(0)
      , next_id(0)
   {
      for (; !src.at_end(); ++src)
         insert(*src);
   }

   template <typename Iterator>
   Table(size_t facet_size, Int n_vertices, Iterator src, std::false_type)
      : facet_alloc(facet_size)
      , cell_alloc(sizeof(cell))
      , facets(&facet::list_ptrs)
      , columns(col_ruler::construct(n_vertices))
      , size_(0)
      , next_id(0)
   {
      for (; !src.at_end(); ++src)
         insert_from_it(entire_range(*src), new_id());
   }

   template <typename Iterator>
   Table(size_t facet_size, Iterator src, std::true_type)
      : facet_alloc(facet_size)
      , cell_alloc(sizeof(cell))
      , facets(&facet::list_ptrs)
      , columns(col_ruler::construct(0))
      , size_(0)
      , next_id(0)
   {
      for (; !src.at_end(); ++src)
         push_back(*src);
   }

   template <typename Iterator>
   Table(size_t facet_size, Int n_vertices, Iterator src, std::true_type)
      : facet_alloc(facet_size)
      , cell_alloc(sizeof(cell))
      , facets(&facet::list_ptrs)
      , columns(col_ruler::construct(n_vertices))
      , size_(0)
      , next_id(0)
   {
      for (; !src.at_end(); ++src)
         push_back_from_it(entire_range(*src));
   }

   const facet* end_facet() const
   {
      return reverse_cast(&facets, &facet::list_ptrs);
   }
   facet* end_facet()
   {
      return reverse_cast(&facets, &facet::list_ptrs);
   }

   class LexOrdered_helper
      : public modified_container_impl< LexOrdered_helper,
                                        mlist< ContainerTag< col_ruler >,
                                               OperationTag< operations::reinterpret<lex_ordered_vertex_list> >,
                                               HiddenTag< Table > > > {
   public:
      const col_ruler& get_container() const { return *hidden().columns; }
   };

protected:
   void push_back_new_facet(facet* f);
   void push_back_facet(facet* f);

   template <typename Iterator>
   void insert_cells(facet* f, Iterator&& src)
   {
      vertex_list::inserter inserter;
      Int v;
      do {
         if (src.at_end()) {
            if (inserter.new_facet_ended()) return;
            erase_facet(*f);
            throw std::runtime_error("attempt to insert a duplicate or empty facet into FacetList");
         }
         v = *src;  ++src;
         if (std::is_same<typename iterator_traits<Iterator>::iterator_category, input_iterator_tag>::value)
            extend_cols(v);
      } while (!inserter.push((*columns)[v], f->push_back(v, cell_alloc)));

      for (; !src.at_end(); ++src) {
         v = *src;
         if (std::is_same<typename iterator_traits<Iterator>::iterator_category, input_iterator_tag>::value)
            extend_cols(v);
         (*columns)[v].push_front(f->push_back(v, cell_alloc));
      }
   }

   template <typename Iterator>
   facet* insert_from_it(Iterator&& src, facet_id_t nid)
   {
      facet* f = new(facet_alloc.allocate()) facet(nid);
      push_back_facet(f);
      ++size_;
      insert_cells(f, std::move(src));
      return f;
   }

   template <typename Iterator>
   void push_back_from_it(Iterator&& src)
   {
      Int new_vertex = *src;
      facet* f = new(facet_alloc.allocate()) facet(new_id());
      cell* newc;
      cell* lex_prev;

      if ((*columns)[new_vertex].first_lex != nullptr) {
         assert(facets.prev != end_facet());
         facet::const_iterator prev_facet_it = facets.prev->begin();
         assert(new_vertex == prev_facet_it->vertex);
         push_back_facet(f);
         do {
            newc = (*columns)[new_vertex].push_front(f->push_back(new_vertex, cell_alloc));
            lex_prev = const_cast<cell*>(prev_facet_it.cur);
            ++src;  ++prev_facet_it;
            if (prev_facet_it.at_end()) break;
            assert(!src.at_end());
            new_vertex = *src;
            assert(new_vertex >= prev_facet_it->vertex);
         } while (new_vertex == prev_facet_it->vertex);
      } else {
         push_back_facet(f);
         lex_prev = (*columns)[new_vertex].lex_head_cell();
         newc = (*columns)[new_vertex].push_front(f->push_back(new_vertex, cell_alloc));
      }

      newc->lex.prev = lex_prev;
      lex_prev->lex.next = newc;

      while (!(++src).at_end()) {
         new_vertex = *src;
         (*columns)[new_vertex].push_front(f->push_back(new_vertex, cell_alloc));
      }
      ++size_;
   }

public:
   template <typename TSet>
   const facet* find_facet(const GenericSet<TSet>& f) const
   {
      auto v_it = entire(f.top());
      if (v_it.at_end()) return nullptr;
      Int v = *v_it;
      if (v >= n_vertices()) return nullptr;

      const cell* c = (*columns)[v].first_lex;
      if (!c) return nullptr;

      for (facet::iterator cur(c); ;) {
         ++v_it;  ++cur;
         if (cur.at_end()) {
            if (v_it.at_end())
               return cur.get_facet();
            else
               break;
         }
         if (v_it.at_end()) break;
         v = *v_it;
         while (cur->vertex != v) {
            if (cur->vertex > v) return nullptr;
            --cur;
            if (!cur.down(&cell::lex)) return nullptr;
            ++cur;
         }
      }
      return nullptr;
   }

   template <typename TSet>
   facet* insert(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      extend_cols(f.top().back());
      return insert_from_it(entire(f.top()), new_id());
   }

   template <typename TSet>
   void push_back(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      extend_cols(f.top().back());
      push_back_from_it(entire(f.top()));
   }

   template <typename TSet>
   Int erase(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      const facet* fp = find_facet(f);
      return fp ? (erase_facet(*fp), 1) : 0;
   }

   void erase_facet(const facet& f);

   template <typename TSet>
   superset_iterator findSupersets(const GenericSet<TSet, Int, operations::cmp>& f, bool show_empty_facet) const
   {
      return superset_iterator(columns->begin(), f, show_empty_facet);
   }

   template <typename TSet, bool check_range>
   subset_iterator<TSet, check_range> findSubsets(const GenericSet<TSet, Int, operations::cmp>& f, bool_constant<check_range>) const
   {
      return subset_iterator<TSet, check_range>(columns->begin(), n_vertices(), f);
   }

protected:
   template <typename Iterator, typename Consumer, typename Model>
   static void consume_erased(const Iterator& ss, Consumer& consumer, Model)
   {
      *consumer = reinterpret_cast<const Facet&>(*ss); ++consumer;
   }

   template <typename Iterator, typename Consumer>
   static void consume_erased(const Iterator& ss, Consumer& consumer, is_scalar)
   {
      *consumer = ss.index(); ++consumer;
   }

   template <typename Iterator, typename Consumer>
   static void consume_erased(const Iterator& ss, Consumer& consumer)
   {
      consume_erased(ss, consumer, typename object_traits<typename iterator_traits<Consumer>::value_type>::model());
   }

   template <typename Iterator, typename Data>
   static void consume_erased(const Iterator&, black_hole<Data>&) {}

public:
   template <typename TSet> static
   Int back_or_nothing(const TSet& f)
   {
      auto last=entire<reversed>(f);
      return last.at_end() ? -1 : *last;
   }

   template <typename TSet, typename Consumer>
   Int eraseSubsets(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      const Int orig_size = size_;
      for (subset_iterator<TSet> ss = findSubsets(f, std::true_type()); !ss.at_end(); ++ss) {
         consume_erased(ss, consumer);
         erase_facet(*ss);
      }
      return orig_size - size_;
   }

   template <typename TSet, typename Consumer>
   Int eraseSupersets(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      if (back_or_nothing(f.top()) >= n_vertices()) {
         return 0;
      }
      const Int orig_size = size_;
      for (superset_iterator ss = findSupersets(f,false); !ss.at_end(); ++ss) {
         consume_erased(ss, consumer);
         erase_facet(*ss);
      }
      return orig_size - size_;
   }

   template <typename TSet, bool can_extend, typename Consumer>
   facet* insertMax(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer,
                    bool_constant<can_extend>)
   {
      Int v_last;
      facet_id_t nid = new_id();
      if (can_extend && (v_last = back_or_nothing(f.top())) >= n_vertices()) {
         extend_cols(v_last);
      } else {
         superset_iterator ss = findSupersets(f, true);
         if (!ss.at_end())
            return nullptr;
      }

      for (subset_iterator<TSet, false> ss = findSubsets(f, std::false_type());  !ss.at_end();  ++ss) {
         consume_erased(ss, consumer);
         erase_facet(*ss);
      }
      return insert_from_it(entire(f.top()), nid);
   }

   template <typename TSet, bool can_extend, typename Consumer>
   facet* insertMin(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer,
                    bool_constant<can_extend>)
   {
      bool accept = false;
      Int v_last;
      facet_id_t nid = new_id();
      if (can_extend && (v_last = back_or_nothing(f.top())) >= n_vertices()) {
         extend_cols(v_last);
      } else {
         superset_iterator ss = findSupersets(f, true);
         if (!ss.at_end()) {
            if (ss.exact_match())
               return nullptr;
            do {
               consume_erased(ss,consumer);
               erase_facet(*ss); 
               accept = true;
            } while (! (++ss).at_end());
         }
      }

      if (!accept) {
         subset_iterator<TSet, false> ss=findSubsets(f, std::false_type());
         if (!ss.at_end())
            return nullptr;
      }
      return insert_from_it(entire(f.top()), nid);
   }

protected:
   chunk_allocator facet_alloc;
   chunk_allocator cell_alloc;
   ptr_pair<facet> facets;
   col_ruler* columns;
   Int size_;
   facet_id_t next_id;

   void extend_cols(Int v)
   {
      if (v >= n_vertices())
         columns=col_ruler::resize(columns, v+1);
   }

   facet_id_t new_id()
   {
      facet_id_t id=next_id;
      if (++next_id == 0) {
         id = squeeze_ids();
         ++next_id;
      }
      return id;
   }

   void skip_ids(Int amount)
   {
      next_id += amount;
   }

   facet_id_t squeeze_ids()
   {
      facet_id_t id = 0;
      for (facet *f = facets.next, *fe = end_facet(); f != fe;  f=f->list_ptrs.next, ++id)
         f->id = id;
      return next_id=id;
   }

   friend class pm::FacetList;
   friend class pm::Cols<pm::FacetList>;
#if POLYMAKE_DEBUG
   bool sanity_check() const;
#endif
};

} // end namespace fl_internal

template <>
class Cols<FacetList>
   : public redirected_container< Cols<FacetList>,
                                  mlist< ContainerTag< const fl_internal::col_ruler >,
                                         HiddenTag< FacetList > > > {
protected:
   ~Cols();
public:
   inline const container& get_container() const;
};

/** @class FacetList

    This is a collection of sets of integral numbers from a closed contiguous
    range [0..n-1], as one usually has to deal with when modeling simplicial
    complexes and related mathematical objects.  Thus we will refer to the
    contained sets as @em facets, and to the set elements as @em vertices.

    The class can efficiently maintain the property of all facets being
    mutually inclusion-free, although this property is not mandatory,
    it can be violated by the application if needed.
    The primary design goal of this class is efficient search, insertion,
    and removal of facets included in or including a given vertex set.

    The data structure is a rectangular grid, similar to IncidenceMatrix,
    interwoven with a forest of suffix trees, indexed with the vertex number.
    This also provides the lexicographical facet ordering as a pleasant side
    effect.  The whole thing is attached to a smart pointer with @ref
    refcounting "reference counting".

    For complexity statements below we (ab-)use the term @em dimension
    (abbreviated as @em dim) for the number of vertices in a facet.
    Conversely, the @em degree (abbreviated as @em deg) of a vertex is the
    number of facets containing it.

    FacetList implements STL's reversible container interface.  During the
    iteration the facets appear in the chronological order, that is, as they
    were added to the list.  Unlike @c std::list, the size() method runs in
    constant time.

    The elements of the list (facets) are of type GenericSet and implement the
    reversible container interface, too.  However, there is no random access or
    cheap existence checks for single vertices; facets have to be scanned
    sequentially.

    The iterators over the facet list have an additional method @c index()
    retrieving the unique integer id assigned to each facet when it was
    inserted into the FacetList.  A new id is generated by each call to any of
    the insert() methods, regardless whether the facet is eventually inserted
    or discarded as being dependent.  Using methods squeeze() and
    skip_facet_id() you can affect the id generated for the next facet.
 */

class FacetList
   : public modified_container_impl< FacetList,
                                     mlist< ContainerTag< const fl_internal::Table >,
                                            OperationTag< pair< operations::reinterpret<fl_internal::Facet>, fl_internal::facet::id2index> > > >
   , public matrix_col_methods<FacetList> {
protected:
   shared_object<fl_internal::Table, AliasHandlerTag<shared_alias_handler>> table;

   friend FacetList& make_mutable_alias(FacetList& alias, FacetList& owner)
   {
      alias.table.make_mutable_alias(owner.table);
      return alias;
   }

public:
   const container& get_container() const { return *table; }

   /** @brief Create an empty list.
      
      Allocate the internal data structures capable of handling sets of
      vertices from the range [0 .. @a n_vertices-1] in advance.  The vertex range can be
      dynamically expanded later, by @c insert* and @c push_back %operations,
      with reallocation costs O(@a n_vertices).
   */
   explicit FacetList(Int n_vertices = 0)
      : table(sizeof(fl_internal::facet), n_vertices) {}

   template <typename Iterator>
   explicit FacetList(Iterator&& src,
                      std::enable_if_t<assess_iterator<Iterator, check_iterator_feature, end_sensitive>::value &&
                                       assess_iterator_value<Iterator, isomorphic_types, Set<Int>>::value,
                                       void**> =nullptr)
      : table(sizeof(fl_internal::facet), std::forward<Iterator>(src), std::false_type()) {}

   /** @brief Initialize the facets from the input sequence.
       The items obtained by dereferencing @a src must be sets of cardinals (of type GenericSet).
   */
   template <typename Iterator>
   FacetList(Iterator&& src, Iterator&& src_end)
      : table(sizeof(fl_internal::facet), make_iterator_range(src, src_end), std::false_type()) {}

   /** @brief As above, but avoiding reallocation during the construction.
       The facets supplied by @a src may not contain vertices outside the range [0, @a n_vertices-1].
   */
   template <typename Iterator>
   FacetList(Int n_vertices, Iterator&& src,
             std::enable_if_t<assess_iterator<Iterator, check_iterator_feature, end_sensitive>::value &&
                              assess_iterator_value<Iterator, isomorphic_types, Set<Int>>::value,
                              std::nullptr_t> = nullptr)
      : table(sizeof(fl_internal::facet), n_vertices, std::forward<Iterator>(src), std::false_type()) {}

   template <typename TSet>
   FacetList(const GenericSet<TSet, fl_internal::Facet::persistent_type, operations::cmp>& ps)
      : table(sizeof(fl_internal::facet), entire(ps.top()), std::true_type()) {}

   template <typename TSet>
   FacetList(Int n_vertices, const GenericSet<TSet, fl_internal::Facet::persistent_type, operations::cmp>& ps)
      : table(sizeof(fl_internal::facet), n_vertices, entire(ps.top()), std::true_type()) {}

   template <typename TMatrix>
   FacetList(const GenericIncidenceMatrix<TMatrix>& m)
      : table(sizeof(fl_internal::facet), m.cols(), entire(rows(m)), std::false_type()) {}

   template <typename Container, typename=std::enable_if_t<isomorphic_to_container_of<Container, Set<Int>, is_set>::value>>
   FacetList(const Container& src)
      : table(sizeof(fl_internal::facet), entire(src), std::false_type()) {}

   /// Swap the contents of two lists in a most efficient way.
   void swap(FacetList& l) { table.swap(l.table); }

   /// Make the list empty, release all allocated resources.
   void clear() { table.apply(shared_clear()); }

   /// Return number of facets in list.
   Int size() const { return table->size_; }
  
   /// True if empty.
   bool empty() const { return table->size_==0; }

   /// Returns the number of vertices.
   Int n_vertices() const { return table->n_vertices(); }

   /// Renumber the facet ids consequently, starting with 0, thus eliminating the gaps.
   void squeeze()
   {
      table->squeeze(operations::binary_noop());
   }

   /// If you want to gather the old ids, pass a binary functor as @a index_consumer.
   template <typename IndexConsumer>
   void squeeze(const IndexConsumer& ic)
   {
      table->squeeze(ic);
   }

   /** @brief Make an artificial gap in the generated facet id sequence.
    *  The facet inserted next will have an id @a amount greater than it would have had without this call.
    */
   void skip_facet_id(Int amount = 1) { table->skip_ids(amount); }

   class LexOrdered
      : public cascade_impl< LexOrdered,
                             mlist< ContainerTag< fl_internal::Table::LexOrdered_helper >,
                                    CascadeDepth< int_constant<2> >,
                                    HiddenTag< fl_internal::Table > > >,
        public GenericSet< LexOrdered, fl_internal::Facet::persistent_type, operations::cmp> {
   protected:
      ~LexOrdered();
   };

   /**  Another view on the list, visiting the facets in lexicographical order.
    *   The result type is a @ref manipulation "masquerade reference" pointing to
    *   a GenericSet< GenericSet<Int> >.
    */
   friend const LexOrdered& lex_ordered(const FacetList& c)
   {
      return reinterpret_cast<const LexOrdered&>(*c.table);
   }

   /** @brief Add a new facet without checking the inclusion relation to the existing facets.
    *
    *  It is allowed to insert a new facet being a subset or superset of existing facets.
    *  However, insertion of an empty set or of a duplicate facet is forbidden.
    *
    *  The operation costs are O(dim + deg).
    */
   template <typename TSet>
   iterator insert(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      if (POLYMAKE_DEBUG || is_wary<TSet>()) {
         if (f.top().empty())
            throw std::runtime_error("FacetList::insert - empty facet");
      }
      return iterator(fl_internal::Table::iterator(table->insert(f)));
   }

   /** @brief Add a facet to the list with least efforts.
    *
    *  This method is primarily thought of as an construction aid, if none of
    *  the explicit constructors above suites, and enables the use of the
    *  convenient std::back_inserter.  The operation costs are O(dim).
    *
    *  The given facet must be lexicographically greater than all facets added
    *  before.  This can only be checked in debugging mode.
    */
   template <typename TSet>
   void push_back(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      if (POLYMAKE_DEBUG || is_wary<TSet>()) {
         if (f.top().empty())
            throw std::runtime_error("FacetList::push_back - empty facet");
      }
      table->push_back(f);
   }

   /** @brief Find the facet equal to the given vertex set and remove it from the list.
    *
    *  Returns 1 if a facet was removed or 0 if no matching facet was found.
    *
    *  The operation costs are O(dim + deg).
    */
   template <typename TSet>
   Int erase(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      return table->erase(f);
   }

   /// Remove the facet pointed to by the given iterator.
   void erase(const iterator& where)
   {
      return table->erase_facet(reinterpret_cast<const fl_internal::facet&>(*where));
   }

   template <typename TSet>
   iterator find(const GenericSet<TSet, Int, operations::cmp>& f) const
   {
      const fl_internal::facet* facet = table->find_facet(f);
      return facet ? iterator(fl_internal::Table::iterator(facet)) : end();
   }

   typedef unary_transform_iterator<fl_internal::superset_iterator, operations::reinterpret<fl_internal::Facet> >
      superset_iterator;

   template <typename TSet>
   superset_iterator findSupersets(const GenericSet<TSet, Int, operations::cmp>& f) const
   {
      return superset_iterator(table->findSupersets(f, false));
   }

   template <typename TSet>
   class subset_iterator
      : public unary_transform_iterator<fl_internal::subset_iterator<TSet>, operations::reinterpret<fl_internal::Facet> > {
      typedef unary_transform_iterator<fl_internal::subset_iterator<TSet>, operations::reinterpret<fl_internal::Facet> > base_t;
   public:
      subset_iterator(const fl_internal::subset_iterator<TSet>& it) : base_t(it) {}
   };

   template <typename TSet>
   subset_iterator<TSet> findSubsets(const GenericSet<TSet, Int, operations::cmp>& f) const
   {
      return subset_iterator<TSet>(table->findSubsets(f, std::true_type()));
   }

   /** @brief Erase all supersets of a given set.
       @return the number of facets actually removed.
   */
   template <typename TSet>
   Int eraseSupersets(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      return table->eraseSupersets(f, black_hole<Int>());
   }

   /** @brief Erase all supersets of a given set.
       @param consumer an output iterator swallowing all erased facets or their IDs
       @return the number of facets actually removed.
   */
   template <typename TSet, typename Consumer>
   Int eraseSupersets(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      return table->eraseSupersets(f, consumer);
   }

   /** @brief Erase all subsets of a given set.
    *  @return the number of facets actually removed.
   */
   template <typename TSet>
   Int eraseSubsets(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      return table->eraseSubsets(f, black_hole<Int>());
   }

   /** @brief Erase all subsets of a given set.
    *  @param consumer an output iterator swallowing all erased facets or their IDs
    *  @return the number of facets actually removed.
   */
   template <typename TSet, typename Consumer>
   Int eraseSubsets(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      return table->eraseSubsets(f, consumer);
   }

   /** Add a new facet @em{if and only if} there are no facets including it.
    *  If this holds, remove all facets that are included in the new one.
    *  The average operation costs are O(dim<sup>2</sup> deg).
    *
    *  @return @c true if the new facet was really included.
    */
   template <typename TSet>
   bool insertMax(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      return table->insertMax(f, black_hole<Int>(), std::true_type());
   }

   /** Add a new facet @em{if and only if} there are no facets included in it.
    *  If this holds, remove all facets including the new one.
    *
    *  @return @c true if the new facet was really included.
    */
   template <typename TSet>
   bool insertMin(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      return table->insertMin(f, black_hole<Int>(), std::true_type());
   }

   /** Add a new facet @em{if and only if} there are no facets including it.
    *  If this holds, remove all facets that are included in the new one.
    *  @param consumer an output iterator swallowing all erased facets or their IDs
    *
    *  @return @c true if the new facet was really included.
    */
   template <typename TSet, typename Consumer>
   bool insertMax(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      return table->insertMax(f, consumer, std::true_type());
   }

   /** Add a new facet @em{if and only if} there are no facets included in it.
    *  If this holds, remove all facets including the new one.
    *  @param consumer an output iterator swallowing all erased facets or their IDs
    *
    *  @return @c true if the new facet was really included.
    */
   template <typename TSet, typename Consumer>
   bool insertMin(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      return table->insertMin(f, consumer, std::true_type());
   }

   /** Slightly optimized versions of @see insertMax.  Assumes that the
       @c FacetList object already has all columns corresponding to the vertices
       of a new facet, and therefore does not need to be expanded.
   */
   template <typename TSet>
   bool replaceMax(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      if (POLYMAKE_DEBUG || is_wary<TSet>()) {
         if (!set_within_range(f.top(), this->cols()))
            throw std::runtime_error("FacetList::replaceMax - invalid face");
      }
      return table->insertMax(f, black_hole<Int>(), std::false_type());
   }

   /** Slightly optimized versions of @see insertMin.  Assumes that the
       @c FacetList object already has all columns corresponding to the vertices
       of a new facet, and therefore does not need to be expanded.
   */
   template <typename TSet>
   bool replaceMin(const GenericSet<TSet, Int, operations::cmp>& f)
   {
      if (POLYMAKE_DEBUG || is_wary<TSet>()) {
         if (!set_within_range(f.top(), this->cols()))
            throw std::runtime_error("FacetList::replaceMin - invalid face");
      }
      return table->insertMin(f, black_hole<Int>(), std::false_type());
   }

   template <typename TSet, typename Consumer>
   bool replaceMax(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      if (POLYMAKE_DEBUG || is_wary<TSet>()) {
         if (!set_within_range(f.top(), this->cols()))
            throw std::runtime_error("FacetList::replaceMax - invalid face");
      }
      return table->insertMax(f, consumer, std::false_type());
   }

   template <typename TSet, typename Consumer>
   bool replaceMin(const GenericSet<TSet, Int, operations::cmp>& f, Consumer consumer)
   {
      if (POLYMAKE_DEBUG || is_wary<TSet>()) {
         if (!set_within_range(f.top(), this->cols()))
            throw std::runtime_error("FacetList::replaceMin - invalid face");
      }
      return table->insertMin(f, consumer, std::false_type());
   }

   friend class Cols<FacetList>;

#if POLYMAKE_DEBUG
   bool sanity_check() const;
   void dump() const;
#endif
};

const Cols<FacetList>::container&
Cols<FacetList>::get_container() const
{
   return *hidden().table->columns;
}

template <>
struct spec_object_traits<FacetList::LexOrdered>
   : spec_object_traits<is_container> {
   static const bool is_always_const=true;
   typedef FacetList masquerade_for;
};

template <typename TSet>
struct check_iterator_feature<FacetList::subset_iterator<TSet>, end_sensitive> : std::true_type {};
template <typename TSet>
struct check_iterator_feature<FacetList::subset_iterator<TSet>, indexed> : std::true_type {};

} // end namespace pm

namespace std {
   inline
   void swap(pm::FacetList& l1, pm::FacetList& l2) { l1.swap(l2); }
}

namespace polymake {
   using pm::FacetList;
}


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