[llvm] 1d8eef4 - [llvm] Add contains(KeyType) -> bool methods to SetVector

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


Author: David Blaikie
Date: 2020-07-17T11:26:27-07:00
New Revision: 1d8eef41f5afa9503ca9a7506c6fcb874da62941

URL: https://github.com/llvm/llvm-project/commit/1d8eef41f5afa9503ca9a7506c6fcb874da62941
DIFF: https://github.com/llvm/llvm-project/commit/1d8eef41f5afa9503ca9a7506c6fcb874da62941.diff

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

Matches C++20 API addition.

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

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SetVector.h
    llvm/unittests/ADT/SetVectorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h
index 91ad72143ed3..32bcd50966cc 100644
--- a/llvm/include/llvm/ADT/SetVector.h
+++ b/llvm/include/llvm/ADT/SetVector.h
@@ -205,6 +205,11 @@ class SetVector {
     return true;
   }
 
+  /// Check if the SetVector contains the given key.
+  bool contains(const key_type &key) const {
+    return set_.find(key) != set_.end();
+  }
+
   /// Count the number of elements of a given key in the SetVector.
   /// \returns 0 if the element is not in the SetVector, 1 if it is.
   size_type count(const key_type &key) const {

diff  --git a/llvm/unittests/ADT/SetVectorTest.cpp b/llvm/unittests/ADT/SetVectorTest.cpp
index b9fef78d2fdb..d5d49fba5e58 100644
--- a/llvm/unittests/ADT/SetVectorTest.cpp
+++ b/llvm/unittests/ADT/SetVectorTest.cpp
@@ -31,3 +31,20 @@ TEST(SetVector, EraseTest) {
   EXPECT_EQ(2, *std::next(S.begin()));
 }
 
+TEST(SetVector, ContainsTest) {
+  SetVector<int> S;
+  S.insert(0);
+  S.insert(1);
+  S.insert(2);
+
+  EXPECT_TRUE(S.contains(0));
+  EXPECT_TRUE(S.contains(1));
+  EXPECT_TRUE(S.contains(2));
+  EXPECT_FALSE(S.contains(-1));
+
+  S.insert(2);
+  EXPECT_TRUE(S.contains(2));
+
+  S.remove(2);
+  EXPECT_FALSE(S.contains(2));
+}


        


More information about the llvm-commits mailing list