From f7090b7ea2f54eb69f31b0199cf4d2dcf5382668 Mon Sep 17 00:00:00 2001 From: anmoreau Date: Wed, 24 Jun 2015 09:56:01 +0000 Subject: Copy - Move - print_tree - main.cpp (building a simple Simplex_tree now works) git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/copy_move@639 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 24b8cdf7b1787ceeb415bbb978d69fdb2a1fe407 --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 78 +++++++++++++++++++++++- src/Simplex_tree/test/simplex_tree_unit_test.cpp | 18 ++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index b79e3c8f..7307085e 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -26,11 +26,13 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -289,6 +291,55 @@ class Simplex_tree { dimension_(-1) { } + /** \brief Copy; copy the whole tree structure. */ + Simplex_tree(Simplex_tree& copy) : root_(NULL, -1) + { + null_vertex_ = copy.null_vertex_; + threshold_ = copy.threshold_; + num_simplices_ = copy.num_simplices_; + filtration_vect_ = copy.filtration_vect_; + dimension_ = copy.dimension_; + std::vector v; + for (auto sh = copy.root_.members().begin(); sh != copy.root_.members().end(); ++sh) + { + v.push_back(sh->first); + if (has_children(sh)) { + rec_copy(sh->second.children(), v); + } + else { + insert_simplex(v, sh->second.filtration()); + } + v.pop_back(); + } + } + + /** rec_copy: DFS, inserts simplices when reaching a leaf.*/ + void rec_copy(Siblings * sib, std::vector v) + { + for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) + { + v.push_back(sh->first); + if (has_children(sh)) { + rec_copy(sh->second.children(), v); + } + else { + insert_simplex(v, sh->second.filtration()); + } + v.pop_back(); + } + } + + + /** \brief Move; moves the whole tree structure. */ + Simplex_tree(Simplex_tree&& old) : null_vertex_(std::move(old.null_vertex_)), threshold_(std::move(old.threshold_)), filtration_vect_(std::move(old.filtration_vect_)) + { + dimension_ = std::move(old.dimension_); + num_simplices_ = std::move(old.num_simplices_); + old.dimension_ = -1; + old.num_simplices_ = 0; + root_ = std::move(old.root_); + } + /** \brief Destructor; deallocates the whole tree structure. */ ~Simplex_tree() { for (auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) { @@ -309,12 +360,37 @@ class Simplex_tree { delete sib; } + public: + /** \brief print_tree: prints the tree in a hierarchical manner. */ + void print_tree() + { + for (auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) + { + std::cout << sh->first << " "; + if (has_children(sh)) + rec_print(sh->second.children()); + std::cout << std::endl; + } + } + + private: + /** rec_print: prints the tree recursively, using DFS. */ + void rec_print(Siblings * sib) + { + for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) + { + std::cout << sh->first << " "; + if (has_children(sh)) + rec_print(sh->second.children()); + } + } + public: /** \brief Returns the key associated to a simplex. * * The filtration must be initialized. */ Simplex_key key(Simplex_handle sh) { - return sh->second.key(); + return sh->second.key(); } /** \brief Returns the simplex associated to a key. * diff --git a/src/Simplex_tree/test/simplex_tree_unit_test.cpp b/src/Simplex_tree/test/simplex_tree_unit_test.cpp index 6b0a1f3d..fdd1a5be 100644 --- a/src/Simplex_tree/test/simplex_tree_unit_test.cpp +++ b/src/Simplex_tree/test/simplex_tree_unit_test.cpp @@ -587,4 +587,22 @@ BOOST_AUTO_TEST_CASE( NSimplexAndSubfaces_tree_insertion ) std::cout << std::endl; } + // TEST Copy constructor / Move + std::cout << "Printing st" << std::endl; + std::cout << &st << std::endl; + std::cout << st; // Vertices test + st.print_tree(); // Hierarchy test + typeST st3 = st, st_move = std::move(st); + std::cout << "Printing a copy of st" << std::endl; + std::cout << &st3 << std::endl; + std::cout << st3; + st3.print_tree(); + std::cout << "Printing a move of st" << std::endl; + std::cout << &st_move << std::endl; + std::cout << st_move; + st_move.print_tree(); + std::cout << "Printing st again" << std::endl; + std::cout << &st << std::endl; + std::cout << st; + st.print_tree(); } -- cgit v1.2.3