[Bf-blender-cvs] [9863f2bfec7] functions: new map.add_override method
Jacques Lucke
noreply at git.blender.org
Tue Jul 9 18:01:05 CEST 2019
Commit: 9863f2bfec7c80770f1015ad972a6eb1f35be9c8
Author: Jacques Lucke
Date: Tue Jul 9 10:53:14 2019 +0200
Branches: functions
https://developer.blender.org/rB9863f2bfec7c80770f1015ad972a6eb1f35be9c8
new map.add_override method
===================================================================
M source/blender/blenlib/BLI_small_map.hpp
M tests/gtests/blenlib/BLI_small_map_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_small_map.hpp b/source/blender/blenlib/BLI_small_map.hpp
index 526de350e41..d7fe1b7306c 100644
--- a/source/blender/blenlib/BLI_small_map.hpp
+++ b/source/blender/blenlib/BLI_small_map.hpp
@@ -80,6 +80,17 @@ template<typename K, typename V, uint N = 4> class SmallMap {
m_lookup.add_new(m_entries.begin(), index);
}
+ /**
+ * Insert a new key-value pair in the map. If the key exists already, the value will be
+ * overriden.
+ * Returns when the value was newly inserted, otherwise false.
+ */
+ bool add_override(const K &key, const V &value)
+ {
+ return this->add_or_modify(
+ key, [value]() { return value; }, [value](V &old_value) { old_value = value; });
+ }
+
/**
* Return true when the key exists in the map, otherwise false.
*/
diff --git a/tests/gtests/blenlib/BLI_small_map_test.cc b/tests/gtests/blenlib/BLI_small_map_test.cc
index e685e5e45f0..41541205863 100644
--- a/tests/gtests/blenlib/BLI_small_map_test.cc
+++ b/tests/gtests/blenlib/BLI_small_map_test.cc
@@ -199,3 +199,15 @@ TEST(small_map, InsertOrModify)
EXPECT_FALSE(map.add_or_modify(1, create_func, modify_func));
EXPECT_EQ(map.lookup(1), 15.0f);
}
+
+TEST(small_map, AddOverride)
+{
+ IntFloatMap map;
+ EXPECT_FALSE(map.contains(3));
+ EXPECT_TRUE(map.add_override(3, 6.0f));
+ EXPECT_EQ(map.lookup(3), 6.0f);
+ EXPECT_FALSE(map.add_override(3, 7.0f));
+ EXPECT_EQ(map.lookup(3), 7.0f);
+ EXPECT_FALSE(map.add(3, 8.0f));
+ EXPECT_EQ(map.lookup(3), 7.0f);
+}
More information about the Bf-blender-cvs
mailing list