diff --git a/include/llvm/ADT/SparseSet.h b/include/llvm/ADT/SparseSet.h index 74457d5fd67..d8acf1ee2f3 100644 --- a/include/llvm/ADT/SparseSet.h +++ b/include/llvm/ADT/SparseSet.h @@ -229,12 +229,15 @@ public: return const_cast(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. /// diff --git a/unittests/ADT/SparseSetTest.cpp b/unittests/ADT/SparseSetTest.cpp index 2b065ea901f..3eea4bde8c0 100644 --- a/unittests/ADT/SparseSetTest.cpp +++ b/unittests/ADT/SparseSetTest.cpp @@ -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()); } @@ -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)); @@ -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()); }