| 130 | } |
| 131 | |
| 132 | std::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 |
no test coverage detected