diff --git a/src/graph/GraphView.cpp b/src/graph/GraphView.cpp index b38f92636c6dfce35b500f9e83f7c9302722f4ab..3f146c74557936d025a25e74c30140e2ae40da75 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,11 +681,35 @@ 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()) - mNodeRegistry.insert(std::make_pair(node->name(), node)); + if (!(node->name()).empty()) { + // 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 pointer + if (it->second.get() == node.get()) { + 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()); + + 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()); + + } + + } 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()); + } + } // check if the node is an input/output node updateInputsOutputsNew(node); @@ -832,10 +857,7 @@ bool Aidge::GraphView::add(std::set<std::shared_ptr<Node>> otherNodes, bool incl for (auto node : nodesToAdd) { 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"); - } - Log::notice("node name \"{}\" is a duplicate, renaming to {}.\n", node->name(), newName); + Log::notice("Node name \"{}\" is a duplicate, renaming to {}.\n", node->name(), newName); node->setName(newName); } }