Skip to content

Commit

Permalink
Allowing storing custom data in simplices
Browse files Browse the repository at this point in the history
  • Loading branch information
mglisse committed Jul 4, 2024
1 parent 18a842d commit 6543043
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Simplex_tree/concept/SimplexTreeOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct SimplexTreeOptions {
typedef FiltrationValue Filtration_value;
/** @brief Must be an integer type. */
typedef SimplexKey Simplex_key;
/** @brief Optional, can be omitted. */
typedef SimplexData Simplex_data;
/** @brief If true, each simplex has extra storage for one `Simplex_key`. Necessary for `Persistent_cohomology`. */
static const bool store_key;
/** @brief If true, each simplex has extra storage for one `Filtration_value`, and this value is propagated by
Expand Down
9 changes: 9 additions & 0 deletions src/Simplex_tree/include/gudhi/Simplex_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Simplex_tree {
*
* Must be an integer type. */
typedef typename Options::Simplex_key Simplex_key;
/** \bried Extra data stored in each simplex. */
typedef typename Get_simplex_data_type<Options>::type Simplex_data;
/** \brief Type for the vertex handle.
*
* Must be a signed integer type. It admits a total order <. */
Expand Down Expand Up @@ -641,6 +643,13 @@ class Simplex_tree {
return -1;
}

/** \brief Returns the extra data stored in a simplex. */
static Simplex_data& simplex_data(Simplex_handle sh) {
GUDHI_CHECK(sh != null_simplex(),
std::invalid_argument("Simplex_tree::simplex_data - no data associated to null_simplex"));
return sh->second.data();
}

/** \brief Returns a Vertex_handle different from all Vertex_handles associated
* to the vertices of the simplicial complex. */
Vertex_handle null_vertex() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#endif

#include <vector>
#include <boost/core/empty_value.hpp>

namespace Gudhi {

Expand All @@ -35,10 +36,12 @@ namespace Gudhi {
template<class SimplexTree>
struct GUDHI_EMPTY_BASE_CLASS_OPTIMIZATION Simplex_tree_node_explicit_storage : SimplexTree::Filtration_simplex_base,
SimplexTree::Key_simplex_base,
SimplexTree::Hooks_simplex_base {
SimplexTree::Hooks_simplex_base,
boost::empty_value<typename SimplexTree::Simplex_data> {
typedef typename SimplexTree::Siblings Siblings;
typedef typename SimplexTree::Filtration_value Filtration_value;
typedef typename SimplexTree::Simplex_key Simplex_key;
typedef typename SimplexTree::Simplex_data Simplex_data;

Simplex_tree_node_explicit_storage(Siblings * sib = nullptr,
Filtration_value filtration = 0)
Expand All @@ -58,6 +61,8 @@ struct GUDHI_EMPTY_BASE_CLASS_OPTIMIZATION Simplex_tree_node_explicit_storage :
return children_;
}

Simplex_data& data() { return boost::empty_value<Simplex_data>::get(); }

private:
Siblings * children_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <gudhi/Simplex_tree/indexing_tag.h>

#include <cstdint>
#include <type_traits> // void_t

namespace Gudhi {

Expand Down Expand Up @@ -91,6 +92,13 @@ struct Simplex_tree_options_fast_persistence {

/** @}*/ // end addtogroup simplex_tree

struct No_simplex_data {};

// Nested `type` is O::Simplex_data if that exists, No_simplex_data otherwise
template <class, class=void> struct Get_simplex_data_type { typedef No_simplex_data type; };
template <class O>
struct Get_simplex_data_type<O, std::void_t<typename O::Simplex_data>> { typedef typename O::Simplex_data type; };

} // namespace Gudhi

#endif // SIMPLEX_TREE_SIMPLEX_TREE_OPTIONS_H_
15 changes: 15 additions & 0 deletions src/Simplex_tree/test/simplex_tree_unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,3 +1241,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(for_each_simplex_skip_iteration, typeST, list_of_t
BOOST_CHECK(num_simplices_by_dim_until_two[0] == num_simplices_by_dim[0]);
BOOST_CHECK(num_simplices_by_dim_until_two[1] == num_simplices_by_dim[1]);
}

struct Options_with_int_data : Simplex_tree_options_minimal {
typedef int Simplex_data;
};

BOOST_AUTO_TEST_CASE(simplex_data) {
Simplex_tree<Options_with_int_data> st;
st.insert_simplex_and_subfaces({0, 1});
st.insert_simplex_and_subfaces({2, 1});
st.insert_simplex_and_subfaces({0, 2});
st.simplex_data(st.find({0, 1})) = 5;
st.expansion(3);
st.simplex_data(st.find({0, 1, 2})) = 4;
BOOST_CHECK(st.simplex_data(st.find({0, 1})) == 5);
}

0 comments on commit 6543043

Please sign in to comment.