Loading...
Searching...
No Matches
Simplex_tree.h
1/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): Clément Maria
4 *
5 * Copyright (C) 2014 Inria
6 *
7 * Modification(s):
8 * - 2023/02 Vincent Rouvreau: Add de/serialize methods for pickle feature
9 * - 2023/07 Clément Maria: Option to link all simplex tree nodes with same label in an intrusive list
10 * - 2023/05 Clément Maria: Edge insertion method for flag complexes
11 * - 2023/05 Hannah Schreiber: Factorization of expansion methods
12 * - 2023/08 Hannah Schreiber (& Clément Maria): Add possibility of stable simplex handles.
13 * - 2024/08 Hannah Schreiber: Generalization of the notion of filtration values.
14 * - 2024/08 Hannah Schreiber: Addition of customizable copy constructor.
15 * - 2024/08 Marc Glisse: Allow storing custom data in simplices.
16 * - 2024/10 Hannah Schreiber: Const version of the Simplex_tree
17 * - 2025/02 Hannah Schreiber (& David Loiseaux): Insertion strategies for `insert_simplex_and_subfaces`
18 * - YYYY/MM Author: Description of the modification
19 */
20
21#ifndef SIMPLEX_TREE_H_
22#define SIMPLEX_TREE_H_
23
24#include <gudhi/Simplex_tree/simplex_tree_options.h>
25#include <gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h>
26#include <gudhi/Simplex_tree/Simplex_tree_siblings.h>
27#include <gudhi/Simplex_tree/Simplex_tree_iterators.h>
28#include <gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h>
29#include <gudhi/Simplex_tree/filtration_value_utils.h> // for de/serialize + empty_filtration_value
30#include <gudhi/Simplex_tree/hooks_simplex_base.h>
31
32#include <gudhi/reader_utils.h>
34#include <gudhi/Debug_utils.h>
35
36#include <boost/container/map.hpp>
37#include <boost/container/flat_map.hpp>
38#include <boost/iterator/transform_iterator.hpp>
39#include <boost/graph/adjacency_list.hpp>
40#include <boost/range/adaptor/reversed.hpp>
41#include <boost/range/adaptor/transformed.hpp>
42#include <boost/range/size.hpp>
43#include <boost/container/static_vector.hpp>
44#include <boost/range/adaptors.hpp>
45
46#include <boost/intrusive/list.hpp>
47#include <boost/intrusive/parent_from_member.hpp>
48
49#ifdef GUDHI_USE_TBB
50#include <tbb/parallel_sort.h>
51#endif
52
53#include <cstddef>
54#include <cstdint> // std::uint8_t
55#include <utility> // for std::move
56#include <vector>
57#include <functional> // for greater<>
58#include <stdexcept>
59#include <limits> // Inf
60#include <initializer_list>
61#include <algorithm> // for std::max
62#include <iterator> // for std::distance
63#include <type_traits> // for std::conditional
64#include <unordered_map>
65#include <iterator> // for std::prev
66
67namespace Gudhi {
68
72
85enum class Extended_simplex_type {UP, DOWN, EXTRA};
86
99
100template<typename SimplexTreeOptions = Simplex_tree_options_default>
101class Simplex_tree {
102 public:
103 typedef SimplexTreeOptions Options;
104 typedef typename Options::Indexing_tag Indexing_tag;
108 typedef typename Options::Filtration_value Filtration_value;
112 typedef typename Options::Simplex_key Simplex_key;
118 typedef typename Get_simplex_data_type<Options>::type Simplex_data;
122 typedef typename Options::Vertex_handle Vertex_handle;
123
124 /* Type of node in the simplex tree. */
126 /* Type of dictionary Vertex_handle -> Node for traversing the simplex tree. */
127 // Note: this wastes space when Vertex_handle is 32 bits and Node is aligned on 64 bits. It would be better to use a
128 // flat_set (with our own comparator) where we can control the layout of the struct (put Vertex_handle and
129 // Simplex_key next to each other).
130 typedef typename boost::container::flat_map<Vertex_handle, Node> flat_map;
131 //Dictionary::iterator remain valid under insertions and deletions,
132 //necessary e.g. when computing oscillating rips zigzag filtrations.
133 typedef typename boost::container::map<Vertex_handle, Node> map;
134 typedef typename std::conditional<Options::stable_simplex_handles,
135 map,
136 flat_map>::type Dictionary;
137
140
141 struct Key_simplex_base_real {
142 Key_simplex_base_real() : key_(-1) {}
143 Key_simplex_base_real(Simplex_key k) : key_(k) {}
144 void assign_key(Simplex_key k) { key_ = k; }
145 Simplex_key key() const { return key_; }
146 private:
147 Simplex_key key_;
148 };
149 struct Key_simplex_base_dummy {
150 Key_simplex_base_dummy() {}
151 // Undefined so it will not link
152 void assign_key(Simplex_key);
153 Simplex_key key() const;
154 };
155
156 struct Extended_filtration_data {
157 Filtration_value minval;
158 Filtration_value maxval;
159 Extended_filtration_data() {}
160
161 Extended_filtration_data(const Filtration_value& vmin, const Filtration_value& vmax)
162 : minval(vmin), maxval(vmax)
163 {}
164 };
165 typedef typename std::conditional<Options::store_key, Key_simplex_base_real, Key_simplex_base_dummy>::type
166 Key_simplex_base;
167
168 struct Filtration_simplex_base_real {
169 Filtration_simplex_base_real() : filt_() {}
170 Filtration_simplex_base_real(Filtration_value f) : filt_(f) {}
171 void assign_filtration(const Filtration_value& f) { filt_ = f; }
172 const Filtration_value& filtration() const { return filt_; }
173 Filtration_value& filtration() { return filt_; }
174
175 static const Filtration_value& get_infinity() { return inf_; }
176 static const Filtration_value& get_minus_infinity() { return minus_inf_; }
177
178 private:
179 Filtration_value filt_;
180
181 inline static const Filtration_value inf_ = std::numeric_limits<Filtration_value>::has_infinity
182 ? std::numeric_limits<Filtration_value>::infinity()
183 : std::numeric_limits<Filtration_value>::max();
184 inline static const Filtration_value minus_inf_ = std::numeric_limits<Filtration_value>::has_infinity
185 ? -std::numeric_limits<Filtration_value>::infinity()
186 : std::numeric_limits<Filtration_value>::lowest();
187 };
188
189 struct Filtration_simplex_base_dummy {
190 Filtration_simplex_base_dummy() {}
191 Filtration_simplex_base_dummy(Filtration_value GUDHI_CHECK_code(f)) {
192 GUDHI_CHECK(f == null_, "filtration value specified in the constructor for a complex that does not store them");
193 }
194 void assign_filtration(const Filtration_value& GUDHI_CHECK_code(f)) {
195 GUDHI_CHECK(f == null_, "filtration value assigned for a complex that does not store them");
196 }
197 const Filtration_value& filtration() const { return null_; }
198
199 private:
200 inline static const Filtration_value null_{Gudhi::simplex_tree::empty_filtration_value};
201 };
202 typedef typename std::conditional<Options::store_filtration, Filtration_simplex_base_real,
203 Filtration_simplex_base_dummy>::type Filtration_simplex_base;
204
205 public:
212 typedef typename Dictionary::const_iterator Simplex_handle;
213
214 private:
215 typedef typename Dictionary::iterator Dictionary_it;
216 typedef typename Dictionary::const_iterator Dictionary_const_it;
217 typedef typename Dictionary_it::value_type Dit_value_t;
218
219 struct return_first {
220 Vertex_handle operator()(const Dit_value_t& p_sh) const {
221 return p_sh.first;
222 }
223 };
224
225 private:
232 using Optimized_star_simplex_iterator = Simplex_tree_optimized_star_simplex_iterator<Simplex_tree>;
234 using Optimized_star_simplex_range = boost::iterator_range<Optimized_star_simplex_iterator>;
235
236 class Fast_cofaces_predicate {
237 Simplex_tree const* st_;
238 int codim_;
239 int dim_;
240 public:
241 Fast_cofaces_predicate(Simplex_tree const* st, int codim, int dim)
242 : st_(st), codim_(codim), dim_(codim + dim) {}
243 bool operator()( const Simplex_handle iter ) const {
244 if (codim_ == 0)
245 // Always true for a star
246 return true;
247 // Specific coface case
248 return dim_ == st_->dimension(iter);
249 }
250 };
251
252 // WARNING: this is safe only because boost::filtered_range is containing a copy of begin and end iterator.
253 // This would not be safe if it was containing a pointer to a range (maybe the case for std::views)
254 using Optimized_cofaces_simplex_filtered_range = boost::filtered_range<Fast_cofaces_predicate,
255 Optimized_star_simplex_range>;
256
257
260 static constexpr int max_dimension() { return 40; }
261 public:
269
273 typedef boost::transform_iterator<return_first, Dictionary_const_it> Complex_vertex_iterator;
275 typedef boost::iterator_range<Complex_vertex_iterator> Complex_vertex_range;
281 typedef boost::iterator_range<Simplex_vertex_iterator> Simplex_vertex_range;
283 typedef typename std::conditional<Options::link_nodes_by_label,
284 Optimized_cofaces_simplex_filtered_range, // faster implementation
285 std::vector<Simplex_handle>>::type Cofaces_simplex_range;
286
290 using Static_vertex_vector = boost::container::static_vector<Vertex_handle, max_dimension()>;
291
297 typedef boost::iterator_range<Boundary_simplex_iterator> Boundary_simplex_range;
303 typedef boost::iterator_range<Boundary_opposite_vertex_simplex_iterator> Boundary_opposite_vertex_simplex_range;
309 typedef boost::iterator_range<Complex_simplex_iterator> Complex_simplex_range;
317 typedef boost::iterator_range<Skeleton_simplex_iterator> Skeleton_simplex_range;
323 typedef boost::iterator_range<Dimension_simplex_iterator> Dimension_simplex_range;
325 typedef std::vector<Simplex_handle> Filtration_simplex_range;
329 typedef typename Filtration_simplex_range::const_iterator Filtration_simplex_iterator;
330
331 /* @} */ // end name range and iterator types
334
338 return Complex_vertex_range(boost::make_transform_iterator(root_.members_.begin(), return_first()),
339 boost::make_transform_iterator(root_.members_.end(), return_first()));
340 }
341
351
365
377
396 Filtration_simplex_range const& filtration_simplex_range(Indexing_tag = Indexing_tag()) const {
398 return filtration_vect_;
399 }
400
408 GUDHI_CHECK(sh != null_simplex(), "empty simplex");
411 }
412
427 template<class SimplexHandle>
432
444 template<class SimplexHandle>
449 // end range and iterator methods
453
456 : null_vertex_(-1),
457 root_(nullptr, null_vertex_),
458 filtration_vect_(),
459 dimension_(-1),
460 dimension_to_be_lowered_(false) {}
461
477 template<typename OtherSimplexTreeOptions, typename F>
478 Simplex_tree(const Simplex_tree<OtherSimplexTreeOptions>& complex_source, F&& translate_filtration_value) {
479#ifdef DEBUG_TRACES
480 std::clog << "Simplex_tree custom copy constructor" << std::endl;
481#endif // DEBUG_TRACES
482 copy_from(complex_source, translate_filtration_value);
483 }
484
488 Simplex_tree(const Simplex_tree& complex_source) {
489#ifdef DEBUG_TRACES
490 std::clog << "Simplex_tree copy constructor" << std::endl;
491#endif // DEBUG_TRACES
492 copy_from(complex_source);
493 }
494
499 Simplex_tree(Simplex_tree && complex_source) {
500#ifdef DEBUG_TRACES
501 std::clog << "Simplex_tree move constructor" << std::endl;
502#endif // DEBUG_TRACES
503 move_from(complex_source);
504 }
505
508 root_members_recursive_deletion();
509 }
510
514 Simplex_tree& operator= (const Simplex_tree& complex_source) {
515#ifdef DEBUG_TRACES
516 std::clog << "Simplex_tree copy assignment" << std::endl;
517#endif // DEBUG_TRACES
518 // Self-assignment detection
519 if (&complex_source != this) {
520 // We start by deleting root_ if not empty
521 root_members_recursive_deletion();
522
523 copy_from(complex_source);
524 }
525 return *this;
526 }
527
532 Simplex_tree& operator=(Simplex_tree&& complex_source) {
533#ifdef DEBUG_TRACES
534 std::clog << "Simplex_tree move assignment" << std::endl;
535#endif // DEBUG_TRACES
536 // Self-assignment detection
537 if (&complex_source != this) {
538 // root_ deletion in case it was not empty
539 root_members_recursive_deletion();
540
541 move_from(complex_source);
542 }
543 return *this;
544 }
545 // end constructor/destructor
546
547 private:
548 // Copy from complex_source to "this"
549 void copy_from(const Simplex_tree& complex_source) {
550 null_vertex_ = complex_source.null_vertex_;
551 filtration_vect_.clear();
552 dimension_ = complex_source.dimension_;
553 dimension_to_be_lowered_ = complex_source.dimension_to_be_lowered_;
554 auto root_source = complex_source.root_;
555
556 // root members copy
557 root_.members() =
558 Dictionary(boost::container::ordered_unique_range, root_source.members().begin(), root_source.members().end());
559 // Needs to reassign children
560 for (auto& map_el : root_.members()) {
561 map_el.second.assign_children(&root_);
562 }
563 // Specific for optional data
564 if constexpr (!std::is_same_v<Simplex_data, No_simplex_data>) {
565 auto dst_iter = root_.members().begin();
566 auto src_iter = root_source.members().begin();
567
568 while(dst_iter != root_.members().end() || src_iter != root_source.members().end()) {
569 dst_iter->second.data() = src_iter->second.data();
570 dst_iter++;
571 src_iter++;
572 }
573 // Check in debug mode members data were copied
574 assert(dst_iter == root_.members().end());
575 assert(src_iter == root_source.members().end());
576 }
577 rec_copy<Options::store_key, true>(
578 &root_, &root_source, [](const Filtration_value& fil) -> const Filtration_value& { return fil; });
579 }
580
581 // Copy from complex_source to "this"
582 template<typename OtherSimplexTreeOptions, typename F>
583 void copy_from(const Simplex_tree<OtherSimplexTreeOptions>& complex_source, F&& translate_filtration_value) {
584 null_vertex_ = complex_source.null_vertex_;
585 filtration_vect_.clear();
586 dimension_ = complex_source.dimension_;
587 dimension_to_be_lowered_ = complex_source.dimension_to_be_lowered_;
588 auto root_source = complex_source.root_;
589
590 // root members copy
591 if constexpr (!Options::stable_simplex_handles) root_.members().reserve(root_source.size());
592 for (auto& p : root_source.members()){
593 if constexpr (Options::store_key && OtherSimplexTreeOptions::store_key) {
594 root_.members().try_emplace(
595 root_.members().end(), p.first, &root_, translate_filtration_value(p.second.filtration()), p.second.key());
596 } else {
597 root_.members().try_emplace(
598 root_.members().end(), p.first, &root_, translate_filtration_value(p.second.filtration()));
599 }
600 }
601
602 rec_copy<OtherSimplexTreeOptions::store_key, false>(&root_, &root_source, translate_filtration_value);
603 }
604
606 template<bool store_key, bool copy_simplex_data, typename OtherSiblings, typename F>
607 void rec_copy(Siblings *sib, OtherSiblings *sib_source, F&& translate_filtration_value) {
608 auto sh_source = sib_source->members().begin();
609 for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh, ++sh_source) {
610 update_simplex_tree_after_node_insertion(sh);
611 if (has_children(sh_source)) {
612 Siblings * newsib = new Siblings(sib, sh_source->first);
613 if constexpr (!Options::stable_simplex_handles) {
614 newsib->members_.reserve(sh_source->second.children()->members().size());
615 }
616 for (auto & child : sh_source->second.children()->members()){
617 Dictionary_it new_it{};
618 if constexpr (store_key && Options::store_key) {
619 new_it = newsib->members_.emplace_hint(
620 newsib->members_.end(),
621 child.first,
622 Node(newsib, translate_filtration_value(child.second.filtration()), child.second.key()));
623 } else {
624 new_it = newsib->members_.emplace_hint(newsib->members_.end(),
625 child.first,
626 Node(newsib, translate_filtration_value(child.second.filtration())));
627 }
628 // Specific for optional data
629 if constexpr (copy_simplex_data && !std::is_same_v<Simplex_data, No_simplex_data>) {
630 new_it->second.data() = child.second.data();
631 }
632 }
633 rec_copy<store_key, copy_simplex_data>(newsib, sh_source->second.children(), translate_filtration_value);
634 sh->second.assign_children(newsib);
635 }
636 }
637 }
638
639 // Move from complex_source to "this"
640 void move_from(Simplex_tree& complex_source) {
641 null_vertex_ = std::move(complex_source.null_vertex_);
642 root_ = std::move(complex_source.root_);
643 filtration_vect_ = std::move(complex_source.filtration_vect_);
644 dimension_ = std::exchange(complex_source.dimension_, -1);
645 dimension_to_be_lowered_ = std::exchange(complex_source.dimension_to_be_lowered_, false);
646 if constexpr (Options::link_nodes_by_label) {
647 nodes_label_to_list_.swap(complex_source.nodes_label_to_list_);
648 }
649 // Need to update root members (children->oncles and children need to point on the new root pointer)
650 for (auto& map_el : root_.members()) {
651 if (map_el.second.children() != &(complex_source.root_)) {
652 // reset children->oncles with the moved root_ pointer value
653 map_el.second.children()->oncles_ = &root_;
654 } else {
655 // if simplex is of dimension 0, oncles_ shall be nullptr
656 GUDHI_CHECK(map_el.second.children()->oncles_ == nullptr,
657 std::invalid_argument("Simplex_tree move constructor from an invalid Simplex_tree"));
658 // and children points on root_ - to be moved
659 map_el.second.assign_children(&root_);
660 }
661 }
662 }
663
664 // delete all root_.members() recursively
665 void root_members_recursive_deletion() {
666 for (auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) {
667 if (has_children(sh)) {
668 rec_delete(sh->second.children());
669 }
670 }
671 root_.members().clear();
672 }
673
674 // Recursive deletion
675 void rec_delete(Siblings * sib) {
676 for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
677 if (has_children(sh)) {
678 rec_delete(sh->second.children());
679 }
680 }
681 delete sib;
682 }
683
684 public:
685 template<typename> friend class Simplex_tree;
686
690 template<class OtherSimplexTreeOptions>
691 bool operator==(const Simplex_tree<OtherSimplexTreeOptions>& st2) const {
692 if ((null_vertex_ != st2.null_vertex_) ||
693 (dimension_ != st2.dimension_ && !dimension_to_be_lowered_ && !st2.dimension_to_be_lowered_))
694 return false;
695 return rec_equal(&root_, &st2.root_);
696 }
697
699 template<class OtherSimplexTreeOptions>
700 bool operator!=(const Simplex_tree<OtherSimplexTreeOptions>& st2) const {
701 return (!(*this == st2));
702 }
703
704 private:
706 template<class OtherSiblings>
707 bool rec_equal(Siblings const* s1, OtherSiblings const* s2) const {
708 if (s1->members().size() != s2->members().size())
709 return false;
710 auto sh2 = s2->members().begin();
711 for (auto sh1 = s1->members().begin();
712 (sh1 != s1->members().end() && sh2 != s2->members().end());
713 ++sh1, ++sh2) {
714 if (sh1->first != sh2->first || !(sh1->second.filtration() == sh2->second.filtration()))
715 return false;
716 if (has_children(sh1) != has_children(sh2))
717 return false;
718 // Recursivity on children only if both have children
719 else if (has_children(sh1))
720 if (!rec_equal(sh1->second.children(), sh2->second.children()))
721 return false;
722 }
723 return true;
724 }
725
730 static const Filtration_value& filtration_(Simplex_handle sh) {
731 GUDHI_CHECK (sh != null_simplex(), "null simplex");
732 return sh->second.filtration();
733 }
734
735 // Transform a Dictionary_const_it into a Dictionary_it
736 Dictionary_it _to_node_it(Simplex_handle sh) {
737 return self_siblings(sh)->to_non_const_it(sh);
738 }
739
740 public:
747 return sh->second.key();
748 }
749
757 return filtration_vect_[idx];
758 }
759
766 if (sh != null_simplex()) {
767 return sh->second.filtration();
768 } else {
769 return Filtration_simplex_base_real::get_infinity();
770 }
771 }
772
777 GUDHI_CHECK(sh != null_simplex(),
778 std::invalid_argument("Simplex_tree::assign_filtration - cannot assign filtration on null_simplex"));
779 _to_node_it(sh)->second.assign_filtration(fv);
780 }
781
787 return Dictionary_const_it();
788 }
789
792 return -1;
793 }
794
797 GUDHI_CHECK(sh != null_simplex(),
798 std::invalid_argument("Simplex_tree::simplex_data - no data associated to null_simplex"));
799 return _to_node_it(sh)->second.data();
800 }
801
804 GUDHI_CHECK(sh != null_simplex(),
805 std::invalid_argument("Simplex_tree::simplex_data - no data associated to null_simplex"));
806 return sh->second.data();
807 }
808
812 return null_vertex_;
813 }
814
816 size_t num_vertices() const {
817 return root_.members_.size();
818 }
819
821 bool is_empty() const {
822 return root_.members_.empty();
823 }
824
825 public:
829 size_t num_simplices() const {
830 return num_simplices(root());
831 }
832
833 private:
835 size_t num_simplices(const Siblings * sib) const {
836 auto sib_begin = sib->members().begin();
837 auto sib_end = sib->members().end();
838 size_t simplices_number = sib->members().size();
839 for (auto sh = sib_begin; sh != sib_end; ++sh) {
840 if (has_children(sh)) {
841 simplices_number += num_simplices(sh->second.children());
842 }
843 }
844 return simplices_number;
845 }
846
853 int dimension(Siblings const* curr_sib) const {
854 int dim = -1;
855 while (curr_sib != nullptr) {
856 ++dim;
857 curr_sib = curr_sib->oncles();
858 }
859 return dim;
860 }
861
862 public:
864 std::vector<size_t> num_simplices_by_dimension() const {
865 if (is_empty()) return {};
866 // std::min in case the upper bound got crazy
867 std::vector<size_t> res(std::min(upper_bound_dimension()+1, max_dimension()+1));
868 auto fun = [&res](Simplex_handle, int dim) -> void { ++res[dim]; };
869 for_each_simplex(fun);
870 if (dimension_to_be_lowered_) {
871 GUDHI_CHECK(res.front() != 0, std::logic_error("Bug in Gudhi: non-empty complex has no vertex"));
872 while (res.back() == 0) res.pop_back();
873 dimension_ = static_cast<int>(res.size()) - 1;
874 dimension_to_be_lowered_ = false;
875 } else {
876 GUDHI_CHECK(res.back() != 0,
877 std::logic_error("Bug in Gudhi: there is no simplex of dimension the dimension of the complex"));
878 }
879 return res;
880 }
881
885 int dimension(Simplex_handle sh) const {
886 return dimension(self_siblings(sh));
887 }
888
894 return dimension_;
895 }
896
903 int dimension() const {
904 if (dimension_to_be_lowered_)
905 lower_upper_bound_dimension();
906 return dimension_;
907 }
908
911 template<class SimplexHandle>
912 bool has_children(SimplexHandle sh) const {
913 // Here we rely on the root using null_vertex(), which cannot match any real vertex.
914 return (sh->second.children()->parent() == sh->first);
915 }
916
917 private:
919
923 Siblings const* children(Simplex_handle sh) const {
924 GUDHI_CHECK(has_children(sh), std::invalid_argument("Simplex_tree::children - argument has no child"));
925 return sh->second.children();
926 }
927
928 public:
936 template<class InputVertexRange = std::initializer_list<Vertex_handle>>
937 Simplex_handle find(const InputVertexRange & s) const {
938 auto first = std::begin(s);
939 auto last = std::end(s);
940
941 if (first == last)
942 return null_simplex(); // ----->>
943
944 // Copy before sorting
945 std::vector<Vertex_handle> copy(first, last);
946 std::sort(std::begin(copy), std::end(copy));
947 return find_simplex(copy);
948 }
949
950 private:
952 Simplex_handle find_simplex(const std::vector<Vertex_handle> & simplex) const {
953 Siblings const* tmp_sib = &root_;
954 Dictionary_const_it tmp_dit;
955 auto vi = simplex.begin();
957 // Equivalent to the first iteration of the normal loop
958 GUDHI_CHECK(contiguous_vertices(), "non-contiguous vertices");
959 Vertex_handle v = *vi++;
960 if(v < 0 || v >= static_cast<Vertex_handle>(root_.members_.size()))
961 return null_simplex();
962 tmp_dit = root_.members_.begin() + v;
963 if (vi == simplex.end())
964 return tmp_dit;
965 if (!has_children(tmp_dit))
966 return null_simplex();
967 tmp_sib = tmp_dit->second.children();
968 }
969 for (;;) {
970 tmp_dit = tmp_sib->members_.find(*vi++);
971 if (tmp_dit == tmp_sib->members_.end())
972 return null_simplex();
973 if (vi == simplex.end())
974 return tmp_dit;
975 if (!has_children(tmp_dit))
976 return null_simplex();
977 tmp_sib = tmp_dit->second.children();
978 }
979 }
980
983 Simplex_handle find_vertex(Vertex_handle v) const {
984 if constexpr (Options::contiguous_vertices && !Options::stable_simplex_handles) {
985 assert(contiguous_vertices());
986 return root_.members_.begin() + v;
987 } else {
988 return root_.members_.find(v);
989 }
990 }
991
992 public:
994 bool contiguous_vertices() const {
995 if (root_.members_.empty()) return true;
996 if (root_.members_.begin()->first != 0) return false;
997 if (std::prev(root_.members_.end())->first != static_cast<Vertex_handle>(root_.members_.size() - 1)) return false;
998 return true;
999 }
1000
1001 private:
1017 template<bool update_fil, bool update_children, bool set_to_null, class Filt>
1018 std::pair<Dictionary_it, bool> insert_node_(Siblings *sib, Vertex_handle v, Filt&& filtration_value) {
1019 std::pair<Dictionary_it, bool> ins = sib->members_.try_emplace(v, sib, std::forward<Filt>(filtration_value));
1020
1021 if constexpr (update_children){
1022 if (!(has_children(ins.first))) {
1023 ins.first->second.assign_children(new Siblings(sib, v));
1024 }
1025 }
1026
1027 if (ins.second){
1028 // Only required when insertion is successful
1029 update_simplex_tree_after_node_insertion(ins.first);
1030 return ins;
1031 }
1032
1033 if constexpr (Options::store_filtration && update_fil){
1034 if (unify_lifetimes(ins.first->second.filtration(), filtration_value)) return ins;
1035 }
1036
1037 if constexpr (set_to_null){
1038 ins.first = Dictionary_it();
1039 }
1040
1041 return ins;
1042 }
1043
1044 protected:
1059 template <class RandomVertexHandleRange = std::initializer_list<Vertex_handle>>
1060 std::pair<Simplex_handle, bool> insert_simplex_raw(const RandomVertexHandleRange& simplex,
1062 Siblings * curr_sib = &root_;
1063 std::pair<Dictionary_it, bool> res_insert;
1064 auto vi = simplex.begin();
1065
1066 for (; vi != std::prev(simplex.end()); ++vi) {
1067 GUDHI_CHECK(*vi != null_vertex(), "cannot use the dummy null_vertex() as a real vertex");
1068 res_insert = insert_node_<false, true, false>(curr_sib, *vi, filtration);
1069 curr_sib = res_insert.first->second.children();
1070 }
1071
1072 GUDHI_CHECK(*vi != null_vertex(), "cannot use the dummy null_vertex() as a real vertex");
1073 res_insert = insert_node_<true, false, true>(curr_sib, *vi, filtration);
1074 if (res_insert.second){
1075 int dim = static_cast<int>(boost::size(simplex)) - 1;
1076 if (dim > dimension_) {
1077 // Update dimension if needed
1078 dimension_ = dim;
1079 }
1080 }
1081 return res_insert;
1082 }
1083
1084 public:
1108 template<class InputVertexRange = std::initializer_list<Vertex_handle>>
1109 std::pair<Simplex_handle, bool> insert_simplex(const InputVertexRange & simplex,
1111 auto first = std::begin(simplex);
1112 auto last = std::end(simplex);
1113
1114 if (first == last)
1115 return std::pair<Simplex_handle, bool>(null_simplex(), true); // ----->>
1116
1117 // Copy before sorting
1118 std::vector<Vertex_handle> copy(first, last);
1119 std::sort(std::begin(copy), std::end(copy));
1120 return insert_simplex_raw(copy, filtration);
1121 }
1122
1169
1170 // Retro-compatibility
1190 template <class InputVertexRange = std::initializer_list<Vertex_handle> >
1191 std::pair<Simplex_handle, bool> insert_simplex_and_subfaces(const InputVertexRange& n_simplex,
1193 {
1195 }
1196
1197 // possibility of different default values depending on chosen strategy
1222 template <class InputVertexRange = std::initializer_list<Vertex_handle>>
1223 std::pair<Simplex_handle, bool> insert_simplex_and_subfaces(Filtration_maintenance insertion_strategy,
1224 const InputVertexRange& n_simplex)
1225 {
1226 auto get_default_value = [](Filtration_maintenance strategy) -> Filtration_value {
1227 switch (strategy) {
1229 return Filtration_simplex_base_real::get_infinity();
1231 return Filtration_simplex_base_real::get_minus_infinity();
1233 return Filtration_value();
1234 default:
1235 throw std::invalid_argument("Given insertion strategy is not available.");
1236 }
1237 };
1238
1239 return insert_simplex_and_subfaces(insertion_strategy, n_simplex, get_default_value(insertion_strategy));
1240 }
1241
1242 // actual insertion method
1263 template <class InputVertexRange = std::initializer_list<Vertex_handle>>
1264 std::pair<Simplex_handle, bool> insert_simplex_and_subfaces(
1265 [[maybe_unused]] Filtration_maintenance insertion_strategy,
1266 const InputVertexRange& n_simplex,
1268 {
1269 auto first = std::begin(n_simplex);
1270 auto last = std::end(n_simplex);
1271
1272 if (first == last) return {null_simplex(), true}; // FIXME: false would make more sense to me.
1273
1274 thread_local std::vector<Vertex_handle> copy;
1275 copy.clear();
1276 copy.insert(copy.end(), first, last);
1277 std::sort(copy.begin(), copy.end());
1278 auto last_unique = std::unique(copy.begin(), copy.end());
1279 copy.erase(last_unique, copy.end());
1280 GUDHI_CHECK_code(for (Vertex_handle v : copy)
1281 GUDHI_CHECK(v != null_vertex(), "cannot use the dummy null_vertex() as a real vertex"););
1282 // Update dimension if needed. We could wait to see if the insertion succeeds, but I doubt there is much to gain.
1283 dimension_ = (std::max)(dimension_, static_cast<int>(std::distance(copy.begin(), copy.end())) - 1);
1284
1285 if constexpr (Options::store_filtration){
1286 switch (insertion_strategy) {
1288 return _rec_insert_simplex_and_subfaces_sorted(root(), copy.begin(), copy.end(), filtration);
1290 return _insert_simplex_and_subfaces_at_highest(root(), copy.begin(), copy.end(), filtration);
1292 return _insert_simplex_and_subfaces_forcing_filtration_value(root(), copy.begin(), copy.end(), filtration);
1293 default:
1294 throw std::invalid_argument("Given insertion strategy is not available.");
1295 }
1296 } else {
1297 // filtration values not stored, so no differences between the strategies
1298 return _rec_insert_simplex_and_subfaces_sorted(root(), copy.begin(), copy.end(), filtration);
1299 }
1300 }
1301
1302 private:
1303 // To insert {1,2,3,4}, we insert {2,3,4} twice, once at the root, and once below 1.
1304 template <class ForwardVertexIterator, bool update_fil = true>
1305 std::pair<Simplex_handle, bool> _rec_insert_simplex_and_subfaces_sorted(Siblings* sib,
1306 ForwardVertexIterator first,
1307 ForwardVertexIterator last,
1308 const Filtration_value& filt) {
1309 // An alternative strategy would be:
1310 // - try to find the complete simplex, if found (and low filtration) exit
1311 // - insert all the vertices at once in sib
1312 // - loop over those (new or not) simplices, with a recursive call(++first, last)
1313 Vertex_handle vertex_one = *first;
1314
1315 // insert_node_<bool update_fil, bool update_children, bool set_to_null>(sib, ...)
1316 // update_fil: if true, calls `unify_lifetimes` on the new and old filtration value of the node
1317 // update_children: if true, assign a child to the node if it did not have one
1318 // set_to_null: if true, sets returned iterator to null simplex
1319
1320 if (++first == last) return insert_node_<update_fil, false, update_fil>(sib, vertex_one, filt);
1321
1322 // TODO: have special code here, we know we are building the whole subtree from scratch.
1323 auto insertion_result = insert_node_<update_fil, true, false>(sib, vertex_one, filt);
1324
1325 auto res = _rec_insert_simplex_and_subfaces_sorted<ForwardVertexIterator, update_fil>(
1326 insertion_result.first->second.children(), first, last, filt);
1327 // No need to continue if the full simplex was already there with a low enough filtration value.
1328 if (res.first != null_simplex())
1329 _rec_insert_simplex_and_subfaces_sorted<ForwardVertexIterator, update_fil>(sib, first, last, filt);
1330 return res;
1331 }
1332
1333 bool _make_subfiltration_non_decreasing(Simplex_handle sh, const Filtration_value& filt) {
1334 Filtration_value& f = _to_node_it(sh)->second.filtration();
1335 bool changed = false;
1336 for (auto sh_b : boundary_simplex_range(sh)) {
1337 bool b_changed = true;
1338 // In this particular loop, only newly inserted faces and eventually (old) top faces can have the same value
1339 // than filt. This avoids going too much down the tree.
1340 if (filt == filtration(sh_b)) b_changed = _make_subfiltration_non_decreasing(sh_b, filt);
1341 // If the face did not change value after calling the recursion, than the intersection won't change f's value.
1342 if (b_changed) changed |= intersect_lifetimes(f, filtration(sh_b));
1343 }
1344 return changed;
1345 }
1346
1347 template <class ForwardVertexIterator>
1348 std::pair<Simplex_handle, bool> _insert_simplex_and_subfaces_at_highest(Siblings* sib,
1349 ForwardVertexIterator first,
1350 ForwardVertexIterator last,
1351 const Filtration_value& filt) {
1352 auto res = _rec_insert_simplex_and_subfaces_sorted<ForwardVertexIterator, false>(sib, first, last, filt);
1353 if (res.second) {
1354 _make_subfiltration_non_decreasing(res.first, filt);
1355 } else {
1356 res.first = null_simplex();
1357 }
1358 return res;
1359 }
1360
1361 template <class ForwardVertexIterator>
1362 std::pair<Simplex_handle, bool> _insert_simplex_and_subfaces_forcing_filtration_value(Siblings* sib,
1363 ForwardVertexIterator first,
1364 ForwardVertexIterator last,
1365 const Filtration_value& filt) {
1366 auto res = _rec_insert_simplex_and_subfaces_sorted<ForwardVertexIterator, false>(sib, first, last, filt);
1367 if (!res.second) {
1368 res.first = null_simplex();
1369 }
1370 return res;
1371 }
1372
1373 public:
1377 _to_node_it(sh)->second.assign_key(key);
1378 }
1379
1383 std::pair<Simplex_handle, Simplex_handle> endpoints(Simplex_handle sh) const {
1384 assert(dimension(sh) == 1);
1385 return { find_vertex(sh->first), find_vertex(self_siblings(sh)->parent()) };
1386 }
1387
1389 template<class SimplexHandle>
1390 Siblings const* self_siblings(SimplexHandle sh) const {
1391 if (sh->second.children()->parent() == sh->first)
1392 return sh->second.children()->oncles();
1393 else
1394 return sh->second.children();
1395 }
1396
1398 template<class SimplexHandle>
1399 Siblings* self_siblings(SimplexHandle sh) {
1400 // Scott Meyers in Effective C++ 3rd Edition. On page 23, Item 3: a non const method can safely call a const one
1401 // Doing it the other way is not safe
1402 return const_cast<Siblings*>(std::as_const(*this).self_siblings(sh));
1403 }
1404
1405 public:
1407 Siblings* root() { return &root_; }
1408
1410 const Siblings * root() const {
1411 return &root_;
1412 }
1413
1420 void set_dimension(int dimension, bool exact=true) {
1421 dimension_to_be_lowered_ = !exact;
1422 dimension_ = dimension;
1423 }
1424
1425 private:
1433 bool reverse_lexicographic_order(Simplex_handle sh1, Simplex_handle sh2) const {
1436 Simplex_vertex_iterator it1 = rg1.begin();
1437 Simplex_vertex_iterator it2 = rg2.begin();
1438 while (it1 != rg1.end() && it2 != rg2.end()) {
1439 if (*it1 == *it2) {
1440 ++it1;
1441 ++it2;
1442 } else {
1443 return *it1 < *it2;
1444 }
1445 }
1446 return ((it1 == rg1.end()) && (it2 != rg2.end()));
1447 }
1448
1455 struct is_before_in_totally_ordered_filtration {
1456 explicit is_before_in_totally_ordered_filtration(Simplex_tree const* st)
1457 : st_(st) { }
1458
1459 bool operator()(const Simplex_handle sh1, const Simplex_handle sh2) const {
1460 // Not using st_->filtration(sh1) because it uselessly tests for null_simplex.
1461 if (!(sh1->second.filtration() == sh2->second.filtration())) {
1462 return sh1->second.filtration() < sh2->second.filtration();
1463 }
1464 // is sh1 a proper subface of sh2
1465 return st_->reverse_lexicographic_order(sh1, sh2);
1466 }
1467
1468 Simplex_tree const* st_;
1469 };
1470
1471 public:
1485 void initialize_filtration(bool ignore_infinite_values = false) const {
1486 if (ignore_infinite_values){
1487 initialize_filtration(is_before_in_totally_ordered_filtration(this), [&](Simplex_handle sh) -> bool {
1488 return is_positive_infinity(filtration(sh));
1489 });
1490 } else {
1491 initialize_filtration(is_before_in_totally_ordered_filtration(this), [](Simplex_handle) -> bool {
1492 return false;
1493 });
1494 }
1495 }
1496
1530 template<typename Comparator, typename Ignorer>
1531 void initialize_filtration(Comparator&& is_before_in_filtration, Ignorer&& ignore_simplex) const {
1532 filtration_vect_.clear();
1533 filtration_vect_.reserve(num_simplices());
1535 if (ignore_simplex(sh)) continue;
1536 filtration_vect_.push_back(sh);
1537 }
1538
1539#ifdef GUDHI_USE_TBB
1540 tbb::parallel_sort(filtration_vect_.begin(), filtration_vect_.end(), is_before_in_filtration);
1541#else
1542 std::stable_sort(filtration_vect_.begin(), filtration_vect_.end(), is_before_in_filtration);
1543#endif
1544 }
1545
1553 if (filtration_vect_.empty()) {
1555 }
1556 }
1557
1564 void clear_filtration() const {
1565 filtration_vect_.clear();
1566 }
1567
1568 private:
1582 void rec_coface(std::vector<Vertex_handle> &vertices, Siblings const*curr_sib, int curr_nbVertices,
1583 std::vector<Simplex_handle>& cofaces, bool star, int nbVertices) const {
1584 if (!(star || curr_nbVertices <= nbVertices)) // dimension of actual simplex <= nbVertices
1585 return;
1586 for (Simplex_handle simplex = curr_sib->members().begin(); simplex != curr_sib->members().end(); ++simplex) {
1587 if (vertices.empty()) {
1588 // If we reached the end of the vertices, and the simplex has more vertices than the given simplex
1589 // => we found a coface
1590
1591 // Add a coface if we want the star or if the number of vertices of the current simplex matches with nbVertices
1592 bool addCoface = (star || curr_nbVertices == nbVertices);
1593 if (addCoface)
1594 cofaces.push_back(simplex);
1595 if ((!addCoface || star) && has_children(simplex)) // Rec call
1596 rec_coface(vertices, simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
1597 } else {
1598 if (simplex->first == vertices.back()) {
1599 // If curr_sib matches with the top vertex
1600 bool equalDim = (star || curr_nbVertices == nbVertices); // dimension of actual simplex == nbVertices
1601 bool addCoface = vertices.size() == 1 && equalDim;
1602 if (addCoface)
1603 cofaces.push_back(simplex);
1604 if ((!addCoface || star) && has_children(simplex)) {
1605 // Rec call
1606 Vertex_handle tmp = vertices.back();
1607 vertices.pop_back();
1608 rec_coface(vertices, simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
1609 vertices.push_back(tmp);
1610 }
1611 } else if (simplex->first > vertices.back()) {
1612 return;
1613 } else {
1614 // (simplex->first < vertices.back()
1615 if (has_children(simplex))
1616 rec_coface(vertices, simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
1617 }
1618 }
1619 }
1620 }
1621
1622 public:
1634
1646 // codimension must be positive or null integer
1647 assert(codimension >= 0);
1648
1649 if constexpr (Options::link_nodes_by_label) {
1651 Static_vertex_vector simp(rg.begin(), rg.end());
1652 // must be sorted in decreasing order
1653 assert(std::is_sorted(simp.begin(), simp.end(), std::greater<Vertex_handle>()));
1654 auto range = Optimized_star_simplex_range(Optimized_star_simplex_iterator(this, std::move(simp)),
1655 Optimized_star_simplex_iterator());
1656 // Lazy filtered range
1657 Fast_cofaces_predicate select(this, codimension, this->dimension(simplex));
1658 return boost::adaptors::filter(range, select);
1659 } else {
1660 Cofaces_simplex_range cofaces;
1662 std::vector<Vertex_handle> copy(rg.begin(), rg.end());
1663 if (codimension + static_cast<int>(copy.size()) > dimension_ + 1 ||
1664 (codimension == 0 && static_cast<int>(copy.size()) > dimension_)) // n+codimension greater than dimension_
1665 return cofaces;
1666 // must be sorted in decreasing order
1667 assert(std::is_sorted(copy.begin(), copy.end(), std::greater<Vertex_handle>()));
1668 bool star = codimension == 0;
1669 rec_coface(copy, &root_, 1, cofaces, star, codimension + static_cast<int>(copy.size()));
1670 return cofaces;
1671 }
1672 }
1673
1674 public:
1698 template<class OneSkeletonGraph>
1699 void insert_graph(const OneSkeletonGraph& skel_graph) {
1700 // the simplex tree must be empty
1701 assert(num_simplices() == 0);
1702
1703 // is there a better way to let the compiler know that we don't mean Simplex_tree::num_vertices?
1704 using boost::num_vertices;
1705
1706 if (num_vertices(skel_graph) == 0) {
1707 return;
1708 }
1709 if (num_edges(skel_graph) == 0) {
1710 dimension_ = 0;
1711 } else {
1712 dimension_ = 1;
1713 }
1714
1715 if constexpr (!Options::stable_simplex_handles)
1716 root_.members_.reserve(num_vertices(skel_graph)); // probably useless in most cases
1717 auto verts = vertices(skel_graph) | boost::adaptors::transformed([&](auto v){
1718 return Dit_value_t(v, Node(&root_, get(vertex_filtration_t(), skel_graph, v))); });
1719 root_.members_.insert(boost::begin(verts), boost::end(verts));
1720 // This automatically sorts the vertices, the graph concept doesn't guarantee the order in which we iterate.
1721
1722 for (Dictionary_it it = boost::begin(root_.members_); it != boost::end(root_.members_); it++) {
1723 update_simplex_tree_after_node_insertion(it);
1724 }
1725
1726 std::pair<typename boost::graph_traits<OneSkeletonGraph>::edge_iterator,
1727 typename boost::graph_traits<OneSkeletonGraph>::edge_iterator> boost_edges = edges(skel_graph);
1728 // boost_edges.first is the equivalent to boost_edges.begin()
1729 // boost_edges.second is the equivalent to boost_edges.end()
1730 for (; boost_edges.first != boost_edges.second; boost_edges.first++) {
1731 auto edge = *(boost_edges.first);
1732 auto u = source(edge, skel_graph);
1733 auto v = target(edge, skel_graph);
1734 if (u == v) throw std::invalid_argument("Self-loops are not simplicial");
1735 // We cannot skip edges with the wrong orientation and expect them to
1736 // come a second time with the right orientation, that does not always
1737 // happen in practice. emplace() should be a NOP when an element with the
1738 // same key is already there, so seeing the same edge multiple times is
1739 // ok.
1740 // Should we actually forbid multiple edges? That would be consistent
1741 // with rejecting self-loops.
1742 if (v < u) std::swap(u, v);
1743 auto sh = _to_node_it(find_vertex(u));
1744 if (!has_children(sh)) {
1745 sh->second.assign_children(new Siblings(&root_, sh->first));
1746 }
1747
1748 insert_node_<false, false, false>(sh->second.children(), v, get(edge_filtration_t(), skel_graph, edge));
1749 }
1750 }
1751
1759 template <class VertexRange = std::initializer_list<Vertex_handle> >
1760 void insert_batch_vertices(VertexRange const& vertices, const Filtration_value& filt = Filtration_value()) {
1761 auto verts = vertices | boost::adaptors::transformed([&](auto v){
1762 return Dit_value_t(v, Node(&root_, filt)); });
1763 root_.members_.insert(boost::begin(verts), boost::end(verts));
1764 if (dimension_ < 0 && !root_.members_.empty()) dimension_ = 0;
1765 if constexpr (Options::link_nodes_by_label) {
1766 for (auto sh = root_.members().begin(); sh != root_.members().end(); sh++) {
1767 // update newly inserted simplex (the one that are not linked)
1768 if (!sh->second.list_max_vertex_hook_.is_linked())
1769 update_simplex_tree_after_node_insertion(sh);
1770 }
1771 }
1772 }
1773
1785 void expansion(int max_dimension) {
1786 if (max_dimension <= 1) return;
1787 clear_filtration(); // Drop the cache.
1788 dimension_ = max_dimension;
1789 for (Dictionary_it root_it = root_.members_.begin();
1790 root_it != root_.members_.end(); ++root_it) {
1791 if (has_children(root_it)) {
1792 siblings_expansion(root_it->second.children(), max_dimension - 1);
1793 }
1794 }
1795 dimension_ = max_dimension - dimension_;
1796 }
1797
1833 , Vertex_handle v
1834 , const Filtration_value& fil
1835 , int dim_max
1836 , std::vector<Simplex_handle>& added_simplices)
1837 {
1856
1857 static_assert(Options::link_nodes_by_label, "Options::link_nodes_by_label must be true");
1858
1859 if (u == v) { // Are we inserting a vertex?
1860 auto res_ins = insert_node_<false, false, false>(&root_, u, fil);
1861 if (res_ins.second) { //if the vertex is not in the complex, insert it
1862 added_simplices.push_back(res_ins.first); //no more insert in root_.members()
1863 if (dimension_ == -1) dimension_ = 0;
1864 }
1865 return; //because the vertex is isolated, no more insertions.
1866 }
1867 // else, we are inserting an edge: ensure that u < v
1868 if (v < u) { std::swap(u,v); }
1869
1870 //Note that we copy Simplex_handle (aka map iterators) in added_simplices
1871 //while we are still modifying the Simplex_tree. Insertions in siblings may
1872 //invalidate Simplex_handles; we take care of this fact by first doing all
1873 //insertion in a Sibling, then inserting all handles in added_simplices.
1874
1875#ifdef GUDHI_DEBUG
1876 //check whether vertices u and v are in the tree. If not, return an error.
1877 auto sh_u = root_.members().find(u);
1878 GUDHI_CHECK(sh_u != root_.members().end() &&
1879 root_.members().find(v) != root_.members().end(),
1880 std::invalid_argument(
1881 "Simplex_tree::insert_edge_as_flag - inserts an edge whose vertices are not in the complex")
1882 );
1883 GUDHI_CHECK(!has_children(sh_u) ||
1884 sh_u->second.children()->members().find(v) == sh_u->second.children()->members().end(),
1885 std::invalid_argument(
1886 "Simplex_tree::insert_edge_as_flag - inserts an already existing edge")
1887 );
1888#endif
1889
1890 // to update dimension
1891 const auto tmp_dim = dimension_;
1892 auto tmp_max_dim = dimension_;
1893
1894 //for all siblings containing a Node labeled with u (including the root), run
1895 //compute_punctual_expansion
1896 //todo parallelize
1897 List_max_vertex* nodes_with_label_u = nodes_by_label(u);//all Nodes with u label
1898
1899 GUDHI_CHECK(nodes_with_label_u != nullptr,
1900 "Simplex_tree::insert_edge_as_flag - cannot find the list of Nodes with label u");
1901
1902 for (auto&& node_as_hook : *nodes_with_label_u)
1903 {
1904 Node& node_u = static_cast<Node&>(node_as_hook); //corresponding node, has label u
1905 Simplex_handle sh_u = simplex_handle_from_node(node_u);
1906 Siblings* sib_u = self_siblings(sh_u);
1907 if (sib_u->members().find(v) != sib_u->members().end()) { //v is the label of a sibling of node_u
1908 int curr_dim = dimension(sib_u);
1909 if (dim_max == -1 || curr_dim < dim_max){
1910 if (!has_children(sh_u)) {
1911 //then node_u was a leaf and now has a new child Node labeled v
1912 //the child v is created in compute_punctual_expansion
1913 node_u.assign_children(new Siblings(sib_u, u));
1914 }
1915 dimension_ = dim_max - curr_dim - 1;
1916 compute_punctual_expansion(
1917 v,
1918 node_u.children(),
1919 fil,
1920 dim_max - curr_dim - 1, //>= 0 if dim_max >= 0, <0 otherwise
1921 added_simplices );
1922 dimension_ = dim_max - dimension_;
1923 if (dimension_ > tmp_max_dim) tmp_max_dim = dimension_;
1924 }
1925 }
1926 }
1927 if (tmp_dim <= tmp_max_dim){
1928 dimension_ = tmp_max_dim;
1929 dimension_to_be_lowered_ = false;
1930 } else {
1931 dimension_ = tmp_dim;
1932 }
1933 }
1934
1935 private:
1945 void compute_punctual_expansion( Vertex_handle v
1946 , Siblings * sib
1947 , const Filtration_value& fil
1948 , int k //k == dim_max - dimension simplices in sib
1949 , std::vector<Simplex_handle>& added_simplices )
1950 { //insertion always succeeds because the edge {u,v} used to not be here.
1951 auto res_ins_v = sib->members().emplace(v, Node(sib,fil));
1952 added_simplices.push_back(res_ins_v.first); //no more insertion in sib
1953 update_simplex_tree_after_node_insertion(res_ins_v.first);
1954
1955 if (k == 0) { // reached the maximal dimension. if max_dim == -1, k is never equal to 0.
1956 dimension_ = 0; // to keep track of the max height of the recursion tree
1957 return;
1958 }
1959
1960 //create the subtree of new Node(v)
1961 create_local_expansion( res_ins_v.first
1962 , sib
1963 , fil
1964 , k
1965 , added_simplices );
1966
1967 //punctual expansion in nodes on the left of v, i.e. with label x < v
1968 for (auto sh = sib->members().begin(); sh != res_ins_v.first; ++sh)
1969 { //if v belongs to N^+(x), punctual expansion
1970 Simplex_handle root_sh = find_vertex(sh->first); //Node(x), x < v
1971 if (has_children(root_sh) &&
1972 root_sh->second.children()->members().find(v) != root_sh->second.children()->members().end())
1973 { //edge {x,v} is in the complex
1974 if (!has_children(sh)){
1975 sh->second.assign_children(new Siblings(sib, sh->first));
1976 }
1977 //insert v in the children of sh, and expand.
1978 compute_punctual_expansion( v
1979 , sh->second.children()
1980 , fil
1981 , k-1
1982 , added_simplices );
1983 }
1984 }
1985 }
1986
1993 void create_local_expansion(
1994 Dictionary_it sh_v //Node with label v which has just been inserted
1995 , Siblings *curr_sib //Siblings containing the node sh_v
1996 , const Filtration_value& fil_uv //Fil value of the edge uv in the zz filtration
1997 , int k //Stopping condition for recursion based on max dim
1998 , std::vector<Simplex_handle> &added_simplices) //range of all new simplices
1999 { //pick N^+(v)
2000 //intersect N^+(v) with labels y > v in curr_sib
2001 Dictionary_it next_it = sh_v;
2002 ++next_it;
2003
2004 if (dimension_ > k) {
2005 dimension_ = k; //to keep track of the max height of the recursion tree
2006 }
2007
2008 create_expansion<true>(curr_sib, sh_v, next_it, fil_uv, k, &added_simplices);
2009 }
2010 //TODO boost::container::ordered_unique_range_t in the creation of a Siblings
2011
2020 void siblings_expansion(
2021 Siblings * siblings // must contain elements
2022 , const Filtration_value& fil
2023 , int k // == max_dim expansion - dimension curr siblings
2024 , std::vector<Simplex_handle> & added_simplices )
2025 {
2026 if (dimension_ > k) {
2027 dimension_ = k; //to keep track of the max height of the recursion tree
2028 }
2029 if (k == 0) { return; } //max dimension
2030 Dictionary_it next = ++(siblings->members().begin());
2031
2032 for (Dictionary_it s_h = siblings->members().begin();
2033 next != siblings->members().end(); ++s_h, ++next)
2034 { //find N^+(s_h)
2035 create_expansion<true>(siblings, s_h, next, fil, k, &added_simplices);
2036 }
2037 }
2038
2041 void siblings_expansion(Siblings * siblings, // must contain elements
2042 int k) {
2043 if (k >= 0 && dimension_ > k) {
2044 dimension_ = k;
2045 }
2046 if (k == 0)
2047 return;
2048 Dictionary_it next = siblings->members().begin();
2049 ++next;
2050
2051 for (Dictionary_it s_h = siblings->members().begin();
2052 s_h != siblings->members().end(); ++s_h, ++next)
2053 {
2054 create_expansion<false>(siblings, s_h, next, s_h->second.filtration(), k);
2055 }
2056 }
2057
2062 template<bool force_filtration_value>
2063 void create_expansion(Siblings * siblings,
2064 Dictionary_it& s_h,
2065 Dictionary_it& next,
2066 const Filtration_value& fil,
2067 int k,
2068 std::vector<Simplex_handle>* added_simplices = nullptr)
2069 {
2070 Simplex_handle root_sh = find_vertex(s_h->first);
2071 thread_local std::vector<std::pair<Vertex_handle, Node> > inter;
2072
2073 if (!has_children(root_sh)) return;
2074
2075 intersection<force_filtration_value>(
2076 inter, // output intersection
2077 next, // begin
2078 siblings->members().end(), // end
2079 root_sh->second.children()->members().begin(),
2080 root_sh->second.children()->members().end(),
2081 fil);
2082 if (inter.size() != 0) {
2083 Siblings * new_sib = new Siblings(siblings, // oncles
2084 s_h->first, // parent
2085 inter); // boost::container::ordered_unique_range_t
2086 for (auto it = new_sib->members().begin(); it != new_sib->members().end(); ++it) {
2087 update_simplex_tree_after_node_insertion(it);
2088 if constexpr (force_filtration_value){
2089 //the way create_expansion is used, added_simplices != nullptr when force_filtration_value == true
2090 added_simplices->push_back(it);
2091 }
2092 }
2093 inter.clear();
2094 s_h->second.assign_children(new_sib);
2095 if constexpr (force_filtration_value){
2096 siblings_expansion(new_sib, fil, k - 1, *added_simplices);
2097 } else {
2098 siblings_expansion(new_sib, k - 1);
2099 }
2100 } else {
2101 // ensure the children property
2102 s_h->second.assign_children(siblings);
2103 inter.clear();
2104 }
2105 }
2106
2109 template<bool force_filtration_value = false>
2110 static void intersection(std::vector<std::pair<Vertex_handle, Node> >& intersection,
2111 Dictionary_const_it begin1, Dictionary_const_it end1,
2112 Dictionary_const_it begin2, Dictionary_const_it end2,
2113 const Filtration_value& filtration_) {
2114 if (begin1 == end1 || begin2 == end2)
2115 return; // ----->>
2116 while (true) {
2117 if (begin1->first == begin2->first) {
2118 if constexpr (force_filtration_value){
2119 intersection.emplace_back(begin1->first, Node(nullptr, filtration_));
2120 } else {
2121 Filtration_value filt = begin1->second.filtration();
2122 intersect_lifetimes(filt, begin2->second.filtration());
2123 intersect_lifetimes(filt, filtration_);
2124 intersection.emplace_back(begin1->first, Node(nullptr, filt));
2125 }
2126 if (++begin1 == end1 || ++begin2 == end2)
2127 return; // ----->>
2128 } else if (begin1->first < begin2->first) {
2129 if (++begin1 == end1)
2130 return;
2131 } else /* begin1->first > begin2->first */ {
2132 if (++begin2 == end2)
2133 return; // ----->>
2134 }
2135 }
2136 }
2137
2138 public:
2157 template< typename Blocker >
2158 void expansion_with_blockers(int max_dim, Blocker block_simplex) {
2159 // Loop must be from the end to the beginning, as higher dimension simplex are always on the left part of the tree
2160 for (auto& simplex : boost::adaptors::reverse(root_.members())) {
2161 if (has_children(&simplex)) {
2162 siblings_expansion_with_blockers(simplex.second.children(), max_dim, max_dim - 1, block_simplex);
2163 }
2164 }
2165 }
2166
2167 private:
2169 template< typename Blocker >
2170 void siblings_expansion_with_blockers(Siblings* siblings, int max_dim, int k, Blocker block_simplex) {
2171 if (dimension_ < max_dim - k) {
2172 dimension_ = max_dim - k;
2173 }
2174 if (k == 0)
2175 return;
2176 // No need to go deeper
2177 if (siblings->members().size() < 2)
2178 return;
2179 // Reverse loop starting before the last one for 'next' to be the last one
2180 for (auto simplex = std::next(siblings->members().rbegin()); simplex != siblings->members().rend(); simplex++) {
2181 std::vector<std::pair<Vertex_handle, Node> > intersection;
2182 for(auto next = siblings->members().rbegin(); next != simplex; next++) {
2183 bool to_be_inserted = true;
2184 Filtration_value filt = simplex->second.filtration();
2185 // If all the boundaries are present, 'next' needs to be inserted
2186 for (Simplex_handle border : boundary_simplex_range(simplex)) {
2187 Simplex_handle border_child = find_child(border, next->first);
2188 if (border_child == null_simplex()) {
2189 to_be_inserted=false;
2190 break;
2191 }
2192 intersect_lifetimes(filt, filtration(border_child));
2193 }
2194 if (to_be_inserted) {
2195 intersection.emplace_back(next->first, Node(nullptr, filt));
2196 }
2197 }
2198 if (intersection.size() != 0) {
2199 // Reverse the order to insert
2200 Siblings * new_sib = new Siblings(
2201 siblings, // oncles
2202 simplex->first, // parent
2203 boost::adaptors::reverse(intersection)); // boost::container::ordered_unique_range_t
2204 simplex->second.assign_children(new_sib);
2205 std::vector<Vertex_handle> blocked_new_sib_vertex_list;
2206 // As all intersections are inserted, we can call the blocker function on all new_sib members
2207 for (auto new_sib_member = new_sib->members().begin();
2208 new_sib_member != new_sib->members().end();
2209 new_sib_member++) {
2210 update_simplex_tree_after_node_insertion(new_sib_member);
2211 bool blocker_result = block_simplex(new_sib_member);
2212 // new_sib member has been blocked by the blocker function
2213 // add it to the list to be removed - do not perform it while looping on it
2214 if (blocker_result) {
2215 blocked_new_sib_vertex_list.push_back(new_sib_member->first);
2216 // update data structures for all deleted simplices
2217 // can be done in the loop as part of another data structure
2218 update_simplex_tree_before_node_removal(new_sib_member);
2219 }
2220 }
2221 if (blocked_new_sib_vertex_list.size() == new_sib->members().size()) {
2222 // Specific case where all have to be deleted
2223 delete new_sib;
2224 // ensure the children property
2225 simplex->second.assign_children(siblings);
2226 } else {
2227 for (auto& blocked_new_sib_member : blocked_new_sib_vertex_list) {
2228 new_sib->members().erase(blocked_new_sib_member);
2229 }
2230 // ensure recursive call
2231 siblings_expansion_with_blockers(new_sib, max_dim, k - 1, block_simplex);
2232 }
2233 } else {
2234 // ensure the children property
2235 simplex->second.assign_children(siblings);
2236 }
2237 }
2238 }
2239
2244 Simplex_handle find_child(Simplex_handle sh, Vertex_handle vh) const {
2245 if (!has_children(sh))
2246 return null_simplex();
2247
2248 Simplex_handle child = sh->second.children()->find(vh);
2249 // Specific case of boost::flat_map does not find, returns boost::flat_map::end()
2250 // in simplex tree we want a null_simplex()
2251 if (child == sh->second.children()->members().end())
2252 return null_simplex();
2253
2254 return child;
2255 }
2256
2257 public:
2267 void print_hasse(std::ostream& os) const {
2268 os << num_simplices() << " " << std::endl;
2269 // TODO: should use complex_simplex_range ?
2270 for (auto sh : filtration_simplex_range()) {
2271 os << dimension(sh) << " ";
2272 for (auto b_sh : boundary_simplex_range(sh)) {
2273 os << key(b_sh) << " ";
2274 }
2275 os << filtration(sh) << " \n";
2276 }
2277 }
2278
2279 public:
2288 template<class Fun>
2289 void for_each_simplex(Fun&& fun) const {
2290 // Wrap callback so it always returns bool
2291 auto f = [&fun](Simplex_handle sh, int dim) -> bool {
2292 if constexpr (std::is_same_v<void, decltype(fun(sh, dim))>) {
2293 fun(sh, dim);
2294 return false;
2295 } else {
2296 return fun(sh, dim);
2297 }
2298 };
2299 if (!is_empty())
2300 rec_for_each_simplex(root(), 0, f);
2301 }
2302
2303 private:
2304 template<class Fun>
2305 void rec_for_each_simplex(const Siblings* sib, int dim, Fun&& fun) const {
2306 Simplex_handle sh = sib->members().end();
2307 GUDHI_CHECK(sh != sib->members().begin(), "Bug in Gudhi: only the root siblings may be empty");
2308 do {
2309 --sh;
2310 if (!fun(sh, dim) && has_children(sh)) {
2311 rec_for_each_simplex(sh->second.children(), dim+1, fun);
2312 }
2313 // We could skip checking has_children for the first element of the iteration, we know it returns false.
2314 }
2315 while(sh != sib->members().begin());
2316 }
2317
2318 public:
2326 bool modified = false;
2327 auto fun = [&modified, this](Simplex_handle sh, int dim) -> void {
2328 if (dim == 0) return;
2329
2330 Filtration_value& current_filt = _to_node_it(sh)->second.filtration();
2331
2332 // Find the maximum filtration value in the border and assigns it if it is greater than the current
2334 // considers NaN as the lowest possible value.
2335 // stores the filtration modification information
2336 modified |= intersect_lifetimes(current_filt, b->second.filtration());
2337 }
2338 };
2339 // Loop must be from the end to the beginning, as higher dimension simplex are always on the left part of the tree
2340 for_each_simplex(fun);
2341
2342 if (modified)
2343 clear_filtration(); // Drop the cache.
2344 return modified;
2345 }
2346
2347 public:
2349 void clear() {
2350 root_members_recursive_deletion();
2352 dimension_ = -1;
2353 dimension_to_be_lowered_ = false;
2354 if constexpr (Options::link_nodes_by_label)
2355 nodes_label_to_list_.clear();
2356 }
2357
2371 return false; // ---->>
2372 bool modified = rec_prune_above_filtration(root(), filtration);
2373 if(modified)
2374 clear_filtration(); // Drop the cache.
2375 return modified;
2376 }
2377
2378 private:
2379 bool rec_prune_above_filtration(Siblings* sib, const Filtration_value& filt) {
2380 auto&& list = sib->members();
2381 bool modified = false;
2382 bool emptied = false;
2383 Simplex_handle last;
2384
2385 auto to_remove = [this, filt](Dit_value_t& simplex) {
2386 //if filt and simplex.second.filtration() are non comparable, should return false.
2387 //if simplex.second.filtration() is NaN, should return true.
2388 if (filt < simplex.second.filtration() || !(simplex.second.filtration() == simplex.second.filtration())) {
2389 if (has_children(&simplex)) rec_delete(simplex.second.children());
2390 // dimension may need to be lowered
2391 dimension_to_be_lowered_ = true;
2392 return true;
2393 }
2394 return false;
2395 };
2396
2397 //TODO: `if constexpr` replaceable by `std::erase_if` in C++20? Has a risk of additional runtime,
2398 //so to benchmark first.
2399 if constexpr (Options::stable_simplex_handles) {
2400 modified = false;
2401 for (auto sh = list.begin(); sh != list.end();) {
2402 if (to_remove(*sh)) {
2403 sh = list.erase(sh);
2404 modified = true;
2405 } else {
2406 ++sh;
2407 }
2408 }
2409 emptied = (list.empty() && sib != root());
2410 } else {
2411 last = std::remove_if(list.begin(), list.end(), to_remove);
2412 modified = (last != list.end());
2413 emptied = (last == list.begin() && sib != root());
2414 }
2415
2416 if (emptied) {
2417 // Removing the whole siblings, parent becomes a leaf.
2418 sib->oncles()->members()[sib->parent()].assign_children(sib->oncles());
2419 delete sib;
2420 // dimension may need to be lowered
2421 dimension_to_be_lowered_ = true;
2422 return true;
2423 } else {
2424 // Keeping some elements of siblings. Remove the others, and recurse in the remaining ones.
2425 if constexpr (!Options::stable_simplex_handles) list.erase(last, list.end());
2426 for (auto&& simplex : list)
2427 if (has_children(&simplex)) modified |= rec_prune_above_filtration(simplex.second.children(), filt);
2428 }
2429
2430 return modified;
2431 }
2432
2433 public:
2438 bool prune_above_dimension(int dimension) {
2439 if (dimension >= dimension_)
2440 return false;
2441
2442 bool modified = false;
2443 if (dimension < 0) {
2444 if (num_vertices() > 0) {
2445 root_members_recursive_deletion();
2446 modified = true;
2447 }
2448 // Force dimension to -1, in case user calls `prune_above_dimension(-10)`
2449 dimension = -1;
2450 } else {
2451 modified = rec_prune_above_dimension(root(), dimension, 0);
2452 }
2453 if(modified) {
2454 // Thanks to `if (dimension >= dimension_)` and dimension forced to -1 `if (dimension < 0)`, we know the new dimension
2455 dimension_ = dimension;
2456 clear_filtration(); // Drop the cache.
2457 }
2458 return modified;
2459 }
2460
2461 private:
2462 bool rec_prune_above_dimension(Siblings* sib, int dim, int actual_dim) {
2463 bool modified = false;
2464 auto&& list = sib->members();
2465
2466 for (auto&& simplex : list)
2467 if (has_children(&simplex)) {
2468 if (actual_dim >= dim) {
2469 rec_delete(simplex.second.children());
2470 simplex.second.assign_children(sib);
2471 modified = true;
2472 } else {
2473 modified |= rec_prune_above_dimension(simplex.second.children(), dim, actual_dim + 1);
2474 }
2475 }
2476
2477 return modified;
2478 }
2479
2480 private:
2486 bool lower_upper_bound_dimension() const {
2487 // reset automatic detection to recompute
2488 dimension_to_be_lowered_ = false;
2489 int new_dimension = -1;
2490 // Browse the tree from the left to the right as higher dimension cells are more likely on the left part of the tree
2492#ifdef DEBUG_TRACES
2493 for (auto vertex : simplex_vertex_range(sh)) {
2494 std::clog << " " << vertex;
2495 }
2496 std::clog << std::endl;
2497#endif // DEBUG_TRACES
2498
2499 int sh_dimension = dimension(sh);
2500 if (sh_dimension >= dimension_)
2501 // Stop browsing as soon as the dimension is reached, no need to go further
2502 return false;
2503 new_dimension = (std::max)(new_dimension, sh_dimension);
2504 }
2505 dimension_ = new_dimension;
2506 return true;
2507 }
2508
2509
2510 public:
2520 // Guarantee the simplex has no children
2521 GUDHI_CHECK(!has_children(sh),
2522 std::invalid_argument("Simplex_tree::remove_maximal_simplex - argument has children"));
2523
2524 update_simplex_tree_before_node_removal(sh);
2525
2526 // Simplex is a leaf, it means the child is the Siblings owning the leaf
2527 Dictionary_it nodeIt = _to_node_it(sh);
2528 Siblings* child = nodeIt->second.children();
2529
2530 if ((child->size() > 1) || (child == root())) {
2531 // Not alone, just remove it from members
2532 // Special case when child is the root of the simplex tree, just remove it from members
2533 child->erase(nodeIt);
2534 } else {
2535 // Sibling is emptied : must be deleted, and its parent must point on his own Sibling
2536 child->oncles()->members().at(child->parent()).assign_children(child->oncles());
2537 delete child;
2538 // dimension may need to be lowered
2539 dimension_to_be_lowered_ = true;
2540 }
2541 }
2542
2559 std::pair<Filtration_value, Extended_simplex_type> decode_extended_filtration(
2560 const Filtration_value& f,
2561 const Extended_filtration_data& efd) const
2562 {
2563 std::pair<Filtration_value, Extended_simplex_type> p;
2564 const Filtration_value& minval = efd.minval;
2565 const Filtration_value& maxval = efd.maxval;
2566 if (f >= -2 && f <= -1) {
2567 p.first = minval + (maxval - minval) * (f + 2);
2568 p.second = Extended_simplex_type::UP;
2569 } else if (f >= 1 && f <= 2) {
2570 p.first = minval - (maxval - minval) * (f - 2);
2571 p.second = Extended_simplex_type::DOWN;
2572 } else {
2573 p.first = std::numeric_limits<Filtration_value>::quiet_NaN();
2574 p.second = Extended_simplex_type::EXTRA;
2575 }
2576 return p;
2577 };
2578
2579 //TODO: externalize this method and `decode_extended_filtration`
2597 Extended_filtration_data extend_filtration() {
2598 clear_filtration(); // Drop the cache.
2599
2600 // Compute maximum and minimum of filtration values
2601 Vertex_handle maxvert = std::numeric_limits<Vertex_handle>::min();
2602 Filtration_value minval = Filtration_simplex_base_real::get_infinity();
2603 Filtration_value maxval = -Filtration_simplex_base_real::get_infinity();
2604 for (auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) {
2605 const Filtration_value& f = this->filtration(sh);
2606 minval = std::min(minval, f);
2607 maxval = std::max(maxval, f);
2608 maxvert = std::max(sh->first, maxvert);
2609 }
2610
2611 GUDHI_CHECK(maxvert < std::numeric_limits<Vertex_handle>::max(), std::invalid_argument("Simplex_tree contains a vertex with the largest Vertex_handle"));
2612 maxvert++;
2613
2614 Simplex_tree st_copy = *this;
2615
2616 // Add point for coning the simplicial complex
2617 this->insert_simplex_raw({maxvert}, -3);
2618
2619 Filtration_value scale = maxval-minval;
2620 if (scale != 0)
2621 scale = 1 / scale;
2622
2623 // For each simplex
2624 std::vector<Vertex_handle> vr;
2625 for (auto sh_copy : st_copy.complex_simplex_range()) {
2626 auto&& simplex_range = st_copy.simplex_vertex_range(sh_copy);
2627 vr.assign(simplex_range.begin(), simplex_range.end());
2628 auto sh = this->find(vr);
2629
2630 // Create cone on simplex
2631 vr.push_back(maxvert);
2632 if (this->dimension(sh) == 0) {
2633 const Filtration_value& v = this->filtration(sh);
2634 Filtration_value scaled_v = (v - minval) * scale;
2635 // Assign ascending value between -2 and -1 to vertex
2636 this->assign_filtration(sh, -2 + scaled_v);
2637 // Assign descending value between 1 and 2 to cone on vertex
2638 this->insert_simplex(vr, 2 - scaled_v);
2639 } else {
2640 // Assign value -3 to simplex and cone on simplex
2641 this->assign_filtration(sh, -3);
2642 this->insert_simplex(vr, -3);
2643 }
2644 }
2645
2646 // Automatically assign good values for simplices
2648
2649 // Return the filtration data
2650 return Extended_filtration_data(minval, maxval);
2651 }
2652
2658 auto filt = filtration_(sh);
2659 for(auto v : simplex_vertex_range(sh))
2660 if(filtration_(find_vertex(v)) == filt)
2661 return v;
2662 return null_vertex();
2663 }
2664
2672 // See issue #251 for potential speed improvements.
2673 auto&& vertices = simplex_vertex_range(sh); // vertices in decreasing order
2674 auto end = std::end(vertices);
2675 auto vi = std::begin(vertices);
2676 GUDHI_CHECK(vi != end, "empty simplex");
2677 auto v0 = *vi;
2678 ++vi;
2679 GUDHI_CHECK(vi != end, "simplex of dimension 0");
2680 if(std::next(vi) == end) return sh; // shortcut for dimension 1
2681 Static_vertex_vector suffix;
2682 suffix.push_back(v0);
2683 auto filt = filtration_(sh);
2684 do
2685 {
2686 Vertex_handle v = *vi;
2687 auto&& children1 = find_vertex(v)->second.children()->members_;
2688 for(auto w : suffix){
2689 // Can we take advantage of the fact that suffix is ordered?
2690 Simplex_handle s = children1.find(w);
2691 if(filtration_(s) == filt)
2692 return s;
2693 }
2694 suffix.push_back(v);
2695 }
2696 while(++vi != end);
2697 return null_simplex();
2698 }
2699
2705 auto filt = filtration_(sh);
2706 // Naive implementation, it can be sped up.
2707 for(auto b : boundary_simplex_range(sh))
2708 if(filtration_(b) == filt)
2710 return sh; // None of its faces has the same filtration.
2711 }
2712
2713 public:
2714 // intrusive list of Nodes with same label using the hooks
2715 typedef boost::intrusive::member_hook<Hooks_simplex_base_link_nodes, typename Hooks_simplex_base_link_nodes::Member_hook_t,
2716 &Hooks_simplex_base_link_nodes::list_max_vertex_hook_>
2717 List_member_hook_t;
2718 // auto_unlink in Member_hook_t is incompatible with constant time size
2719 typedef boost::intrusive::list<Hooks_simplex_base_link_nodes, List_member_hook_t,
2720 boost::intrusive::constant_time_size<false>> List_max_vertex;
2721 // type of hooks stored in each Node, Node inherits from Hooks_simplex_base
2722 typedef typename std::conditional<Options::link_nodes_by_label, Hooks_simplex_base_link_nodes,
2723 Hooks_simplex_base_dummy>::type Hooks_simplex_base;
2724
2727 private:
2728 // if Options::link_nodes_by_label is true, store the lists of Nodes with same label, empty otherwise.
2729 // unordered_map Vertex_handle v -> list of all Nodes with label v.
2730 std::unordered_map<Vertex_handle, List_max_vertex> nodes_label_to_list_;
2731
2732 List_max_vertex* nodes_by_label(Vertex_handle v) {
2733 // Scott Meyers in Effective C++ 3rd Edition. On page 23, Item 3: a non const method can safely call a const one
2734 // Doing it the other way is not safe
2735 return const_cast<List_max_vertex*>(std::as_const(*this).nodes_by_label(v));
2736 }
2737
2738 List_max_vertex const* nodes_by_label(Vertex_handle v) const {
2739 if constexpr (Options::link_nodes_by_label) {
2740 auto it_v = nodes_label_to_list_.find(v);
2741 if (it_v != nodes_label_to_list_.end()) {
2742 return &(it_v->second);
2743 } else {
2744 return nullptr;
2745 }
2746 }
2747 return nullptr;
2748 }
2749
2752 static Simplex_handle simplex_handle_from_node(const Node& node) {
2753 if constexpr (Options::stable_simplex_handles){
2754 //Relies on the Dictionary type to be boost::container::map<Vertex_handle, Node>.
2755 //If the type changes or boost fundamentally changes something on the structure of their map,
2756 //a safer/more general but much slower version is:
2757 // if (node.children()->parent() == label) { // verifies if node is a leaf
2758 // return children->oncles()->find(label);
2759 // } else {
2760 // return children->members().find(label);
2761 // }
2762 //Requires an additional parameter "Vertex_handle label" which is the label of the node.
2763
2764 Dictionary_const_it testIt = node.children()->members().begin();
2765 const Node* testNode = &testIt->second;
2766 auto testIIt = testIt.get();
2767 auto testPtr = testIIt.pointed_node();
2768 //distance between node and pointer to map pair in memory
2769 auto shift = (const char*)(testNode) - (const char*)(testPtr);
2770
2771 //decltype(testPtr) = boost::intrusive::compact_rbtree_node<void*>*
2772 decltype(testPtr) sh_ptr = decltype(testPtr)((const char*)(&node) - shift); //shifts from node to pointer
2773 //decltype(testIIt) =
2774 //boost::intrusive::tree_iterator<
2775 // boost::intrusive::bhtraits<
2776 // boost::container::base_node<
2777 // std::pair<const int, Simplex_tree_node_explicit_storage<Simplex_tree>>,
2778 // boost::container::dtl::intrusive_tree_hook<void*, boost::container::red_black_tree, true>, true>,
2779 // boost::intrusive::rbtree_node_traits<void*, true>,
2780 // boost::intrusive::normal_link,
2781 // boost::intrusive::dft_tag,
2782 // 3>,
2783 // false>
2784 decltype(testIIt) sh_ii;
2785 sh_ii = sh_ptr; //creates ``subiterator'' from pointer
2786 Dictionary_const_it sh(sh_ii); //creates iterator from subiterator
2787
2788 return sh;
2789 } else {
2790 //node needs to be casted as non const, because a const pair* cannot be casted into a Simplex_handle
2791 return (Simplex_handle)(boost::intrusive::get_parent_from_member<Dit_value_t>(const_cast<Node*>(&node),
2792 &Dit_value_t::second));
2793 }
2794 }
2795
2796 // Give access to Simplex_tree_optimized_cofaces_rooted_subtrees_simplex_iterator and keep nodes_by_label and
2797 // simplex_handle_from_node private
2798 friend class Simplex_tree_optimized_cofaces_rooted_subtrees_simplex_iterator<Simplex_tree>;
2799
2800 private:
2801 // update all extra data structures in the Simplex_tree. Must be called after all
2802 // simplex insertions.
2803 void update_simplex_tree_after_node_insertion(Simplex_handle sh) {
2804#ifdef DEBUG_TRACES
2805 std::clog << "update_simplex_tree_after_node_insertion" << std::endl;
2806#endif // DEBUG_TRACES
2807 if constexpr (Options::link_nodes_by_label) {
2808 // Creates an entry with sh->first if not already in the map and insert sh->second at the end of the list
2809 nodes_label_to_list_[sh->first].push_back(_to_node_it(sh)->second);
2810 }
2811 }
2812
2813 // update all extra data structures in the Simplex_tree. Must be called before
2814 // all simplex removals
2815 void update_simplex_tree_before_node_removal(Simplex_handle sh) {
2816#ifdef DEBUG_TRACES
2817 std::clog << "update_simplex_tree_before_node_removal" << std::endl;
2818#endif // DEBUG_TRACES
2819 if constexpr (Options::link_nodes_by_label) {
2820 _to_node_it(sh)->second.unlink_hooks(); // remove from lists of same label Nodes
2821 if (nodes_label_to_list_[sh->first].empty())
2822 nodes_label_to_list_.erase(sh->first);
2823 }
2824 }
2825
2826 public:
2835 void reset_filtration(const Filtration_value& filtration, int min_dim = 0) {
2836 rec_reset_filtration(&root_, filtration, min_dim);
2837 clear_filtration(); // Drop the cache.
2838 }
2839
2840 private:
2846 void rec_reset_filtration(Siblings * sib, const Filtration_value& filt_value, int min_depth) {
2847 for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
2848 if (min_depth <= 0) {
2849 sh->second.assign_filtration(filt_value);
2850 }
2851 if (has_children(sh)) {
2852 rec_reset_filtration(sh->second.children(), filt_value, min_depth - 1);
2853 }
2854 }
2855 }
2856
2857 std::size_t num_simplices_and_filtration_serialization_size(Siblings const* sib, std::size_t& fv_byte_size) const {
2858 using namespace Gudhi::simplex_tree;
2859
2860 auto sib_begin = sib->members().begin();
2861 auto sib_end = sib->members().end();
2862 size_t simplices_number = sib->members().size();
2863 for (auto sh = sib_begin; sh != sib_end; ++sh) {
2865 fv_byte_size += get_serialization_size_of(sh->second.filtration());
2866 if (has_children(sh)) {
2867 simplices_number += num_simplices_and_filtration_serialization_size(sh->second.children(), fv_byte_size);
2868 }
2869 }
2870 return simplices_number;
2871 }
2872
2873 public:
2883 std::size_t get_serialization_size() const {
2884 const std::size_t vh_byte_size = sizeof(Vertex_handle);
2885 std::size_t fv_byte_size = 0;
2886 const std::size_t tree_size = num_simplices_and_filtration_serialization_size(&root_, fv_byte_size);
2887 const std::size_t buffer_byte_size = vh_byte_size + fv_byte_size + tree_size * 2 * vh_byte_size;
2888#ifdef DEBUG_TRACES
2889 std::clog << "Gudhi::simplex_tree::get_serialization_size - buffer size = " << buffer_byte_size << std::endl;
2890#endif // DEBUG_TRACES
2891 return buffer_byte_size;
2892 }
2893
2908 /* Let's take the following simplicial complex as example: */
2909 /* (vertices are represented as letters to ease the understanding) */
2910 /* o---o---o */
2911 /* a b\X/c */
2912 /* o */
2913 /* d */
2914 /* The simplex tree is: */
2915 /* a o b o c o d o */
2916 /* | |\ | */
2917 /* b o c o o d o d */
2918 /* | */
2919 /* d o */
2920 /* The serialization is (without filtration values that comes right after vertex handle value): */
2921 /* 04(number of vertices)0a 0b 0c 0d(list of vertices)01(number of [a] children)0b([a,b] simplex) */
2922 /* 00(number of [a,b] children)02(number of [b] children)0c 0d(list of [b] children)01(number of [b,c] children) */
2923 /* 0d(list of [b,c] children)00(number of [b,c,d] children)00(number of [b,d] children)01(number of [c] children) */
2924 /* 0d(list of [c] children)00(number of [b,d] children)00(number of [d] children) */
2925 /* Without explanation and with filtration values: */
2926 /* 04 0a F(a) 0b F(b) 0c F(c) 0d F(d) 01 0b F(a,b) 00 02 0c F(b,c) 0d F(b,d) 01 0d F(b,c,d) 00 00 01 0d F(c,d) 00 00 */
2927 void serialize(char* buffer, const std::size_t buffer_size) const {
2928 char* buffer_end = rec_serialize(&root_, buffer);
2929 if (static_cast<std::size_t>(buffer_end - buffer) != buffer_size)
2930 throw std::invalid_argument("Serialization does not match end of buffer");
2931 }
2932
2933 private:
2935 char* rec_serialize(Siblings const *sib, char* buffer) const {
2936 char* ptr = buffer;
2937 ptr = serialize_value_to_char_buffer(static_cast<Vertex_handle>(sib->members().size()), ptr);
2938#ifdef DEBUG_TRACES
2939 std::clog << "\n" << sib->members().size() << " : ";
2940#endif // DEBUG_TRACES
2941 for (auto& map_el : sib->members()) {
2942 ptr = serialize_value_to_char_buffer(map_el.first, ptr); // Vertex
2944 ptr = serialize_value_to_char_buffer(map_el.second.filtration(), ptr); // Filtration
2945#ifdef DEBUG_TRACES
2946 std::clog << " [ " << map_el.first << " | " << map_el.second.filtration() << " ] ";
2947#endif // DEBUG_TRACES
2948 }
2949 for (auto& map_el : sib->members()) {
2950 if (has_children(&map_el)) {
2951 ptr = rec_serialize(map_el.second.children(), ptr);
2952 } else {
2953 ptr = serialize_value_to_char_buffer(static_cast<Vertex_handle>(0), ptr);
2954#ifdef DEBUG_TRACES
2955 std::cout << "\n0 : ";
2956#endif // DEBUG_TRACES
2957 }
2958 }
2959 return ptr;
2960 }
2961
2962 public:
2979 void deserialize(const char* buffer, const std::size_t buffer_size) {
2980 deserialize(buffer, buffer_size, [](Filtration_value& filtration, const char* ptr) {
2981 return deserialize_value_from_char_buffer(filtration, ptr);
2982 });
2983 }
2984
3007 template <class F>
3008 void deserialize(const char* buffer, const std::size_t buffer_size, F&& deserialize_filtration_value) {
3009 GUDHI_CHECK(num_vertices() == 0, std::logic_error("Simplex_tree::deserialize - Simplex_tree must be empty"));
3010 const char* ptr = buffer;
3011 // Needs to read size before recursivity to manage new siblings for children
3012 Vertex_handle members_size;
3013 ptr = deserialize_value_from_char_buffer(members_size, ptr);
3014 ptr = rec_deserialize(&root_, members_size, ptr, 0, deserialize_filtration_value);
3015 if (static_cast<std::size_t>(ptr - buffer) != buffer_size) {
3016 throw std::invalid_argument("Deserialization does not match end of buffer");
3017 }
3018 }
3019
3020 private:
3022 template <class F>
3023 const char* rec_deserialize(Siblings* sib,
3024 Vertex_handle members_size,
3025 const char* ptr,
3026 int dim,
3027 [[maybe_unused]] F&& deserialize_filtration_value)
3028 {
3029 // In case buffer is just a 0 char
3030 if (members_size > 0) {
3031 if constexpr (!Options::stable_simplex_handles) sib->members_.reserve(members_size);
3032 Vertex_handle vertex;
3033 // Set explicitly to zero to avoid false positive error raising in debug mode when store_filtration == false
3034 // and to force array like Filtration_value value to be empty.
3035 Filtration_value filtration(Gudhi::simplex_tree::empty_filtration_value);
3036 for (Vertex_handle idx = 0; idx < members_size; idx++) {
3037 ptr = deserialize_value_from_char_buffer(vertex, ptr);
3038 if constexpr (Options::store_filtration) {
3039 ptr = deserialize_filtration_value(filtration, ptr);
3040 }
3041 // Default is no children
3042 // If store_filtration is false, `filtration` is ignored.
3043 sib->members_.emplace_hint(sib->members_.end(), vertex, Node(sib, filtration));
3044 }
3045 Vertex_handle child_size;
3046 for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
3047 update_simplex_tree_after_node_insertion(sh);
3048 ptr = deserialize_value_from_char_buffer(child_size, ptr);
3049 if (child_size > 0) {
3050 Siblings* child = new Siblings(sib, sh->first);
3051 sh->second.assign_children(child);
3052 ptr = rec_deserialize(child, child_size, ptr, dim + 1, deserialize_filtration_value);
3053 }
3054 }
3055 if (dim > dimension_) {
3056 // Update dimension if needed
3057 dimension_ = dim;
3058 }
3059 }
3060 return ptr;
3061 }
3062
3063 private:
3064 Vertex_handle null_vertex_;
3067 Siblings root_;
3068
3069 // all mutable as their content has no impact on the content of the simplex tree itself
3070 // they correspond to some kind of cache or helper attributes.
3072 mutable std::vector<Simplex_handle> filtration_vect_;
3074 mutable int dimension_;
3075 mutable bool dimension_to_be_lowered_;
3076};
3077
3078// Print a Simplex_tree in os.
3079template<typename...T>
3080std::ostream& operator<<(std::ostream & os, const Simplex_tree<T...> & st) {
3081 for (auto sh : st.filtration_simplex_range()) {
3082 os << st.dimension(sh) << " ";
3083 for (auto v : st.simplex_vertex_range(sh)) {
3084 os << v << " ";
3085 }
3086 os << st.filtration(sh) << "\n"; // TODO(VR): why adding the key ?? not read ?? << " " << st.key(sh) << " \n";
3087 }
3088 return os;
3089}
3090
3091template<typename...T>
3092std::istream& operator>>(std::istream & is, Simplex_tree<T...> & st) {
3093 typedef Simplex_tree<T...> ST;
3094 std::vector<typename ST::Vertex_handle> simplex;
3095 typename ST::Filtration_value fil;
3096 int max_dim = -1;
3097 while (read_simplex(is, simplex, fil)) {
3098 // read all simplices in the file as a list of vertices
3099 // Warning : simplex_size needs to be casted in int - Can be 0
3100 int dim = static_cast<int> (simplex.size() - 1);
3101 if (max_dim < dim) {
3102 max_dim = dim;
3103 }
3104 // insert every simplex in the simplex tree
3105 st.insert_simplex(simplex, fil);
3106 simplex.clear();
3107 }
3108 st.set_dimension(max_dim);
3109
3110 return is;
3111}
3112 // end addtogroup simplex_tree
3114
3115} // namespace Gudhi
3116
3117#endif // SIMPLEX_TREE_H_
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:187
Iterator over the simplices of the boundary of a simplex.
Definition Simplex_tree_iterators.h:81
Iterator over the simplices of a simplicial complex.
Definition Simplex_tree_iterators.h:295
Iterator over the simplices of the simplicial complex that match the dimension specified by the param...
Definition Simplex_tree_iterators.h:451
Iterator over the simplices of the star of a simplex.
Definition Simplex_tree_star_simplex_iterators.h:162
Data structure to store a set of nodes in a SimplexTree sharing the same parent node.
Definition Simplex_tree_siblings.h:29
Iterator over the vertices of a simplex in a SimplexTree.
Definition Simplex_tree_iterators.h:36
Iterator over the simplices of the skeleton of a given dimension of the simplicial complex.
Definition Simplex_tree_iterators.h:369
Simplex Tree data structure for representing simplicial complexes.
Definition Simplex_tree.h:101
Simplex_tree(Simplex_tree &&complex_source)
User-defined move constructor relocates the whole tree structure including extra data (Simplex_data) ...
Definition Simplex_tree.h:499
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(Filtration_maintenance insertion_strategy, const InputVertexRange &n_simplex)
Inserts a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles,...
Definition Simplex_tree.h:1223
std::vector< size_t > num_simplices_by_dimension() const
Returns the number of simplices of each dimension in the simplex tree.
Definition Simplex_tree.h:864
void for_each_simplex(Fun &&fun) const
Definition Simplex_tree.h:2289
Options::Filtration_value Filtration_value
Definition Simplex_tree.h:108
Simplex_tree_boundary_opposite_vertex_simplex_iterator< Simplex_tree > Boundary_opposite_vertex_simplex_iterator
Definition Simplex_tree.h:301
int dimension(Simplex_handle sh) const
Returns the dimension of a simplex.
Definition Simplex_tree.h:885
Filtration_maintenance
List of insertion strategies for insert_simplex_and_subfaces, which takes a simplex and a filtration...
Definition Simplex_tree.h:1127
@ IGNORE_VALIDITY
If the simplex to insert:
Definition Simplex_tree.h:1167
@ INCREASE_NEW
Let be the filtration value given as argument. Inserts the simplex as follows:
Definition Simplex_tree.h:1152
@ LOWER_EXISTING
Let be the filtration value given as argument. Inserts the simplex as follows:
Definition Simplex_tree.h:1140
Siblings const * self_siblings(SimplexHandle sh) const
Definition Simplex_tree.h:1390
std::conditional< Options::link_nodes_by_label, Optimized_cofaces_simplex_filtered_range, std::vector< Simplex_handle > >::type Cofaces_simplex_range
Definition Simplex_tree.h:285
void initialize_filtration(bool ignore_infinite_values=false) const
Initializes the filtration cache, i.e. sorts the simplices according to their order in the filtration...
Definition Simplex_tree.h:1485
Complex_simplex_range complex_simplex_range() const
Returns a range over the simplices of the simplicial complex.
Definition Simplex_tree.h:347
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:2325
bool operator==(const Simplex_tree< OtherSimplexTreeOptions > &st2) const
Checks if two simplex trees are equal. Any extra data (Simplex_data) stored in the simplices are igno...
Definition Simplex_tree.h:691
Vertex_handle vertex_with_same_filtration(Simplex_handle sh) const
Returns a vertex of sh that has the same filtration value as sh if it exists, and null_vertex() other...
Definition Simplex_tree.h:2657
bool operator!=(const Simplex_tree< OtherSimplexTreeOptions > &st2) const
Checks if two simplex trees are different.
Definition Simplex_tree.h:700
bool prune_above_dimension(int dimension)
Remove all simplices of dimension greater than a given value.
Definition Simplex_tree.h:2438
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:811
Simplex_handle edge_with_same_filtration(Simplex_handle sh) const
Returns an edge of sh that has the same filtration value as sh if it exists, and null_simplex() other...
Definition Simplex_tree.h:2671
boost::iterator_range< Boundary_simplex_iterator > Boundary_simplex_range
Definition Simplex_tree.h:297
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh) const
Returns a range over the vertices of a simplex.
Definition Simplex_tree.h:407
Boundary_opposite_vertex_simplex_range boundary_opposite_vertex_simplex_range(SimplexHandle sh) const
Given a simplex, returns a range over the simplices of its boundary and their opposite vertices.
Definition Simplex_tree.h:445
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(const InputVertexRange &n_simplex, const Filtration_value &filtration=Filtration_value())
Inserts a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles,...
Definition Simplex_tree.h:1191
Simplex_tree_simplex_vertex_iterator< Simplex_tree > Simplex_vertex_iterator
Definition Simplex_tree.h:279
void clear()
Remove all the simplices, leaving an empty complex.
Definition Simplex_tree.h:2349
static Simplex_key key(Simplex_handle sh)
Definition Simplex_tree.h:746
Simplex_tree(const Simplex_tree< OtherSimplexTreeOptions > &complex_source, F &&translate_filtration_value)
Construct the simplex tree as the copy of a given simplex tree with eventually different template par...
Definition Simplex_tree.h:478
boost::iterator_range< Dimension_simplex_iterator > Dimension_simplex_range
Definition Simplex_tree.h:323
Cofaces_simplex_range star_simplex_range(const Simplex_handle simplex) const
Compute the star of a n simplex.
Definition Simplex_tree.h:1631
boost::transform_iterator< return_first, Dictionary_const_it > Complex_vertex_iterator
Definition Simplex_tree.h:273
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by the given simplex handle has children.
Definition Simplex_tree.h:912
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:2158
boost::iterator_range< Simplex_vertex_iterator > Simplex_vertex_range
Definition Simplex_tree.h:281
void reset_filtration(const Filtration_value &filtration, int min_dim=0)
This function resets the filtration value of all the simplices of dimension at least min_dim....
Definition Simplex_tree.h:2835
void remove_maximal_simplex(Simplex_handle sh)
Remove a maximal simplex.
Definition Simplex_tree.h:2519
std::pair< Filtration_value, Extended_simplex_type > decode_extended_filtration(const Filtration_value &f, const Extended_filtration_data &efd) const
Retrieve the original filtration value for a given simplex in the Simplex_tree. Since the computation...
Definition Simplex_tree.h:2559
void assign_key(Simplex_handle sh, Simplex_key key)
Definition Simplex_tree.h:1376
Options::Simplex_key Simplex_key
Definition Simplex_tree.h:112
Simplex_tree()
Constructs an empty simplex tree.
Definition Simplex_tree.h:455
boost::iterator_range< Complex_simplex_iterator > Complex_simplex_range
Definition Simplex_tree.h:309
Dictionary::const_iterator Simplex_handle
Definition Simplex_tree.h:212
Simplex_tree_boundary_simplex_iterator< Simplex_tree > Boundary_simplex_iterator
Definition Simplex_tree.h:295
void expansion(int max_dimension)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition Simplex_tree.h:1785
const Simplex_data & simplex_data(Simplex_handle sh) const
Returns the extra data stored in a simplex.
Definition Simplex_tree.h:803
Simplex_tree_siblings< Simplex_tree, Dictionary > Siblings
Definition Simplex_tree.h:139
void insert_batch_vertices(VertexRange const &vertices, const Filtration_value &filt=Filtration_value())
Inserts several vertices.
Definition Simplex_tree.h:1760
Siblings * self_siblings(SimplexHandle sh)
Definition Simplex_tree.h:1399
Simplex_tree_dimension_simplex_iterator< Simplex_tree > Dimension_simplex_iterator
Definition Simplex_tree.h:321
static const Filtration_value & filtration(Simplex_handle sh)
Definition Simplex_tree.h:765
std::pair< Simplex_handle, Simplex_handle > endpoints(Simplex_handle sh) const
Definition Simplex_tree.h:1383
bool is_empty() const
Returns whether the complex is empty.
Definition Simplex_tree.h:821
Simplex_tree_complex_simplex_iterator< Simplex_tree > Complex_simplex_iterator
Definition Simplex_tree.h:307
Simplex_handle simplex(Simplex_key idx) const
Returns the simplex that has index idx in the filtration.
Definition Simplex_tree.h:756
void print_hasse(std::ostream &os) const
Write the hasse diagram of the simplicial complex in os.
Definition Simplex_tree.h:2267
Simplex_data & simplex_data(Simplex_handle sh)
Returns the extra data stored in a simplex.
Definition Simplex_tree.h:796
std::vector< Simplex_handle > Filtration_simplex_range
Definition Simplex_tree.h:325
Filtration_simplex_range::const_iterator Filtration_simplex_iterator
Definition Simplex_tree.h:329
int dimension() const
Returns the dimension of the simplicial complex.
Definition Simplex_tree.h:903
Extended_filtration_data extend_filtration()
Extend filtration for computing extended persistence. This function only uses the filtration values a...
Definition Simplex_tree.h:2597
void maybe_initialize_filtration() const
Initializes the filtration cache if it isn't initialized yet.
Definition Simplex_tree.h:1552
Options::Vertex_handle Vertex_handle
Definition Simplex_tree.h:122
Skeleton_simplex_range skeleton_simplex_range(int dim) const
Returns a range over the simplices of the dim-skeleton of the simplicial complex.
Definition Simplex_tree.h:361
Filtration_simplex_range const & filtration_simplex_range(Indexing_tag=Indexing_tag()) const
Returns a range over the simplices of the simplicial complex, in the order of the filtration.
Definition Simplex_tree.h:396
bool prune_above_filtration(const Filtration_value &filtration)
Prune above filtration value given as parameter. That is: if is the given filtration value and the ...
Definition Simplex_tree.h:2369
size_t num_vertices() const
Returns the number of vertices in the complex.
Definition Simplex_tree.h:816
Cofaces_simplex_range cofaces_simplex_range(const Simplex_handle simplex, int codimension) const
Compute the cofaces of a n simplex.
Definition Simplex_tree.h:1645
void clear_filtration() const
Clears the filtration cache produced by initialize_filtration().
Definition Simplex_tree.h:1564
Dimension_simplex_range dimension_simplex_range(int dim) const
Returns a range over the simplices of the simplicial complex that match the dimension specified by th...
Definition Simplex_tree.h:373
Simplex_tree(const Simplex_tree &complex_source)
User-defined copy constructor reproduces the whole tree structure including extra data (Simplex_data)...
Definition Simplex_tree.h:488
size_t num_simplices() const
Returns the number of simplices in the simplex_tree.
Definition Simplex_tree.h:829
boost::iterator_range< Complex_vertex_iterator > Complex_vertex_range
Definition Simplex_tree.h:275
static Simplex_key null_key()
Returns a fixed number not in the interval [0, num_simplices()).
Definition Simplex_tree.h:791
Simplex_handle minimal_simplex_with_same_filtration(Simplex_handle sh) const
Returns a minimal face of sh that has the same filtration value as sh.
Definition Simplex_tree.h:2704
void assign_filtration(Simplex_handle sh, const Filtration_value &fv)
Definition Simplex_tree.h:776
int upper_bound_dimension() const
Returns an upper bound on the dimension of the simplicial complex.
Definition Simplex_tree.h:893
Siblings * root()
Definition Simplex_tree.h:1407
Simplex_tree_skeleton_simplex_iterator< Simplex_tree > Skeleton_simplex_iterator
Definition Simplex_tree.h:314
Simplex_tree & operator=(Simplex_tree &&complex_source)
User-defined move assignment relocates the whole tree structure including extra data (Simplex_data) s...
Definition Simplex_tree.h:532
const Siblings * root() const
Definition Simplex_tree.h:1410
boost::iterator_range< Boundary_opposite_vertex_simplex_iterator > Boundary_opposite_vertex_simplex_range
Definition Simplex_tree.h:303
void insert_edge_as_flag(Vertex_handle u, Vertex_handle v, const Filtration_value &fil, int dim_max, std::vector< Simplex_handle > &added_simplices)
Adds a new vertex or a new edge in a flag complex, as well as all simplices of its star,...
Definition Simplex_tree.h:1832
Simplex_tree & operator=(const Simplex_tree &complex_source)
User-defined copy assignment reproduces the whole tree structure including extra data (Simplex_data) ...
Definition Simplex_tree.h:514
Complex_vertex_range complex_vertex_range() const
Returns a range over the vertices of the simplicial complex. The order is increasing according to < o...
Definition Simplex_tree.h:337
void insert_graph(const OneSkeletonGraph &skel_graph)
Inserts a 1-skeleton in an empty Simplex_tree.
Definition Simplex_tree.h:1699
Get_simplex_data_type< Options >::type Simplex_data
Definition Simplex_tree.h:118
Simplex_handle find(const InputVertexRange &s) const
Given a range of Vertex_handles, returns the Simplex_handle of the simplex in the simplicial complex ...
Definition Simplex_tree.h:937
std::pair< Simplex_handle, bool > insert_simplex(const InputVertexRange &simplex, const Filtration_value &filtration=Filtration_value())
Insert a simplex, represented by a range of Vertex_handles, in the simplicial complex.
Definition Simplex_tree.h:1109
boost::iterator_range< Skeleton_simplex_iterator > Skeleton_simplex_range
Definition Simplex_tree.h:317
~Simplex_tree()
Destructor; deallocates the whole tree structure.
Definition Simplex_tree.h:507
void initialize_filtration(Comparator &&is_before_in_filtration, Ignorer &&ignore_simplex) const
Initializes the filtration cache, i.e. sorts the simplices according to the specified order....
Definition Simplex_tree.h:1531
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:786
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(Filtration_maintenance insertion_strategy, const InputVertexRange &n_simplex, const Filtration_value &filtration)
Insert a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles,...
Definition Simplex_tree.h:1264
Boundary_simplex_range boundary_simplex_range(SimplexHandle sh) const
Returns a range over the simplices of the boundary of a simplex.
Definition Simplex_tree.h:428
void set_dimension(int dimension, bool exact=true)
Set a dimension for the simplicial complex.
Definition Simplex_tree.h:1420
Graph simplicial complex methods.
bool is_positive_infinity(const Arithmetic_filtration_value &f)
Returns true if and only if the given filtration value is at infinity. This is the overload for when ...
Definition filtration_value_utils.h:43
bool unify_lifetimes(Arithmetic_filtration_value &f1, const Arithmetic_filtration_value &f2)
Given two filtration values at which a simplex exists, stores in the first value the minimal union of...
Definition filtration_value_utils.h:62
bool intersect_lifetimes(Arithmetic_filtration_value &f1, const Arithmetic_filtration_value &f2)
Given two filtration values, stores in the first value the lowest common upper bound of the two value...
Definition filtration_value_utils.h:79
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14
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
This file includes common file reader for GUDHI.
No hook when SimplexTreeOptions::link_nodes_by_label is false.
Definition hooks_simplex_base.h:20
Node of a simplex tree with filtration value and simplex key.
Definition Simplex_tree_node_explicit_storage.h:40
Concept of the template parameter for the class Gudhi::Simplex_tree<SimplexTreeOptions>.
Definition SimplexTreeOptions.h:17
static const bool store_key
If true, each simplex has extra storage for one Simplex_key. Necessary for Persistent_cohomology.
Definition SimplexTreeOptions.h:29
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:34
static const bool link_nodes_by_label
If true, the lists of Node with same label are stored to enhance cofaces and stars access.
Definition SimplexTreeOptions.h:39
IndexingTag Indexing_tag
Forced for now.
Definition SimplexTreeOptions.h:19
static const bool stable_simplex_handles
If true, Simplex_handle will not be invalidated after insertions or removals.
Definition SimplexTreeOptions.h:41
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:37