[Bf-blender-cvs] [08539312b24] master: BLI: add some missing methods to Map and SetVector

Jacques Lucke noreply at git.blender.org
Fri Sep 13 12:11:06 CEST 2019


Commit: 08539312b24b7f0a9a5334e85b9d5c5b532b6453
Author: Jacques Lucke
Date:   Fri Sep 13 12:09:06 2019 +0200
Branches: master
https://developer.blender.org/rB08539312b24b7f0a9a5334e85b9d5c5b532b6453

BLI: add some missing methods to Map and SetVector

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

M	source/blender/blenlib/BLI_map.h
M	source/blender/blenlib/BLI_set_vector.h
M	tests/gtests/blenlib/BLI_map_test.cc

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

diff --git a/source/blender/blenlib/BLI_map.h b/source/blender/blenlib/BLI_map.h
index 5328dac1106..8ee0624df19 100644
--- a/source/blender/blenlib/BLI_map.h
+++ b/source/blender/blenlib/BLI_map.h
@@ -44,7 +44,7 @@ namespace BLI {
     do {
 
 #define ITER_SLOTS_END(R_OFFSET) \
-      R_OFFSET = (R_OFFSET + 1) & OFFSET_MASK; \
+      R_OFFSET = (R_OFFSET + 1u) & OFFSET_MASK; \
     } while (R_OFFSET != initial_offset); \
     perturb >>= 5; \
     hash = hash * 5 + 1 + perturb; \
@@ -126,6 +126,11 @@ template<typename KeyT, typename ValueT, typename Allocator = GuardedAllocator>
       return m_status[offset] == IS_EMPTY;
     }
 
+    bool is_dummy(uint offset) const
+    {
+      return m_status[offset] == IS_DUMMY;
+    }
+
     KeyT *key(uint offset) const
     {
       return (KeyT *)(m_keys + offset * sizeof(KeyT));
@@ -167,6 +172,26 @@ template<typename KeyT, typename ValueT, typename Allocator = GuardedAllocator>
  public:
   Map() = default;
 
+  /**
+   * Allocate memory such that at least min_usable_slots can be added before the map has to grow
+   * again.
+   */
+  void reserve(uint min_usable_slots)
+  {
+    if (m_array.slots_usable() < min_usable_slots) {
+      this->grow(min_usable_slots);
+    }
+  }
+
+  /**
+   * Remove all elements from the map.
+   */
+  void clear()
+  {
+    this->~Map();
+    new (this) Map();
+  }
+
   /**
    * Insert a new key-value-pair in the map.
    * Asserts when the key existed before.
@@ -397,7 +422,7 @@ template<typename KeyT, typename ValueT, typename Allocator = GuardedAllocator>
         else if (item.is_set(offset)) {
           const KeyT &key = *item.key(offset);
           const ValueT &value = *item.value(offset);
-          uint32_t collisions = this->count_collisions(value);
+          uint32_t collisions = this->count_collisions(key);
           std::cout << "    " << key << " -> " << value << "  \t Collisions: " << collisions
                     << '\n';
         }
diff --git a/source/blender/blenlib/BLI_set_vector.h b/source/blender/blenlib/BLI_set_vector.h
index 92c660a88f2..36e69e7e3cc 100644
--- a/source/blender/blenlib/BLI_set_vector.h
+++ b/source/blender/blenlib/BLI_set_vector.h
@@ -131,6 +131,16 @@ template<typename T, typename Allocator = GuardedAllocator> class SetVector {
     this->add_multiple(values);
   }
 
+  /**
+   * Allocate memory such that at least min_usable_slots can be added without having to grow again.
+   */
+  void reserve(uint min_usable_slots)
+  {
+    if (m_array.slots_usable() < min_usable_slots) {
+      this->grow(min_usable_slots);
+    }
+  }
+
   /**
    * Add a new element. The method assumes that the value did not exist before.
    */
diff --git a/tests/gtests/blenlib/BLI_map_test.cc b/tests/gtests/blenlib/BLI_map_test.cc
index a7711fb3985..8d5b178aea6 100644
--- a/tests/gtests/blenlib/BLI_map_test.cc
+++ b/tests/gtests/blenlib/BLI_map_test.cc
@@ -241,3 +241,20 @@ TEST(map, MoveAssignment)
   EXPECT_EQ(map1.size(), 0);
   EXPECT_EQ(map1.lookup_ptr(4), nullptr);
 }
+
+TEST(map, Clear)
+{
+  IntFloatMap map;
+  map.add(1, 1.0f);
+  map.add(2, 5.0f);
+
+  EXPECT_EQ(map.size(), 2);
+  EXPECT_TRUE(map.contains(1));
+  EXPECT_TRUE(map.contains(2));
+
+  map.clear();
+
+  EXPECT_EQ(map.size(), 0);
+  EXPECT_FALSE(map.contains(1));
+  EXPECT_FALSE(map.contains(2));
+}



More information about the Bf-blender-cvs mailing list