diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h --- a/llvm/include/llvm/ADT/DenseMap.h +++ b/llvm/include/llvm/ADT/DenseMap.h @@ -141,10 +141,15 @@ setNumTombstones(0); } + /// Return true if the specified key is in the map, false otherwise. + bool contains(const_arg_type_t Val) const { + const BucketT *TheBucket; + return LookupBucketFor(Val, TheBucket); + } + /// Return 1 if the specified key is in the map, 0 otherwise. size_type count(const_arg_type_t Val) const { - const BucketT *TheBucket; - return LookupBucketFor(Val, TheBucket) ? 1 : 0; + return contains(Val) ? 1 : 0; } iterator find(const_arg_type_t Val) { diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h --- a/llvm/include/llvm/ADT/MapVector.h +++ b/llvm/include/llvm/ADT/MapVector.h @@ -140,10 +140,9 @@ return std::make_pair(begin() + I, false); } - size_type count(const KeyT &Key) const { - typename MapType::const_iterator Pos = Map.find(Key); - return Pos == Map.end()? 0 : 1; - } + bool contains(const KeyT &Key) const { return Map.find(Key) != Map.end(); } + + size_type count(const KeyT &Key) const { return contains(Key) ? 1 : 0; } iterator find(const KeyT &Key) { typename MapType::const_iterator Pos = Map.find(Key); diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -249,8 +249,11 @@ /// if the key is not in the map. ValueTy &operator[](StringRef Key) { return try_emplace(Key).first->second; } + /// contains - Return true if the element is in the map, false otherwise. + bool contains(StringRef Key) const { return find(Key) != end(); } + /// count - Return 1 if the element is in the map, 0 otherwise. - size_type count(StringRef Key) const { return find(Key) == end() ? 0 : 1; } + size_type count(StringRef Key) const { return contains(Key) ? 1 : 0; } template size_type count(const StringMapEntry &MapEntry) const { diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -122,6 +122,7 @@ // Lookup tests EXPECT_FALSE(this->Map.count(this->getKey())); + EXPECT_FALSE(this->Map.contains(this->getKey())); EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end()); EXPECT_EQ(typename TypeParam::mapped_type(), this->Map.lookup(this->getKey())); @@ -153,6 +154,7 @@ // Lookup tests EXPECT_TRUE(this->Map.count(this->getKey())); + EXPECT_TRUE(this->Map.contains(this->getKey())); EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin()); EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey())); EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); diff --git a/llvm/unittests/ADT/MapVectorTest.cpp b/llvm/unittests/ADT/MapVectorTest.cpp --- a/llvm/unittests/ADT/MapVectorTest.cpp +++ b/llvm/unittests/ADT/MapVectorTest.cpp @@ -87,8 +87,10 @@ MV.insert(std::make_pair(5, 6)); ASSERT_EQ(MV.size(), 3u); + ASSERT_TRUE(MV.contains(1)); MV.erase(MV.find(1)); ASSERT_EQ(MV.size(), 2u); + ASSERT_FALSE(MV.contains(1)); ASSERT_EQ(MV.find(1), MV.end()); ASSERT_EQ(MV[3], 4); ASSERT_EQ(MV[5], 6); diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -41,6 +41,7 @@ EXPECT_TRUE(testMap.begin() == testMap.end()); // Lookup tests + EXPECT_FALSE(testMap.contains(testKey)); EXPECT_EQ(0u, testMap.count(testKey)); EXPECT_EQ(0u, testMap.count(StringRef(testKeyFirst, testKeyLength))); EXPECT_EQ(0u, testMap.count(testKeyStr)); @@ -64,6 +65,7 @@ EXPECT_TRUE(it == testMap.end()); // Lookup tests + EXPECT_TRUE(testMap.contains(testKey)); EXPECT_EQ(1u, testMap.count(testKey)); EXPECT_EQ(1u, testMap.count(StringRef(testKeyFirst, testKeyLength))); EXPECT_EQ(1u, testMap.count(testKeyStr));