23 #ifndef SRC_SIMPLEX_TREE_INCLUDE_GUDHI_SIMPLEX_TREE_H_
24 #define SRC_SIMPLEX_TREE_INCLUDE_GUDHI_SIMPLEX_TREE_H_
26 #include <gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h>
27 #include <gudhi/Simplex_tree/Simplex_tree_siblings.h>
28 #include <gudhi/Simplex_tree/Simplex_tree_iterators.h>
29 #include <gudhi/Simplex_tree/indexing_tag.h>
31 #include <boost/container/flat_map.hpp>
32 #include <boost/iterator/transform_iterator.hpp>
33 #include <boost/graph/adjacency_list.hpp>
86 template<
typename IndexingTag = linear_indexing_tag,
108 typedef Simplex_tree_node_explicit_storage<Simplex_tree> Node;
110 typedef typename boost::container::flat_map<Vertex_handle, Node> Dictionary;
121 typedef Simplex_tree_siblings<Simplex_tree, Dictionary> Siblings;
129 typedef typename Dictionary::iterator Dictionary_it;
130 typedef typename Dictionary_it::value_type Dit_value_t;
132 struct return_first {
195 boost::make_transform_iterator(root_.members_.begin(), return_first()),
196 boost::make_transform_iterator(root_.members_.end(), return_first()));
238 if (filtration_vect_.empty()) {
242 filtration_vect_.end());
246 return filtration_simplex_range(Indexing_tag());
287 root_(NULL, null_vertex_),
294 for (
auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) {
296 rec_delete(sh->second.children());
303 void rec_delete(Siblings * sib) {
304 for (
auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
306 rec_delete(sh->second.children());
317 return sh->second.key();
323 return filtration_vect_[
key];
330 return sh->second.filtration();
344 return Dictionary_it(NULL);
358 return root_.members_.size();
364 return num_simplices_;
373 while (curr_sib != NULL) {
375 curr_sib = curr_sib->oncles();
387 return (sh->second.children()->parent() == sh->first);
398 template<
class RandomAccessVertexRange>
400 if (s.begin() == s.end())
401 std::cerr <<
"Empty simplex \n";
403 sort(s.begin(), s.end());
405 Siblings * tmp_sib = &root_;
406 Dictionary_it tmp_dit;
409 tmp_dit = tmp_sib->members_.find(v);
410 if (tmp_dit == tmp_sib->members_.end()) {
416 tmp_sib = tmp_dit->second.children();
424 return root_.members_.begin() + v;
451 template<
class RandomAccessVertexRange>
454 if (simplex.empty()) {
455 return std::pair<Simplex_handle, bool>(
null_simplex(),
true);
458 sort(simplex.begin(), simplex.end());
460 Siblings * curr_sib = &root_;
461 std::pair<Simplex_handle, bool> res_insert;
462 typename RandomAccessVertexRange::iterator vi;
463 for (vi = simplex.begin(); vi != simplex.end() - 1; ++vi) {
464 res_insert = curr_sib->members_.emplace(*vi, Node(curr_sib, filtration));
466 res_insert.first->second.assign_children(
new Siblings(curr_sib, *vi));
468 curr_sib = res_insert.first->second.children();
470 res_insert = curr_sib->members_.emplace(*vi, Node(curr_sib, filtration));
471 if (!res_insert.second) {
472 if (res_insert.first->second.filtration() >
filtration) {
473 res_insert.first->second.assign_filtration(filtration);
476 return std::pair<Simplex_handle, bool>(
null_simplex(),
false);
485 sh->second.assign_key(key);
493 return std::pair<Simplex_handle, Simplex_handle>(
494 root_.members_.find(sh->first),
500 if (sh->second.children()->parent() == sh->first)
501 return sh->second.children()->oncles();
503 return sh->second.children();
554 filtration_vect_.clear();
558 filtration_vect_.push_back(*cpx_it);
562 std::stable_sort(filtration_vect_.begin(), filtration_vect_.end(),
563 is_before_in_filtration(
this));
574 bool reverse_lexicographic_order(Simplex_handle sh1, Simplex_handle sh2) {
579 while (it1 != rg1.end() && it2 != rg2.end()) {
587 return ((it1 == rg1.end()) && (it2 != rg2.end()));
595 struct is_before_in_filtration {
600 bool operator()(
const Simplex_handle sh1,
const Simplex_handle sh2)
const {
601 if (st_->filtration(sh1) != st_->filtration(sh2)) {
602 return st_->filtration(sh1) < st_->filtration(sh2);
605 return st_->reverse_lexicographic_order(sh1, sh2);
630 template<
class OneSkeletonGraph>
634 if (boost::num_vertices(skel_graph) == 0) {
637 if (num_edges(skel_graph) == 0) {
643 num_simplices_ = boost::num_vertices(skel_graph)
644 + boost::num_edges(skel_graph);
645 root_.members_.reserve(boost::num_vertices(skel_graph));
647 typename boost::graph_traits<OneSkeletonGraph>::vertex_iterator v_it,
649 for (tie(v_it, v_it_end) = boost::vertices(skel_graph); v_it != v_it_end;
651 root_.members_.emplace_hint(
652 root_.members_.end(), *v_it,
653 Node(&root_, boost::get(vertex_filtration_t(), skel_graph, *v_it)));
655 typename boost::graph_traits<OneSkeletonGraph>::edge_iterator e_it,
657 for (tie(e_it, e_it_end) = boost::edges(skel_graph); e_it != e_it_end;
659 auto u = source(*e_it, skel_graph);
660 auto v = target(*e_it, skel_graph);
664 sh->second.assign_children(
new Siblings(&root_, sh->first));
667 sh->second.children()->members().emplace(
669 Node(sh->second.children(),
670 boost::get(edge_filtration_t(), skel_graph, *e_it)));
686 dimension_ = max_dim;
687 for (Dictionary_it root_it = root_.members_.begin();
688 root_it != root_.members_.end(); ++root_it) {
690 siblings_expansion(root_it->second.children(), max_dim - 1);
693 dimension_ = max_dim - dimension_;
698 void siblings_expansion(Siblings * siblings,
700 if (dimension_ > k) {
705 Dictionary_it next = siblings->members().begin();
708 static std::vector<std::pair<Vertex_handle, Node> > inter;
709 for (Dictionary_it s_h = siblings->members().begin();
710 s_h != siblings->members().end(); ++s_h, ++next) {
716 siblings->members().end(),
717 root_sh->second.children()->members().begin(),
718 root_sh->second.children()->members().end(),
719 s_h->second.filtration());
720 if (inter.size() != 0) {
721 this->num_simplices_ += inter.size();
722 Siblings * new_sib =
new Siblings(siblings,
726 s_h->second.assign_children(new_sib);
727 siblings_expansion(new_sib, k - 1);
729 s_h->second.assign_children(siblings);
737 void intersection(std::vector<std::pair<Vertex_handle, Node> >& intersection,
738 Dictionary_it begin1, Dictionary_it end1,
739 Dictionary_it begin2, Dictionary_it end2,
741 if (begin1 == end1 || begin2 == end2)
744 if (begin1->first == begin2->first) {
745 intersection.push_back(
746 std::pair<Vertex_handle, Node>(
748 Node(NULL, maximum(begin1->second.filtration(), begin2->second.filtration(),
filtration))));
751 if (begin1 == end1 || begin2 == end2)
754 if (begin1->first < begin2->first) {
767 Filtration_value maximum(Filtration_value a, Filtration_value b,
768 Filtration_value c) {
769 Filtration_value max = (a < b) ? b : a;
770 return ((max < c) ? c : max);
785 os <<
key(b_sh) <<
" ";
792 Vertex_handle null_vertex_;
794 Filtration_value threshold_;
796 unsigned int num_simplices_;
800 std::vector<Simplex_handle> filtration_vect_;
806 template<
typename T1,
typename T2,
typename T3>
807 std::ostream& operator<<(std::ostream & os, Simplex_tree<T1, T2, T3> & st) {
808 for (
auto sh : st.filtration_simplex_range()) {
809 os << st.dimension(sh) <<
" ";
810 for (
auto v : st.simplex_vertex_range(sh)) {
813 os << st.filtration(sh) <<
"\n";
817 template<
typename T1,
typename T2,
typename T3>
818 std::istream& operator>>(std::istream & is, Simplex_tree<T1, T2, T3> & st) {
821 std::vector<typename Simplex_tree<T1, T2, T3>::Vertex_handle> simplex;
822 typename Simplex_tree<T1, T2, T3>::Filtration_value fil;
823 typename Simplex_tree<T1, T2, T3>::Filtration_value max_fil = 0;
825 size_t num_simplices = 0;
826 while (read_simplex(is, simplex, fil)) {
828 int dim =
static_cast<int>(simplex.size() - 1);
835 st.insert(simplex, fil);
838 st.set_num_simplices(num_simplices);
839 st.set_dimension(max_dim);
840 st.set_filtration(max_fil);
848 #endif // SRC_SIMPLEX_TREE_INCLUDE_GUDHI_SIMPLEX_TREE_H_
unspecified Simplex_key
Key associated to each simplex.
Definition: FilteredComplex.h:35
Simplex_handle null_simplex()
Returns a Simplex_handle different from all Simplex_handles associated to the simplices in the simpli...
Definition: Simplex_tree.h:343
SimplexKey Simplex_key
Key associated to each simplex.
Definition: Simplex_tree.h:101
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:175
Complex_simplex_range complex_simplex_range()
Returns a range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:204
Tag for a linear ordering of simplices.
Definition: indexing_tag.h:32
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:91
boost::transform_iterator< return_first, Dictionary_it > Complex_vertex_iterator
Iterator over the vertices of the simplicial complex.
Definition: Simplex_tree.h:150
Simplex_key null_key()
Returns a key different for all keys associated to the simplices of the simplicial complex...
Definition: Simplex_tree.h:348
Concept describing an indexing scheme (see FilteredComplex) for applying continuous maps to a cell co...
Definition: IndexingTag.h:30
~Simplex_tree()
Destructor; deallocates the whole tree structure.
Definition: Simplex_tree.h:293
void set_filtration(Filtration_value fil)
Definition: Simplex_tree.h:525
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:484
Simplex_handle simplex(Simplex_key key)
Returns the simplex associated to a key.
Definition: Simplex_tree.h:322
Simplex_key key(Simplex_handle sh)
Returns the key associated to a simplex.
Definition: Simplex_tree.h:316
Simplex_handle find(const RandomAccessVertexRange &s)
Given a range of Vertex_handles, returns the Simplex_handle of the simplex in the simplicial complex ...
Definition: Simplex_tree.h:399
Key type used as simplex identifier.
Definition: SimplexKey.h:27
Complex_vertex_range complex_vertex_range()
Returns a range over the vertices of the simplicial complex.
Definition: Simplex_tree.h:193
void print_hasse(std::ostream &os)
Write the hasse diagram of the simplicial complex in os.
Definition: Simplex_tree.h:780
void insert_graph(const OneSkeletonGraph &skel_graph)
Inserts a 1-skeleton in an empty Simplex_tree.
Definition: Simplex_tree.h:631
std::vector< Simplex_handle >::iterator Filtration_simplex_iterator
Iterator over the simplices of the simplicial complex, ordered by the filtration. ...
Definition: Simplex_tree.h:182
unspecified Indexing_tag
Specifies the nature of the indexing scheme.
Definition: FilteredComplex.h:44
FiltrationValue Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:97
Simplex_tree_complex_simplex_iterator< Simplex_tree > Complex_simplex_iterator
Iterator over the simplices of the simplicial complex.
Definition: Simplex_tree.h:168
Filtration_value filtration(Simplex_handle sh)
Returns the filtration value of a simplex.
Definition: Simplex_tree.h:328
Simplex_tree_simplex_vertex_iterator< Simplex_tree > Simplex_vertex_iterator
Iterator over the vertices of a simplex.
Definition: Simplex_tree.h:156
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:218
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:32
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh)
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:254
unspecified Boundary_simplex_range
Range giving access to the simplices in the boundary of a simplex.
Definition: FilteredComplex.h:85
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Definition: Simplex_tree.h:370
boost::iterator_range< Boundary_simplex_iterator > Boundary_simplex_range
Range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:164
void set_num_simplices(const unsigned int &num_simplices)
Definition: Simplex_tree.h:529
Vertex_handle null_vertex()
Returns a Vertex_handle different from all Vertex_handles associated to the vertices of the simplicia...
Definition: Simplex_tree.h:353
Simplex_tree_boundary_simplex_iterator< Simplex_tree > Boundary_simplex_iterator
Iterator over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:162
Dictionary::iterator Simplex_handle
Handle type to a simplex contained in the simplicial complex represented byt he simplex tree...
Definition: Simplex_tree.h:126
int dimension()
Returns an upper bound on the dimension of the simplicial complex.
Definition: Simplex_tree.h:380
Filtration_simplex_range filtration_simplex_range(linear_indexing_tag)
Returns a range over the simplices of the simplicial complex, in the order of the filtration...
Definition: Simplex_tree.h:237
unspecified Filtration_simplex_range
Range over the simplices of the complex in the order of the filtration.
Definition: FilteredComplex.h:110
boost::iterator_range< Simplex_vertex_iterator > Simplex_vertex_range
Range over the vertices of a simplex.
Definition: Simplex_tree.h:158
Filtration_value filtration()
Returns an upper bound of the filtration values of the simplices.
Definition: Simplex_tree.h:336
boost::iterator_range< Complex_simplex_iterator > Complex_simplex_range
Range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:170
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:178
Siblings * self_siblings(Simplex_handle sh)
Definition: Simplex_tree.h:499
void set_dimension(int dimension)
Definition: Simplex_tree.h:533
const unsigned int & num_simplices() const
Returns the number of simplices in the complex.
Definition: Simplex_tree.h:363
std::pair< Simplex_handle, bool > insert(RandomAccessVertexRange &simplex, Filtration_value filtration)
Insert a simplex, represented by a range of Vertex_handles, in the simplicial complex.
Definition: Simplex_tree.h:452
Boundary_simplex_range boundary_simplex_range(Simplex_handle sh)
Returns a range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:273
VertexHandle Vertex_handle
Type for the vertex handle.
Definition: Simplex_tree.h:105
Siblings * root()
Definition: Simplex_tree.h:519
boost::iterator_range< Complex_vertex_iterator > Complex_vertex_range
Range over the vertices of the simplicial complex.
Definition: Simplex_tree.h:152
std::pair< Simplex_handle, Simplex_handle > endpoints(Simplex_handle sh)
Definition: Simplex_tree.h:492
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:685
boost::iterator_range< Filtration_simplex_iterator > Filtration_simplex_range
Range over the simplices of the simplicial complex, ordered by the filtration.
Definition: Simplex_tree.h:184
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:26
size_t num_vertices()
Returns the number of vertices in the complex.
Definition: Simplex_tree.h:357
Simplex_tree()
Constructs an empty simplex tree.
Definition: Simplex_tree.h:283
bool has_children(Simplex_handle sh)
Returns true iff the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:386
unspecified Filtration_value
Type for the value of the filtration function.
Definition: FilteredComplex.h:39
Simplex_handle find_vertex(Vertex_handle v)
Returns the Simplex_handle corresponding to the 0-simplex representing the vertex with Vertex_handle ...
Definition: Simplex_tree.h:423
void initialize_filtration()
Initializes the filtrations, i.e. sort the simplices according to their order in the filtration and i...
Definition: Simplex_tree.h:553