[PATCH] D69429: [ADT] Adds equality operator for SmallPtrSet

Yevgeny Rouban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 04:28:21 PDT 2019


yrouban created this revision.
yrouban added reviewers: fedor.sergeev, chandlerc, mgrang, MatzeB.
Herald added a subscriber: dexonsmith.
Herald added a project: LLVM.

This change makes it easier to migrate existing code that uses std::set (which support equality comparison) to SmallPtrSet.


https://reviews.llvm.org/D69429

Files:
  llvm/include/llvm/ADT/SmallPtrSet.h
  llvm/unittests/ADT/SmallPtrSetTest.cpp


Index: llvm/unittests/ADT/SmallPtrSetTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -329,3 +329,24 @@
   EXPECT_EQ(IntSet.count(Pair), 1u);
   EXPECT_NE(IntSet.find(Pair), IntSet.end());
 }
+
+// Test equality comparison.
+TEST(SmallPtrSetTest, EqualityComparison) {
+  int buf[10];
+
+  SmallPtrSet<int *, 2> a;
+  SmallPtrSet<int *, 2> b;
+  SmallPtrSet<int *, 2> c;
+
+  a.insert(&buf[0]);
+  a.insert(&buf[1]);
+  b.insert(&buf[1]);
+  b.insert(&buf[0]);
+  c.insert(&buf[1]);
+  c.insert(&buf[2]);
+
+  EXPECT_EQ(a, b);
+  EXPECT_EQ(b, a);
+  EXPECT_NE(b, c);
+  EXPECT_NE(c, a);
+}
Index: llvm/include/llvm/ADT/SmallPtrSet.h
===================================================================
--- llvm/include/llvm/ADT/SmallPtrSet.h
+++ llvm/include/llvm/ADT/SmallPtrSet.h
@@ -409,6 +409,32 @@
   }
 };
 
+/// 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 (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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69429.226409.patch
Type: text/x-patch
Size: 1888 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191025/bc28bf3f/attachment.bin>


More information about the llvm-commits mailing list