From 64219f581d1113aa8f763fdd3e9096c77ecadeed Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Thu, 22 Aug 2019 12:36:03 +0200 Subject: [PATCH] ADD: inserting and removing children --- include/Tree/tree.hpp | 18 +++++++++++++++++- src/Tree/tree.cpp | 16 +++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index 76ffc2a..4d0fbd4 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -149,6 +149,13 @@ namespace Tree */ const unsigned int getNrChildren() const; + /** + * @brief get the iterator of the given child + * + * @param child - the childnode to find the iterator for + * @return - the const iterator for the given child + */ + std::list>::const_iterator getChildIterator(const std::shared_ptr child) const; /** * @brief deletes all the children and their children and ... from the tree @@ -172,7 +179,7 @@ namespace Tree * @param c - the node to remove from the nodes list of children * */ - void removeChild(const std::shared_ptr &c); + void removeChild(const std::shared_ptr &c); /** * @brief adds a child to the node which is already a shared_ptr @@ -214,6 +221,15 @@ namespace Tree template void prependChildren(Iter begin, Iter end) { std::copy( begin, end, std::front_inserter( children ) );} + /** + * @brief inserts a child before the given iterator which is already a shared_ptr + * + * @param c - a shared_ptr to a BaseNode + * @param it - the iterator before which to insert the node + */ + void insertChild(const std::shared_ptr &c, std::list>::const_iterator it) {c->setParent(shared_from_this());children.insert(it,c);} + + /** * @brief finds the next node in the tree of a given type * diff --git a/src/Tree/tree.cpp b/src/Tree/tree.cpp index 0ab8a05..ffbb9cc 100644 --- a/src/Tree/tree.cpp +++ b/src/Tree/tree.cpp @@ -19,6 +19,20 @@ along with this program. If not, see . #include #include +std::list>::const_iterator Tree::BaseNode::getChildIterator(const std::shared_ptr child) const +{ + std::list>::const_iterator childIT; + + for(childIT=children.begin(); childIT != children.end(); ++childIT) + { + if (*childIT == child) + { + return childIT; + } + } + + throw std::runtime_error("child does not exist"); +} std::shared_ptr Tree::BaseNode::findNext(const std::string &t) const { @@ -82,7 +96,7 @@ void Tree::BaseNode::printTree(const uint32_t depth) } -void Tree::BaseNode::removeChild(const std::shared_ptr &c) +void Tree::BaseNode::removeChild(const std::shared_ptr &c) { std::list>::iterator it; it=std::find(children.begin(), children.end(),c);