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

Yevgeny Rouban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 27 23:56:45 PDT 2019


yrouban updated this revision to Diff 226616.
yrouban marked 2 inline comments as done.
yrouban added a comment.

Addressed all comments.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69429/new/

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,41 @@
   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);
+}
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 (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


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


More information about the llvm-commits mailing list