From 587b4bf9537ad52a4d1c92142d49c5117a238f86 Mon Sep 17 00:00:00 2001 From: Jan Kleinert Date: Sun, 15 Sep 2024 01:24:33 +0200 Subject: [PATCH 1/2] WIP #22 --- parametric/dag.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/parametric/dag.hpp b/parametric/dag.hpp index 2656ea0..bc89914 100644 --- a/parametric/dag.hpp +++ b/parametric/dag.hpp @@ -250,6 +250,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 +267,27 @@ class DAGNode } } + /** + * @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; + } + } + + void add_child(const NodeRef& child_new) { + childs.push_back(child_new); + } + /** * @brief returns the number of parents */ From d112b6ed41cbe9a5bbb4d9922d48e87ea7f368b1 Mon Sep 17 00:00:00 2001 From: Jan Kleinert Date: Sun, 15 Sep 2024 13:28:33 +0200 Subject: [PATCH 2/2] add function swap_node --- parametric/dag.hpp | 85 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/parametric/dag.hpp b/parametric/dag.hpp index bc89914..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 @@ -267,26 +292,6 @@ class DAGNode } } - /** - * @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; - } - } - - void add_child(const NodeRef& child_new) { - childs.push_back(child_new); - } /** * @brief returns the number of parents @@ -371,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 */