Commit: 9fa5d75d80d022d84f6f29d4cd361cb7b14a5283
Author: Jacques Lucke
Date:   Sat Jul 20 12:57:12 2019 +0200
Branches: functions
https://developer.blender.org/rB9fa5d75d80d022d84f6f29d4cd361cb7b14a5283

Fix moving sets and maps

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

M       source/blender/blenlib/BLI_array_lookup.hpp
M       tests/gtests/blenlib/BLI_small_map_test.cc

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

diff --git a/source/blender/blenlib/BLI_array_lookup.hpp 
b/source/blender/blenlib/BLI_array_lookup.hpp
index b88a6951508..2b1a9ed2fd6 100644
--- a/source/blender/blenlib/BLI_array_lookup.hpp
+++ b/source/blender/blenlib/BLI_array_lookup.hpp
@@ -76,6 +76,32 @@ class ArrayLookup {
     m_length = 0;
   }
 
+  ArrayLookup(const ArrayLookup &other) = default;
+  ArrayLookup(ArrayLookup &&other)
+      : m_map(std::move(other.m_map)),
+        m_length(other.m_length),
+        m_dummy_amount(other.m_dummy_amount),
+        m_max_used_slots(other.m_max_used_slots),
+        m_slot_mask(other.m_slot_mask)
+  {
+    other.~ArrayLookup();
+    new (&other) ArrayLookup();
+  }
+
+  ArrayLookup &operator=(const ArrayLookup &other) = default;
+
+  ArrayLookup &operator=(ArrayLookup &&other)
+  {
+    if (this == &other) {
+      return *this;
+    }
+
+    this->~ArrayLookup();
+    new (this) ArrayLookup(std::move(other));
+
+    return *this;
+  }
+
   bool contains(Item *array, const Key &key) const
   {
     ITER_SLOTS (key, slot, state) {
diff --git a/tests/gtests/blenlib/BLI_small_map_test.cc 
b/tests/gtests/blenlib/BLI_small_map_test.cc
index 41541205863..a065ec79ab0 100644
--- a/tests/gtests/blenlib/BLI_small_map_test.cc
+++ b/tests/gtests/blenlib/BLI_small_map_test.cc
@@ -211,3 +211,43 @@ TEST(small_map, AddOverride)
   EXPECT_FALSE(map.add(3, 8.0f));
   EXPECT_EQ(map.lookup(3), 7.0f);
 }
+
+TEST(small_map, MoveConstructorSmall)
+{
+  IntFloatMap map1;
+  map1.add(1, 2.0f);
+  map1.add(4, 1.0f);
+  IntFloatMap map2(std::move(map1));
+  EXPECT_EQ(map2.size(), 2);
+  EXPECT_EQ(map2.lookup(1), 2.0f);
+  EXPECT_EQ(map2.lookup(4), 1.0f);
+  EXPECT_EQ(map1.size(), 0);
+  EXPECT_EQ(map1.lookup_ptr(4), nullptr);
+}
+
+TEST(small_map, MoveConstructorLarge)
+{
+  IntFloatMap map1;
+  for (uint i = 0; i < 100; i++) {
+    map1.add_new(i, i);
+  }
+  IntFloatMap map2(std::move(map1));
+  EXPECT_EQ(map2.size(), 100);
+  EXPECT_EQ(map2.lookup(1), 1.0f);
+  EXPECT_EQ(map2.lookup(4), 4.0f);
+  EXPECT_EQ(map1.size(), 0);
+  EXPECT_EQ(map1.lookup_ptr(4), nullptr);
+}
+
+TEST(small_map, MoveAssignment)
+{
+  IntFloatMap map1;
+  map1.add(1, 2.0f);
+  map1.add(4, 1.0f);
+  IntFloatMap map2 = std::move(map1);
+  EXPECT_EQ(map2.size(), 2);
+  EXPECT_EQ(map2.lookup(1), 2.0f);
+  EXPECT_EQ(map2.lookup(4), 1.0f);
+  EXPECT_EQ(map1.size(), 0);
+  EXPECT_EQ(map1.lookup_ptr(4), nullptr);
+}

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to