[Bf-blender-cvs] [5db4d8269ee] functions: add multiple new elements to multimap

Jacques Lucke noreply at git.blender.org
Wed Jul 3 19:14:10 CEST 2019


Commit: 5db4d8269ee76fbb85fb1a89c72c611182503d90
Author: Jacques Lucke
Date:   Wed Jul 3 16:33:53 2019 +0200
Branches: functions
https://developer.blender.org/rB5db4d8269ee76fbb85fb1a89c72c611182503d90

add multiple new elements to multimap

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

M	source/blender/blenlib/BLI_multimap.hpp
M	tests/gtests/blenlib/BLI_multimap_test.cc

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

diff --git a/source/blender/blenlib/BLI_multimap.hpp b/source/blender/blenlib/BLI_multimap.hpp
index eb66416ff88..a881c9f9c5e 100644
--- a/source/blender/blenlib/BLI_multimap.hpp
+++ b/source/blender/blenlib/BLI_multimap.hpp
@@ -72,6 +72,15 @@ template<typename K, typename V> class MultiMap {
     m_map.add_new(key, Entry(key, ptr, 1, 1));
   }
 
+  void add_multiple_new(const K &key, ArrayRef<V> values)
+  {
+    BLI_assert(!m_map.contains(key));
+    uint amount = values.size();
+    V *ptr = m_pool.allocate_array<V>(amount);
+    std::uninitialized_copy_n(values.begin(), amount, ptr);
+    m_map.add_new(key, Entry(key, ptr, amount, amount));
+  }
+
   bool contains(const K &key) const
   {
     return m_map.contains(key);
diff --git a/tests/gtests/blenlib/BLI_multimap_test.cc b/tests/gtests/blenlib/BLI_multimap_test.cc
index 9f3f73dd9de..9208ebd058c 100644
--- a/tests/gtests/blenlib/BLI_multimap_test.cc
+++ b/tests/gtests/blenlib/BLI_multimap_test.cc
@@ -50,6 +50,19 @@ TEST(multimap, AddMany)
   EXPECT_EQ(map.lookup(7).size(), 10);
 }
 
+TEST(multimap, AddMultipleNew)
+{
+  IntMap map;
+  map.add_multiple_new(3, {6, 7, 8});
+  map.add_multiple_new(2, {1, 2, 5, 7});
+
+  EXPECT_EQ(map.size(), 2);
+  EXPECT_TRUE(map.contains(3));
+  EXPECT_TRUE(map.contains(2));
+  EXPECT_TRUE(map.lookup(2).contains(2));
+  EXPECT_FALSE(map.lookup(2).contains(3));
+}
+
 TEST(multimap, ValuesForKey)
 {
   IntMap map;



More information about the Bf-blender-cvs mailing list