| 1 |
#include <unordered_map> |
| 2 |
#ifndef FC_DEBUG |
| 3 |
#include <random> |
| 4 |
#endif |
| 5 |
|
| 6 |
#include "ElementMap.h" |
| 7 |
#include "ElementNamingUtils.h" |
| 8 |
|
| 9 |
#include "App/Application.h" |
| 10 |
#include "Base/Console.h" |
| 11 |
#include "Document.h" |
| 12 |
#include "DocumentObject.h" |
| 13 |
|
| 14 |
#include <boost/algorithm/string/classification.hpp> |
| 15 |
#include <boost/algorithm/string/split.hpp> |
| 16 |
#include <boost/io/ios_state.hpp> |
| 17 |
|
| 18 |
|
| 19 |
FC_LOG_LEVEL_INIT("ElementMap", true, 2); // NOLINT |
| 20 |
|
| 21 |
namespace Data |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
// Because the existence of hierarchical element maps, for the same document |
| 26 |
// we may store an element map more than once in multiple objects. And because |
| 27 |
// we may want to support partial loading, we choose to tolerate such redundancy |
| 28 |
// for now. |
| 29 |
// |
| 30 |
// In order to not waste memory space when the file is loaded, we use the |
| 31 |
// following two maps to assign a one-time id for each unique element map. The |
| 32 |
// id will be saved together with the element map. |
| 33 |
// |
| 34 |
// When restoring, we'll read back the id and lookup for an existing element map |
| 35 |
// with the same id, and skip loading the current map if one is found. |
| 36 |
// |
| 37 |
// TODO: Note that the same redundancy can be found when saving OCC shapes, |
| 38 |
// because we currently save shapes for each object separately. After restoring, |
| 39 |
// any shape sharing is lost. But again, we do want to keep separate shape files |
| 40 |
// because of partial loading. The same technique used here can be applied to |
| 41 |
// restore shape sharing. |
| 42 |
static std::unordered_map<const ElementMap*, unsigned> _elementMapToId; |
| 43 |
static std::unordered_map<unsigned, ElementMapPtr> _idToElementMap; |
| 44 |
|
| 45 |
|
| 46 |
void ElementMap::init() |
| 47 |
{ |
| 48 |
static bool inited; |
| 49 |
if (!inited) { |
| 50 |
inited = true; |
| 51 |
::App::GetApplication().signalStartSaveDocument.connect( |
| 52 |
[](const ::App::Document&, const std::string&) { |
| 53 |
_elementMapToId.clear(); |
| 54 |
}); |
| 55 |
::App::GetApplication().signalFinishSaveDocument.connect( |
| 56 |
[](const ::App::Document&, const std::string&) { |
| 57 |
_elementMapToId.clear(); |
| 58 |
}); |
| 59 |
::App::GetApplication().signalStartRestoreDocument.connect([](const ::App::Document&) { |
| 60 |
_idToElementMap.clear(); |
| 61 |
}); |
| 62 |
::App::GetApplication().signalFinishRestoreDocument.connect([](const ::App::Document&) { |
| 63 |
_idToElementMap.clear(); |
| 64 |
}); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
ElementMap::ElementMap() |
| 69 |
{ |
| 70 |
init(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
void ElementMap::beforeSave(const ::App::StringHasherRef& hasherRef) const |
| 75 |
{ |
| 76 |
unsigned& id = _elementMapToId[this]; |
| 77 |
if (id == 0U) { |
| 78 |
id = _elementMapToId.size(); |
| 79 |
} |
| 80 |
this->_id = id; |
| 81 |
|
| 82 |
for (auto& indexedName : this->indexedNames) { |
| 83 |
for (const MappedNameRef& mappedName : indexedName.second.names) { |
| 84 |
for (const MappedNameRef* ref = &mappedName; ref; ref = ref->next.get()) { |
| 85 |
for (const ::App::StringIDRef& sid : ref->sids) { |
| 86 |
if (sid.isFromSameHasher(hasherRef)) { |
| 87 |
sid.mark(); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
for (auto& childPair : indexedName.second.children) { |
| 93 |
if (childPair.second.elementMap) { |
| 94 |
childPair.second.elementMap->beforeSave(hasherRef); |
| 95 |
} |
| 96 |
for (auto& sid : childPair.second.sids) { |
| 97 |
if (sid.isFromSameHasher(hasherRef)) { |
| 98 |
sid.mark(); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void ElementMap::save(std::ostream& stream, |
| 106 |
int index, |
| 107 |
const std::map<const ElementMap*, int>& childMapSet, |
| 108 |
const std::map<QByteArray, int>& postfixMap) const |
| 109 |
{ |
| 110 |
stream << "\nElementMap " << index << ' ' << this->_id << ' ' << this->indexedNames.size() |
| 111 |
<< '\n'; |
| 112 |
|
| 113 |
for (auto& indexedName : this->indexedNames) { |
| 114 |
stream << '\n' << indexedName.first << '\n'; |
| 115 |
|
| 116 |
stream << "\nChildCount " << indexedName.second.children.size() << '\n'; |
| 117 |
for (auto& vv : indexedName.second.children) { |
| 118 |
auto& child = vv.second; |
| 119 |
int mapIndex = 0; |
| 120 |
if (child.elementMap) { |
| 121 |
auto it = childMapSet.find(child.elementMap.get()); |
| 122 |
if (it == childMapSet.end() || it->second == 0) { |
| 123 |
FC_ERR("Invalid child element map"); // NOLINT |
| 124 |
} |
| 125 |
else { |
| 126 |
mapIndex = it->second; |
| 127 |
} |
| 128 |
} |
| 129 |
stream << child.indexedName.getIndex() << ' ' << child.offset << ' ' << child.count |
| 130 |
<< ' ' << child.tag << ' ' << mapIndex << ' '; |
| 131 |
stream.write(child.postfix.constData(), child.postfix.size()); |
| 132 |
stream << ' ' << '0'; |
| 133 |
for (auto& sid : child.sids) { |
| 134 |
if (sid.isMarked()) { |
| 135 |
stream << '.' << sid.value(); |
| 136 |
} |
| 137 |
} |
| 138 |
stream << '\n'; |
| 139 |
} |
| 140 |
|
| 141 |
stream << "\nNameCount " << indexedName.second.names.size() << '\n'; |
| 142 |
if (indexedName.second.names.empty()) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
boost::io::ios_flags_saver ifs(stream); |
| 147 |
stream << std::hex; |
| 148 |
|
| 149 |
for (auto& dequeueOfMappedNameRef : indexedName.second.names) { |
| 150 |
for (auto ref = &dequeueOfMappedNameRef; ref; ref = ref->next.get()) { |
| 151 |
if (!ref->name) { |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
::App::StringID::IndexID prefixID {}; |
| 156 |
prefixID.id = 0; |
| 157 |
IndexedName idx(ref->name.dataBytes()); |
| 158 |
bool printName = true; |
| 159 |
if (idx) { |
| 160 |
auto key = QByteArray::fromRawData(idx.getType(), |
| 161 |
static_cast<int>(qstrlen(idx.getType()))); |
| 162 |
auto it = postfixMap.find(key); |
| 163 |
if (it != postfixMap.end()) { |
| 164 |
stream << ':' << it->second << '.' << idx.getIndex(); |
| 165 |
printName = false; |
| 166 |
} |
| 167 |
} |
| 168 |
else { |
| 169 |
prefixID = ::App::StringID::fromString(ref->name.dataBytes()); |
| 170 |
if (prefixID.id != 0) { |
| 171 |
for (auto& sid : ref->sids) { |
| 172 |
if (sid.isMarked() && sid.value() == prefixID.id) { |
| 173 |
stream << '$'; |
| 174 |
stream.write(ref->name.dataBytes().constData(), |
| 175 |
ref->name.dataBytes().size()); |
| 176 |
printName = false; |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
if (printName) { |
| 181 |
prefixID.id = 0; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
if (printName) { |
| 186 |
stream << ';'; |
| 187 |
stream.write(ref->name.dataBytes().constData(), ref->name.dataBytes().size()); |
| 188 |
} |
| 189 |
|
| 190 |
const QByteArray& postfix = ref->name.postfixBytes(); |
| 191 |
if (postfix.isEmpty()) { |
| 192 |
stream << ".0"; |
| 193 |
} |
| 194 |
else { |
| 195 |
auto it = postfixMap.find(postfix); |
| 196 |
assert(it != postfixMap.end()); |
| 197 |
stream << '.' << it->second; |
| 198 |
} |
| 199 |
for (auto& sid : ref->sids) { |
| 200 |
if (sid.isMarked() && sid.value() != prefixID.id) { |
| 201 |
stream << '.' << sid.value(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
stream << ' '; |
| 206 |
} |
| 207 |
stream << "0\n"; |
| 208 |
} |
| 209 |
} |
| 210 |
stream << "\nEndMap\n"; |
| 211 |
} |
| 212 |
|
| 213 |
void ElementMap::save(std::ostream& stream) const |
| 214 |
{ |
| 215 |
std::map<const ElementMap*, int> childMapSet; |
| 216 |
std::vector<const ElementMap*> childMaps; |
| 217 |
std::map<QByteArray, int> postfixMap; |
| 218 |
std::vector<QByteArray> postfixes; |
| 219 |
|
| 220 |
collectChildMaps(childMapSet, childMaps, postfixMap, postfixes); |
| 221 |
|
| 222 |
stream << this->_id << " PostfixCount " << postfixes.size() << '\n'; |
| 223 |
for (auto& postfix : postfixes) { |
| 224 |
stream.write(postfix.constData(), postfix.size()); |
| 225 |
stream << '\n'; |
| 226 |
} |
| 227 |
int index = 0; |
| 228 |
stream << "\nMapCount " << childMaps.size() << '\n'; |
| 229 |
for (auto& elementMap : childMaps) { |
| 230 |
elementMap->save(stream, ++index, childMapSet, postfixMap); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
ElementMapPtr ElementMap::restore(::App::StringHasherRef hasherRef, std::istream& stream) |
| 235 |
{ |
| 236 |
const char* msg = "Invalid element map"; |
| 237 |
|
| 238 |
unsigned id = 0; |
| 239 |
int count = 0; |
| 240 |
std::string tmp; |
| 241 |
if (!(stream >> id >> tmp >> count) || tmp != "PostfixCount") { |
| 242 |
FC_THROWM(Base::RuntimeError, msg); // NOLINT |
| 243 |
} |
| 244 |
|
| 245 |
auto& map = _idToElementMap[id]; |
| 246 |
if (map) { |
| 247 |
return map; |
| 248 |
} |
| 249 |
|
| 250 |
std::vector<std::string> postfixes; |
| 251 |
postfixes.reserve(count); |
| 252 |
for (int i = 0; i < count; ++i) { |
| 253 |
postfixes.emplace_back(); |
| 254 |
stream >> postfixes.back(); |
| 255 |
} |
| 256 |
|
| 257 |
std::vector<ElementMapPtr> childMaps; |
| 258 |
count = 0; |
| 259 |
constexpr int practicalMaximum {(1 << 30) / sizeof(ElementMapPtr)}; // a 1GB child map vector: almost certainly a bug |
| 260 |
if (!(stream >> tmp >> count) || tmp != "MapCount" || count == 0 || count > practicalMaximum) { |
| 261 |
FC_THROWM(Base::RuntimeError, msg); // NOLINT |
| 262 |
} |
| 263 |
childMaps.reserve(count - 1); |
| 264 |
for (int i = 0; i < count - 1; ++i) { |
| 265 |
childMaps.push_back( |
| 266 |
std::make_shared<ElementMap>()->restore(hasherRef, stream, childMaps, postfixes)); |
| 267 |
} |
| 268 |
|
| 269 |
return restore(hasherRef, stream, childMaps, postfixes); |
| 270 |
} |
| 271 |
|
| 272 |
ElementMapPtr ElementMap::restore(::App::StringHasherRef hasherRef, |
| 273 |
std::istream& stream, |
| 274 |
std::vector<ElementMapPtr>& childMaps, |
| 275 |
const std::vector<std::string>& postfixes) |
| 276 |
{ |
| 277 |
const char* msg = "Invalid element map"; |
| 278 |
const int hexBase {16}; |
| 279 |
const int decBase {10}; |
| 280 |
std::string tmp; |
| 281 |
int index = 0; |
| 282 |
int typeCount = 0; |
| 283 |
unsigned id = 0; |
| 284 |
if (!(stream >> tmp >> index >> id >> typeCount) || tmp != "ElementMap") { |
| 285 |
FC_THROWM(Base::RuntimeError, msg); // NOLINT |
| 286 |
} |
| 287 |
constexpr int maxTypeCount(1000); |
| 288 |
if (typeCount < 0 || typeCount > maxTypeCount) { |
| 289 |
FC_THROWM(Base::RuntimeError, "Bad type count in element map, ignoring map"); // NOLINT |
| 290 |
} |
| 291 |
|
| 292 |
auto& map = _idToElementMap[id]; |
| 293 |
if (map) { |
| 294 |
while (tmp != "EndMap") { |
| 295 |
if (!std::getline(stream, tmp)) { |
| 296 |
FC_THROWM(Base::RuntimeError, "unexpected end of child element map"); // NOLINT |
| 297 |
} |
| 298 |
} |
| 299 |
return map; |
| 300 |
} |
| 301 |
|
| 302 |
const char* hasherWarn = nullptr; |
| 303 |
const char* hasherIDWarn = nullptr; |
| 304 |
const char* postfixWarn = nullptr; |
| 305 |
const char* childSIDWarn = nullptr; |
| 306 |
std::vector<std::string> tokens; |
| 307 |
|
| 308 |
for (int i = 0; i < typeCount; ++i) { |
| 309 |
int outerCount = 0; |
| 310 |
if (!(stream >> tmp)) { |
| 311 |
FC_THROWM(Base::RuntimeError, "missing element type"); // NOLINT |
| 312 |
} |
| 313 |
IndexedName idx(tmp.c_str(), 1); |
| 314 |
|
| 315 |
if (!(stream >> tmp >> outerCount) || tmp != "ChildCount") { |
| 316 |
FC_THROWM(Base::RuntimeError, "missing element child count"); // NOLINT |
| 317 |
} |
| 318 |
|
| 319 |
auto& indices = this->indexedNames[idx.getType()]; |
| 320 |
for (int j = 0; j < outerCount; ++j) { |
| 321 |
int cIndex = 0; |
| 322 |
int offset = 0; |
| 323 |
int count = 0; |
| 324 |
long tag = 0; |
| 325 |
int mapIndex = 0; |
| 326 |
if (!(stream >> cIndex >> offset >> count >> tag >> mapIndex >> tmp)) { |
| 327 |
FC_THROWM(Base::RuntimeError, "Invalid element child"); // NOLINT |
| 328 |
} |
| 329 |
if (cIndex < 0) { |
| 330 |
FC_THROWM(Base::RuntimeError, "Invalid element child index"); // NOLINT |
| 331 |
} |
| 332 |
if (offset < 0) { |
| 333 |
FC_THROWM(Base::RuntimeError, "Invalid element child offset"); // NOLINT |
| 334 |
} |
| 335 |
if (mapIndex >= index || mapIndex < 0 || mapIndex > (int)childMaps.size()) { |
| 336 |
FC_THROWM(Base::RuntimeError, "Invalid element child map index"); // NOLINT |
| 337 |
} |
| 338 |
auto& child = indices.children[cIndex + offset + count]; |
| 339 |
child.indexedName = IndexedName::fromConst(idx.getType(), cIndex); |
| 340 |
child.offset = offset; |
| 341 |
child.count = count; |
| 342 |
child.tag = tag; |
| 343 |
if (mapIndex > 0) { |
| 344 |
child.elementMap = childMaps[mapIndex - 1]; |
| 345 |
} |
| 346 |
else { |
| 347 |
child.elementMap = nullptr; |
| 348 |
} |
| 349 |
child.postfix = tmp.c_str(); |
| 350 |
this->childElements[child.postfix].childMap = &child; |
| 351 |
this->childElementSize += child.count; |
| 352 |
|
| 353 |
if (!(stream >> tmp)) { |
| 354 |
FC_THROWM(Base::RuntimeError, "Invalid element child string id"); // NOLINT |
| 355 |
} |
| 356 |
|
| 357 |
tokens.clear(); |
| 358 |
boost::split(tokens, tmp, boost::is_any_of(".")); |
| 359 |
if (tokens.size() > 1) { |
| 360 |
child.sids.reserve(static_cast<int>(tokens.size()) - 1); |
| 361 |
for (unsigned k = 1; k < tokens.size(); ++k) { |
| 362 |
// The element child string ID is saved as decimal |
| 363 |
// instead of hex by accident. To simplify maintenance |
| 364 |
// of backward compatibility, it is not corrected, and |
| 365 |
// just restored as decimal here. |
| 366 |
long childID = strtol(tokens[k].c_str(), nullptr, decBase); |
| 367 |
auto sid = hasherRef->getID(childID); |
| 368 |
if (!sid) { |
| 369 |
childSIDWarn = "Missing element child string id"; |
| 370 |
} |
| 371 |
else { |
| 372 |
child.sids.push_back(sid); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if (!(stream >> tmp >> outerCount) || tmp != "NameCount") { |
| 379 |
FC_THROWM(Base::RuntimeError, "missing element name outerCount"); // NOLINT |
| 380 |
} |
| 381 |
|
| 382 |
boost::io::ios_flags_saver ifs(stream); |
| 383 |
stream >> std::hex; |
| 384 |
|
| 385 |
indices.names.resize(outerCount); |
| 386 |
for (int j = 0; j < outerCount; ++j) { |
| 387 |
idx.setIndex(j); |
| 388 |
auto* ref = &indices.names[j]; |
| 389 |
int innerCount = 0; |
| 390 |
while (true) { |
| 391 |
if (!(stream >> tmp)) { |
| 392 |
FC_THROWM(Base::RuntimeError, "Failed to read element name"); // NOLINT |
| 393 |
} |
| 394 |
if (tmp == "0") { |
| 395 |
break; |
| 396 |
} |
| 397 |
if (innerCount++ != 0) { |
| 398 |
ref->next = std::make_unique<MappedNameRef>(); |
| 399 |
ref = ref->next.get(); |
| 400 |
} |
| 401 |
tokens.clear(); |
| 402 |
boost::split(tokens, tmp, boost::is_any_of(".")); |
| 403 |
if (tokens.size() < 2) { |
| 404 |
FC_THROWM(Base::RuntimeError, "Invalid element entry"); // NOLINT |
| 405 |
} |
| 406 |
|
| 407 |
int offset = 1; |
| 408 |
::App::StringID::IndexID prefixID {}; |
| 409 |
prefixID.id = 0; |
| 410 |
|
| 411 |
switch (tokens[0][0]) { |
| 412 |
case ':': { |
| 413 |
if (tokens.size() < 3) { |
| 414 |
FC_THROWM(Base::RuntimeError, "Invalid element entry"); // NOLINT |
| 415 |
} |
| 416 |
++offset; |
| 417 |
long elementNameIndex = strtol(tokens[0].c_str() + 1, nullptr, hexBase); |
| 418 |
if (elementNameIndex <= 0 || elementNameIndex > (int)postfixes.size()) { |
| 419 |
FC_THROWM(Base::RuntimeError, "Invalid element name index"); // NOLINT |
| 420 |
} |
| 421 |
long elementIndex = strtol(tokens[1].c_str(), nullptr, hexBase); |
| 422 |
ref->name = MappedName( |
| 423 |
IndexedName::fromConst(postfixes[elementNameIndex - 1].c_str(), |
| 424 |
static_cast<int>(elementIndex))); |
| 425 |
break; |
| 426 |
} |
| 427 |
case '$': |
| 428 |
ref->name = MappedName(tokens[0].c_str() + 1); |
| 429 |
prefixID = ::App::StringID::fromString(ref->name.dataBytes()); |
| 430 |
break; |
| 431 |
case ';': |
| 432 |
ref->name = MappedName(tokens[0].c_str() + 1); |
| 433 |
break; |
| 434 |
default: |
| 435 |
FC_THROWM(Base::RuntimeError, "Invalid element name marker"); // NOLINT |
| 436 |
} |
| 437 |
|
| 438 |
if (tokens[offset] != "0") { |
| 439 |
long postfixIndex = strtol(tokens[offset].c_str(), nullptr, hexBase); |
| 440 |
if (postfixIndex <= 0 || postfixIndex > (int)postfixes.size()) { |
| 441 |
postfixWarn = "Invalid element postfix index"; |
| 442 |
} |
| 443 |
else { |
| 444 |
ref->name += postfixes[postfixIndex - 1]; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
this->mappedNames.emplace(ref->name, idx); |
| 449 |
|
| 450 |
if (!hasherRef) { |
| 451 |
if (offset + 1 < (int)tokens.size()) { |
| 452 |
hasherWarn = "No hasherRef"; |
| 453 |
} |
| 454 |
continue; |
| 455 |
} |
| 456 |
|
| 457 |
ref->sids.reserve((tokens.size() - offset - 1 + prefixID.id) != 0U ? 1 : 0); |
| 458 |
if (prefixID.id != 0) { |
| 459 |
auto sid = hasherRef->getID(prefixID.id); |
| 460 |
if (!sid) { |
| 461 |
hasherIDWarn = "Missing element name prefix id"; |
| 462 |
} |
| 463 |
else { |
| 464 |
ref->sids.push_back(sid); |
| 465 |
} |
| 466 |
} |
| 467 |
for (int l = offset + 1; l < (int)tokens.size(); ++l) { |
| 468 |
long readID = strtol(tokens[l].c_str(), nullptr, hexBase); |
| 469 |
auto sid = hasherRef->getID(readID); |
| 470 |
if (!sid) { |
| 471 |
hasherIDWarn = "Invalid element name string id"; |
| 472 |
} |
| 473 |
else { |
| 474 |
ref->sids.push_back(sid); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
if (hasherWarn) { |
| 481 |
FC_WARN(hasherWarn); // NOLINT |
| 482 |
} |
| 483 |
if (hasherIDWarn) { |
| 484 |
FC_WARN(hasherIDWarn); // NOLINT |
| 485 |
} |
| 486 |
if (postfixWarn) { |
| 487 |
FC_WARN(postfixWarn); // NOLINT |
| 488 |
} |
| 489 |
if (childSIDWarn) { |
| 490 |
FC_WARN(childSIDWarn); // NOLINT |
| 491 |
} |
| 492 |
|
| 493 |
if (!(stream >> tmp) || tmp != "EndMap") { |
| 494 |
FC_THROWM(Base::RuntimeError, "unexpected end of child element map"); // NOLINT |
| 495 |
} |
| 496 |
|
| 497 |
return shared_from_this(); |
| 498 |
} |
| 499 |
|
| 500 |
MappedName ElementMap::addName(MappedName& name, |
| 501 |
const IndexedName& idx, |
| 502 |
const ElementIDRefs& sids, |
| 503 |
bool overwrite, |
| 504 |
IndexedName* existing) |
| 505 |
{ |
| 506 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 507 |
if (name.find("#") >= 0 && name.findTagInElementName() < 0) { |
| 508 |
FC_ERR("missing tag postfix " << name); // NOLINT |
| 509 |
} |
| 510 |
} |
| 511 |
while (true) { |
| 512 |
if (overwrite) { |
| 513 |
erase(idx); |
| 514 |
} |
| 515 |
auto ret = mappedNames.insert(std::make_pair(name, idx)); |
| 516 |
if (ret.second) { // element just inserted did not exist yet in the map |
| 517 |
ret.first->first.compact(); // FIXME see MappedName.cpp |
| 518 |
mappedRef(idx).append(ret.first->first, sids); |
| 519 |
FC_TRACE(idx << " -> " << name); // NOLINT |
| 520 |
return ret.first->first; |
| 521 |
} |
| 522 |
if (ret.first->second == idx) { |
| 523 |
FC_TRACE("duplicate " << idx << " -> " << name); // NOLINT |
| 524 |
return ret.first->first; |
| 525 |
} |
| 526 |
if (!overwrite) { |
| 527 |
if (existing) { |
| 528 |
*existing = ret.first->second; |
| 529 |
} |
| 530 |
return {}; |
| 531 |
} |
| 532 |
|
| 533 |
erase(ret.first->first); |
| 534 |
}; |
| 535 |
} |
| 536 |
|
| 537 |
void ElementMap::addPostfix(const QByteArray& postfix, |
| 538 |
std::map<QByteArray, int>& postfixMap, |
| 539 |
std::vector<QByteArray>& postfixes) |
| 540 |
{ |
| 541 |
if (postfix.isEmpty()) { |
| 542 |
return; |
| 543 |
} |
| 544 |
auto res = postfixMap.insert(std::make_pair(postfix, 0)); |
| 545 |
if (res.second) { |
| 546 |
postfixes.push_back(postfix); |
| 547 |
res.first->second = (int)postfixes.size(); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
MappedName ElementMap::setElementName(const IndexedName& element, |
| 552 |
const MappedName& name, |
| 553 |
long masterTag, |
| 554 |
const ElementIDRefs* sid, |
| 555 |
bool overwrite) |
| 556 |
{ |
| 557 |
if (!element) { |
| 558 |
throw Base::ValueError("Invalid input"); |
| 559 |
} |
| 560 |
if (!name) { |
| 561 |
erase(element); |
| 562 |
return {}; |
| 563 |
} |
| 564 |
|
| 565 |
for (int i = 0, count = name.size(); i < count; ++i) { |
| 566 |
char check = name[i]; |
| 567 |
if (check == '.' || (std::isspace((int)check) != 0)) { |
| 568 |
FC_THROWM(Base::RuntimeError, "Illegal character in mapped name: " << name); // NOLINT |
| 569 |
} |
| 570 |
} |
| 571 |
for (const char* readChar = element.getType(); *readChar != 0; ++readChar) { |
| 572 |
char check = *readChar; |
| 573 |
if (check == '.' || (std::isspace((int)check) != 0)) { |
| 574 |
FC_THROWM(Base::RuntimeError, // NOLINT |
| 575 |
"Illegal character in element name: " << element); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
// Originally in ComplexGeoData::setElementName |
| 580 |
// LinkStable/src/App/ComplexGeoData.cpp#L1631 |
| 581 |
// No longer possible after map separated in ElementMap.cpp |
| 582 |
|
| 583 |
// if(!_ElementMap) |
| 584 |
// resetElementMap(std::make_shared<ElementMap>()); |
| 585 |
|
| 586 |
ElementIDRefs _sid; |
| 587 |
if (!sid) { |
| 588 |
sid = &_sid; |
| 589 |
} |
| 590 |
|
| 591 |
std::ostringstream ss; |
| 592 |
Data::MappedName mappedName(name); |
| 593 |
for (int i = 0;;) { |
| 594 |
IndexedName existing; |
| 595 |
MappedName res = this->addName(mappedName, element, *sid, overwrite, &existing); |
| 596 |
if (res) { |
| 597 |
return res; |
| 598 |
} |
| 599 |
const int maxAttempts {100}; |
| 600 |
if (++i == maxAttempts) { |
| 601 |
FC_ERR("unresolved duplicate element mapping '" // NOLINT |
| 602 |
<< name << ' ' << element << '/' << existing); |
| 603 |
return name; |
| 604 |
} |
| 605 |
if (sid != &_sid) { |
| 606 |
_sid = *sid; |
| 607 |
} |
| 608 |
mappedName = renameDuplicateElement(i, element, existing, name, _sid, masterTag); |
| 609 |
if (!mappedName) { |
| 610 |
return name; |
| 611 |
} |
| 612 |
sid = &_sid; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
// try to hash element name while preserving the source tag |
| 617 |
void ElementMap::encodeElementName(char element_type, |
| 618 |
MappedName& name, |
| 619 |
std::ostringstream& ss, |
| 620 |
ElementIDRefs* sids, |
| 621 |
long masterTag, |
| 622 |
const char* postfix, |
| 623 |
long tag, |
| 624 |
bool forceTag) const |
| 625 |
{ |
| 626 |
if (postfix && (postfix[0] != 0)) { |
| 627 |
if (!boost::starts_with(postfix, ELEMENT_MAP_PREFIX)) { |
| 628 |
ss << ELEMENT_MAP_PREFIX; |
| 629 |
} |
| 630 |
ss << postfix; |
| 631 |
} |
| 632 |
long inputTag = 0; |
| 633 |
if (!forceTag && (ss.tellp() == 0)) { |
| 634 |
if ((tag == 0) || tag == masterTag) { |
| 635 |
return; |
| 636 |
} |
| 637 |
name.findTagInElementName(&inputTag, nullptr, nullptr, nullptr, true); |
| 638 |
if (inputTag == tag) { |
| 639 |
return; |
| 640 |
} |
| 641 |
} |
| 642 |
else if ((tag == 0) || (!forceTag && tag == masterTag)) { |
| 643 |
int pos = name.findTagInElementName(&inputTag, nullptr, nullptr, nullptr, true); |
| 644 |
if (inputTag != 0) { |
| 645 |
tag = inputTag; |
| 646 |
// About to encode the same tag used last time. This usually means |
| 647 |
// the owner object is doing multistep modeling. Let's not |
| 648 |
// recursively encode the same tag too many times. It will be a |
| 649 |
// waste of memory, because the intermediate shapes has no |
| 650 |
// corresponding objects, so no real value for history tracing. |
| 651 |
// |
| 652 |
// On the other hand, we still need to distinguish the original name |
| 653 |
// from the input object from the element name of the intermediate |
| 654 |
// shapes. So we limit ourselves to encode only one extra level |
| 655 |
// using the same tag. In order to do that, we need to de-hash the |
| 656 |
// previous level name, and check for its tag. |
| 657 |
Data::MappedName mappedName(name, 0, pos); |
| 658 |
Data::MappedName prev = dehashElementName(mappedName); |
| 659 |
long prevTag = 0; |
| 660 |
prev.findTagInElementName(&prevTag, nullptr, nullptr, nullptr, true); |
| 661 |
if (prevTag == inputTag || prevTag == -inputTag) { |
| 662 |
name = mappedName; |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
if (sids && this->hasher) { |
| 668 |
name = hashElementName(name, *sids); |
| 669 |
if (!forceTag && (tag == 0) && (ss.tellp() != 0)) { |
| 670 |
forceTag = true; |
| 671 |
} |
| 672 |
} |
| 673 |
if (forceTag || (tag != 0)) { |
| 674 |
assert(element_type); |
| 675 |
auto pos = ss.tellp(); |
| 676 |
boost::io::ios_flags_saver ifs(ss); |
| 677 |
ss << POSTFIX_TAG << std::hex; |
| 678 |
if (tag < 0) { |
| 679 |
ss << '-' << -tag; |
| 680 |
} |
| 681 |
else if (tag != 0) { |
| 682 |
ss << tag; |
| 683 |
} |
| 684 |
assert(pos >= 0); |
| 685 |
if (pos != 0) { |
| 686 |
ss << ':' << pos; |
| 687 |
} |
| 688 |
ss << ',' << element_type; |
| 689 |
} |
| 690 |
name += ss.str(); |
| 691 |
} |
| 692 |
|
| 693 |
MappedName ElementMap::hashElementName(const MappedName& name, ElementIDRefs& sids) const |
| 694 |
{ |
| 695 |
if (!this->hasher || !name) { |
| 696 |
return name; |
| 697 |
} |
| 698 |
if (name.find(ELEMENT_MAP_PREFIX) < 0) { |
| 699 |
return name; |
| 700 |
} |
| 701 |
App::StringIDRef sid = this->hasher->getID(name, sids); |
| 702 |
const auto& related = sid.relatedIDs(); |
| 703 |
if (related == sids) { |
| 704 |
sids.clear(); |
| 705 |
sids.push_back(sid); |
| 706 |
} |
| 707 |
else { |
| 708 |
ElementIDRefs tmp; |
| 709 |
tmp.push_back(sid); |
| 710 |
for (auto& checkSID : sids) { |
| 711 |
if (related.indexOf(checkSID) < 0) { |
| 712 |
tmp.push_back(checkSID); |
| 713 |
} |
| 714 |
} |
| 715 |
sids = tmp; |
| 716 |
} |
| 717 |
return MappedName(sid.toString()); |
| 718 |
} |
| 719 |
|
| 720 |
MappedName ElementMap::dehashElementName(const MappedName& name) const |
| 721 |
{ |
| 722 |
if (name.empty()) { |
| 723 |
return name; |
| 724 |
} |
| 725 |
if (!this->hasher) { |
| 726 |
return name; |
| 727 |
} |
| 728 |
auto id = App::StringID::fromString(name.toRawBytes()); |
| 729 |
if (!id) { |
| 730 |
return name; |
| 731 |
} |
| 732 |
auto sid = this->hasher->getID(id); |
| 733 |
if (!sid) { |
| 734 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_TRACE)) { |
| 735 |
FC_WARN("failed to find hash id " << id); // NOLINT |
| 736 |
} |
| 737 |
else { |
| 738 |
FC_LOG("failed to find hash id " << id); // NOLINT |
| 739 |
} |
| 740 |
return name; |
| 741 |
} |
| 742 |
if (sid.isHashed()) { |
| 743 |
FC_LOG("cannot de-hash id " << id); // NOLINT |
| 744 |
return name; |
| 745 |
} |
| 746 |
MappedName ret(sid); |
| 747 |
// sid.toString());// FIXME .toString() was missing in original function. is this |
| 748 |
// correct? |
| 749 |
FC_TRACE("de-hash " << name << " -> " << ret); // NOLINT |
| 750 |
return ret; |
| 751 |
} |
| 752 |
|
| 753 |
MappedName ElementMap::renameDuplicateElement(int index, |
| 754 |
const IndexedName& element, |
| 755 |
const IndexedName& element2, |
| 756 |
const MappedName& name, |
| 757 |
ElementIDRefs& sids, |
| 758 |
long masterTag) const |
| 759 |
{ |
| 760 |
int idx {0}; |
| 761 |
#ifdef FC_DEBUG |
| 762 |
idx = index; |
| 763 |
#else |
| 764 |
static std::random_device _RD; |
| 765 |
static std::mt19937 _RGEN(_RD()); |
| 766 |
static std::uniform_int_distribution<> _RDIST(1, 10000); |
| 767 |
(void)index; |
| 768 |
idx = _RDIST(_RGEN); |
| 769 |
#endif |
| 770 |
std::ostringstream ss; |
| 771 |
ss << ELEMENT_MAP_PREFIX << 'D' << std::hex << idx; |
| 772 |
MappedName renamed(name); |
| 773 |
encodeElementName(element.getType()[0], renamed, ss, &sids, masterTag); |
| 774 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 775 |
FC_WARN("duplicate element mapping '" // NOLINT |
| 776 |
<< name << " -> " << renamed << ' ' << element << '/' << element2); |
| 777 |
} |
| 778 |
return renamed; |
| 779 |
} |
| 780 |
|
| 781 |
void ElementMap::erase(const MappedName& name) |
| 782 |
{ |
| 783 |
auto it = this->mappedNames.find(name); |
| 784 |
if (it == this->mappedNames.end()) { |
| 785 |
return; |
| 786 |
} |
| 787 |
MappedNameRef* ref = findMappedRef(it->second); |
| 788 |
if (!ref) { |
| 789 |
return; |
| 790 |
} |
| 791 |
ref->erase(name); |
| 792 |
this->mappedNames.erase(it); |
| 793 |
} |
| 794 |
|
| 795 |
void ElementMap::erase(const IndexedName& idx) |
| 796 |
{ |
| 797 |
auto iter = this->indexedNames.find(idx.getType()); |
| 798 |
if (iter == this->indexedNames.end()) { |
| 799 |
return; |
| 800 |
} |
| 801 |
auto& indices = iter->second; |
| 802 |
if (idx.getIndex() >= (int)indices.names.size()) { |
| 803 |
return; |
| 804 |
} |
| 805 |
auto& ref = indices.names[idx.getIndex()]; |
| 806 |
for (auto* nameRef = &ref; nameRef; nameRef = nameRef->next.get()) { |
| 807 |
this->mappedNames.erase(nameRef->name); |
| 808 |
} |
| 809 |
ref.clear(); |
| 810 |
} |
| 811 |
|
| 812 |
unsigned long ElementMap::size() const |
| 813 |
{ |
| 814 |
return mappedNames.size() + childElementSize; |
| 815 |
} |
| 816 |
|
| 817 |
bool ElementMap::empty() const |
| 818 |
{ |
| 819 |
return mappedNames.empty() && childElementSize == 0; |
| 820 |
} |
| 821 |
|
| 822 |
IndexedName ElementMap::find(const MappedName& name, ElementIDRefs* sids) const |
| 823 |
{ |
| 824 |
auto nameIter = mappedNames.find(name); |
| 825 |
if (nameIter == mappedNames.end()) { |
| 826 |
if (childElements.isEmpty()) { |
| 827 |
return IndexedName(); |
| 828 |
} |
| 829 |
|
| 830 |
int len = 0; |
| 831 |
if (name.findTagInElementName(nullptr, &len, nullptr, nullptr, false, false) < 0) { |
| 832 |
return IndexedName(); |
| 833 |
} |
| 834 |
QByteArray key = name.toRawBytes(len); |
| 835 |
auto it = this->childElements.find(key); |
| 836 |
if (it == this->childElements.end()) { |
| 837 |
return IndexedName(); |
| 838 |
} |
| 839 |
|
| 840 |
const auto& child = *it.value().childMap; |
| 841 |
IndexedName res; |
| 842 |
|
| 843 |
MappedName childName = MappedName::fromRawData(name, 0, len); |
| 844 |
if (child.elementMap) { |
| 845 |
res = child.elementMap->find(childName, sids); |
| 846 |
} |
| 847 |
else { |
| 848 |
res = childName.toIndexedName(); |
| 849 |
} |
| 850 |
|
| 851 |
if (res && boost::equals(res.getType(), child.indexedName.getType()) |
| 852 |
&& child.indexedName.getIndex() <= res.getIndex() |
| 853 |
&& child.indexedName.getIndex() + child.count > res.getIndex()) { |
| 854 |
res.setIndex(res.getIndex() + it.value().childMap->offset); |
| 855 |
return res; |
| 856 |
} |
| 857 |
|
| 858 |
return IndexedName(); |
| 859 |
} |
| 860 |
|
| 861 |
if (sids) { |
| 862 |
const MappedNameRef* ref = findMappedRef(nameIter->second); |
| 863 |
for (; ref; ref = ref->next.get()) { |
| 864 |
if (ref->name == name) { |
| 865 |
if (sids->empty()) { |
| 866 |
*sids = ref->sids; |
| 867 |
} |
| 868 |
else { |
| 869 |
*sids += ref->sids; |
| 870 |
} |
| 871 |
break; |
| 872 |
} |
| 873 |
} |
| 874 |
} |
| 875 |
return nameIter->second; |
| 876 |
} |
| 877 |
|
| 878 |
MappedName ElementMap::find(const IndexedName& idx, ElementIDRefs* sids) const |
| 879 |
{ |
| 880 |
if (!idx) { |
| 881 |
return {}; |
| 882 |
} |
| 883 |
|
| 884 |
auto iter = this->indexedNames.find(idx.getType()); |
| 885 |
if (iter == this->indexedNames.end()) { |
| 886 |
return {}; |
| 887 |
} |
| 888 |
|
| 889 |
auto& indices = iter->second; |
| 890 |
if (idx.getIndex() < (int)indices.names.size()) { |
| 891 |
const MappedNameRef& ref = indices.names[idx.getIndex()]; |
| 892 |
if (ref.name) { |
| 893 |
if (sids) { |
| 894 |
if (!sids->size()) { |
| 895 |
*sids = ref.sids; |
| 896 |
} |
| 897 |
else { |
| 898 |
*sids += ref.sids; |
| 899 |
} |
| 900 |
} |
| 901 |
return ref.name; |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
auto it = indices.children.upper_bound(idx.getIndex()); |
| 906 |
if (it != indices.children.end() |
| 907 |
&& it->second.indexedName.getIndex() + it->second.offset <= idx.getIndex()) { |
| 908 |
auto& child = it->second; |
| 909 |
MappedName name; |
| 910 |
IndexedName childIdx(idx.getType(), idx.getIndex() - child.offset); |
| 911 |
if (child.elementMap) { |
| 912 |
name = child.elementMap->find(childIdx, sids); |
| 913 |
} |
| 914 |
else { |
| 915 |
name = MappedName(childIdx); |
| 916 |
} |
| 917 |
if (name) { |
| 918 |
name += child.postfix; |
| 919 |
return name; |
| 920 |
} |
| 921 |
} |
| 922 |
return {}; |
| 923 |
} |
| 924 |
|
| 925 |
std::vector<std::pair<MappedName, ElementIDRefs>> ElementMap::findAll(const IndexedName& idx) const |
| 926 |
{ |
| 927 |
std::vector<std::pair<MappedName, ElementIDRefs>> res; |
| 928 |
if (!idx) { |
| 929 |
return res; |
| 930 |
} |
| 931 |
|
| 932 |
auto iter = this->indexedNames.find(idx.getType()); |
| 933 |
if (iter == this->indexedNames.end()) { |
| 934 |
return res; |
| 935 |
} |
| 936 |
|
| 937 |
auto& indices = iter->second; |
| 938 |
if (idx.getIndex() < (int)indices.names.size()) { |
| 939 |
const MappedNameRef& ref = indices.names[idx.getIndex()]; |
| 940 |
int count = 0; |
| 941 |
for (auto nameRef = &ref; nameRef; nameRef = nameRef->next.get()) { |
| 942 |
if (nameRef->name) { |
| 943 |
++count; |
| 944 |
} |
| 945 |
} |
| 946 |
if (count != 0) { |
| 947 |
res.reserve(count); |
| 948 |
for (auto nameRef = &ref; nameRef; nameRef = nameRef->next.get()) { |
| 949 |
if (nameRef->name) { |
| 950 |
res.emplace_back(nameRef->name, nameRef->sids); |
| 951 |
} |
| 952 |
} |
| 953 |
return res; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
auto it = indices.children.upper_bound(idx.getIndex()); |
| 958 |
if (it != indices.children.end() |
| 959 |
&& it->second.indexedName.getIndex() + it->second.offset <= idx.getIndex()) { |
| 960 |
auto& child = it->second; |
| 961 |
IndexedName childIdx(idx.getType(), idx.getIndex() - child.offset); |
| 962 |
if (child.elementMap) { |
| 963 |
res = child.elementMap->findAll(childIdx); |
| 964 |
for (auto& v : res) { |
| 965 |
v.first += child.postfix; |
| 966 |
} |
| 967 |
} |
| 968 |
else { |
| 969 |
res.emplace_back(MappedName(childIdx) + child.postfix, ElementIDRefs()); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
return res; |
| 974 |
} |
| 975 |
|
| 976 |
const MappedNameRef* ElementMap::findMappedRef(const IndexedName& idx) const |
| 977 |
{ |
| 978 |
auto iter = this->indexedNames.find(idx.getType()); |
| 979 |
if (iter == this->indexedNames.end()) { |
| 980 |
return nullptr; |
| 981 |
} |
| 982 |
auto& indices = iter->second; |
| 983 |
if (idx.getIndex() >= (int)indices.names.size()) { |
| 984 |
return nullptr; |
| 985 |
} |
| 986 |
return &indices.names[idx.getIndex()]; |
| 987 |
} |
| 988 |
|
| 989 |
MappedNameRef* ElementMap::findMappedRef(const IndexedName& idx) |
| 990 |
{ |
| 991 |
auto iter = this->indexedNames.find(idx.getType()); |
| 992 |
if (iter == this->indexedNames.end()) { |
| 993 |
return nullptr; |
| 994 |
} |
| 995 |
auto& indices = iter->second; |
| 996 |
if (idx.getIndex() >= (int)indices.names.size()) { |
| 997 |
return nullptr; |
| 998 |
} |
| 999 |
return &indices.names[idx.getIndex()]; |
| 1000 |
} |
| 1001 |
|
| 1002 |
MappedNameRef& ElementMap::mappedRef(const IndexedName& idx) |
| 1003 |
{ |
| 1004 |
assert(idx); |
| 1005 |
auto& indices = this->indexedNames[idx.getType()]; |
| 1006 |
if (idx.getIndex() >= (int)indices.names.size()) { |
| 1007 |
indices.names.resize(idx.getIndex() + 1); |
| 1008 |
} |
| 1009 |
return indices.names[idx.getIndex()]; |
| 1010 |
} |
| 1011 |
|
| 1012 |
bool ElementMap::hasChildElementMap() const |
| 1013 |
{ |
| 1014 |
return !childElements.empty(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
void ElementMap::hashChildMaps(long masterTag) |
| 1018 |
{ |
| 1019 |
if (childElements.empty() || !this->hasher) { |
| 1020 |
return; |
| 1021 |
} |
| 1022 |
std::ostringstream ss; |
| 1023 |
for (auto& indexedNameIndexedElements : this->indexedNames) { |
| 1024 |
for (auto& indexedChild : indexedNameIndexedElements.second.children) { |
| 1025 |
auto& child = indexedChild.second; |
| 1026 |
int len = 0; |
| 1027 |
long tag = 0; |
| 1028 |
int pos = MappedName::fromRawData(child.postfix) |
| 1029 |
.findTagInElementName(&tag, &len, nullptr, nullptr, false, false); |
| 1030 |
// TODO: What is this 10? |
| 1031 |
if (pos > 10) { |
| 1032 |
MappedName postfix = |
| 1033 |
hashElementName(MappedName::fromRawData(child.postfix.constData(), pos), |
| 1034 |
child.sids); |
| 1035 |
ss.str(""); |
| 1036 |
ss << MAPPED_CHILD_ELEMENTS_PREFIX << postfix; |
| 1037 |
MappedName tmp; |
| 1038 |
encodeElementName(child.indexedName[0], |
| 1039 |
tmp, |
| 1040 |
ss, |
| 1041 |
nullptr, |
| 1042 |
masterTag, |
| 1043 |
nullptr, |
| 1044 |
child.tag, |
| 1045 |
true); |
| 1046 |
this->childElements.remove(child.postfix); |
| 1047 |
child.postfix = tmp.toBytes(); |
| 1048 |
this->childElements[child.postfix].childMap = &child; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
void ElementMap::collectChildMaps(std::map<const ElementMap*, int>& childMapSet, |
| 1055 |
std::vector<const ElementMap*>& childMaps, |
| 1056 |
std::map<QByteArray, int>& postfixMap, |
| 1057 |
std::vector<QByteArray>& postfixes) const |
| 1058 |
{ |
| 1059 |
auto res = childMapSet.insert(std::make_pair(this, 0)); |
| 1060 |
if (!res.second) { |
| 1061 |
return; |
| 1062 |
} |
| 1063 |
|
| 1064 |
for (auto& indexedName : this->indexedNames) { |
| 1065 |
addPostfix(QByteArray::fromRawData(indexedName.first, |
| 1066 |
static_cast<int>(qstrlen(indexedName.first))), |
| 1067 |
postfixMap, |
| 1068 |
postfixes); |
| 1069 |
|
| 1070 |
for (auto& childPair : indexedName.second.children) { |
| 1071 |
auto& child = childPair.second; |
| 1072 |
if (child.elementMap) { |
| 1073 |
child.elementMap->collectChildMaps(childMapSet, childMaps, postfixMap, postfixes); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
for (auto& mappedName : this->mappedNames) { |
| 1079 |
addPostfix(mappedName.first.constPostfix(), postfixMap, postfixes); |
| 1080 |
} |
| 1081 |
|
| 1082 |
childMaps.push_back(this); |
| 1083 |
res.first->second = (int)childMaps.size(); |
| 1084 |
} |
| 1085 |
|
| 1086 |
void ElementMap::addChildElements(long masterTag, const std::vector<MappedChildElements>& children) |
| 1087 |
{ |
| 1088 |
std::ostringstream ss; |
| 1089 |
ss << std::hex; |
| 1090 |
|
| 1091 |
// To avoid possibly very long recursive child map lookup, resulting very |
| 1092 |
// long mapped names, we try to resolve the grand child map now. |
| 1093 |
std::vector<MappedChildElements> expansion; |
| 1094 |
for (auto it = children.begin(); it != children.end(); ++it) { |
| 1095 |
auto& child = *it; |
| 1096 |
if (!child.elementMap || child.elementMap->childElements.empty()) { |
| 1097 |
if (!expansion.empty()) { |
| 1098 |
expansion.push_back(child); |
| 1099 |
} |
| 1100 |
continue; |
| 1101 |
} |
| 1102 |
auto& indices = child.elementMap->indexedNames[child.indexedName.getType()]; |
| 1103 |
if (indices.children.empty()) { |
| 1104 |
if (!expansion.empty()) { |
| 1105 |
expansion.push_back(child); |
| 1106 |
} |
| 1107 |
continue; |
| 1108 |
} |
| 1109 |
|
| 1110 |
// Note that it is allowable to have both mapped names and child map. We |
| 1111 |
// may have to split the current child mapping into pieces. |
| 1112 |
|
| 1113 |
int start = child.indexedName.getIndex(); |
| 1114 |
int end = start + child.count; |
| 1115 |
for (auto iter = indices.children.upper_bound(start); iter != indices.children.end(); |
| 1116 |
++iter) { |
| 1117 |
auto& grandchild = iter->second; |
| 1118 |
int istart = grandchild.indexedName.getIndex() + grandchild.offset; |
| 1119 |
int iend = istart + grandchild.count; |
| 1120 |
if (end <= istart) { |
| 1121 |
break; |
| 1122 |
} |
| 1123 |
if (istart >= end) { |
| 1124 |
if (!expansion.empty()) { |
| 1125 |
expansion.push_back(child); |
| 1126 |
expansion.back().indexedName.setIndex(start); |
| 1127 |
expansion.back().count = end - start; |
| 1128 |
} |
| 1129 |
break; |
| 1130 |
} |
| 1131 |
if (expansion.empty()) { |
| 1132 |
const int extra {10}; |
| 1133 |
expansion.reserve(children.size() + extra); |
| 1134 |
expansion.insert(expansion.end(), children.begin(), it); |
| 1135 |
} |
| 1136 |
expansion.push_back(child); |
| 1137 |
auto* entry = &expansion.back(); |
| 1138 |
if (istart > start) { |
| 1139 |
entry->indexedName.setIndex(start); |
| 1140 |
entry->count = istart - start; |
| 1141 |
|
| 1142 |
expansion.push_back(child); |
| 1143 |
entry = &expansion.back(); |
| 1144 |
} |
| 1145 |
else { |
| 1146 |
istart = start; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (iend > end) { |
| 1150 |
iend = end; |
| 1151 |
} |
| 1152 |
|
| 1153 |
entry->indexedName.setIndex(istart - grandchild.offset); |
| 1154 |
entry->count = iend - istart; |
| 1155 |
entry->offset += grandchild.offset; |
| 1156 |
entry->elementMap = grandchild.elementMap; |
| 1157 |
entry->sids += grandchild.sids; |
| 1158 |
if (grandchild.postfix.size() != 0) { |
| 1159 |
if ((entry->postfix.size() != 0) |
| 1160 |
&& !entry->postfix.startsWith(ELEMENT_MAP_PREFIX)) { |
| 1161 |
entry->postfix = grandchild.postfix + ELEMENT_MAP_PREFIX + entry->postfix; |
| 1162 |
} |
| 1163 |
else { |
| 1164 |
entry->postfix = grandchild.postfix + entry->postfix; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
start = iend; |
| 1169 |
if (start >= end) { |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
if (!expansion.empty() && start < end) { |
| 1174 |
expansion.push_back(child); |
| 1175 |
expansion.back().indexedName.setIndex(start); |
| 1176 |
expansion.back().count = end - start; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
for (auto& child : expansion.empty() ? children : expansion) { |
| 1181 |
if (!child.indexedName || (child.count == 0)) { |
| 1182 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 1183 |
FC_ERR("invalid mapped child element"); // NOLINT |
| 1184 |
} |
| 1185 |
continue; |
| 1186 |
} |
| 1187 |
|
| 1188 |
ss.str(""); |
| 1189 |
MappedName tmp; |
| 1190 |
|
| 1191 |
ChildMapInfo* entry = nullptr; |
| 1192 |
|
| 1193 |
// do child mapping only if the child element count >= 5 |
| 1194 |
const int threshold {5}; |
| 1195 |
|
| 1196 |
// skip encoding only when masterTag=0, child.tag=0, and count is exactly at threshold |
| 1197 |
bool skipEncoding = (masterTag == 0 && child.tag == 0 && child.count == threshold && child.elementMap); |
| 1198 |
|
| 1199 |
if ((child.count >= threshold && !skipEncoding) || !child.elementMap) { |
| 1200 |
encodeElementName(child.indexedName[0], |
| 1201 |
tmp, |
| 1202 |
ss, |
| 1203 |
nullptr, |
| 1204 |
masterTag, |
| 1205 |
child.postfix.constData(), |
| 1206 |
child.tag, |
| 1207 |
true); |
| 1208 |
|
| 1209 |
// Perform some disambiguation in case the same shape is mapped |
| 1210 |
// multiple times, e.g. draft array. |
| 1211 |
entry = &childElements[tmp.toBytes()]; |
| 1212 |
int mapIndex = entry->mapIndices[child.elementMap.get()]++; |
| 1213 |
++entry->index; |
| 1214 |
if (entry->index != 1 && child.elementMap && mapIndex == 0) { |
| 1215 |
// This child has duplicated 'tag' and 'postfix', but it |
| 1216 |
// has its own element map. We'll expand this map now. |
| 1217 |
entry = nullptr; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
if (!entry) { |
| 1222 |
IndexedName childIdx(child.indexedName); |
| 1223 |
IndexedName idx(childIdx.getType(), childIdx.getIndex() + child.offset); |
| 1224 |
for (int i = 0; i < child.count; ++i, ++childIdx, ++idx) { |
| 1225 |
ElementIDRefs sids; |
| 1226 |
MappedName name = child.elementMap->find(childIdx, &sids); |
| 1227 |
if (!name) { |
| 1228 |
if ((child.tag == 0) || child.tag == masterTag) { |
| 1229 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 1230 |
FC_WARN("unmapped element"); // NOLINT |
| 1231 |
} |
| 1232 |
continue; |
| 1233 |
} |
| 1234 |
name = MappedName(childIdx); |
| 1235 |
} |
| 1236 |
ss.str(""); |
| 1237 |
encodeElementName(idx[0], |
| 1238 |
name, |
| 1239 |
ss, |
| 1240 |
&sids, |
| 1241 |
masterTag, |
| 1242 |
child.postfix.constData(), |
| 1243 |
child.tag); |
| 1244 |
setElementName(idx, name, masterTag, &sids); |
| 1245 |
} |
| 1246 |
continue; |
| 1247 |
} |
| 1248 |
|
| 1249 |
if (entry->index != 1) { |
| 1250 |
// There is some ambiguity in child mapping. We need some |
| 1251 |
// additional postfix for disambiguation. NOTE: We are not |
| 1252 |
// using ComplexGeoData::indexPostfix() so we don't confuse |
| 1253 |
// other code that actually uses this postfix for indexing |
| 1254 |
// purposes. Here, we just need some postfix for |
| 1255 |
// disambiguation. We don't need to extract the index. |
| 1256 |
ss.str(""); |
| 1257 |
ss << ELEMENT_MAP_PREFIX << ":C" << entry->index - 1; |
| 1258 |
|
| 1259 |
tmp.clear(); |
| 1260 |
encodeElementName(child.indexedName[0], |
| 1261 |
tmp, |
| 1262 |
ss, |
| 1263 |
nullptr, |
| 1264 |
masterTag, |
| 1265 |
child.postfix.constData(), |
| 1266 |
child.tag, |
| 1267 |
true); |
| 1268 |
|
| 1269 |
entry = &childElements[tmp.toBytes()]; |
| 1270 |
if (entry->childMap) { |
| 1271 |
FC_ERR("duplicate mapped child element"); // NOLINT |
| 1272 |
continue; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
auto& indices = this->indexedNames[child.indexedName.getType()]; |
| 1277 |
auto res = |
| 1278 |
indices.children.emplace(child.indexedName.getIndex() + child.offset + child.count, |
| 1279 |
child); |
| 1280 |
if (!res.second) { |
| 1281 |
if (!entry->childMap) { |
| 1282 |
this->childElements.remove(tmp.toBytes()); |
| 1283 |
} |
| 1284 |
FC_ERR("duplicate mapped child element"); // NOLINT |
| 1285 |
continue; |
| 1286 |
} |
| 1287 |
|
| 1288 |
auto& insertedChild = res.first->second; |
| 1289 |
insertedChild.postfix = tmp.toBytes(); |
| 1290 |
entry->childMap = &insertedChild; |
| 1291 |
childElementSize += insertedChild.count; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
std::vector<ElementMap::MappedChildElements> ElementMap::getChildElements() const |
| 1296 |
{ |
| 1297 |
std::vector<MappedChildElements> res; |
| 1298 |
res.reserve(this->childElements.size()); |
| 1299 |
for (auto& childElement : this->childElements) { |
| 1300 |
res.push_back(*childElement.childMap); |
| 1301 |
} |
| 1302 |
return res; |
| 1303 |
} |
| 1304 |
|
| 1305 |
std::vector<MappedElement> ElementMap::getAll() const |
| 1306 |
{ |
| 1307 |
std::vector<MappedElement> ret; |
| 1308 |
ret.reserve(size()); |
| 1309 |
for (auto& mappedName : this->mappedNames) { |
| 1310 |
ret.emplace_back(mappedName.first, mappedName.second); |
| 1311 |
} |
| 1312 |
for (auto& childElement : this->childElements) { |
| 1313 |
auto& child = *childElement.childMap; |
| 1314 |
IndexedName idx(child.indexedName); |
| 1315 |
idx.setIndex(idx.getIndex() + child.offset); |
| 1316 |
IndexedName childIdx(child.indexedName); |
| 1317 |
for (int i = 0; i < child.count; ++i, ++idx, ++childIdx) { |
| 1318 |
MappedName name; |
| 1319 |
if (child.elementMap) { |
| 1320 |
name = child.elementMap->find(childIdx); |
| 1321 |
} |
| 1322 |
else { |
| 1323 |
name = MappedName(childIdx); |
| 1324 |
} |
| 1325 |
if (name) { |
| 1326 |
name += child.postfix; |
| 1327 |
ret.emplace_back(name, idx); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |
| 1331 |
return ret; |
| 1332 |
} |
| 1333 |
|
| 1334 |
long ElementMap::getElementHistory(const MappedName& name, |
| 1335 |
long masterTag, |
| 1336 |
MappedName* original, |
| 1337 |
std::vector<MappedName>* history) const |
| 1338 |
{ |
| 1339 |
long tag = 0; |
| 1340 |
int len = 0; |
| 1341 |
int pos = name.findTagInElementName(&tag, &len, nullptr, nullptr, true); |
| 1342 |
if (pos < 0) { |
| 1343 |
if (original) { |
| 1344 |
*original = name; |
| 1345 |
} |
| 1346 |
return tag; |
| 1347 |
} |
| 1348 |
if (!original && !history) { |
| 1349 |
return tag; |
| 1350 |
} |
| 1351 |
|
| 1352 |
MappedName tmp; |
| 1353 |
MappedName& ret = original ? *original : tmp; |
| 1354 |
if (name.startsWith(ELEMENT_MAP_PREFIX)) { |
| 1355 |
unsigned offset = ELEMENT_MAP_PREFIX_SIZE; |
| 1356 |
ret = MappedName::fromRawData(name, static_cast<int>(offset)); |
| 1357 |
} |
| 1358 |
else { |
| 1359 |
ret = name; |
| 1360 |
} |
| 1361 |
|
| 1362 |
while (true) { |
| 1363 |
if ((len == 0) || len > pos) { |
| 1364 |
FC_WARN("invalid name length " << name); // NOLINT |
| 1365 |
return 0; |
| 1366 |
} |
| 1367 |
bool deHashed = false; |
| 1368 |
if (ret.startsWith(MAPPED_CHILD_ELEMENTS_PREFIX, len)) { |
| 1369 |
int offset = (int)POSTFIX_TAG_SIZE; |
| 1370 |
MappedName tmp2 = MappedName::fromRawData(ret, len + offset, pos - len - offset); |
| 1371 |
MappedName postfix = dehashElementName(tmp2); |
| 1372 |
if (postfix != tmp2) { |
| 1373 |
deHashed = true; |
| 1374 |
ret = MappedName::fromRawData(ret, 0, len) + postfix; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
if (!deHashed) { |
| 1378 |
ret = dehashElementName(MappedName::fromRawData(ret, 0, len)); |
| 1379 |
} |
| 1380 |
|
| 1381 |
long tag2 = 0; |
| 1382 |
pos = ret.findTagInElementName(&tag2, &len, nullptr, nullptr, true); |
| 1383 |
if (pos < 0 || (tag2 != tag && tag2 != -tag && tag != masterTag && -tag != masterTag)) { |
| 1384 |
return tag; |
| 1385 |
} |
| 1386 |
tag = tag2; |
| 1387 |
if (history) { |
| 1388 |
history->push_back(ret.copy()); |
| 1389 |
} |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
void ElementMap::traceElement(const MappedName& name, long masterTag, TraceCallback cb) const |
| 1394 |
{ |
| 1395 |
long encodedTag = 0; |
| 1396 |
int len = 0; |
| 1397 |
|
| 1398 |
auto pos = name.findTagInElementName(&encodedTag, &len, nullptr, nullptr, true); |
| 1399 |
if (cb(name, len, encodedTag, masterTag) || pos < 0) { |
| 1400 |
return; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (name.startsWith(POSTFIX_EXTERNAL_TAG, len)) { |
| 1404 |
return; |
| 1405 |
} |
| 1406 |
|
| 1407 |
std::set<long> tagSet; |
| 1408 |
|
| 1409 |
std::vector<MappedName> names; |
| 1410 |
if (masterTag) { |
| 1411 |
tagSet.insert(std::abs(masterTag)); |
| 1412 |
} |
| 1413 |
if (encodedTag) { |
| 1414 |
tagSet.insert(std::abs(encodedTag)); |
| 1415 |
} |
| 1416 |
names.push_back(name); |
| 1417 |
|
| 1418 |
masterTag = encodedTag; |
| 1419 |
MappedName tmp; |
| 1420 |
bool first = true; |
| 1421 |
|
| 1422 |
// TODO: element tracing without object is inherently unsafe, because of |
| 1423 |
// possible external linking object which means the element may be encoded |
| 1424 |
// using external string table. Looking up the wrong table may accidentally |
| 1425 |
// cause circular mapping, and is actually quite easy to reproduce. See |
| 1426 |
// |
| 1427 |
// https://github.com/realthunder/FreeCAD_assembly3/issues/968 |
| 1428 |
// |
| 1429 |
// An arbitrary depth limit is set here to not waste time. 'tagSet' above is |
| 1430 |
// also used for early detection of 'recursive' mapping. |
| 1431 |
|
| 1432 |
for (int index = 0; index < 50; ++index) { |
| 1433 |
if (!len || len > pos) { |
| 1434 |
return; |
| 1435 |
} |
| 1436 |
if (first) { |
| 1437 |
first = false; |
| 1438 |
size_t offset = 0; |
| 1439 |
if (name.startsWith(ELEMENT_MAP_PREFIX)) { |
| 1440 |
offset = ELEMENT_MAP_PREFIX_SIZE; |
| 1441 |
} |
| 1442 |
tmp = MappedName(name, offset, len); |
| 1443 |
} |
| 1444 |
else { |
| 1445 |
tmp = MappedName(tmp, 0, len); |
| 1446 |
} |
| 1447 |
tmp = dehashElementName(tmp); |
| 1448 |
names.push_back(tmp); |
| 1449 |
encodedTag = 0; |
| 1450 |
pos = tmp.findTagInElementName(&encodedTag, &len, nullptr, nullptr, true); |
| 1451 |
if (pos >= 0 && tmp.startsWith(POSTFIX_EXTERNAL_TAG, len)) { |
| 1452 |
break; |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (encodedTag && masterTag != std::abs(encodedTag) |
| 1456 |
&& !tagSet.insert(std::abs(encodedTag)).second) { |
| 1457 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 1458 |
FC_WARN("circular element mapping"); |
| 1459 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_TRACE)) { |
| 1460 |
auto doc = App::GetApplication().getActiveDocument(); |
| 1461 |
if (doc) { |
| 1462 |
auto obj = doc->getObjectByID(masterTag); |
| 1463 |
if (obj) { |
| 1464 |
FC_LOG("\t" << obj->getFullName() << obj->getFullName() << "." << name); |
| 1465 |
} |
| 1466 |
} |
| 1467 |
for (auto& errname : names) { |
| 1468 |
FC_ERR("\t" << errname); |
| 1469 |
} |
| 1470 |
} |
| 1471 |
} |
| 1472 |
break; |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (cb(tmp, len, encodedTag, masterTag) || pos < 0) { |
| 1476 |
return; |
| 1477 |
} |
| 1478 |
masterTag = encodedTag; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
|
| 1483 |
} // Namespace Data |