Sanitize index check
[libstick.git] / include / libstick-0.1 / simplicialcomplex.h
index 1183a92d699dfa896853e8148abb988f3ca06ed4..1695e25abc6fbe611bb7fa9bfc27e12a618c5f03 100644 (file)
@@ -10,7 +10,7 @@
 
 #include <iostream>
 
-#include <libstick-0.1/booleanmatrix.h>
+#include "booleanmatrix.h"
 
 
 namespace libstick {
@@ -92,7 +92,7 @@ class simplicial_complex {
         class simplex_order {
 
             public:
-                typedef boolean_colrowmatrix<IT> boundary_matrix;
+                typedef boolean_colmatrix<IT> boundary_matrix;
 
                 /** Create a standard order of the complex c, i.e., the identity permutation. */
                 simplex_order(const simplcompltype &c) :
@@ -117,10 +117,14 @@ class simplicial_complex {
 
                 /** Get i-th simplex in the simplex order. */
                 const simplex& get_simplex(index_type i) const {
-                    assert(i < size());
+                    assert(0 <= i && i < size());
                     return c.simplices[order.at(i)];
                 }
 
+                const simplcompltype& get_complex() const {
+                    return c;
+                }
+
                 /** Returns true iff the faces of simplex i are before i in this order. */
                 bool is_filtration() const {
                     assert(size() == c.size());
@@ -145,13 +149,22 @@ class simplicial_complex {
                     return is_filtration();
                 }
 
+                /** Randomize order. It has hardly any impact on runtime, but
+                 * it makes cycles "nicer" when the simplice's function values
+                 * are constant.
+                 * */
+                void randomize_order() {
+                    std::random_shuffle(order.begin(), order.end());
+                    restore_revorder_from_order();
+                }
+
                 /** Sort simplices such that is_monotone() gives true. This
                  * requires that the complex's is_monotone() gave true
                  * beforehand.*/
                 void make_monotone_filtration() {
                     assert(c.is_monotone());
 
-                    sort(order.begin(), order.end(), cmp_monotone_filtration(c));
+                    std::sort(order.begin(), order.end(), cmp_monotone_filtration(c));
                     restore_revorder_from_order();
 
                     assert(c.is_monotone());
@@ -198,6 +211,11 @@ class simplicial_complex {
             add_simplex(simplex::create_minusonedim_simplex());
         }
 
+        /** Remove all simplices except the dummy simplex */
+        void clear() {
+            simplices.resize(1);
+        }
+
         /** Return number of simplices. */
         size_t size() const {
             return simplices.size();