[PATCH] D69417: [ADT] add equality operator for SmallSet

Fedor Sergeev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 17:46:45 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rG4aee4c99c543: [ADT] add equality operator for SmallSet (authored by fedor.sergeev).

Changed prior to commit:
  https://reviews.llvm.org/D69417?vs=226361&id=227005#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D69417

Files:
  llvm/include/llvm/ADT/SmallSet.h
  llvm/unittests/ADT/SmallSetTest.cpp


Index: llvm/unittests/ADT/SmallSetTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallSetTest.cpp
+++ llvm/unittests/ADT/SmallSetTest.cpp
@@ -142,3 +142,28 @@
   Iter = std::move(Iter2);
   EXPECT_EQ("str 0", *Iter);
 }
+
+TEST(SmallSetTest, EqualityComparisonTest) {
+  SmallSet<int, 8> s1small;
+  SmallSet<int, 10> s2small;
+  SmallSet<int, 3> s3large;
+  SmallSet<int, 8> s4large;
+
+  for (int i = 1; i < 5; i++) {
+    s1small.insert(i);
+    s2small.insert(5 - i);
+    s3large.insert(i);
+  }
+  for (int i = 1; i < 11; i++)
+    s4large.insert(i);
+
+  EXPECT_EQ(s1small, s1small);
+  EXPECT_EQ(s3large, s3large);
+
+  EXPECT_EQ(s1small, s2small);
+  EXPECT_EQ(s1small, s3large);
+  EXPECT_EQ(s2small, s3large);
+
+  EXPECT_NE(s1small, s4large);
+  EXPECT_NE(s4large, s3large);
+}
Index: llvm/include/llvm/ADT/SmallSet.h
===================================================================
--- llvm/include/llvm/ADT/SmallSet.h
+++ llvm/include/llvm/ADT/SmallSet.h
@@ -248,6 +248,31 @@
 template <typename PointeeType, unsigned N>
 class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
 
+/// Equality comparison for SmallSet.
+///
+/// Iterates over elements of LHS confirming that each element is also a member
+/// of RHS, and that RHS contains no additional values.
+/// Equivalent to N calls to RHS.count.
+/// For small-set mode amortized complexity is O(N^2)
+/// For large-set mode amortized complexity is linear, worst case is O(N^2) (if
+/// every hash collides).
+template <typename T, unsigned LN, unsigned RN, typename C>
+bool operator==(const SmallSet<T, LN, C> &LHS, const SmallSet<T, RN, C> &RHS) {
+  if (LHS.size() != RHS.size())
+    return false;
+
+  // All elements in LHS must also be in RHS
+  return all_of(LHS, [&RHS](const T &E) { return RHS.count(E); });
+}
+
+/// Inequality comparison for SmallSet.
+///
+/// Equivalent to !(LHS == RHS). See operator== for performance notes.
+template <typename T, unsigned LN, unsigned RN, typename C>
+bool operator!=(const SmallSet<T, LN, C> &LHS, const SmallSet<T, RN, C> &RHS) {
+  return !(LHS == RHS);
+}
+
 } // end namespace llvm
 
 #endif // LLVM_ADT_SMALLSET_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69417.227005.patch
Type: text/x-patch
Size: 2217 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191030/09437d41/attachment.bin>


More information about the llvm-commits mailing list