From d38a7d11d256484c9ba1392ce24c965a9bf5546e Mon Sep 17 00:00:00 2001 From: idealbuq <iryna.dealbuquerquesilva@cea.fr> Date: Fri, 14 Mar 2025 13:31:16 +0000 Subject: [PATCH] Corrected logic of creating unique names in the GraphView.add() method. --- src/graph/GraphView.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/graph/GraphView.cpp b/src/graph/GraphView.cpp index 182162149..ad70862ec 100644 --- a/src/graph/GraphView.cpp +++ b/src/graph/GraphView.cpp @@ -37,6 +37,7 @@ #include "aidge/utils/ErrorHandling.hpp" #include "aidge/utils/Types.h" +#include <cstdio> // Include cstdio for printf const std::shared_ptr<Aidge::Node> Aidge::GraphView::operator[](const std::string& nodeName) const { return (mNodeRegistry.find(nodeName) != mNodeRegistry.cend()) ? mNodeRegistry.at(nodeName) : nullptr; @@ -680,20 +681,37 @@ void Aidge::GraphView::add(std::shared_ptr<Node> node, bool includeLearnablePara mRootNode = node; } - // add to the GraphView nodes + // Add to the GraphView nodes node->addView(shared_from_this()); mNodes.insert(node); if (!(node->name()).empty()) { - if (mNodeRegistry.find(node->name()) != mNodeRegistry.end()) { - std::string newName = node->createUniqueName(node->name()); - while (mNodeRegistry.find(newName) != mNodeRegistry.end()) { - newName = node->createUniqueName(newName + "_1"); + // Check if a node with the same name exists in the registry + auto it = mNodeRegistry.find(node->name()); + if (it != mNodeRegistry.end()) { + // Found a node with the same name, check if it's the same node (by memory address) + if (it->second == node) { + Log::debug("Node \"{}\" is already registered and is the same instance. Skipping insertion.\n", node->name()); + } else { + // Different instance with the same name – generate an unique name + std::string newName = node->createUniqueName(node->name()); + + while (mNodeRegistry.find(newName) != mNodeRegistry.end()) { + newName = node->createUniqueName(newName + "_1"); + } + + Log::notice("Node name \"{}\" is a duplicate, renaming it to {}.\n", node->name(), newName); + node->setName(newName); + + // Insert the renamed node + mNodeRegistry.insert(std::make_pair(node->name(), node)); + Log::debug("Inserted renamed node \"{}\" into registry.\n", node->name()); + } - Log::notice("node name \"{}\" is a duplicate, renaming to {}.\n", node->name(), newName); - node->setName(newName); - } - else { - mNodeRegistry.insert(std::make_pair(node->name(), node)); + + } else { + // No duplicate found, insert the node with its original name + mNodeRegistry.insert(std::make_pair(node->name(), node)); + Log::debug("Inserted node \"{}\" into registry.\n", node->name()); } } -- GitLab