Skip to content

Commit

Permalink
[llvm] Add contains(KeyType) -> bool methods to SmallSet
Browse files Browse the repository at this point in the history
Matches C++20 API addition.

Differential Revision: https://reviews.llvm.org/D83449
  • Loading branch information
dwblaikie committed Jul 17, 2020
1 parent a0385bd commit dd4426b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions llvm/include/llvm/ADT/SmallSet.h
Expand Up @@ -232,6 +232,13 @@ class SmallSet {
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(); }

Expand Down
25 changes: 25 additions & 0 deletions llvm/unittests/ADT/SmallSetTest.cpp
Expand Up @@ -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));
}

0 comments on commit dd4426b

Please sign in to comment.