FIX: some fixes

This commit is contained in:
Dominik Meyer 2023-11-08 15:30:20 +01:00
parent 343e880558
commit d178ccb9a3
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97

View File

@ -18,6 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <Tree/tree.hpp> #include <Tree/tree.hpp>
#include <memory>
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 Tree::BaseNode::getChildIterator(const std::shared_ptr<const Tree::BaseNode> child) const
{ {
@ -100,14 +101,22 @@ 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);
children.erase(it); if (it!=children.end())
{
it=children.erase(it);
}
} }
void Tree::BaseNode::deleteChild(const std::shared_ptr<Tree::BaseNode> &c) void Tree::BaseNode::deleteChild(const std::shared_ptr<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);
if (it!=children.end())
{
children.erase(it); children.erase(it);
}
it = children.erase(it);
c->setParent(nullptr); c->setParent(nullptr);
} }
@ -129,3 +138,14 @@ void Tree::BaseNode::deleteChildren()
} }
} }
void Tree::BaseNode::replace(std::shared_ptr<Tree::BaseNode> n)
{
n->setParent(getParent());
n->addChildren(getChildrenBegin(), getChildrenEnd());
auto it=getParent()->getChildIterator(shared_from_this());
getParent()->insertChild(n, it);
getParent()->removeChild(shared_from_this());
}