From 3cfbc95ca8e4dc10a2ad3d2fb065b21ec2e3696b Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Fri, 17 Jul 2020 10:41:03 -0700 Subject: [PATCH] [llvm] Add contains(KeyType) -> bool methods to SetVector Matches C++20 API addition. Differential Revision: https://reviews.llvm.org/D83449 --- include/llvm/ADT/SetVector.h | 5 +++++ unittests/ADT/SetVectorTest.cpp | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/llvm/ADT/SetVector.h b/include/llvm/ADT/SetVector.h index 91ad72143ed..32bcd50966c 100644 --- a/include/llvm/ADT/SetVector.h +++ b/include/llvm/ADT/SetVector.h @@ -205,6 +205,11 @@ public: 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/unittests/ADT/SetVectorTest.cpp b/unittests/ADT/SetVectorTest.cpp index b9fef78d2fd..d5d49fba5e5 100644 --- a/unittests/ADT/SetVectorTest.cpp +++ b/unittests/ADT/SetVectorTest.cpp @@ -31,3 +31,20 @@ TEST(SetVector, EraseTest) { EXPECT_EQ(2, *std::next(S.begin())); } +TEST(SetVector, ContainsTest) { + SetVector 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)); +}