[llvm] 645bb8e - [llvm] Add contains(KeyType) -> bool methods to StringSet

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 11:27:01 PDT 2020


Author: David Blaikie
Date: 2020-07-17T11:26:27-07:00
New Revision: 645bb8e2086c9d72a5c29d0b224c563f70555365

URL: https://github.com/llvm/llvm-project/commit/645bb8e2086c9d72a5c29d0b224c563f70555365
DIFF: https://github.com/llvm/llvm-project/commit/645bb8e2086c9d72a5c29d0b224c563f70555365.diff

LOG: [llvm] Add contains(KeyType) -> bool methods to StringSet

Matches C++20 API addition.

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

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringSet.h
    llvm/unittests/ADT/StringSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringSet.h b/llvm/include/llvm/ADT/StringSet.h
index 63d929399a4e..c4245175544b 100644
--- a/llvm/include/llvm/ADT/StringSet.h
+++ b/llvm/include/llvm/ADT/StringSet.h
@@ -45,6 +45,9 @@ class StringSet : public StringMap<NoneType, AllocatorTy> {
   insert(const StringMapEntry<ValueTy> &mapEntry) {
     return insert(mapEntry.getKey());
   }
+
+  /// Check if the set contains the given \c key.
+  bool contains(StringRef key) const { return Base::FindKey(key) != -1; }
 };
 
 } // end namespace llvm

diff  --git a/llvm/unittests/ADT/StringSetTest.cpp b/llvm/unittests/ADT/StringSetTest.cpp
index e27306dd696f..0071f5a3775b 100644
--- a/llvm/unittests/ADT/StringSetTest.cpp
+++ b/llvm/unittests/ADT/StringSetTest.cpp
@@ -53,4 +53,23 @@ TEST_F(StringSetTest, EmptyString) {
   EXPECT_EQ(Count, 1UL);
 }
 
+TEST_F(StringSetTest, Contains) {
+  StringSet<> Set;
+  EXPECT_FALSE(Set.contains(""));
+  EXPECT_FALSE(Set.contains("test"));
+
+  Set.insert("");
+  Set.insert("test");
+  EXPECT_TRUE(Set.contains(""));
+  EXPECT_TRUE(Set.contains("test"));
+
+  Set.insert("test");
+  EXPECT_TRUE(Set.contains(""));
+  EXPECT_TRUE(Set.contains("test"));
+
+  Set.erase("test");
+  EXPECT_TRUE(Set.contains(""));
+  EXPECT_FALSE(Set.contains("test"));
+}
+
 } // end anonymous namespace


        


More information about the llvm-commits mailing list