From 22739d8ac435f534ab94fd9cdf83cc4c39598bfb Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Thu, 4 Apr 2019 23:27:58 +0200 Subject: [PATCH 1/4] ADD: added method to prepend a child --- include/Tree/tree.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index a97a221..a6fd400 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -170,6 +170,14 @@ namespace Tree */ void addChild(const std::shared_ptr &c) {c->setParent(shared_from_this()); children.push_back(c); } + + /** + * @brief prepends a child to the node which is already a shared_ptr + * + * @param c - a shared_ptr to a BaseNode + */ + void prependChild(const std::shared_ptr &c) {c->setParent(shared_from_this()); children.push_front(c); } + /** * @brief adds a container of nodes to the list of children * From 2850647c665df4795ee9eae3c9899f7e3b7e814d Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sun, 7 Apr 2019 16:32:56 +0200 Subject: [PATCH 2/4] FIX: check if child is not nullptr on adding --- include/Tree/tree.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index a6fd400..16171b2 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -168,7 +168,13 @@ namespace Tree * * @param c - a shared_ptr to a BaseNode */ - void addChild(const std::shared_ptr &c) {c->setParent(shared_from_this()); children.push_back(c); } + void addChild(const std::shared_ptr &c) { + if (c!=nullptr) + { + c->setParent(shared_from_this()); + children.push_back(c); + } + } /** From cf58516dafde24e3cb3c9308e600e594c393a710 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Wed, 10 Apr 2019 09:52:46 +0200 Subject: [PATCH 3/4] ADD: method to prepend a child --- include/Tree/tree.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index 16171b2..e373f44 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -194,6 +194,15 @@ namespace Tree void addChildren(Iter begin, Iter end) { std::copy( begin, end, std::back_inserter( children ) );} + /** + * @brief prepends a container of nodes to the list of children + * + * @param begin - iterator to the beginning of the container of objects of type std::shared_ptr + * @param end - iterator to the end of the container of objects of type std::shared_ptr + */ + template + void prependChildren(Iter begin, Iter end) { std::copy( begin, end, std::front_inserter( children ) );} + /** * @brief finds the next node in the tree of a given type * From 6d55169a5f9708933cf4f31ff6f2e3fa522e1586 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Wed, 10 Apr 2019 09:53:08 +0200 Subject: [PATCH 4/4] ADD: method to search for all nodes with a given base --- include/Tree/tree.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index e373f44..9f8bfb5 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -225,12 +225,25 @@ namespace Tree /** * @brief finds all nodes of a given base * - * @param base - the base type of the node as a string + * @param b - the base type of the node as a string * @param c - a container for holding all found nodes, has to be holding objects of type std::shared_ptr */ - template - void findAllBase(const std::string &base, Container &c); + template + void findAllBase(const std::string &b, Container &c) + { + std::list>::iterator it; + if (base() == b) + { + c.push_back(shared_from_this()); + } + + for(it = children.begin(); it != children.end(); ++it) + { + (*it)->findAllBase(b,c); + } + + } /** * @brief checks if the given node is a direct child of this node