31#include <fmt/format.h>
75 if (this->mask.
size() <= number) {
76 this->mask.
resize(number + 1);
78 if (this->mask[number]) {
79 if (!this->numbers_multi_usages.has_value()) {
80 this->numbers_multi_usages.emplace();
82 int &multi_usages_num = this->numbers_multi_usages->lookup_or_add(number, 1);
87 this->mask[number].set(
true);
91 if (this->max_value_in_use) {
95 this->max_value_in_use = number;
105 "Trying to unregister a number suffix higher than current size of the bit "
106 "vector, should never happen.");
108 if (this->numbers_multi_usages.has_value() && this->numbers_multi_usages->contains(number)) {
109 int &multi_usages_num = this->numbers_multi_usages->lookup(number);
112 if (multi_usages_num == 1) {
113 this->numbers_multi_usages->remove_contained(number);
120 this->mask[number].set(
false);
124 this->max_value_in_use.value()--;
127 this->max_value_in_use.reset();
140 if (this->mask.
size() < 2) {
148 std::optional<int64_t>
result = find_first_0_index(search_mask);
155 return int(this->mask.
size());
157 if (this->max_value_in_use) {
158 if (this->max_value_in_use.value() + 1 <=
MAX_NUMBER) {
159 return this->max_value_in_use.value() + 1;
195 type_map.full_names.clear();
196 type_map.base_name_to_num_suffix.clear();
200 if ((
id == ignore_id) || (!this->is_global && (
id->lib !=
library))) {
206 if (this->is_global) {
224 "The key (name) already exists in the namemap, should only happen when "
225 "`do_global` is true.");
238 val->mark_used(number);
252 if (this->is_global) {
258 val->mark_used(number);
266 false,
"Adding a name to Main namemaps that was already in it, should never happen.");
272 val->mark_used(number);
287 std::unique_ptr<UniqueName_Value> *val =
nullptr;
289 if (this->is_global) {
294 false,
"Removing a name from Main namemaps that was not in it, should never happen.");
315 false,
"Removing a name from Main namemaps that was not in it, should never happen.");
326 val->get()->mark_unused(number);
327 if (!val->get()->max_value_in_use) {
341 if (*r_name_map ==
nullptr) {
344#ifdef DEBUG_PRINT_MEMORY_USAGE
351 printf(
"NameMap memory usage: full_names %.1fKB, base_names %.1fKB\n",
352 size_full_names / 1024.0,
353 size_base_names / 1024.0);
355 MEM_SAFE_DELETE(*r_name_map);
360 auto bmain_namemap_clear = [](
Main *bmain_iter) ->
void {
363 for (
Library *lib_iter = bmain_iter->libraries.first(); lib_iter !=
nullptr;
364 lib_iter =
static_cast<Library *
>(lib_iter->id.next))
372 "Main should always be part of its own `split_mains`");
374 bmain_namemap_clear(bmain_iter);
378 bmain_namemap_clear(&bmain);
388 const bool ensure_created)
404 const bool ensure_created)
406 if (lib !=
nullptr) {
413 if (ensure_created && bmain.
name_map ==
nullptr) {
414 bmain.
name_map = MEM_new<UniqueName_Map>(__func__,
false);
455 std::string &r_name_final)
461 r_name_final = fmt::format(
"{}.{:03}", base_name, number);
472 r_name_final = base_name;
476 while (r_name_final.size() > 8) {
479 BLI_strncpy(base_name_modified, r_name_final.c_str(), r_name_final.size() + 1);
480 base_name_modified[r_name_final.size() - 1] =
'\0';
485 r_name_final = base_name_modified;
488 if (!val || val->get()->max_value_in_use.value_or(0) <
MAX_NUMBER) {
495 const StringRef new_base_name = r_name_final;
496 r_name_final = fmt::format(
"{}_{:03}", r_name_final, suffix);
497 while (r_name_final.size() <
MAX_ID_NAME - 2 - 12) {
500 if (!val || val->get()->max_value_in_use.value_or(0) <
MAX_NUMBER) {
504 r_name_final = fmt::format(
"{}_{:03}", new_base_name, suffix);
512 "Impossible to find an available name for '%s' base name, even by editing that base "
513 "name. This should never happen in real-life scenarii. Now trying to brute-force "
514 "generate random names until a free one is found.",
521 if (!val || val->get()->max_value_in_use.value_or(0) <
MAX_NUMBER) {
529 std::string &r_name_full,
530 const bool do_unique_in_bmain)
541 bool is_name_changed =
false;
558 name_map->
add_name(type_map, r_name_full, name_base, number);
559 if (name_map_other !=
nullptr) {
560 name_map_other->
add_name(
GS(
id.name), r_name_full, name_base, number);
562 return is_name_changed;
568 is_name_changed =
true;
573 const int number_to_use = val->get_smallest_unused();
585 name_map->
add_name(type_map, r_name_full, name_base, number_to_use);
586 if (name_map_other !=
nullptr) {
587 name_map_other->
add_name(
GS(
id.name), r_name_full, name_base, number_to_use);
589 return is_name_changed;
595 std::string r_name_full = r_name;
597 const bool is_name_modified =
namemap_get_name(bmain,
id, r_name_full,
false);
600 return is_name_modified;
604 std::string r_name_full = r_name;
606 const bool is_name_modified =
namemap_get_name(bmain,
id, r_name_full,
true);
609 return is_name_modified;
619 const short id_code =
GS(
id.name);
622 if (name_map_local) {
627 if (name_map_global) {
641 return a.
lib ==
b.lib && a.
name ==
b.name;
649 bool is_valid =
true;
650 ListBaseT<ID> *lb_iter;
652 for (
ID &id_iter : lb_iter->items_mutable()) {
653 if (id_validated.
contains(&id_iter)) {
659 if (!id_names_libs.
add(key)) {
663 "ID name '%s' (from library '%s') is found more than once",
665 id_iter.lib !=
nullptr ? id_iter.lib->filepath :
"<None>");
676 key.
name = id_iter.name;
677 if (!id_names_libs.
add(key)) {
681 "\tID has been renamed to '%s', but it still seems to be already in use",
685 CLOG_WARN(&
LOG,
"\tID has been renamed to '%s'", id_iter.name);
686 id_validated.
add(&id_iter);
691 "ID name '%s' (from library '%s') is found more than once",
693 id_iter.lib !=
nullptr ? id_iter.lib->filepath :
"<None>");
698 if (name_map ==
nullptr) {
710 "ID name '%s' (from library '%s') exists in current Main, but is not listed in "
713 id_iter.lib !=
nullptr ? id_iter.lib->filepath :
"<None>");
718 "ID name '%s' (from library '%s') exists in current Main, but is not listed in "
721 id_iter.lib !=
nullptr ? id_iter.lib->filepath :
"<None>");
739 fmt::format(
"{}{}",
StringRef{
reinterpret_cast<char *
>(&idcode), 2}, id_name), lib};
744 "ID name '%s' (from library '%s') is listed in the namemap, but does not "
745 "exists in current Main",
747 lib !=
nullptr ? lib->
filepath :
"<None>");
751 "ID name '%s' (from library '%s') is listed in the namemap, but does not "
752 "exists in current Main",
754 lib !=
nullptr ? lib->
filepath :
"<None>");
763 }
while (lib !=
nullptr);
765 if (is_valid || !do_fix) {
#define FOREACH_MAIN_ID_END
#define FOREACH_MAIN_LISTBASE_END
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
ID and Library types, which are fundamental for SDNA.
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
const Value * lookup_ptr(const Key &key) const
int64_t size_in_bytes() const
bool add(const Key &key, const Value &value)
Value & lookup_or_add_as(ForwardKey &&key, ForwardValue &&...value)
void remove_contained(const Key &key)
bool remove(const Key &key)
KeyIterator keys() const &
bool contains(const Key &key) const
bool contains(const Key &key) const
constexpr const char * c_str() const
void resize(const int64_t new_size_in_bits, const bool value=false)
constexpr bool is_empty() const
constexpr int64_t size() const
void max_inplace(T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static UniqueName_Map * get_global_namemap_for(Main &bmain, ID *ignore_id, const bool ensure_created)
unsigned __int64 uint64_t
constexpr int NO_AVAILABLE_NUMBER
static bool main_namemap_validate_and_fix(Main &bmain, const bool do_fix)
static UniqueName_Map * get_namemap_for(Main &bmain, Library *lib, ID *ignore_id, const bool ensure_created)
void BKE_main_namemap_destroy(UniqueName_Map **r_name_map) ATTR_NONNULL()
bool BKE_main_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
bool BKE_main_global_namemap_contain_name(Main &bmain, short id_type, StringRef name)
bool BKE_main_namemap_contain_name(Main &bmain, Library *lib, short id_type, StringRef name)
const char * BKE_id_name(const ID &id)
void BKE_main_namemap_remove_id(Main &bmain, ID &id)
static bool namemap_get_name(Main &bmain, ID &id, std::string &r_name_full, const bool do_unique_in_bmain)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
short BKE_idtype_idcode_iter_step(int *idtype_index)
bool BKE_main_namemap_validate_and_fix(Main &bmain)
int BKE_idtype_idcode_to_index(short idcode)
void BKE_main_namemap_clear(Main &bmain)
bool BKE_main_namemap_validate(Main &bmain)
static bool id_name_final_build(UniqueName_TypeMap &type_map, const StringRefNull base_name, const int number, std::string &r_name_final)
bool BKE_main_global_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
ID_Type GS(const char *name)
size_t BLI_string_split_name_number(const char *name, char delim, char *r_name_left, int *r_number) ATTR_NONNULL(1
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
IDNewNameResult BKE_id_new_name_validate(Main &bmain, ListBaseT< ID > &lb, ID &id, const char *newname, IDNewNameMode mode, bool do_linked_data)
ListBaseT< ID > * which_libbase(Main *bmain, short type)
bke::library::LibraryRuntime * runtime
std::shared_ptr< VectorSet< Main * > > split_mains
UniqueName_Map * name_map_global
ListBaseT< Library > libraries
UniqueName_Map * name_map
void remove_full_name(UniqueName_TypeMap &type_map, StringRef name_full)
UniqueName_TypeMap & find_by_type(const short id_type)
void add_name(const short id_type, StringRef name_full, StringRef name_base, const int number)
void remove_full_name(const short id_type, StringRef name_full)
void add_name(UniqueName_TypeMap &type_map, StringRef name_full, StringRef name_base, const int number)
void populate(Main &bmain, Library *library, ID *ignore_id)
UniqueName_Map(const bool is_global)
std::array< UniqueName_TypeMap, INDEX_ID_MAX > type_maps
Map< std::string, int > full_names
Map< std::string, std::unique_ptr< UniqueName_Value > > base_name_to_num_suffix
int get_smallest_unused()
void mark_used(const int number)
static constexpr int max_exact_tracking
std::optional< Map< int, int > > numbers_multi_usages
std::optional< int > max_value_in_use
void mark_unused(const int number)
friend bool operator==(const Uniqueness_Key &a, const Uniqueness_Key &b)
UniqueName_Map * name_map