diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h --- a/llvm/include/llvm/ADT/DenseSet.h +++ b/llvm/include/llvm/ADT/DenseSet.h @@ -173,6 +173,11 @@ return ConstIterator(TheMap.find(V)); } + /// Check if the set contains the given element. + bool contains(const_arg_type_t V) const { + return TheMap.find(V) != TheMap.end(); + } + /// Alternative version of find() which allows a different, and possibly less /// expensive, key type. /// The DenseMapInfo is responsible for supplying methods diff --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h --- a/llvm/include/llvm/ADT/SetVector.h +++ b/llvm/include/llvm/ADT/SetVector.h @@ -205,6 +205,11 @@ 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/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h --- a/llvm/include/llvm/ADT/SmallPtrSet.h +++ b/llvm/include/llvm/ADT/SmallPtrSet.h @@ -378,6 +378,9 @@ iterator find(ConstPtrType Ptr) const { return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr))); } + bool contains(ConstPtrType Ptr) const { + return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer(); + } template void insert(IterT I, IterT E) { diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h --- a/llvm/include/llvm/ADT/SmallSet.h +++ b/llvm/include/llvm/ADT/SmallSet.h @@ -232,6 +232,13 @@ 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/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h --- a/llvm/include/llvm/ADT/SparseSet.h +++ b/llvm/include/llvm/ADT/SparseSet.h @@ -229,12 +229,15 @@ 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/llvm/include/llvm/ADT/StringSet.h b/llvm/include/llvm/ADT/StringSet.h --- a/llvm/include/llvm/ADT/StringSet.h +++ b/llvm/include/llvm/ADT/StringSet.h @@ -45,6 +45,9 @@ insert(const StringMapEntry &mapEntry) { return insert(mapEntry.getKey()); } + + /// Check if the set contains the given \c key. + bool contains(StringRef key) const { return Base::FindKey(key) != -1; } }; } // end namespace llvm diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp --- a/llvm/unittests/ADT/DenseSetTest.cpp +++ b/llvm/unittests/ADT/DenseSetTest.cpp @@ -227,7 +227,7 @@ Map.insert(B); EXPECT_EQ(Map.count(B), 1u); EXPECT_EQ(Map.count(C), 1u); - EXPECT_NE(Map.find(B), Map.end()); - EXPECT_NE(Map.find(C), Map.end()); + EXPECT_TRUE(Map.contains(B)); + EXPECT_TRUE(Map.contains(C)); } } diff --git a/llvm/unittests/ADT/SetVectorTest.cpp b/llvm/unittests/ADT/SetVectorTest.cpp --- a/llvm/unittests/ADT/SetVectorTest.cpp +++ b/llvm/unittests/ADT/SetVectorTest.cpp @@ -31,3 +31,20 @@ 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)); +} diff --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp --- a/llvm/unittests/ADT/SmallPtrSetTest.cpp +++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp @@ -313,8 +313,8 @@ IntSet.insert(B); EXPECT_EQ(IntSet.count(B), 1u); EXPECT_EQ(IntSet.count(C), 1u); - EXPECT_NE(IntSet.find(B), IntSet.end()); - EXPECT_NE(IntSet.find(C), IntSet.end()); + EXPECT_TRUE(IntSet.contains(B)); + EXPECT_TRUE(IntSet.contains(C)); } // Verify that we automatically get the const version of PointerLikeTypeTraits @@ -327,7 +327,7 @@ TestPair Pair(&A[0], 1); IntSet.insert(Pair); EXPECT_EQ(IntSet.count(Pair), 1u); - EXPECT_NE(IntSet.find(Pair), IntSet.end()); + EXPECT_TRUE(IntSet.contains(Pair)); } // Test equality comparison. @@ -367,3 +367,31 @@ EXPECT_NE(c, e); EXPECT_NE(e, d); } + +TEST(SmallPtrSetTest, Contains) { + SmallPtrSet Set; + int buf[4] = {0, 11, 22, 11}; + EXPECT_FALSE(Set.contains(&buf[0])); + EXPECT_FALSE(Set.contains(&buf[1])); + + Set.insert(&buf[0]); + Set.insert(&buf[1]); + EXPECT_TRUE(Set.contains(&buf[0])); + EXPECT_TRUE(Set.contains(&buf[1])); + EXPECT_FALSE(Set.contains(&buf[3])); + + Set.insert(&buf[1]); + EXPECT_TRUE(Set.contains(&buf[0])); + EXPECT_TRUE(Set.contains(&buf[1])); + EXPECT_FALSE(Set.contains(&buf[3])); + + Set.erase(&buf[1]); + EXPECT_TRUE(Set.contains(&buf[0])); + EXPECT_FALSE(Set.contains(&buf[1])); + + Set.insert(&buf[1]); + Set.insert(&buf[2]); + EXPECT_TRUE(Set.contains(&buf[0])); + EXPECT_TRUE(Set.contains(&buf[1])); + EXPECT_TRUE(Set.contains(&buf[2])); +} diff --git a/llvm/unittests/ADT/SmallSetTest.cpp b/llvm/unittests/ADT/SmallSetTest.cpp --- a/llvm/unittests/ADT/SmallSetTest.cpp +++ b/llvm/unittests/ADT/SmallSetTest.cpp @@ -167,3 +167,28 @@ 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)); +} diff --git a/llvm/unittests/ADT/SparseSetTest.cpp b/llvm/unittests/ADT/SparseSetTest.cpp --- a/llvm/unittests/ADT/SparseSetTest.cpp +++ b/llvm/unittests/ADT/SparseSetTest.cpp @@ -25,15 +25,15 @@ 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 @@ 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 @@ 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()); } diff --git a/llvm/unittests/ADT/StringSetTest.cpp b/llvm/unittests/ADT/StringSetTest.cpp --- a/llvm/unittests/ADT/StringSetTest.cpp +++ b/llvm/unittests/ADT/StringSetTest.cpp @@ -53,4 +53,23 @@ EXPECT_EQ(Count, 1UL); } +TEST_F(StringSetTest, Contains) { + StringSet<> Set; + EXPECT_FALSE(Set.contains("")); + EXPECT_FALSE(Set.contains("test")); + + Set.insert(""); + Set.insert("test"); + EXPECT_TRUE(Set.contains("")); + EXPECT_TRUE(Set.contains("test")); + + Set.insert("test"); + EXPECT_TRUE(Set.contains("")); + EXPECT_TRUE(Set.contains("test")); + + Set.erase("test"); + EXPECT_TRUE(Set.contains("")); + EXPECT_FALSE(Set.contains("test")); +} + } // end anonymous namespace