diff options
Diffstat (limited to 'source/blender/blenlib/BLI_map.hh')
-rw-r--r-- | source/blender/blenlib/BLI_map.hh | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh index 56ec5b7010c..5819430fe97 100644 --- a/source/blender/blenlib/BLI_map.hh +++ b/source/blender/blenlib/BLI_map.hh @@ -514,6 +514,38 @@ class Map { } /** + * Returns a reference to the value corresponding to the given key. If the key is not in the map, + * a new key-value-pair is added and a reference to the value in the map is returned. + */ + Value &lookup_or_add(const Key &key, const Value &value) + { + return this->lookup_or_add_as(key, value); + } + Value &lookup_or_add(const Key &key, Value &&value) + { + return this->lookup_or_add_as(key, std::move(value)); + } + Value &lookup_or_add(Key &&key, const Value &value) + { + return this->lookup_or_add_as(std::move(key), value); + } + Value &lookup_or_add(Key &&key, Value &&value) + { + return this->lookup_or_add_as(std::move(key), std::move(value)); + } + + /** + * Same as `lookup_or_add`, but accepts other key types that are supported by the hash + * function. + */ + template<typename ForwardKey, typename ForwardValue> + Value &lookup_or_add_as(ForwardKey &&key, ForwardValue &&value) + { + return this->lookup_or_add__impl( + std::forward<ForwardKey>(key), std::forward<ForwardValue>(value), m_hash(key)); + } + + /** * Returns a reference to the value that corresponds to the given key. If the key is not yet in * the map, it will be newly added. * @@ -521,22 +553,24 @@ class Map { * take no parameters and return the value to be inserted. */ template<typename CreateValueF> - Value &lookup_or_add(const Key &key, const CreateValueF &create_value) + Value &lookup_or_add_cb(const Key &key, const CreateValueF &create_value) { - return this->lookup_or_add_as(key, create_value); + return this->lookup_or_add_cb_as(key, create_value); } - template<typename CreateValueF> Value &lookup_or_add(Key &&key, const CreateValueF &create_value) + template<typename CreateValueF> + Value &lookup_or_add_cb(Key &&key, const CreateValueF &create_value) { - return this->lookup_or_add_as(std::move(key), create_value); + return this->lookup_or_add_cb_as(std::move(key), create_value); } /** - * Same as `lookup_or_add`, but accepts other key types that are supported by the hash function. + * Same as `lookup_or_add_cb`, but accepts other key types that are supported by the hash + * function. */ template<typename ForwardKey, typename CreateValueF> - Value &lookup_or_add_as(ForwardKey &&key, const CreateValueF &create_value) + Value &lookup_or_add_cb_as(ForwardKey &&key, const CreateValueF &create_value) { - return this->lookup_or_add__impl(std::forward<ForwardKey>(key), create_value, m_hash(key)); + return this->lookup_or_add_cb__impl(std::forward<ForwardKey>(key), create_value, m_hash(key)); } /** @@ -558,7 +592,7 @@ class Map { */ template<typename ForwardKey> Value &lookup_or_add_default_as(ForwardKey &&key) { - return this->lookup_or_add(std::forward<ForwardKey>(key), []() { return Value(); }); + return this->lookup_or_add_cb(std::forward<ForwardKey>(key), []() { return Value(); }); } /** @@ -1024,7 +1058,7 @@ class Map { } template<typename ForwardKey, typename CreateValueF> - Value &lookup_or_add__impl(ForwardKey &&key, const CreateValueF &create_value, uint32_t hash) + Value &lookup_or_add_cb__impl(ForwardKey &&key, const CreateValueF &create_value, uint32_t hash) { this->ensure_can_add(); @@ -1042,6 +1076,24 @@ class Map { } template<typename ForwardKey, typename ForwardValue> + Value &lookup_or_add__impl(ForwardKey &&key, ForwardValue &&value, uint32_t hash) + { + this->ensure_can_add(); + + MAP_SLOT_PROBING_BEGIN (hash, slot) { + if (slot.is_empty()) { + slot.occupy(std::forward<ForwardKey>(key), std::forward<ForwardValue>(value), hash); + m_occupied_and_removed_slots++; + return *slot.value(); + } + if (slot.contains(key, m_is_equal, hash)) { + return *slot.value(); + } + } + MAP_SLOT_PROBING_END(); + } + + template<typename ForwardKey, typename ForwardValue> bool add_overwrite__impl(ForwardKey &&key, ForwardValue &&value, uint32_t hash) { auto create_func = [&](Value *ptr) { |