X-Git-Url: https://git.sthu.org/?p=libstick.git;a=blobdiff_plain;f=include%2Flibstick-0.1%2Fsimplicialcomplex.h;h=988c090f56fb2378ad43044418b1fc7e0b6f0b98;hp=707fd4f3240ae7fe71c4c90e7a4d1ab25926689e;hb=4b8265e5bcfd2332a3333c4129352e10c2d59c2f;hpb=b7f36c820bf1c247ff02c5647e9cca597a9a1ba9 diff --git a/include/libstick-0.1/simplicialcomplex.h b/include/libstick-0.1/simplicialcomplex.h index 707fd4f..988c090 100644 --- a/include/libstick-0.1/simplicialcomplex.h +++ b/include/libstick-0.1/simplicialcomplex.h @@ -23,7 +23,7 @@ namespace libstick { * Each 0-dimensional simplex automatically has this simplex as its face. * Consequently, the innner class simplex_order gives the extended boundary * matrix. */ -template +template class simplicial_complex { public: @@ -116,7 +116,7 @@ class simplicial_complex { } /** Get i-th simplex in the simplex order. */ - const Simplex& get_simplex(size_t i) const { + const Simplex& get_simplex(index_type i) const { assert(i < size()); return c.simplices[order.at(i)]; } @@ -204,14 +204,31 @@ class simplicial_complex { } /** Add a simplex to the complex. The dimension of the faces must be - * dim-1, and they must already be part of the complex. */ - void add_simplex(int dim, index_type* faces, value_type value) { - add_simplex(Simplex::create(dim, faces, value)); + * dim-1, and they must already be part of the complex. Returns the + * index of the added simplex. */ + index_type add_simplex(int dim, index_type* faces, value_type value) { + return add_simplex(Simplex::create(dim, faces, value)); + } + + /** Add a simplex to the complex of at least dimension 1. The dimension + * of the faces must be dim-1, and they must already be part of the + * complex. The value of the simplex is set to the maximum value of its + * faces. Returns the index of the added simplex. */ + index_type add_simplex(int dim, index_type* faces) { + assert(dim >= 1); + + // Get max value of its faces + VT value = simplices[faces[0]].value; + for (size_t i=0; i < Simplex::face_count_bydim(dim); ++i) + value = std::max(value, simplices[faces[i]].value); + + return add_simplex(dim, faces, value); } /** Add a simplex to the complex. The dimension of the faces must be - * dim-1, and they must already be part of the complex. */ - void add_simplex(Simplex s) { + * dim-1, and they must already be part of the complex. Returns the + * index of the added simplex. */ + index_type add_simplex(Simplex s) { // Check requirements for faces for (unsigned i=0; i < s.face_count(); ++i) { // Faces are already in complex. @@ -220,7 +237,12 @@ class simplicial_complex { assert(simplices[s.faces[i]].dim == s.dim-1); } + // index_type must be large enough + assert(simplices.size() < std::numeric_limits::max()); + + index_type idx = simplices.size(); simplices.push_back(s); + return idx; } /** Add an array of simplices */