[llvm] Add contains(KeyType) -> bool methods to SmallSet
authorDavid Blaikie <dblaikie@gmail.com>
Fri, 17 Jul 2020 17:42:23 +0000 (10:42 -0700)
committerDavid Blaikie <dblaikie@gmail.com>
Fri, 17 Jul 2020 18:26:27 +0000 (11:26 -0700)
Matches C++20 API addition.

Differential Revision: https://reviews.llvm.org/D83449

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

index a03fa7dd84235b977d8e3c7c35833c0328478301..0600e528ee6928ef6f8f13230c1551f6f5289954 100644 (file)
@@ -232,6 +232,13 @@ public:
     return {Set.end()};
   }
 
+  /// Check if the SmallSet contains the given element.
+  bool contains(const T &V) const {
+    if (isSmall())
+      return vfind(V) != Vector.end();
+    return Set.find(V) != Set.end();
+  }
+
 private:
   bool isSmall() const { return Set.empty(); }
 
index 06682ce823dcfbcb45337ff80f1006ad155cd0f6..26cab828c784de8bf6c20bd2d48266f5ff9d686f 100644 (file)
@@ -167,3 +167,28 @@ TEST(SmallSetTest, EqualityComparisonTest) {
   EXPECT_NE(s1small, s4large);
   EXPECT_NE(s4large, s3large);
 }
+
+TEST(SmallSetTest, Contains) {
+  SmallSet<int, 2> Set;
+  EXPECT_FALSE(Set.contains(0));
+  EXPECT_FALSE(Set.contains(1));
+
+  Set.insert(0);
+  Set.insert(1);
+  EXPECT_TRUE(Set.contains(0));
+  EXPECT_TRUE(Set.contains(1));
+
+  Set.insert(1);
+  EXPECT_TRUE(Set.contains(0));
+  EXPECT_TRUE(Set.contains(1));
+
+  Set.erase(1);
+  EXPECT_TRUE(Set.contains(0));
+  EXPECT_FALSE(Set.contains(1));
+
+  Set.insert(1);
+  Set.insert(2);
+  EXPECT_TRUE(Set.contains(0));
+  EXPECT_TRUE(Set.contains(1));
+  EXPECT_TRUE(Set.contains(2));
+}