diff options
author | Jacques Lucke <jacques@blender.org> | 2020-06-10 15:12:24 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-06-10 15:15:04 +0300 |
commit | 84a0a6d16c85105cf755141c4d49afc52eb157e3 (patch) | |
tree | 2b751e4dc6cb98cbb04459eb0b8c89db5d969cf5 /source/blender/blenlib | |
parent | f367f1e5a55e1c657f9d2088f6537fb2e73492f0 (diff) |
BLI: update behavior of Map.lookup_or_add
Previously, this function would expect a callback function as parameter.
This behavior is now in Map.lookup_or_add_cb. The new version just
takes the key and value directly.
Diffstat (limited to 'source/blender/blenlib')
-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) { |