[llvm] 2170067 - [ADT] Implement {DenseMap, MapVector, StringMap}::contains
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 13 09:13:59 PDT 2023
Author: Kazu Hirata
Date: 2023-03-13T09:13:28-07:00
New Revision: 21700677cb44f58a8f4c9d9ed6c05dd049108854
URL: https://github.com/llvm/llvm-project/commit/21700677cb44f58a8f4c9d9ed6c05dd049108854
DIFF: https://github.com/llvm/llvm-project/commit/21700677cb44f58a8f4c9d9ed6c05dd049108854.diff
LOG: [ADT] Implement {DenseMap,MapVector,StringMap}::contains
This patch implements the C++20-style contains() for DenseMap,
MapVector, and StringMap.
With this patch, every set and map container type that has count()
also has contains().
Differential Revision: https://reviews.llvm.org/D145895
Added:
Modified:
llvm/include/llvm/ADT/DenseMap.h
llvm/include/llvm/ADT/MapVector.h
llvm/include/llvm/ADT/StringMap.h
llvm/unittests/ADT/DenseMapTest.cpp
llvm/unittests/ADT/MapVectorTest.cpp
llvm/unittests/ADT/StringMapTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 12a6673cc62c1..8a680c031d302 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -141,10 +141,15 @@ class DenseMapBase : public DebugEpochBase {
setNumTombstones(0);
}
+ /// Return true if the specified key is in the map, false otherwise.
+ bool contains(const_arg_type_t<KeyT> 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<KeyT> Val) const {
- const BucketT *TheBucket;
- return LookupBucketFor(Val, TheBucket) ? 1 : 0;
+ return contains(Val) ? 1 : 0;
}
iterator find(const_arg_type_t<KeyT> Val) {
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 9d908f3af4ed0..a129b63f8be7a 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -140,10 +140,9 @@ class MapVector {
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
index 443714c02389f..17e81bcc2ab94 100644
--- a/llvm/include/llvm/ADT/StringMap.h
+++ b/llvm/include/llvm/ADT/StringMap.h
@@ -249,8 +249,11 @@ class StringMap : public StringMapImpl,
/// 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 <typename InputTy>
size_type count(const StringMapEntry<InputTy> &MapEntry) const {
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index 69bf4e19e4eea..79d57d35265c2 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -122,6 +122,7 @@ TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
// 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 @@ TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
// 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
index 552f9956bdc2c..1a371cbfba81e 100644
--- a/llvm/unittests/ADT/MapVectorTest.cpp
+++ b/llvm/unittests/ADT/MapVectorTest.cpp
@@ -87,8 +87,10 @@ TEST(MapVectorTest, erase) {
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
index 431b3973d6d6c..f9b138e9a4721 100644
--- a/llvm/unittests/ADT/StringMapTest.cpp
+++ b/llvm/unittests/ADT/StringMapTest.cpp
@@ -41,6 +41,7 @@ class StringMapTest : public testing::Test {
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 @@ class StringMapTest : public testing::Test {
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));
More information about the llvm-commits
mailing list