From fb8ce008feadcaf6a936740a3ed54d50970c731c Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Fri, 11 Feb 2022 23:11:26 +0100 Subject: __copy__, __deepcopy__, copy, and copy ctors. Still pb with the doc --- .github/next_release.md | 3 +++ 1 file changed, 3 insertions(+) (limited to '.github') diff --git a/.github/next_release.md b/.github/next_release.md index e21b25c7..3946404b 100644 --- a/.github/next_release.md +++ b/.github/next_release.md @@ -13,6 +13,9 @@ Below is a list of changes made since GUDHI 3.5.0: - [Representations](https://gudhi.inria.fr/python/latest/representations.html#gudhi.representations.vector_methods.BettiCurve) - A more flexible Betti curve class capable of computing exact curves +- [Simplex tree](https://gudhi.inria.fr/python/latest/simplex_tree_ref.html) + - `__copy__`, `__deepcopy__`, `copy` and copy constructors + - Installation - Boost ≥ 1.66.0 is now required (was ≥ 1.56.0). -- cgit v1.2.3 From 43981a4d487669fe2002337ab62b72dd9e83a64a Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Mon, 14 Feb 2022 11:08:00 +0100 Subject: Remove shallow copy --- .github/next_release.md | 2 +- src/python/gudhi/simplex_tree.pyx | 54 +++++++++++++----------------------- src/python/test/test_simplex_tree.py | 50 ++------------------------------- 3 files changed, 23 insertions(+), 83 deletions(-) (limited to '.github') diff --git a/.github/next_release.md b/.github/next_release.md index 3946404b..3d4761eb 100644 --- a/.github/next_release.md +++ b/.github/next_release.md @@ -14,7 +14,7 @@ Below is a list of changes made since GUDHI 3.5.0: - A more flexible Betti curve class capable of computing exact curves - [Simplex tree](https://gudhi.inria.fr/python/latest/simplex_tree_ref.html) - - `__copy__`, `__deepcopy__`, `copy` and copy constructors + - `__deepcopy__`, `copy` and copy constructors - Installation - Boost ≥ 1.66.0 is now required (was ≥ 1.56.0). diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx index ed7c3b92..0213e363 100644 --- a/src/python/gudhi/simplex_tree.pyx +++ b/src/python/gudhi/simplex_tree.pyx @@ -30,7 +30,6 @@ cdef class SimplexTree: # unfortunately 'cdef public Simplex_tree_interface_full_featured* thisptr' is not possible # Use intptr_t instead to cast the pointer cdef public intptr_t thisptr - cdef bool __thisptr_to_be_deleted # Get the pointer casted as it should be cdef Simplex_tree_interface_full_featured* get_ptr(self) nogil: @@ -39,36 +38,32 @@ cdef class SimplexTree: cdef Simplex_tree_persistence_interface * pcohptr # Fake constructor that does nothing but documenting the constructor - def __init__(self, other = None, copy = True): + def __init__(self, other = None): """SimplexTree constructor. - :param other: If `other` is a SimplexTree (default = None), the SimplexTree is constructed from a deep/shallow copy of `other`. + + :param other: If `other` is a `None` (default value), an empty `SimplexTree` is created. + If `other` is a `SimplexTree`, the `SimplexTree` is constructed from a deep copy of `other`. :type other: SimplexTree - :param copy: If `True`, the copy will be deep and if `False, the copy will be shallow. Default is `True`. - :type copy: bool - :returns: A simplex tree that is a (deep or shallow) copy of itself. + :returns: An empty or a copy simplex tree. :rtype: SimplexTree - :note: copy constructor requires :func:`compute_persistence` to be launched again as the result is not copied. + + :note: If the `SimplexTree` is a copy, it requires :func:`compute_persistence` to be launched again as the + persistence result is not copied. """ # The real cython constructor - def __cinit__(self, other = None, copy = True): + def __cinit__(self, other = None): cdef SimplexTree ostr if other and type(other) is SimplexTree: ostr = other - if copy: - self.thisptr = (new Simplex_tree_interface_full_featured(dereference(ostr.get_ptr()))) - else: - self.thisptr = ostr.thisptr - # Avoid double free - The original is in charge of deletion - self.__thisptr_to_be_deleted = False + self.thisptr = (new Simplex_tree_interface_full_featured(dereference(ostr.get_ptr()))) else: - self.__thisptr_to_be_deleted = True self.thisptr = (new Simplex_tree_interface_full_featured()) def __dealloc__(self): cdef Simplex_tree_interface_full_featured* ptr = self.get_ptr() # Avoid double free - The original is in charge of deletion - if ptr != NULL and self.__thisptr_to_be_deleted: + if ptr != NULL: del ptr if self.pcohptr != NULL: del self.pcohptr @@ -83,33 +78,24 @@ cdef class SimplexTree: """ return self.pcohptr != NULL - def copy(self, deep=True): + def copy(self): """ - :param deep: If `True`, the copy will be deep and if `False`, the copy will be shallow. Default is `True`. - :type deep: bool - :returns: A simplex tree that is a (deep or shallow) copy of itself. + :returns: A simplex tree that is a deep copy of itself. :rtype: SimplexTree - :note: copy requires :func:`compute_persistence` to be launched again as the result is not copied. + + :note: copy requires :func:`compute_persistence` to be launched again as the persistence result is not copied. """ stree = SimplexTree() cdef Simplex_tree_interface_full_featured* stree_ptr cdef Simplex_tree_interface_full_featured* self_ptr=self.get_ptr() - if deep: - with nogil: - stree_ptr = new Simplex_tree_interface_full_featured(dereference(self_ptr)) - - stree.thisptr = (stree_ptr) - else: - stree.thisptr = self.thisptr - # Avoid double free - The original is in charge of deletion - stree.__thisptr_to_be_deleted = False - return stree + with nogil: + stree_ptr = new Simplex_tree_interface_full_featured(dereference(self_ptr)) - def __copy__(self): - return self.copy(deep=False) + stree.thisptr = (stree_ptr) + return stree def __deepcopy__(self): - return self.copy(deep=True) + return self.copy() def filtration(self, simplex): """This function returns the filtration value for a given N-simplex in diff --git a/src/python/test/test_simplex_tree.py b/src/python/test/test_simplex_tree.py index 6db6d8fb..62dcc865 100755 --- a/src/python/test/test_simplex_tree.py +++ b/src/python/test/test_simplex_tree.py @@ -454,7 +454,7 @@ def test_simplex_tree_deep_copy(): # persistence is not copied st.compute_persistence() - st_copy = st.copy(deep=True) + st_copy = st.copy() # TODO(VR): when #463 is merged, replace with # assert st_copy == st assert st_copy.num_vertices() == st.num_vertices() @@ -476,36 +476,13 @@ def test_simplex_tree_deep_copy(): del st del st_copy -def test_simplex_tree_shallow_copy(): - st = SimplexTree() - st.insert([1, 2, 3], 0.) - # persistence is not copied - st.compute_persistence() - - st_copy = st.copy(deep=False) - # TODO(VR): when #463 is merged, replace with - # assert st_copy == st - assert st_copy.num_vertices() == st.num_vertices() - assert st_copy.num_simplices() == st.num_simplices() - assert list(st_copy.get_filtration()) == list(st.get_filtration()) - - assert st.__is_persistence_defined() == True - assert st_copy.__is_persistence_defined() == False - - st_copy.assign_filtration([1, 2, 3], 2.) - assert list(st_copy.get_filtration()) == list(st.get_filtration()) - - # test double free - del st - del st_copy - def test_simplex_tree_deep_copy_constructor(): st = SimplexTree() st.insert([1, 2, 3], 0.) # persistence is not copied st.compute_persistence() - st_copy = SimplexTree(st, copy = True) + st_copy = SimplexTree(st) # TODO(VR): when #463 is merged, replace with # assert st_copy == st assert st_copy.num_vertices() == st.num_vertices() @@ -526,26 +503,3 @@ def test_simplex_tree_deep_copy_constructor(): # test double free del st del st_copy - -def test_simplex_tree_shallow_copy(): - st = SimplexTree() - st.insert([1, 2, 3], 0.) - # persistence is not copied - st.compute_persistence() - - st_copy = SimplexTree(st, copy = False) - # TODO(VR): when #463 is merged, replace with - # assert st_copy == st - assert st_copy.num_vertices() == st.num_vertices() - assert st_copy.num_simplices() == st.num_simplices() - assert list(st_copy.get_filtration()) == list(st.get_filtration()) - - assert st.__is_persistence_defined() == True - assert st_copy.__is_persistence_defined() == False - - st_copy.assign_filtration([1, 2, 3], 2.) - assert list(st_copy.get_filtration()) == list(st.get_filtration()) - - # test double free - del st - del st_copy -- cgit v1.2.3