BLI: support checking if two maps are the same

This commit is contained in:
Jacques Lucke
2023-11-21 13:38:27 +01:00
parent 6064e726d4
commit 813078daec
3 changed files with 46 additions and 0 deletions
+27
View File
@@ -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)
{
@@ -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<typename Key, typename Value> class MultiValueMap {
{
map_.clear_and_shrink();
}
BLI_STRUCT_EQUALITY_OPERATORS_1(MultiValueMap, map_)
};
} // namespace blender
@@ -694,6 +694,22 @@ TEST(map, VectorKey)
EXPECT_EQ(map.size(), 1);
}
TEST(map, Equality)
{
Map<int, int> a;
Map<int, int> 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.
*/