BLI: support == and != for Set

This makes it more convenient to check if two sets contain the same keys.
This commit is contained in:
Jacques Lucke 2023-03-28 14:16:30 +02:00
parent fbcddfcd68
commit d81d743537
2 changed files with 40 additions and 0 deletions

@ -646,6 +646,24 @@ class Set {
return !Intersects(a, b);
}
friend bool operator==(const Set &a, const Set &b)
{
if (a.size() != b.size()) {
return false;
}
for (const Key &key : a) {
if (!b.contains(key)) {
return false;
}
}
return true;
}
friend bool operator!=(const Set &a, const Set &b)
{
return !(a == b);
}
private:
BLI_NOINLINE void realloc_and_reinsert(const int64_t min_usable_slots)
{

@ -600,6 +600,28 @@ TEST(set, RemoveUniquePtrWithRaw)
EXPECT_TRUE(set.is_empty());
}
TEST(set, Equality)
{
const Set<int> a = {1, 2, 3, 4, 5};
const Set<int> b = {5, 2, 3, 1, 4};
const Set<int> c = {1, 2, 3};
const Set<int> d = {1, 2, 3, 4, 5, 6};
const Set<int> e = {};
const Set<int> f = {10, 11, 12, 13, 14};
EXPECT_EQ(a, a);
EXPECT_EQ(a, b);
EXPECT_EQ(b, a);
EXPECT_NE(a, c);
EXPECT_NE(a, d);
EXPECT_NE(a, e);
EXPECT_NE(a, f);
EXPECT_NE(c, a);
EXPECT_NE(d, a);
EXPECT_NE(e, a);
EXPECT_NE(f, a);
}
/**
* Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot.
*/