diff --git a/parametric/dag.hpp b/parametric/dag.hpp index 2656ea0..f0931a5 100644 --- a/parametric/dag.hpp +++ b/parametric/dag.hpp @@ -36,6 +36,16 @@ typedef std::shared_ptr NodeRef; */ void add_parent(const NodeRef& child, const NodeRef& parent); + +/** + * @brief swap_node replaces old_node in old_node's DAG with new_node. + * The old_node remains valid, but its children now use new_node instead. + * The new_node's previous parent-child-relations are kept in place. + * @param old_node + * @param new_node + */ +void swap_node(const NodeRef& old_node, const NodeRef& new_node); + /** * @brief This class provides a basis node of a directed acyclic graph (DAG) * @@ -104,6 +114,21 @@ class DAGNode child->parents.push_back(parent); } + friend void swap_node(const NodeRef& old_node, const NodeRef& new_node) + { + auto const& children = old_node->get_children(); + for (auto const& c : children) { + if (auto cp = c.lock(); cp) { + cp->swap_parent( + *old_node, + new_node + ); + old_node->remove_child(cp); + new_node->add_child(cp); + } + } + } + /** * @brief Returns the identifier of the node * @return The id as a string @@ -250,6 +275,9 @@ class DAGNode } } + /** + * @brief swap_child swaps a child without adding this as parent to child_new + */ void swap_child(const DAGNode& child_old, const NodeRef& child_new) { if( @@ -264,6 +292,7 @@ class DAGNode } } + /** * @brief returns the number of parents */ @@ -347,6 +376,46 @@ class DAGNode std::string _id; /**< @private */ protected: + + /** + * @brief swap_parent swaps a parent without adding this as child to parent_new + */ + void swap_parent(const DAGNode& parent_old, const NodeRef& parent_new) + { + if ( + auto search = std::find_if( + parents.begin(), parents.end(), + [&parent_old](NodeRef& parent) { return parent.get() == &parent_old; } + ); + search != parents.end() + ) + { + *search = parent_new; + } + } + + /** + * @brief add_child adds a new child, without modifying the parent list of the child + * @param child_new + */ + void add_child(const NodeRef& child_new) { + childs.push_back(child_new); + } + + void remove_child(const NodeRef& child) { + if( + auto search = std::find_if( + childs.begin(), childs.end(), + [&child](std::weak_ptr& c){ return c.lock().get() == child.get(); } + ); + search != childs.end() + ) + { + childs.erase(search); + } + } + + mutable std::vector> childs; /**< @brief Pointer to child nodes */ std::vector> parents; /**< @brief Pointer to parent nodes */