[llvm] a7716a3 - [ADT] Add equality operator for SmallPtrSet
Yevgeny Rouban via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 5 20:18:19 PST 2019
Author: Yevgeny Rouban
Date: 2019-11-06T11:17:51+07:00
New Revision: a7716a3c3c9cc2f1d511d93688de5cb9f163d4c2
URL: https://github.com/llvm/llvm-project/commit/a7716a3c3c9cc2f1d511d93688de5cb9f163d4c2
DIFF: https://github.com/llvm/llvm-project/commit/a7716a3c3c9cc2f1d511d93688de5cb9f163d4c2.diff
LOG: [ADT] Add equality operator for SmallPtrSet
Reviewed By: tellenbach
Differential Revision: https://reviews.llvm.org/D69429
Added:
Modified:
llvm/include/llvm/ADT/SmallPtrSet.h
llvm/unittests/ADT/SmallPtrSetTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 913518230d2d..1d8280063c80 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -409,6 +409,32 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
}
};
+/// 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
diff --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp
index cff1632586be..3226fe615509 100644
--- a/llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -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);
+}
More information about the llvm-commits
mailing list