UniqueNameGenerator.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file UniqueNameGenerator.cpp
  35. * @brief Implementation for the unique name generator.
  36. */
  37. #include "UniqueNameGenerator.h"
  38. #include <algorithm>
  39. #include <list>
  40. #include <map>
  41. #include <numeric>
  42. namespace Assimp {
  43. namespace MDL {
  44. namespace HalfLife {
  45. UniqueNameGenerator::UniqueNameGenerator() :
  46. template_name_("unnamed"),
  47. separator_("_") {
  48. }
  49. UniqueNameGenerator::UniqueNameGenerator(const char *template_name) :
  50. template_name_(template_name),
  51. separator_("_") {
  52. }
  53. UniqueNameGenerator::UniqueNameGenerator(const char *template_name, const char *separator) :
  54. template_name_(template_name),
  55. separator_(separator) {
  56. }
  57. UniqueNameGenerator::~UniqueNameGenerator() = default;
  58. void UniqueNameGenerator::make_unique(std::vector<std::string> &names) {
  59. struct DuplicateInfo {
  60. DuplicateInfo() :
  61. indices(),
  62. next_id(0) {
  63. }
  64. std::list<size_t> indices;
  65. size_t next_id;
  66. };
  67. std::vector<size_t> empty_names_indices;
  68. std::vector<size_t> template_name_duplicates;
  69. std::map<std::string, DuplicateInfo> names_to_duplicates;
  70. const std::string template_name_with_separator(template_name_ + separator_);
  71. auto format_name = [&](const std::string &base_name, size_t id) -> std::string {
  72. return base_name + separator_ + std::to_string(id);
  73. };
  74. auto generate_unique_name = [&](const std::string &base_name) -> std::string {
  75. auto *duplicate_info = &names_to_duplicates[base_name];
  76. std::string new_name;
  77. bool found_identical_name;
  78. bool tried_with_base_name_only = false;
  79. do {
  80. // Assume that no identical name exists.
  81. found_identical_name = false;
  82. if (!tried_with_base_name_only) {
  83. // First try with only the base name.
  84. new_name = base_name;
  85. } else {
  86. // Create the name expected to be unique.
  87. new_name = format_name(base_name, duplicate_info->next_id);
  88. }
  89. // Check in the list of duplicates for an identical name.
  90. for (size_t i = 0;
  91. i < names.size() &&
  92. !found_identical_name;
  93. ++i) {
  94. if (new_name == names[i])
  95. found_identical_name = true;
  96. }
  97. if (tried_with_base_name_only)
  98. ++duplicate_info->next_id;
  99. tried_with_base_name_only = true;
  100. } while (found_identical_name);
  101. return new_name;
  102. };
  103. for (size_t i = 0; i < names.size(); ++i) {
  104. // Check for empty names.
  105. if (names[i].find_first_not_of(' ') == std::string::npos) {
  106. empty_names_indices.push_back(i);
  107. continue;
  108. }
  109. /* Check for potential duplicate.
  110. a) Either if this name is the same as the template name or
  111. b) <template name><separator> is found at the beginning. */
  112. if (names[i] == template_name_ ||
  113. names[i].substr(0, template_name_with_separator.length()) == template_name_with_separator)
  114. template_name_duplicates.push_back(i);
  115. // Map each unique name to it's duplicate.
  116. if (names_to_duplicates.count(names[i]) == 0)
  117. names_to_duplicates.insert({ names[i], DuplicateInfo()});
  118. else
  119. names_to_duplicates[names[i]].indices.push_back(i);
  120. }
  121. // Make every non-empty name unique.
  122. for (auto it = names_to_duplicates.begin();
  123. it != names_to_duplicates.end(); ++it) {
  124. for (auto it2 = it->second.indices.begin();
  125. it2 != it->second.indices.end();
  126. ++it2)
  127. names[*it2] = generate_unique_name(it->first);
  128. }
  129. // Generate a unique name for every empty string.
  130. if (template_name_duplicates.size()) {
  131. // At least one string ressembles to <template name>.
  132. for (auto it = empty_names_indices.begin();
  133. it != empty_names_indices.end(); ++it)
  134. names[*it] = generate_unique_name(template_name_);
  135. } else {
  136. // No string alike <template name> exists.
  137. size_t i = 0;
  138. for (auto it = empty_names_indices.begin();
  139. it != empty_names_indices.end(); ++it, ++i)
  140. names[*it] = format_name(template_name_, i);
  141. }
  142. }
  143. } // namespace HalfLife
  144. } // namespace MDL
  145. } // namespace Assimp