Skip to content
Snippets Groups Projects

Use createUniqueName() while adding nodes from a Graphview to another.

1 unresolved thread
1 file
+ 28
10
Compare changes
  • Side-by-side
  • Inline
+ 28
10
@@ -37,6 +37,7 @@
@@ -37,6 +37,7 @@
#include "aidge/utils/ErrorHandling.hpp"
#include "aidge/utils/ErrorHandling.hpp"
#include "aidge/utils/Types.h"
#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 {
const std::shared_ptr<Aidge::Node> Aidge::GraphView::operator[](const std::string& nodeName) const {
return (mNodeRegistry.find(nodeName) != mNodeRegistry.cend()) ? mNodeRegistry.at(nodeName) : nullptr;
return (mNodeRegistry.find(nodeName) != mNodeRegistry.cend()) ? mNodeRegistry.at(nodeName) : nullptr;
@@ -674,20 +675,37 @@ void Aidge::GraphView::add(std::shared_ptr<Node> node, bool includeLearnablePara
@@ -674,20 +675,37 @@ void Aidge::GraphView::add(std::shared_ptr<Node> node, bool includeLearnablePara
mRootNode = node;
mRootNode = node;
}
}
// add to the GraphView nodes
// Add to the GraphView nodes
node->addView(shared_from_this());
node->addView(shared_from_this());
mNodes.insert(node);
mNodes.insert(node);
if (!(node->name()).empty()) {
if (!(node->name()).empty()) {
if (mNodeRegistry.find(node->name()) != mNodeRegistry.end()) {
// Check if a node with the same name exists in the registry
std::string newName = node->createUniqueName(node->name());
auto it = mNodeRegistry.find(node->name());
while (mNodeRegistry.find(newName) != mNodeRegistry.end()) {
if (it != mNodeRegistry.end()) {
newName = node->createUniqueName(newName + "_1");
// 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 {
}
// No duplicate found, insert the node with its original name
else {
mNodeRegistry.insert(std::make_pair(node->name(), node));
mNodeRegistry.insert(std::make_pair(node->name(), node));
Log::debug("Inserted node \"{}\" into registry.\n", node->name());
}
}
}
}
Loading