diff --git a/python_binding/graph/pybind_Node.cpp b/python_binding/graph/pybind_Node.cpp index 1fa552ce153b2b0f655ca9f38d1d80f62390184b..d8e77bb259cbcbae7940a09dc405bb8f50b5b79b 100644 --- a/python_binding/graph/pybind_Node.cpp +++ b/python_binding/graph/pybind_Node.cpp @@ -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", diff --git a/src/graph/Node.cpp b/src/graph/Node.cpp index 382052535cc6b5cd8089f720b8fa9f8d3a0ebce1..12080f590683571a611ccd522c630651f55049f0 100644 --- a/src/graph/Node.cpp +++ b/src/graph/Node.cpp @@ -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; } ///////////////////////////////////////////////////////