MCPcopy Create free account
hub / github.com/MITK/MITK / GetUniqueName

Method GetUniqueName

Modules/Core/src/DataManagement/mitkDataStorage.cpp:132–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

130}
131
132std::string mitk::DataStorage::GetUniqueName(const std::string& baseName, const DataNode* sourceNode, bool onlyDirectDerivations) const
133{
134 if (sourceNode == nullptr)
135 {
136 if (this->GetNamedNode(baseName) == nullptr)
137 return baseName;
138 }
139 else
140 {
141 if (this->GetNamedDerivedNode(baseName.c_str(), sourceNode, onlyDirectDerivations))
142 return baseName;
143 }
144
145 const std::regex numberPattern("\\s(\\d+)$");
146 std::set<int> takenNumbers;
147
148 auto condition = mitk::NodePredicateFunction::New([&](const mitk::DataNode *node) {
149 const auto name = node->GetName();
150
151 if (name.rfind(baseName, 0) == 0)
152 {
153 std::string remainingName = name.substr(baseName.length());
154 std::smatch match;
155
156 if (std::regex_match(remainingName, match, numberPattern))
157 {
158 takenNumbers.insert(std::stoi(match[1].str()));
159 return true;
160 }
161 }
162
163 return false;
164 });
165
166 auto nodes = sourceNode != nullptr
167 ? this->GetDerivations(sourceNode, condition, onlyDirectDerivations)
168 : this->GetSubset(condition);
169
170 if (nodes->empty())
171 return baseName + " 2";
172
173 int nextNumber = 2;
174
175 for (int takenNumber : takenNumbers)
176 {
177 if (takenNumber == nextNumber)
178 {
179 ++nextNumber;
180 }
181 else if (takenNumber > nextNumber)
182 {
183 break;
184 }
185 }
186
187 return baseName + ' ' + std::to_string(nextNumber);
188}
189

Callers 3

CreateStrokeNodeNameMethod · 0.80
CreateLassoNodeNameMethod · 0.80
CreateBoxNodeNameMethod · 0.80

Calls 7

GetNamedNodeMethod · 0.95
GetNamedDerivedNodeMethod · 0.95
GetSubsetMethod · 0.95
GetDerivationsMethod · 0.80
NewFunction · 0.50
GetNameMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected