diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh index 3b8e5cd1afa..eab2ce7e7f0 100644 --- a/source/blender/blenlib/BLI_map.hh +++ b/source/blender/blenlib/BLI_map.hh @@ -1015,6 +1015,33 @@ class Map { return this->count_collisions__impl(key, hash_(key)); } + /** + * True if both maps have the same key-value-pairs. + */ + friend bool operator==(const Map &a, const Map &b) + { + if (a.size() != b.size()) { + return false; + } + for (const Item item : a.items()) { + const Key &key = item.key; + const Value &value_a = item.value; + const Value *value_b = b.lookup_ptr(key); + if (value_b == nullptr) { + return false; + } + if (value_a != *value_b) { + return false; + } + } + return true; + } + + friend bool operator!=(const Map &a, const Map &b) + { + return !(a == b); + } + private: BLI_NOINLINE void realloc_and_reinsert(int64_t min_usable_slots) { diff --git a/source/blender/blenlib/BLI_multi_value_map.hh b/source/blender/blenlib/BLI_multi_value_map.hh index 814e556296d..259c9b5158e 100644 --- a/source/blender/blenlib/BLI_multi_value_map.hh +++ b/source/blender/blenlib/BLI_multi_value_map.hh @@ -21,6 +21,7 @@ */ #include "BLI_map.hh" +#include "BLI_struct_equality_utils.hh" #include "BLI_vector.hh" namespace blender { @@ -157,6 +158,8 @@ template class MultiValueMap { { map_.clear_and_shrink(); } + + BLI_STRUCT_EQUALITY_OPERATORS_1(MultiValueMap, map_) }; } // namespace blender diff --git a/source/blender/blenlib/tests/BLI_map_test.cc b/source/blender/blenlib/tests/BLI_map_test.cc index e9f48fd3ebe..d9b31ea7d16 100644 --- a/source/blender/blenlib/tests/BLI_map_test.cc +++ b/source/blender/blenlib/tests/BLI_map_test.cc @@ -694,6 +694,22 @@ TEST(map, VectorKey) EXPECT_EQ(map.size(), 1); } +TEST(map, Equality) +{ + Map a; + Map b; + + EXPECT_EQ(a, b); + a.add(3, 4); + EXPECT_NE(a, b); + b.add(3, 4); + EXPECT_EQ(a, b); + + a.add(4, 10); + b.add(4, 11); + EXPECT_NE(a, b); +} + /** * Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot. */