Skip to content

Commit

Permalink
[llvm] Add contains(KeyType) -> bool methods to SparseSet
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 dd4426b commit 39000aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 6 additions & 3 deletions llvm/include/llvm/ADT/SparseSet.h
Expand Up @@ -229,12 +229,15 @@ class SparseSet {
return const_cast<SparseSet*>(this)->findIndex(KeyIndexOf(Key));
}

/// Check if the set contains the given \c Key.
///
/// @param Key A valid key to find.
bool contains(const KeyT &Key) const { return find(Key) == end() ? 0 : 1; }

/// count - Returns 1 if this set contains an element identified by Key,
/// 0 otherwise.
///
size_type count(const KeyT &Key) const {
return find(Key) == end() ? 0 : 1;
}
size_type count(const KeyT &Key) const { return contains(Key) ? 1 : 0; }

/// insert - Attempts to insert a new element.
///
Expand Down
12 changes: 7 additions & 5 deletions llvm/unittests/ADT/SparseSetTest.cpp
Expand Up @@ -25,15 +25,15 @@ TEST(SparseSetTest, EmptySet) {
Set.setUniverse(10);

// Lookups on empty set.
EXPECT_TRUE(Set.find(0) == Set.end());
EXPECT_TRUE(Set.find(9) == Set.end());
EXPECT_FALSE(Set.contains(0));
EXPECT_FALSE(Set.contains(9));

// Same thing on a const reference.
const USet &CSet = Set;
EXPECT_TRUE(CSet.empty());
EXPECT_TRUE(CSet.begin() == CSet.end());
EXPECT_EQ(0u, CSet.size());
EXPECT_TRUE(CSet.find(0) == CSet.end());
EXPECT_FALSE(CSet.contains(0));
USet::const_iterator I = CSet.find(5);
EXPECT_TRUE(I == CSet.end());
}
Expand All @@ -51,8 +51,9 @@ TEST(SparseSetTest, SingleEntrySet) {
EXPECT_TRUE(Set.begin() + 1 == Set.end());
EXPECT_EQ(1u, Set.size());

EXPECT_TRUE(Set.find(0) == Set.end());
EXPECT_TRUE(Set.find(9) == Set.end());
EXPECT_FALSE(Set.contains(0));
EXPECT_FALSE(Set.contains(9));
EXPECT_TRUE(Set.contains(5));

EXPECT_FALSE(Set.count(0));
EXPECT_TRUE(Set.count(5));
Expand All @@ -71,6 +72,7 @@ TEST(SparseSetTest, SingleEntrySet) {
USet::iterator I = Set.find(5);
EXPECT_TRUE(I == Set.begin());
I = Set.erase(I);
EXPECT_FALSE(Set.contains(5));
EXPECT_TRUE(I == Set.end());
EXPECT_TRUE(Set.empty());
}
Expand Down

0 comments on commit 39000aa

Please sign in to comment.