summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorROUVREAU Vincent <vincent.rouvreau@inria.fr>2020-08-12 13:06:03 +0200
committerROUVREAU Vincent <vincent.rouvreau@inria.fr>2020-08-12 13:06:03 +0200
commit458bc2dcf5044e1d5fde5326b2be35e526abd457 (patch)
tree9e1cca62b825421feec3835f1b548e0ef8dafad8
parent8f9c065df7f4629555ef09292c14c293891f1bdc (diff)
code review: boundaries uses only once find and return a pair of iterator. Exception is raised when not found. tested
-rw-r--r--src/python/gudhi/simplex_tree.pxd3
-rw-r--r--src/python/gudhi/simplex_tree.pyx14
-rw-r--r--src/python/include/Simplex_tree_interface.h12
-rwxr-xr-xsrc/python/test/test_simplex_tree.py9
4 files changed, 23 insertions, 15 deletions
diff --git a/src/python/gudhi/simplex_tree.pxd b/src/python/gudhi/simplex_tree.pxd
index a64599e7..e1b03391 100644
--- a/src/python/gudhi/simplex_tree.pxd
+++ b/src/python/gudhi/simplex_tree.pxd
@@ -71,8 +71,7 @@ cdef extern from "Simplex_tree_interface.h" namespace "Gudhi":
vector[Simplex_tree_simplex_handle].const_iterator get_filtration_iterator_end() nogil
Simplex_tree_skeleton_iterator get_skeleton_iterator_begin(int dimension) nogil
Simplex_tree_skeleton_iterator get_skeleton_iterator_end(int dimension) nogil
- Simplex_tree_boundary_iterator get_boundary_iterator_begin(vector[int] simplex) nogil
- Simplex_tree_boundary_iterator get_boundary_iterator_end(vector[int] simplex) nogil
+ pair[Simplex_tree_boundary_iterator, Simplex_tree_boundary_iterator] get_boundary_iterators(vector[int] simplex) nogil except +
cdef extern from "Persistent_cohomology_interface.h" namespace "Gudhi":
cdef cppclass Simplex_tree_persistence_interface "Gudhi::Persistent_cohomology_interface<Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_full_featured>>":
diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx
index 3ebae923..bc5b43f4 100644
--- a/src/python/gudhi/simplex_tree.pyx
+++ b/src/python/gudhi/simplex_tree.pyx
@@ -293,12 +293,14 @@ cdef class SimplexTree:
:returns: The (simplices of the) boundary of a simplex
:rtype: generator with tuples(simplex, filtration)
"""
- cdef Simplex_tree_boundary_iterator it = self.get_ptr().get_boundary_iterator_begin(simplex)
- cdef Simplex_tree_boundary_iterator end = self.get_ptr().get_boundary_iterator_end(simplex)
-
- while it != end:
- yield self.get_ptr().get_simplex_and_filtration(dereference(it))
- preincrement(it)
+ cdef pair[Simplex_tree_boundary_iterator, Simplex_tree_boundary_iterator] it = self.get_ptr().get_boundary_iterators(simplex)
+
+ try:
+ while it.first != it.second:
+ yield self.get_ptr().get_simplex_and_filtration(dereference(it.first))
+ preincrement(it.first)
+ except RuntimeError:
+ print("simplex not found - cannot find boundaries")
def remove_maximal_simplex(self, simplex):
diff --git a/src/python/include/Simplex_tree_interface.h b/src/python/include/Simplex_tree_interface.h
index c4f18eeb..2c695d1b 100644
--- a/src/python/include/Simplex_tree_interface.h
+++ b/src/python/include/Simplex_tree_interface.h
@@ -190,14 +190,12 @@ class Simplex_tree_interface : public Simplex_tree<SimplexTreeOptions> {
return Base::skeleton_simplex_range(dimension).end();
}
- Boundary_simplex_iterator get_boundary_iterator_begin(const Simplex& simplex) {
+ std::pair<Boundary_simplex_iterator, Boundary_simplex_iterator> get_boundary_iterators(const Simplex& simplex) {
+ auto bd_sh = Base::find(simplex);
+ if (bd_sh == Base::null_simplex())
+ throw std::runtime_error("simplex not found - cannot find boundaries");
// this specific case works because the range is just a pair of iterators - won't work if range was a vector
- return Base::boundary_simplex_range(Base::find(simplex)).begin();
- }
-
- Boundary_simplex_iterator get_boundary_iterator_end(const Simplex& simplex) {
- // this specific case works because the range is just a pair of iterators - won't work if range was a vector
- return Base::boundary_simplex_range(Base::find(simplex)).end();
+ return std::make_pair(Base::boundary_simplex_range(bd_sh).begin(), Base::boundary_simplex_range(bd_sh).end());
}
};
diff --git a/src/python/test/test_simplex_tree.py b/src/python/test/test_simplex_tree.py
index 036e88df..828400fb 100755
--- a/src/python/test/test_simplex_tree.py
+++ b/src/python/test/test_simplex_tree.py
@@ -350,3 +350,12 @@ def test_boundaries_iterator():
assert list(st.get_boundaries([1, 2, 3])) == [([1, 2], 1.0), ([1, 3], 1.0), ([2, 3], 1.0)]
assert list(st.get_boundaries([2, 3, 4])) == [([2, 3], 1.0), ([2, 4], 2.0), ([3, 4], 2.0)]
assert list(st.get_boundaries([2])) == []
+
+ with pytest.raises(RuntimeError):
+ list(st.get_boundaries([]))
+
+ with pytest.raises(RuntimeError):
+ list(st.get_boundaries([0, 4])) # (0, 4) does not exist
+
+ with pytest.raises(RuntimeError):
+ list(st.get_boundaries([6])) # (6) does not exist