12#ifndef SIMPLEX_TREE_H_
13#define SIMPLEX_TREE_H_
15#include <gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h>
16#include <gudhi/Simplex_tree/Simplex_tree_siblings.h>
17#include <gudhi/Simplex_tree/Simplex_tree_iterators.h>
18#include <gudhi/Simplex_tree/indexing_tag.h>
19#include <gudhi/Simplex_tree/serialization_utils.h>
23#include <gudhi/Debug_utils.h>
25#include <boost/container/flat_map.hpp>
26#include <boost/iterator/transform_iterator.hpp>
27#include <boost/graph/adjacency_list.hpp>
28#include <boost/range/adaptor/reversed.hpp>
29#include <boost/range/adaptor/transformed.hpp>
30#include <boost/range/size.hpp>
31#include <boost/container/static_vector.hpp>
34#include <tbb/parallel_sort.h>
42#include <initializer_list>
67struct Simplex_tree_options_full_featured;
82template<
typename SimplexTreeOptions = Simplex_tree_options_full_featured>
106 typedef typename boost::container::flat_map<Vertex_handle, Node> Dictionary;
113 struct Key_simplex_base_real {
114 Key_simplex_base_real() : key_(-1) {}
120 struct Key_simplex_base_dummy {
121 Key_simplex_base_dummy() {}
123 void assign_key(Simplex_key);
124 Simplex_key key()
const;
126 struct Extended_filtration_data {
129 Extended_filtration_data(){}
132 typedef typename std::conditional<Options::store_key, Key_simplex_base_real, Key_simplex_base_dummy>::type
135 struct Filtration_simplex_base_real {
136 Filtration_simplex_base_real() : filt_(0) {}
142 struct Filtration_simplex_base_dummy {
143 Filtration_simplex_base_dummy() {}
144 void assign_filtration(
Filtration_value GUDHI_CHECK_code(f)) { GUDHI_CHECK(f == 0,
"filtration value specified for a complex that does not store them"); }
148 Filtration_simplex_base_dummy>::type Filtration_simplex_base;
159 typedef typename Dictionary::iterator Dictionary_it;
160 typedef typename Dictionary_it::value_type Dit_value_t;
162 struct return_first {
232 boost::make_transform_iterator(root_.members_.begin(), return_first()),
233 boost::make_transform_iterator(root_.members_.end(), return_first()));
277 return filtration_vect_;
306 template<
class SimplexHandle>
323 template<
class SimplexHandle>
336 root_(nullptr, null_vertex_),
343 std::clog <<
"Simplex_tree copy constructor" << std::endl;
345 copy_from(complex_source);
353 std::clog <<
"Simplex_tree move constructor" << std::endl;
355 move_from(complex_source);
359 complex_source.dimension_ = -1;
364 root_members_recursive_deletion();
370 std::clog <<
"Simplex_tree copy assignment" << std::endl;
373 if (&complex_source !=
this) {
375 root_members_recursive_deletion();
377 copy_from(complex_source);
387 std::clog <<
"Simplex_tree move assignment" << std::endl;
390 if (&complex_source !=
this) {
392 root_members_recursive_deletion();
394 move_from(complex_source);
403 null_vertex_ = complex_source.null_vertex_;
404 filtration_vect_.clear();
405 dimension_ = complex_source.dimension_;
406 auto root_source = complex_source.root_;
409 root_.members() = Dictionary(boost::container::ordered_unique_range, root_source.members().begin(), root_source.members().end());
411 for (
auto& map_el : root_.members()) {
412 map_el.second.assign_children(&root_);
414 rec_copy(&root_, &root_source);
419 for (
auto sh = sib->members().begin(), sh_source = sib_source->members().begin();
420 sh != sib->members().end(); ++sh, ++sh_source) {
423 newsib->members_.reserve(sh_source->second.children()->members().size());
424 for (
auto & child : sh_source->second.children()->members())
425 newsib->members_.emplace_hint(newsib->members_.end(), child.first, Node(newsib, child.second.filtration()));
426 rec_copy(newsib, sh_source->second.children());
427 sh->second.assign_children(newsib);
434 null_vertex_ = std::move(complex_source.null_vertex_);
435 root_ = std::move(complex_source.root_);
436 filtration_vect_ = std::move(complex_source.filtration_vect_);
437 dimension_ = complex_source.dimension_;
440 for (
auto& map_el : root_.members()) {
441 if (map_el.second.children() != &(complex_source.root_)) {
443 map_el.second.children()->oncles_ = &root_;
446 GUDHI_CHECK(map_el.second.children()->oncles_ ==
nullptr,
447 std::invalid_argument(
"Simplex_tree move constructor from an invalid Simplex_tree"));
449 map_el.second.assign_children(&root_);
455 void root_members_recursive_deletion() {
456 for (
auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) {
458 rec_delete(sh->second.children());
461 root_.members().clear();
466 for (
auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
468 rec_delete(sh->second.children());
477 if ((null_vertex_ != st2.null_vertex_) ||
478 (dimension_ != st2.dimension_ && !dimension_to_be_lowered_ && !st2.dimension_to_be_lowered_))
480 return rec_equal(&root_, &st2.root_);
485 return (!(*
this == st2));
491 if (s1->members().size() != s2->members().size())
493 for (
auto sh1 = s1->members().begin(), sh2 = s2->members().begin();
494 (sh1 != s1->members().end() && sh2 != s2->members().end()); ++sh1, ++sh2) {
495 if (sh1->first != sh2->first || sh1->second.filtration() != sh2->second.filtration())
501 if (!rec_equal(sh1->second.children(), sh2->second.children()))
513 return sh->second.filtration();
523 return sh->second.key();
531 return filtration_vect_[idx];
541 return sh->second.filtration();
543 return std::numeric_limits<Filtration_value>::infinity();
552 std::invalid_argument(
"Simplex_tree::assign_filtration - cannot assign filtration on null_simplex"));
553 sh->second.assign_filtration(fv);
561 return Dictionary_it(
nullptr);
577 return root_.members_.size();
582 return root_.members_.empty();
594 auto sib_begin = sib->members().begin();
595 auto sib_end = sib->members().end();
596 size_t simplices_number = sib_end - sib_begin;
597 for (
auto sh = sib_begin; sh != sib_end; ++sh) {
602 return simplices_number;
612 while (curr_sib !=
nullptr) {
614 curr_sib = curr_sib->oncles();
629 if (dimension_to_be_lowered_)
630 lower_upper_bound_dimension();
636 template<
class SimplexHandle>
639 return (sh->second.children()->parent() == sh->first);
649 template<
class InputVertexRange = std::initializer_list<Vertex_handle>>
651 auto first = std::begin(s);
652 auto last = std::end(s);
658 std::vector<Vertex_handle> copy(first, last);
659 std::sort(std::begin(copy), std::end(copy));
660 return find_simplex(copy);
665 Simplex_handle find_simplex(
const std::vector<Vertex_handle> &
simplex) {
667 Dictionary_it tmp_dit;
671 GUDHI_CHECK(contiguous_vertices(),
"non-contiguous vertices");
673 if(v < 0 || v >=
static_cast<Vertex_handle>(root_.members_.size()))
675 tmp_dit = root_.members_.begin() + v;
680 tmp_sib = tmp_dit->second.children();
683 tmp_dit = tmp_sib->members_.find(*vi++);
684 if (tmp_dit == tmp_sib->members_.end())
690 tmp_sib = tmp_dit->second.children();
698 assert(contiguous_vertices());
699 return root_.members_.begin() + v;
701 return root_.members_.find(v);
707 bool contiguous_vertices()
const {
708 if (root_.members_.empty())
return true;
709 if (root_.members_.begin()->first != 0)
return false;
710 if (std::prev(root_.members_.end())->first !=
static_cast<Vertex_handle>(root_.members_.size() - 1))
return false;
729 template <
class RandomVertexHandleRange = std::initializer_list<Vertex_handle>>
730 std::pair<Simplex_handle, bool> insert_simplex_raw(
const RandomVertexHandleRange&
simplex,
733 std::pair<Simplex_handle, bool> res_insert;
735 for (; vi != std::prev(
simplex.end()); ++vi) {
736 GUDHI_CHECK(*vi !=
null_vertex(),
"cannot use the dummy null_vertex() as a real vertex");
737 res_insert = curr_sib->members_.emplace(*vi, Node(curr_sib,
filtration));
739 res_insert.first->second.assign_children(
new Siblings(curr_sib, *vi));
741 curr_sib = res_insert.first->second.children();
743 GUDHI_CHECK(*vi !=
null_vertex(),
"cannot use the dummy null_vertex() as a real vertex");
744 res_insert = curr_sib->members_.emplace(*vi, Node(curr_sib,
filtration));
745 if (!res_insert.second) {
747 if (res_insert.first->second.filtration() >
filtration) {
749 res_insert.first->second.assign_filtration(
filtration);
753 return std::pair<Simplex_handle, bool>(
null_simplex(),
false);
756 int dim =
static_cast<int>(boost::size(
simplex)) - 1;
757 if (dim > dimension_) {
788 template<
class InputVertexRange = std::initializer_list<Vertex_handle>>
791 auto first = std::begin(
simplex);
795 return std::pair<Simplex_handle, bool>(
null_simplex(),
true);
798 std::vector<Vertex_handle> copy(first, last);
799 std::sort(std::begin(copy), std::end(copy));
817 template<
class InputVertexRange = std::initializer_list<Vertex_handle>>
820 auto first = std::begin(Nsimplex);
821 auto last = std::end(Nsimplex);
826 thread_local std::vector<Vertex_handle> copy;
828 copy.insert(copy.end(), first, last);
829 std::sort(copy.begin(), copy.end());
830 auto last_unique = std::unique(copy.begin(), copy.end());
831 copy.erase(last_unique, copy.end());
834 GUDHI_CHECK(v !=
null_vertex(),
"cannot use the dummy null_vertex() as a real vertex");
837 dimension_ = (std::max)(dimension_,
static_cast<int>(std::distance(copy.begin(), copy.end())) - 1);
839 return rec_insert_simplex_and_subfaces_sorted(
root(), copy.begin(), copy.end(),
filtration);
844 template<
class ForwardVertexIterator>
845 std::pair<Simplex_handle, bool> rec_insert_simplex_and_subfaces_sorted(
Siblings* sib,
846 ForwardVertexIterator first,
847 ForwardVertexIterator last,
854 auto&& dict = sib->members();
855 auto insertion_result = dict.emplace(vertex_one, Node(sib, filt));
856 Simplex_handle simplex_one = insertion_result.first;
857 bool one_is_new = insertion_result.second;
866 if (++first == last)
return insertion_result;
869 simplex_one->second.assign_children(
new Siblings(sib, vertex_one));
870 auto res = rec_insert_simplex_and_subfaces_sorted(simplex_one->second.children(), first, last, filt);
872 if (res.first !=
null_simplex()) rec_insert_simplex_and_subfaces_sorted(sib, first, last, filt);
880 sh->second.assign_key(
key);
888 return { find_vertex(sh->first), find_vertex(
self_siblings(sh)->parent()) };
892 template<
class SimplexHandle>
894 if (sh->second.children()->parent() == sh->first)
895 return sh->second.children()->oncles();
897 return sh->second.children();
913 dimension_to_be_lowered_ = !exact;
926 filtration_vect_.clear();
929 filtration_vect_.push_back(sh);
941 tbb::parallel_sort(filtration_vect_.begin(), filtration_vect_.end(), is_before_in_filtration(
this));
943 std::stable_sort(filtration_vect_.begin(), filtration_vect_.end(), is_before_in_filtration(
this));
950 if (filtration_vect_.empty()) {
959 filtration_vect_.clear();
975 void rec_coface(std::vector<Vertex_handle> &vertices,
Siblings *curr_sib,
int curr_nbVertices,
976 std::vector<Simplex_handle>& cofaces,
bool star,
int nbVertices) {
977 if (!(star || curr_nbVertices <= nbVertices))
979 for (Simplex_handle
simplex = curr_sib->members().begin();
simplex != curr_sib->members().end(); ++
simplex) {
980 if (vertices.empty()) {
985 bool addCoface = (star || curr_nbVertices == nbVertices);
989 rec_coface(vertices,
simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
991 if (
simplex->first == vertices.back()) {
993 bool equalDim = (star || curr_nbVertices == nbVertices);
994 bool addCoface = vertices.size() == 1 && equalDim;
1000 vertices.pop_back();
1001 rec_coface(vertices,
simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
1002 vertices.push_back(tmp);
1004 }
else if (
simplex->first > vertices.back()) {
1009 rec_coface(vertices,
simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
1035 assert(codimension >= 0);
1037 std::vector<Vertex_handle> copy(rg.begin(), rg.end());
1038 if (codimension +
static_cast<int>(copy.size()) > dimension_ + 1 ||
1039 (codimension == 0 &&
static_cast<int>(copy.size()) > dimension_))
1042 assert(std::is_sorted(copy.begin(), copy.end(), std::greater<Vertex_handle>()));
1043 bool star = codimension == 0;
1044 rec_coface(copy, &root_, 1, cofaces, star, codimension +
static_cast<int>(copy.size()));
1056 bool reverse_lexicographic_order(Simplex_handle sh1, Simplex_handle sh2) {
1061 while (it1 != rg1.end() && it2 != rg2.end()) {
1069 return ((it1 == rg1.end()) && (it2 != rg2.end()));
1078 struct is_before_in_filtration {
1082 bool operator()(
const Simplex_handle sh1,
const Simplex_handle sh2)
const {
1084 if (sh1->second.filtration() != sh2->second.filtration()) {
1085 return sh1->second.filtration() < sh2->second.filtration();
1088 return st_->reverse_lexicographic_order(sh1, sh2);
1118 template<
class OneSkeletonGraph>
1124 using boost::num_vertices;
1129 if (num_edges(skel_graph) == 0) {
1136 auto verts = vertices(skel_graph) | boost::adaptors::transformed([&](
auto v){
1137 return Dit_value_t(v,
Node(&root_, get(vertex_filtration_t(), skel_graph, v))); });
1138 root_.members_.insert(boost::begin(verts), boost::end(verts));
1141 std::pair<typename boost::graph_traits<OneSkeletonGraph>::edge_iterator,
1142 typename boost::graph_traits<OneSkeletonGraph>::edge_iterator> boost_edges = edges(skel_graph);
1145 for (; boost_edges.first != boost_edges.second; boost_edges.first++) {
1146 auto edge = *(boost_edges.first);
1147 auto u = source(edge, skel_graph);
1148 auto v = target(edge, skel_graph);
1149 if (u == v)
throw std::invalid_argument(
"Self-loops are not simplicial");
1157 if (v < u) std::swap(u, v);
1158 auto sh = find_vertex(u);
1160 sh->second.assign_children(
new Siblings(&root_, sh->first));
1163 sh->second.children()->members().emplace(v,
1164 Node(sh->second.children(), get(edge_filtration_t(), skel_graph, edge)));
1175 template <
class VertexRange>
1177 auto verts = vertices | boost::adaptors::transformed([&](
auto v){
1178 return Dit_value_t(v,
Node(&root_, filt)); });
1179 root_.members_.insert(boost::begin(verts), boost::end(verts));
1180 if (dimension_ < 0 && !root_.members_.empty()) dimension_ = 0;
1195 if (max_dim <= 1)
return;
1197 dimension_ = max_dim;
1198 for (Dictionary_it root_it = root_.members_.begin();
1199 root_it != root_.members_.end(); ++root_it) {
1201 siblings_expansion(root_it->second.children(), max_dim - 1);
1204 dimension_ = max_dim - dimension_;
1209 void siblings_expansion(
Siblings * siblings,
1211 if (dimension_ > k) {
1216 Dictionary_it next = siblings->members().begin();
1219 thread_local std::vector<std::pair<Vertex_handle, Node> > inter;
1220 for (Dictionary_it s_h = siblings->members().begin();
1221 s_h != siblings->members().end(); ++s_h, ++next) {
1222 Simplex_handle root_sh = find_vertex(s_h->first);
1227 siblings->members().end(),
1228 root_sh->second.children()->members().begin(),
1229 root_sh->second.children()->members().end(),
1230 s_h->second.filtration());
1231 if (inter.size() != 0) {
1236 s_h->second.assign_children(new_sib);
1237 siblings_expansion(new_sib, k - 1);
1240 s_h->second.assign_children(siblings);
1249 static void intersection(std::vector<std::pair<Vertex_handle, Node> >& intersection,
1250 Dictionary_it begin1, Dictionary_it end1,
1251 Dictionary_it begin2, Dictionary_it end2,
1253 if (begin1 == end1 || begin2 == end2)
1256 if (begin1->first == begin2->first) {
1257 Filtration_value filt = (std::max)({begin1->second.filtration(), begin2->second.filtration(), filtration_});
1258 intersection.emplace_back(begin1->first, Node(
nullptr, filt));
1259 if (++begin1 == end1 || ++begin2 == end2)
1261 }
else if (begin1->first < begin2->first) {
1262 if (++begin1 == end1)
1265 if (++begin2 == end2)
1290 template<
typename Blocker >
1293 for (
auto&
simplex : boost::adaptors::reverse(root_.members())) {
1295 siblings_expansion_with_blockers(
simplex.second.children(), max_dim, max_dim - 1, block_simplex);
1302 template<
typename Blocker >
1303 void siblings_expansion_with_blockers(
Siblings* siblings,
int max_dim,
int k, Blocker block_simplex) {
1304 if (dimension_ < max_dim - k) {
1305 dimension_ = max_dim - k;
1310 if (siblings->members().size() < 2)
1313 for (
auto simplex = siblings->members().rbegin() + 1;
simplex != siblings->members().rend();
simplex++) {
1314 std::vector<std::pair<Vertex_handle, Node> > intersection;
1315 for(
auto next = siblings->members().rbegin(); next !=
simplex; next++) {
1316 bool to_be_inserted =
true;
1320 Simplex_handle border_child = find_child(border, next->first);
1322 to_be_inserted=
false;
1325 filt = (std::max)(filt,
filtration(border_child));
1327 if (to_be_inserted) {
1328 intersection.emplace_back(next->first, Node(
nullptr, filt));
1331 if (intersection.size() != 0) {
1335 boost::adaptors::reverse(intersection));
1336 simplex->second.assign_children(new_sib);
1337 std::vector<Vertex_handle> blocked_new_sib_vertex_list;
1339 for (
auto new_sib_member = new_sib->members().begin();
1340 new_sib_member != new_sib->members().end();
1342 bool blocker_result = block_simplex(new_sib_member);
1345 if (blocker_result) {
1346 blocked_new_sib_vertex_list.push_back(new_sib_member->first);
1349 if (blocked_new_sib_vertex_list.size() == new_sib->members().size()) {
1353 simplex->second.assign_children(siblings);
1355 for (
auto& blocked_new_sib_member : blocked_new_sib_vertex_list) {
1356 new_sib->members().erase(blocked_new_sib_member);
1359 siblings_expansion_with_blockers(new_sib, max_dim, k - 1, block_simplex);
1363 simplex->second.assign_children(siblings);
1372 Simplex_handle find_child(Simplex_handle sh,
Vertex_handle vh)
const {
1376 Simplex_handle child = sh->second.children()->find(vh);
1379 if (child == sh->second.children()->members().end())
1397 os <<
key(b_sh) <<
" ";
1411 bool modified =
false;
1413 for (
auto&
simplex : boost::adaptors::reverse(root_.members())) {
1415 modified |= rec_make_filtration_non_decreasing(
simplex.second.children());
1428 bool rec_make_filtration_non_decreasing(
Siblings * sib) {
1429 bool modified =
false;
1432 for (
auto&
simplex : boost::adaptors::reverse(sib->members())) {
1436 [](Simplex_handle sh1, Simplex_handle sh2) {
1443 if (!(
simplex.second.filtration() >= max_filt_border_value)) {
1446 simplex.second.assign_filtration(max_filt_border_value);
1449 modified |= rec_make_filtration_non_decreasing(
simplex.second.children());
1473 auto&& list = sib->members();
1474 auto last = std::remove_if(list.begin(), list.end(), [
this,filt](Dit_value_t&
simplex) {
1475 if (simplex.second.filtration() <= filt) return false;
1476 if (has_children(&simplex)) rec_delete(simplex.second.children());
1478 dimension_to_be_lowered_ = true;
1482 bool modified = (last != list.end());
1483 if (last == list.begin() && sib !=
root()) {
1485 sib->oncles()->members()[sib->parent()].assign_children(sib->oncles());
1488 dimension_to_be_lowered_ =
true;
1492 list.erase(last, list.end());
1495 modified |= rec_prune_above_filtration(
simplex.second.children(), filt);
1509 bool modified =
false;
1512 root_members_recursive_deletion();
1529 bool rec_prune_above_dimension(
Siblings* sib,
int dim,
int actual_dim) {
1530 bool modified =
false;
1531 auto&& list = sib->members();
1535 if (actual_dim >= dim) {
1536 rec_delete(
simplex.second.children());
1537 simplex.second.assign_children(sib);
1540 modified |= rec_prune_above_dimension(
simplex.second.children(), dim, actual_dim + 1);
1553 bool lower_upper_bound_dimension() {
1555 dimension_to_be_lowered_ =
false;
1556 int new_dimension = -1;
1561 std::clog <<
" " << vertex;
1563 std::clog << std::endl;
1567 if (sh_dimension >= dimension_)
1570 new_dimension = (std::max)(new_dimension, sh_dimension);
1572 dimension_ = new_dimension;
1589 std::invalid_argument(
"Simplex_tree::remove_maximal_simplex - argument has children"));
1592 Siblings* child = sh->second.children();
1594 if ((child->size() > 1) || (child ==
root())) {
1600 child->oncles()->members().at(child->parent()).assign_children(child->oncles());
1603 dimension_to_be_lowered_ =
true;
1624 std::pair<Filtration_value, Extended_simplex_type> p;
1627 if (f >= -2 && f <= -1){
1628 p.first = minval + (maxval-minval)*(f + 2); p.second = Extended_simplex_type::UP;
1630 else if (f >= 1 && f <= 2){
1631 p.first = minval - (maxval-minval)*(f - 2); p.second = Extended_simplex_type::DOWN;
1634 p.first = std::numeric_limits<Filtration_value>::quiet_NaN(); p.second = Extended_simplex_type::EXTRA;
1656 Vertex_handle maxvert = std::numeric_limits<Vertex_handle>::min();
1657 Filtration_value minval = std::numeric_limits<Filtration_value>::infinity();
1658 Filtration_value maxval = -std::numeric_limits<Filtration_value>::infinity();
1659 for (
auto sh = root_.members().begin(); sh != root_.members().end(); ++sh){
1661 minval = std::min(minval, f);
1662 maxval = std::max(maxval, f);
1663 maxvert = std::max(sh->first, maxvert);
1666 GUDHI_CHECK(maxvert < std::numeric_limits<Vertex_handle>::max(), std::invalid_argument(
"Simplex_tree contains a vertex with the largest Vertex_handle"));
1672 this->insert_simplex_raw({maxvert}, -3);
1675 std::vector<Vertex_handle> vr;
1683 auto sh = this->
find(vr);
1686 vr.push_back(maxvert);
1706 Extended_filtration_data efd(minval, maxval);
1715 auto filt = filtration_(sh);
1717 if(filtration_(find_vertex(v)) == filt)
1731 auto end = std::end(vertices);
1732 auto vi = std::begin(vertices);
1733 GUDHI_CHECK(vi != end,
"empty simplex");
1736 GUDHI_CHECK(vi != end,
"simplex of dimension 0");
1737 if(std::next(vi) == end)
return sh;
1738 boost::container::static_vector<Vertex_handle, 40> suffix;
1739 suffix.push_back(v0);
1740 auto filt = filtration_(sh);
1744 auto&& children1 = find_vertex(v)->second.children()->members_;
1745 for(
auto w : suffix){
1748 if(filtration_(s) == filt)
1751 suffix.push_back(v);
1762 auto filt = filtration_(sh);
1765 if(filtration_(b) == filt)
1780 rec_reset_filtration(&root_, filt_value, min_dim);
1791 for (
auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
1792 if (min_depth <= 0) {
1793 sh->second.assign_filtration(filt_value);
1796 rec_reset_filtration(sh->second.children(), filt_value, min_depth - 1);
1809 std::size_t get_serialization_size() {
1812 const std::size_t buffer_byte_size = vh_byte_size +
num_simplices() * (fv_byte_size + 2 * vh_byte_size);
1814 std::clog <<
"Gudhi::simplex_tree::get_serialization_size - buffer size = " << buffer_byte_size << std::endl;
1816 return buffer_byte_size;
1848 void serialize(
char* buffer,
const std::size_t buffer_size) {
1849 char* buffer_end = rec_serialize(&root_, buffer);
1850 if (
static_cast<std::size_t
>(buffer_end - buffer) != buffer_size)
1851 throw std::invalid_argument(
"Serialization does not match end of buffer");
1856 char* rec_serialize(
Siblings *sib,
char* buffer) {
1858 ptr = Gudhi::simplex_tree::serialize_trivial(
static_cast<Vertex_handle>(sib->members().size()), ptr);
1860 std::clog <<
"\n" << sib->members().size() <<
" : ";
1862 for (
auto& map_el : sib->members()) {
1863 ptr = Gudhi::simplex_tree::serialize_trivial(map_el.first, ptr);
1865 ptr = Gudhi::simplex_tree::serialize_trivial(map_el.second.filtration(), ptr);
1867 std::clog <<
" [ " << map_el.first <<
" | " << map_el.second.filtration() <<
" ] ";
1870 for (
auto& map_el : sib->members()) {
1872 ptr = rec_serialize(map_el.second.children(), ptr);
1874 ptr = Gudhi::simplex_tree::serialize_trivial(
static_cast<Vertex_handle>(0), ptr);
1876 std::cout <<
"\n0 : ";
1897 void deserialize(
const char* buffer,
const std::size_t buffer_size) {
1898 GUDHI_CHECK(
num_vertices() == 0, std::logic_error(
"Simplex_tree::deserialize - Simplex_tree must be empty"));
1899 const char* ptr = buffer;
1902 ptr = Gudhi::simplex_tree::deserialize_trivial(members_size, ptr);
1903 ptr = rec_deserialize(&root_, members_size, ptr, 0);
1904 if (
static_cast<std::size_t
>(ptr - buffer) != buffer_size) {
1905 throw std::invalid_argument(
"Deserialization does not match end of buffer");
1913 if (members_size > 0) {
1914 sib->members_.reserve(members_size);
1918 ptr = Gudhi::simplex_tree::deserialize_trivial(vertex, ptr);
1920 ptr = Gudhi::simplex_tree::deserialize_trivial(
filtration, ptr);
1922 sib->members_.emplace_hint(sib->members_.end(), vertex, Node(sib,
filtration));
1925 sib->members_.emplace_hint(sib->members_.end(), vertex, Node(sib));
1929 for (
auto& map_el : sib->members()) {
1930 ptr = Gudhi::simplex_tree::deserialize_trivial(child_size, ptr);
1931 if (child_size > 0) {
1933 map_el.second.assign_children(child);
1934 ptr = rec_deserialize(child, child_size, ptr, dim + 1);
1937 if (dim > dimension_) {
1951 std::vector<Simplex_handle> filtration_vect_;
1954 bool dimension_to_be_lowered_ =
false;
1958template<
typename...T>
1970template<
typename...T>
1973 std::vector<typename ST::Vertex_handle> simplex;
1979 int dim =
static_cast<int> (simplex.size() - 1);
1980 if (max_dim < dim) {
1998 typedef int Vertex_handle;
1999 typedef double Filtration_value;
2000 typedef std::uint32_t Simplex_key;
2001 static const bool store_key =
true;
2002 static const bool store_filtration =
true;
2003 static const bool contiguous_vertices =
false;
2014 typedef int Vertex_handle;
2015 typedef float Filtration_value;
2016 typedef std::uint32_t Simplex_key;
2017 static const bool store_key =
true;
2018 static const bool store_filtration =
true;
2019 static const bool contiguous_vertices =
true;
Extended simplex type data structure for representing the type of simplices in an extended filtration...
Iterator over the simplices of the boundary of a simplex and their opposite vertices.
Definition: Simplex_tree_iterators.h:190
Iterator over the simplices of the boundary of a simplex.
Definition: Simplex_tree_iterators.h:83
Iterator over the simplices of a simplicial complex.
Definition: Simplex_tree_iterators.h:300
Data structure to store a set of nodes in a SimplexTree sharing the same parent node.
Definition: Simplex_tree_siblings.h:31
Iterator over the vertices of a simplex in a SimplexTree.
Definition: Simplex_tree_iterators.h:38
Iterator over the simplices of the skeleton of a given dimension of the simplicial complex.
Definition: Simplex_tree_iterators.h:374
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:83
static Siblings * self_siblings(SimplexHandle sh)
Definition: Simplex_tree.h:893
Simplex_tree(Simplex_tree &&complex_source)
User-defined move constructor relocates the whole tree structure.
Definition: Simplex_tree.h:351
bool operator==(Simplex_tree &st2)
Checks if two simplex trees are equal.
Definition: Simplex_tree.h:476
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:90
Cofaces_simplex_range cofaces_simplex_range(const Simplex_handle simplex, int codimension)
Compute the cofaces of a n simplex.
Definition: Simplex_tree.h:1032
Simplex_tree_boundary_opposite_vertex_simplex_iterator< Simplex_tree > Boundary_opposite_vertex_simplex_iterator
Iterator over the simplices of the boundary of a simplex and their opposite vertices.
Definition: Simplex_tree.h:200
void reset_filtration(Filtration_value filt_value, int min_dim=0)
This function resets the filtration value of all the simplices of dimension at least min_dim....
Definition: Simplex_tree.h:1779
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex.
Definition: Simplex_tree.h:550
bool make_filtration_non_decreasing()
This function ensures that each simplex has a higher filtration value than its faces by increasing th...
Definition: Simplex_tree.h:1410
Dictionary::iterator Simplex_handle
Handle type to a simplex contained in the simplicial complex represented by the simplex tree.
Definition: Simplex_tree.h:156
Vertex_handle vertex_with_same_filtration(Simplex_handle sh)
Returns a vertex of sh that has the same filtration value as sh if it exists, and null_vertex() other...
Definition: Simplex_tree.h:1714
Filtration_simplex_range const & filtration_simplex_range(Indexing_tag=Indexing_tag())
Returns a range over the simplices of the simplicial complex, in the order of the filtration.
Definition: Simplex_tree.h:275
bool prune_above_dimension(int dimension)
Remove all simplices of dimension greater than a given value.
Definition: Simplex_tree.h:1505
std::pair< Simplex_handle, bool > insert_simplex(const InputVertexRange &simplex, Filtration_value filtration=0)
Insert a simplex, represented by a range of Vertex_handles, in the simplicial complex.
Definition: Simplex_tree.h:789
Vertex_handle null_vertex() const
Returns a Vertex_handle different from all Vertex_handles associated to the vertices of the simplicia...
Definition: Simplex_tree.h:571
boost::iterator_range< Boundary_simplex_iterator > Boundary_simplex_range
Range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:196
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh) const
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:286
void clear_filtration()
Clears the filtration cache produced by initialize_filtration().
Definition: Simplex_tree.h:958
Simplex_tree_simplex_vertex_iterator< Simplex_tree > Simplex_vertex_iterator
Iterator over the vertices of a simplex.
Definition: Simplex_tree.h:186
static Simplex_key key(Simplex_handle sh)
Returns the key associated to a simplex.
Definition: Simplex_tree.h:522
static Filtration_value filtration(Simplex_handle sh)
Returns the filtration value of a simplex.
Definition: Simplex_tree.h:539
bool operator!=(Simplex_tree &st2)
Checks if two simplex trees are different.
Definition: Simplex_tree.h:484
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:637
Cofaces_simplex_range star_simplex_range(const Simplex_handle simplex)
Compute the star of a n simplex.
Definition: Simplex_tree.h:1021
void expansion_with_blockers(int max_dim, Blocker block_simplex)
Expands a simplex tree containing only a graph. Simplices corresponding to cliques in the graph are a...
Definition: Simplex_tree.h:1291
boost::iterator_range< Simplex_vertex_iterator > Simplex_vertex_range
Range over the vertices of a simplex.
Definition: Simplex_tree.h:188
void remove_maximal_simplex(Simplex_handle sh)
Remove a maximal simplex.
Definition: Simplex_tree.h:1586
std::pair< Simplex_handle, Simplex_handle > endpoints(Simplex_handle sh)
Definition: Simplex_tree.h:886
void assign_key(Simplex_handle sh, Simplex_key key)
Assign a value 'key' to the key of the simplex represented by the Simplex_handle 'sh'.
Definition: Simplex_tree.h:879
Options::Simplex_key Simplex_key
Key associated to each simplex.
Definition: Simplex_tree.h:94
Simplex_tree()
Constructs an empty simplex tree.
Definition: Simplex_tree.h:334
void maybe_initialize_filtration()
Initializes the filtration cache if it isn't initialized yet.
Definition: Simplex_tree.h:949
boost::iterator_range< Complex_simplex_iterator > Complex_simplex_range
Range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:208
Simplex_tree_boundary_simplex_iterator< Simplex_tree > Boundary_simplex_iterator
Iterator over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:194
Simplex_tree_siblings< Simplex_tree, Dictionary > Siblings
Set of nodes sharing a same parent in the simplex tree.
Definition: Simplex_tree.h:109
Simplex_handle find(const InputVertexRange &s)
Given a range of Vertex_handles, returns the Simplex_handle of the simplex in the simplicial complex ...
Definition: Simplex_tree.h:650
std::vector< Simplex_handle > Cofaces_simplex_range
Range over the cofaces of a simplex.
Definition: Simplex_tree.h:190
Boundary_opposite_vertex_simplex_range boundary_opposite_vertex_simplex_range(SimplexHandle sh)
Given a simplex, returns a range over the simplices of its boundary and their opposite vertices.
Definition: Simplex_tree.h:324
bool is_empty() const
Returns whether the complex is empty.
Definition: Simplex_tree.h:581
Simplex_handle edge_with_same_filtration(Simplex_handle sh)
Returns an edge of sh that has the same filtration value as sh if it exists, and null_simplex() other...
Definition: Simplex_tree.h:1728
Simplex_tree_complex_simplex_iterator< Simplex_tree > Complex_simplex_iterator
Iterator over the simplices of the simplicial complex.
Definition: Simplex_tree.h:206
Simplex_handle simplex(Simplex_key idx) const
Returns the simplex that has index idx in the filtration.
Definition: Simplex_tree.h:530
void initialize_filtration()
Initializes the filtration cache, i.e. sorts the simplices according to their order in the filtration...
Definition: Simplex_tree.h:925
Skeleton_simplex_range skeleton_simplex_range(int dim)
Returns a range over the simplices of the dim-skeleton of the simplicial complex.
Definition: Simplex_tree.h:255
boost::transform_iterator< return_first, Dictionary_it > Complex_vertex_iterator
Iterator over the vertices of the simplicial complex.
Definition: Simplex_tree.h:180
void insert_batch_vertices(VertexRange const &vertices, Filtration_value filt=0)
Inserts several vertices.
Definition: Simplex_tree.h:1176
std::vector< Simplex_handle > Filtration_simplex_range
Range over the simplices of the simplicial complex, ordered by the filtration.
Definition: Simplex_tree.h:218
Filtration_simplex_range::const_iterator Filtration_simplex_iterator
Iterator over the simplices of the simplicial complex, ordered by the filtration.
Definition: Simplex_tree.h:222
Extended_filtration_data extend_filtration()
Extend filtration for computing extended persistence. This function only uses the filtration values a...
Definition: Simplex_tree.h:1652
Simplex_handle minimal_simplex_with_same_filtration(Simplex_handle sh)
Returns a minimal face of sh that has the same filtration value as sh.
Definition: Simplex_tree.h:1761
Options::Vertex_handle Vertex_handle
Type for the vertex handle.
Definition: Simplex_tree.h:98
std::pair< Filtration_value, Extended_simplex_type > decode_extended_filtration(Filtration_value f, const Extended_filtration_data &efd)
Retrieve the original filtration value for a given simplex in the Simplex_tree. Since the computation...
Definition: Simplex_tree.h:1623
size_t num_vertices() const
Returns the number of vertices in the complex.
Definition: Simplex_tree.h:576
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:1194
Simplex_tree(const Simplex_tree &complex_source)
User-defined copy constructor reproduces the whole tree structure.
Definition: Simplex_tree.h:341
boost::iterator_range< Complex_vertex_iterator > Complex_vertex_range
Range over the vertices of the simplicial complex.
Definition: Simplex_tree.h:182
static Simplex_key null_key()
Returns a fixed number not in the interval [0, num_simplices()).
Definition: Simplex_tree.h:565
Boundary_simplex_range boundary_simplex_range(SimplexHandle sh)
Returns a range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:307
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Definition: Simplex_tree.h:609
bool prune_above_filtration(Filtration_value filtration)
Prune above filtration value given as parameter.
Definition: Simplex_tree.h:1464
int upper_bound_dimension() const
Returns an upper bound on the dimension of the simplicial complex.
Definition: Simplex_tree.h:620
Siblings * root()
Definition: Simplex_tree.h:902
int dimension()
Returns the dimension of the simplicial complex.
Definition: Simplex_tree.h:628
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(const InputVertexRange &Nsimplex, Filtration_value filtration=0)
Insert a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles,...
Definition: Simplex_tree.h:818
size_t num_simplices()
returns the number of simplices in the simplex_tree.
Definition: Simplex_tree.h:587
Simplex_tree_skeleton_simplex_iterator< Simplex_tree > Skeleton_simplex_iterator
Iterator over the simplices of the skeleton of the simplicial complex, for a given dimension.
Definition: Simplex_tree.h:213
Simplex_tree & operator=(Simplex_tree &&complex_source)
User-defined move assignment relocates the whole tree structure.
Definition: Simplex_tree.h:385
boost::iterator_range< Boundary_opposite_vertex_simplex_iterator > Boundary_opposite_vertex_simplex_range
Range over the simplices of the boundary of a simplex and their opposite vertices.
Definition: Simplex_tree.h:202
Simplex_tree & operator=(const Simplex_tree &complex_source)
User-defined copy assignment reproduces the whole tree structure.
Definition: Simplex_tree.h:368
void insert_graph(const OneSkeletonGraph &skel_graph)
Inserts a 1-skeleton in an empty Simplex_tree.
Definition: Simplex_tree.h:1119
boost::iterator_range< Skeleton_simplex_iterator > Skeleton_simplex_range
Range over the simplices of the skeleton of the simplicial complex, for a given dimension.
Definition: Simplex_tree.h:216
void print_hasse(std::ostream &os)
Write the hasse diagram of the simplicial complex in os.
Definition: Simplex_tree.h:1392
~Simplex_tree()
Destructor; deallocates the whole tree structure.
Definition: Simplex_tree.h:363
static Simplex_handle null_simplex()
Returns a Simplex_handle different from all Simplex_handles associated to the simplices in the simpli...
Definition: Simplex_tree.h:560
Complex_simplex_range complex_simplex_range()
Returns a range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:241
Complex_vertex_range complex_vertex_range()
Returns a range over the vertices of the simplicial complex. The order is increasing according to < o...
Definition: Simplex_tree.h:230
void set_dimension(int dimension, bool exact=true)
Set a dimension for the simplicial complex.
Definition: Simplex_tree.h:912
Graph simplicial complex methods.
std::ostream & operator<<(std::ostream &os, const Permutahedral_representation< Vertex, OrderedSetPartition > &simplex)
Print a permutahedral representation to a stream.
Definition: Permutahedral_representation.h:173
This file includes common file reader for GUDHI.
bool read_simplex(std::istream &in_, std::vector< Vertex_handle > &simplex, Filtration_value &fil)
Read a face from a file.
Definition: reader_utils.h:158
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
Node of a simplex tree with filtration value and simplex key.
Definition: Simplex_tree_node_explicit_storage.h:29
Definition: Simplex_tree.h:2012
Definition: Simplex_tree.h:1996
Tag for a linear ordering of simplices.
Definition: indexing_tag.h:20
Concept describing an indexing scheme (see FilteredComplex) for applying continuous maps to a cell co...
Definition: IndexingTag.h:18
Key type used as simplex identifier.
Definition: SimplexKey.h:15
Concept of the template parameter for the class Gudhi::Simplex_tree<SimplexTreeOptions>.
Definition: SimplexTreeOptions.h:15
static const bool store_filtration
If true, each simplex has extra storage for one Filtration_value, and this value is propagated by ope...
Definition: SimplexTreeOptions.h:27
static constexpr bool contiguous_vertices
If true, the list of vertices present in the complex must always be 0, ..., num_vertices-1,...
Definition: SimplexTreeOptions.h:29
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15