diff options
-rw-r--r-- | source/blender/blenlib/BLI_map.h | 29 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_set_vector.h | 10 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_map_test.cc | 17 |
3 files changed, 54 insertions, 2 deletions
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)); @@ -168,6 +173,26 @@ template<typename KeyT, typename ValueT, typename Allocator = GuardedAllocator> 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 @@ -132,6 +132,16 @@ template<typename T, typename Allocator = GuardedAllocator> class SetVector { } /** + * 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. */ void add_new(const T &value) 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)); +} |