| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2024 Kevin Martin <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <tuple> |
| 26 |
#include <vector> |
| 27 |
#include <string> |
| 28 |
#include <set> |
| 29 |
|
| 30 |
#include "UniqueNameManager.h" |
| 31 |
|
| 32 |
std::tuple<std::string, std::string, unsigned int, Base::UnlimitedUnsigned> Base::UniqueNameManager::decomposeName( |
| 33 |
const std::string& name |
| 34 |
) const |
| 35 |
{ |
| 36 |
auto suffixStart = getNameSuffixStartPosition(name); |
| 37 |
auto digitsStart = std::find_if_not(suffixStart, name.crend(), [](char c) { |
| 38 |
return std::isdigit(c); |
| 39 |
}); |
| 40 |
unsigned int digitCount = digitsStart - suffixStart; |
| 41 |
return std::tuple<std::string, std::string, unsigned int, UnlimitedUnsigned> { |
| 42 |
name.substr(0, name.crend() - digitsStart), |
| 43 |
name.substr(name.crend() - suffixStart), |
| 44 |
digitCount, |
| 45 |
UnlimitedUnsigned::fromString(name.substr(name.crend() - digitsStart, digitCount)) |
| 46 |
}; |
| 47 |
} |
| 48 |
bool Base::UniqueNameManager::haveSameBaseName(const std::string& first, const std::string& second) const |
| 49 |
{ |
| 50 |
auto firstSuffixStart = getNameSuffixStartPosition(first); |
| 51 |
auto secondSuffixStart = getNameSuffixStartPosition(second); |
| 52 |
if (firstSuffixStart - first.crbegin() != secondSuffixStart - second.crbegin()) { |
| 53 |
// The suffixes are different lengths |
| 54 |
return false; |
| 55 |
} |
| 56 |
auto firstDigitsStart = std::find_if_not(firstSuffixStart, first.crend(), [](char c) { |
| 57 |
return std::isdigit(c); |
| 58 |
}); |
| 59 |
auto secondDigitsStart = std::find_if_not(secondSuffixStart, second.crend(), [](char c) { |
| 60 |
return std::isdigit(c); |
| 61 |
}); |
| 62 |
return std::equal(firstDigitsStart, first.crend(), secondDigitsStart, second.crend()); |
| 63 |
} |
| 64 |
|
| 65 |
void Base::UniqueNameManager::addExactName(const std::string& name) |
| 66 |
{ |
| 67 |
auto [baseName, nameSuffix, digitCount, digitsValue] = decomposeName(name); |
| 68 |
baseName += nameSuffix; |
| 69 |
auto baseNameEntry = uniqueSeeds.find(baseName); |
| 70 |
if (baseNameEntry == uniqueSeeds.end()) { |
| 71 |
// First use of baseName |
| 72 |
baseNameEntry |
| 73 |
= uniqueSeeds |
| 74 |
.emplace(baseName, std::vector<PiecewiseSparseIntegerSet<UnlimitedUnsigned>>()) |
| 75 |
.first; |
| 76 |
} |
| 77 |
if (digitCount >= baseNameEntry->second.size()) { |
| 78 |
// First use of this digitCount |
| 79 |
baseNameEntry->second.resize(digitCount + 1); |
| 80 |
} |
| 81 |
PiecewiseSparseIntegerSet<UnlimitedUnsigned>& baseNameAndDigitCountEntry |
| 82 |
= baseNameEntry->second[digitCount]; |
| 83 |
|
| 84 |
if (baseNameAndDigitCountEntry.contains(digitsValue)) { |
| 85 |
// We already have at least one instance of the name. |
| 86 |
// Increment the duplicateCounts entry for that name, |
| 87 |
// making one if none is present with an initial count of 1 |
| 88 |
// representing the singleton element we already have. |
| 89 |
duplicateCounts.try_emplace(name, 1U).first->second++; |
| 90 |
return; |
| 91 |
} |
| 92 |
baseNameAndDigitCountEntry.add(digitsValue); |
| 93 |
} |
| 94 |
std::string Base::UniqueNameManager::makeUniqueName( |
| 95 |
const std::string& modelName, |
| 96 |
std::size_t minDigits |
| 97 |
) const |
| 98 |
{ |
| 99 |
auto [namePrefix, nameSuffix, digitCount, digitsValue] = decomposeName(modelName); |
| 100 |
std::string baseName = namePrefix + nameSuffix; |
| 101 |
auto baseNameEntry = uniqueSeeds.find(baseName); |
| 102 |
if (baseNameEntry == uniqueSeeds.end()) { |
| 103 |
// First use of baseName, just return it with no unique digits |
| 104 |
return baseName; |
| 105 |
} |
| 106 |
// We don't care about the digit count or value of the suggested name, |
| 107 |
// we always use at least the most digits ever used before. |
| 108 |
digitCount = baseNameEntry->second.size() - 1; |
| 109 |
if (digitCount < minDigits) { |
| 110 |
// Caller is asking for more digits than we have in any registered name. |
| 111 |
// We start the longer digit string at 000...0001 even though we might have shorter strings |
| 112 |
// with larger numeric values. |
| 113 |
digitCount = minDigits; |
| 114 |
digitsValue = UnlimitedUnsigned(1); |
| 115 |
} |
| 116 |
else { |
| 117 |
digitsValue = baseNameEntry->second[digitCount].next(); |
| 118 |
} |
| 119 |
std::string digits = digitsValue.toString(); |
| 120 |
if (digitCount > digits.size()) { |
| 121 |
namePrefix += std::string(digitCount - digits.size(), '0'); |
| 122 |
} |
| 123 |
return namePrefix + digits + nameSuffix; |
| 124 |
} |
| 125 |
|
| 126 |
void Base::UniqueNameManager::removeExactName(const std::string& name) |
| 127 |
{ |
| 128 |
auto duplicateCountFound = duplicateCounts.find(name); |
| 129 |
if (duplicateCountFound != duplicateCounts.end()) { |
| 130 |
// The name has duplicates. Decrement the duplicate count. |
| 131 |
if (--duplicateCountFound->second <= 1) { |
| 132 |
// After removal, there are no duplicates, only a single instance. |
| 133 |
// Remove the duplicateCounts entry. |
| 134 |
duplicateCounts.erase(duplicateCountFound); |
| 135 |
} |
| 136 |
return; |
| 137 |
} |
| 138 |
auto [baseName, nameSuffix, digitCount, digitsValue] = decomposeName(name); |
| 139 |
baseName += nameSuffix; |
| 140 |
auto baseNameEntry = uniqueSeeds.find(baseName); |
| 141 |
if (baseNameEntry == uniqueSeeds.end()) { |
| 142 |
// name must not be registered, so nothing to do. |
| 143 |
return; |
| 144 |
} |
| 145 |
auto& digitValueSets = baseNameEntry->second; |
| 146 |
if (digitCount >= digitValueSets.size()) { |
| 147 |
// First use of this digitCount, name must not be registered, so nothing to do. |
| 148 |
return; |
| 149 |
} |
| 150 |
digitValueSets[digitCount].remove(digitsValue); |
| 151 |
// an element of digitValueSets may now be newly empty and so may other elements below it |
| 152 |
// Prune off all such trailing empty entries. |
| 153 |
auto lastNonemptyEntry |
| 154 |
= std::find_if(digitValueSets.crbegin(), digitValueSets.crend(), [](auto& it) { |
| 155 |
return !it.empty(); |
| 156 |
}); |
| 157 |
if (lastNonemptyEntry == digitValueSets.crend()) { |
| 158 |
// All entries are empty, so the entire baseName can be forgotten. |
| 159 |
uniqueSeeds.erase(baseName); |
| 160 |
} |
| 161 |
else { |
| 162 |
digitValueSets.resize(digitValueSets.crend() - lastNonemptyEntry); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
bool Base::UniqueNameManager::containsName(const std::string& name) const |
| 167 |
{ |
| 168 |
if (duplicateCounts.find(name) != duplicateCounts.end()) { |
| 169 |
// There are at least two instances of the name |
| 170 |
return true; |
| 171 |
} |
| 172 |
auto [baseName, nameSuffix, digitCount, digitsValue] = decomposeName(name); |
| 173 |
baseName += nameSuffix; |
| 174 |
auto baseNameEntry = uniqueSeeds.find(baseName); |
| 175 |
if (baseNameEntry == uniqueSeeds.end()) { |
| 176 |
// base name is not registered |
| 177 |
return false; |
| 178 |
} |
| 179 |
if (digitCount >= baseNameEntry->second.size()) { |
| 180 |
// First use of this digitCount, name must not be registered, so not in collection |
| 181 |
return false; |
| 182 |
} |
| 183 |
return baseNameEntry->second[digitCount].contains(digitsValue); |
| 184 |
} |