Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2020-06-10 20:02:28 +0300
committerJacques Lucke <jacques@blender.org>2020-06-10 20:02:34 +0300
commit474b2889339e24e602a7498c30ed07e2c47e6982 (patch)
tree89086e555908acf84b37bdb71e79e1203b350004 /source/blender/blenlib/BLI_map.hh
parent20658e6a29bd33264d99fcee9cea1886d1c9d0c9 (diff)
BLI: add Map.pop_default method
There is a nice use case for this in depsgraph code. Also I added some previously missing calls to std::move.
Diffstat (limited to 'source/blender/blenlib/BLI_map.hh')
-rw-r--r--source/blender/blenlib/BLI_map.hh43
1 files changed, 41 insertions, 2 deletions
diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh
index 13f1a967d65..9737367ebca 100644
--- a/source/blender/blenlib/BLI_map.hh
+++ b/source/blender/blenlib/BLI_map.hh
@@ -406,6 +406,28 @@ class Map {
}
/**
+ * Get the value that corresponds to the given key and remove it from the map. If the key is not
+ * in the map, return the given default value instead.
+ */
+ Value pop_default(const Key &key, const Value &default_value)
+ {
+ return this->pop_default_as(key, default_value);
+ }
+ Value pop_default(const Key &key, Value &&default_value)
+ {
+ return this->pop_default_as(key, std::move(default_value));
+ }
+
+ /**
+ * Same as `pop_default`, but accepts other key types that are supported by the hash function.
+ */
+ template<typename ForwardKey, typename ForwardValue>
+ Value pop_default_as(const ForwardKey &key, ForwardValue &&default_value)
+ {
+ return this->pop_default__impl(key, std::forward<ForwardValue>(default_value), m_hash(key));
+ }
+
+ /**
* This method can be used to implement more complex custom behavior without having to do
* multiple lookups
*
@@ -1039,7 +1061,7 @@ class Map {
MAP_SLOT_PROBING_BEGIN (hash, slot) {
if (slot.contains(key, m_is_equal, hash)) {
- Value value = *slot.value();
+ Value value = std::move(*slot.value());
slot.remove();
return value;
}
@@ -1051,7 +1073,7 @@ class Map {
{
MAP_SLOT_PROBING_BEGIN (hash, slot) {
if (slot.contains(key, m_is_equal, hash)) {
- Optional<Value> value = *slot.value();
+ Optional<Value> value = std::move(*slot.value());
slot.remove();
m_removed_slots++;
return value;
@@ -1063,6 +1085,23 @@ class Map {
MAP_SLOT_PROBING_END();
}
+ template<typename ForwardKey, typename ForwardValue>
+ Value pop_default__impl(const ForwardKey &key, ForwardValue &&default_value, uint32_t hash)
+ {
+ MAP_SLOT_PROBING_BEGIN (hash, slot) {
+ if (slot.contains(key, m_is_equal, hash)) {
+ Value value = std::move(*slot.value());
+ slot.remove();
+ m_removed_slots++;
+ return value;
+ }
+ if (slot.is_empty()) {
+ return std::forward<ForwardValue>(default_value);
+ }
+ }
+ MAP_SLOT_PROBING_END();
+ }
+
template<typename ForwardKey, typename CreateValueF, typename ModifyValueF>
auto add_or_modify__impl(ForwardKey &&key,
const CreateValueF &create_value,