1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

[ADT] Add equality operator for SmallPtrSet

Reviewed By: tellenbach
Differential Revision: https://reviews.llvm.org/D69429
This commit is contained in:
Yevgeny Rouban 2019-11-06 11:17:51 +07:00
parent 33ea4ca3d8
commit a162363827
2 changed files with 64 additions and 0 deletions

View File

@ -409,6 +409,32 @@ private:
}
};
/// Equality comparison for SmallPtrSet.
///
/// Iterates over elements of LHS confirming that each value from LHS is also in
/// RHS, and that no additional values are in RHS.
template <typename PtrType>
bool operator==(const SmallPtrSetImpl<PtrType> &LHS,
const SmallPtrSetImpl<PtrType> &RHS) {
if (LHS.size() != RHS.size())
return false;
for (const auto *KV : LHS)
if (!RHS.count(KV))
return false;
return true;
}
/// Inequality comparison for SmallPtrSet.
///
/// Equivalent to !(LHS == RHS).
template <typename PtrType>
bool operator!=(const SmallPtrSetImpl<PtrType> &LHS,
const SmallPtrSetImpl<PtrType> &RHS) {
return !(LHS == RHS);
}
/// SmallPtrSet - This class implements a set which is optimized for holding
/// SmallSize or less elements. This internally rounds up SmallSize to the next
/// power of two if it is not already a power of two. See the comments above

View File

@ -329,3 +329,41 @@ TEST(SmallPtrSetTest, ConstNonPtrTest) {
EXPECT_EQ(IntSet.count(Pair), 1u);
EXPECT_NE(IntSet.find(Pair), IntSet.end());
}
// Test equality comparison.
TEST(SmallPtrSetTest, EqualityComparison) {
int buf[3];
for (int i = 0; i < 3; ++i)
buf[i] = 0;
SmallPtrSet<int *, 1> a;
a.insert(&buf[0]);
a.insert(&buf[1]);
SmallPtrSet<int *, 2> b;
b.insert(&buf[1]);
b.insert(&buf[0]);
SmallPtrSet<int *, 3> c;
c.insert(&buf[1]);
c.insert(&buf[2]);
SmallPtrSet<int *, 4> d;
d.insert(&buf[0]);
SmallPtrSet<int *, 5> e;
e.insert(&buf[0]);
e.insert(&buf[1]);
e.insert(&buf[2]);
EXPECT_EQ(a, b);
EXPECT_EQ(b, a);
EXPECT_NE(b, c);
EXPECT_NE(c, a);
EXPECT_NE(d, a);
EXPECT_NE(a, d);
EXPECT_NE(a, e);
EXPECT_NE(e, a);
EXPECT_NE(c, e);
EXPECT_NE(e, d);
}