Skip to content
Snippets Groups Projects
Commit 99225364 authored by Benjamin Halimi's avatar Benjamin Halimi
Browse files

Enhance createUniqueName

parent 71f09c1f
No related branches found
No related tags found
2 merge requests!212Version 0.3.0,!180Enhance createUniqueName
......@@ -48,6 +48,16 @@ void init_Node(py::module& m) {
:rtype: str
)mydelimiter")
.def("create_unique_name", &Node::createUniqueName, py::arg("base_name"),
R"mydelimiter(
Given a base name, generate a new name which is unique in all the GraphViews containing this node.
:param base_name: proposed name for the node.
:type base_name: str
:rtype: str
)mydelimiter")
.def("__repr__", &Node::repr)
.def("add_child",
......
......@@ -73,13 +73,24 @@ void Aidge::Node::setName(const std::string& name) {
mName = name;
}
std::string Aidge::Node::createUniqueName(std::string name){
for (auto graphView : views()){
if (graphView->inView(name)){
return createUniqueName(name.append("_"));
std::string Aidge::Node::createUniqueName(std::string baseName)
{
int index = 0;
bool nameAlreadyUsed = true;
std::string newName;
while (nameAlreadyUsed) {
std::string suffix = "_" + std::to_string(index);
newName = (index == 0) ? baseName : baseName + suffix;
nameAlreadyUsed = false;
for (auto graphView : views()) {
if (graphView->inView(newName)) {
nameAlreadyUsed = true;
break;
}
}
index++;
}
return name;
return newName;
}
///////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment