From 294e3ec1ba1c1ebd486998c9a2f191bf5f1eab82 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Fri, 17 Jul 2020 10:42:23 -0700 Subject: [PATCH] [llvm] Add contains(KeyType) -> bool methods to SmallSet Matches C++20 API addition. Differential Revision: https://reviews.llvm.org/D83449 --- include/llvm/ADT/SmallSet.h | 7 +++++++ unittests/ADT/SmallSetTest.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/llvm/ADT/SmallSet.h b/include/llvm/ADT/SmallSet.h index a03fa7dd842..0600e528ee6 100644 --- a/include/llvm/ADT/SmallSet.h +++ b/include/llvm/ADT/SmallSet.h @@ -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(); } diff --git a/unittests/ADT/SmallSetTest.cpp b/unittests/ADT/SmallSetTest.cpp index 06682ce823d..26cab828c78 100644 --- a/unittests/ADT/SmallSetTest.cpp +++ b/unittests/ADT/SmallSetTest.cpp @@ -167,3 +167,28 @@ TEST(SmallSetTest, EqualityComparisonTest) { EXPECT_NE(s1small, s4large); EXPECT_NE(s4large, s3large); } + +TEST(SmallSetTest, Contains) { + SmallSet 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)); +}