ADD: inserting and removing children

This commit is contained in:
Dominik Meyer 2019-08-22 12:36:03 +02:00
parent 75e6d2007b
commit 64219f581d
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
2 changed files with 32 additions and 2 deletions

View File

@ -149,6 +149,13 @@ namespace Tree
*/ */
const unsigned int getNrChildren() const; 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<std::shared_ptr<Tree::BaseNode>>::const_iterator getChildIterator(const std::shared_ptr<const Tree::BaseNode> child) const;
/** /**
* @brief deletes all the children and their children and ... from the tree * @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 * @param c - the node to remove from the nodes list of children
* *
*/ */
void removeChild(const std::shared_ptr<Tree::BaseNode> &c); void removeChild(const std::shared_ptr<const Tree::BaseNode> &c);
/** /**
* @brief adds a child to the node which is already a shared_ptr * @brief adds a child to the node which is already a shared_ptr
@ -214,6 +221,15 @@ namespace Tree
template <typename Iter> template <typename Iter>
void prependChildren(Iter begin, Iter end) { std::copy( begin, end, std::front_inserter( children ) );} 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<Tree::BaseNode> &c, std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator it) {c->setParent(shared_from_this());children.insert(it,c);}
/** /**
* @brief finds the next node in the tree of a given type * @brief finds the next node in the tree of a given type
* *

View File

@ -19,6 +19,20 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iomanip> #include <iomanip>
#include <Tree/tree.hpp> #include <Tree/tree.hpp>
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator Tree::BaseNode::getChildIterator(const std::shared_ptr<const Tree::BaseNode> child) const
{
std::list<std::shared_ptr<Tree::BaseNode>>::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<const Tree::BaseNode> Tree::BaseNode::findNext(const std::string &t) const std::shared_ptr<const Tree::BaseNode> 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<Tree::BaseNode> &c) void Tree::BaseNode::removeChild(const std::shared_ptr<const Tree::BaseNode> &c)
{ {
std::list<std::shared_ptr<Tree::BaseNode>>::iterator it; std::list<std::shared_ptr<Tree::BaseNode>>::iterator it;
it=std::find(children.begin(), children.end(),c); it=std::find(children.begin(), children.end(),c);