[Bf-blender-cvs] [0338da10633] functions: smallmap: lookup or insert using key function

Jacques Lucke noreply at git.blender.org
Mon May 20 11:20:05 CEST 2019


Commit: 0338da10633c6032cb1526820542ba36b607c90c
Author: Jacques Lucke
Date:   Mon May 20 11:07:23 2019 +0200
Branches: functions
https://developer.blender.org/rB0338da10633c6032cb1526820542ba36b607c90c

smallmap: lookup or insert using key function

===================================================================

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 cfe275376f3..c21779adfe9 100644
--- a/source/blender/blenlib/BLI_small_map.hpp
+++ b/source/blender/blenlib/BLI_small_map.hpp
@@ -132,6 +132,11 @@ template<typename K, typename V, uint N = 4> class SmallMap {
     return this->lookup_ref(key);
   }
 
+  V &lookup_ref_or_insert_key_func(const K &key, V (*create_value)(const K &key))
+  {
+    return lookup_ref_or_insert_func(key, create_value, key);
+  }
+
   uint size() const
   {
     return m_entries.size();
diff --git a/tests/gtests/blenlib/BLI_small_map_test.cc b/tests/gtests/blenlib/BLI_small_map_test.cc
index f11ac8260ca..5ab6743cd06 100644
--- a/tests/gtests/blenlib/BLI_small_map_test.cc
+++ b/tests/gtests/blenlib/BLI_small_map_test.cc
@@ -225,3 +225,18 @@ TEST(small_map, LookupOrInsertFunc_FuncCalledOnce)
   EXPECT_EQ(map.lookup_ref_or_insert_func(0, inc_value_and_return_42, &counter), 42.0f);
   EXPECT_EQ(counter, 1);
 }
+
+float add_10(const int &value)
+{
+  return value + 10.0f;
+}
+
+TEST(small_map, LookupOrInsertKeyFunc)
+{
+  IntFloatMap map;
+  EXPECT_EQ(map.lookup_ref_or_insert_key_func(4, add_10), 14.0f);
+  EXPECT_EQ(map.lookup_ref_or_insert_key_func(10, add_10), 20.0f);
+
+  EXPECT_EQ(map.lookup(4), 14.0f);
+  EXPECT_EQ(map.lookup(10), 20.0f);
+}



More information about the Bf-blender-cvs mailing list