- Notifications
You must be signed in to change notification settings - Fork 15.3k
ADT: Complete the at() methods for DenseMap and MapVector #169147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+49 −4
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Make it easier to use these containers as drop-in replacements for std::map. commit-id:8b1d1826
Member
| @llvm/pr-subscribers-llvm-adt Author: Nicolai Hähnle (nhaehnle) ChangesMake it easier to use these containers as drop-in replacements for Full diff: https://github.com/llvm/llvm-project/pull/169147.diff 4 Files Affected:
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h index a706f68fab81b..fe8868619730e 100644 --- a/llvm/include/llvm/ADT/DenseMap.h +++ b/llvm/include/llvm/ADT/DenseMap.h @@ -219,6 +219,14 @@ class DenseMapBase : public DebugEpochBase { return Default; } + /// at - Return the entry for the specified key, or abort if no such + /// entry exists. + [[nodiscard]] ValueT &at(const_arg_type_t<KeyT> Val) { + auto Iter = this->find(std::move(Val)); + assert(Iter != this->end() && "DenseMap::at failed due to a missing key"); + return Iter->second; + } + /// at - Return the entry for the specified key, or abort if no such /// entry exists. [[nodiscard]] const ValueT &at(const_arg_type_t<KeyT> Val) const { diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h index 82f2c4977e01d..80bcb7e0b7ba4 100644 --- a/llvm/include/llvm/ADT/MapVector.h +++ b/llvm/include/llvm/ADT/MapVector.h @@ -148,14 +148,28 @@ class MapVector { [[nodiscard]] iterator find(const KeyT &Key) { typename MapType::const_iterator Pos = Map.find(Key); - return Pos == Map.end()? Vector.end() : - (Vector.begin() + Pos->second); + return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second); } [[nodiscard]] const_iterator find(const KeyT &Key) const { typename MapType::const_iterator Pos = Map.find(Key); - return Pos == Map.end()? Vector.end() : - (Vector.begin() + Pos->second); + return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second); + } + + /// at - Return the entry for the specified key, or abort if no such + /// entry exists. + [[nodiscard]] ValueT &at(const KeyT &Key) { + auto Pos = Map.find(Key); + assert(Pos != Map.end() && "MapVector::at failed due to a missing key"); + return Vector[Pos->second].second; + } + + /// at - Return the entry for the specified key, or abort if no such + /// entry exists. + [[nodiscard]] const ValueT &at(const KeyT &Key) const { + auto Pos = Map.find(Key); + assert(Pos != Map.end() && "MapVector::at failed due to a missing key"); + return Vector[Pos->second].second; } /// Remove the last element from the vector. diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp index 273ee09fc1e28..553d159d33b1a 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -200,6 +200,14 @@ TYPED_TEST(DenseMapTest, AtTest) { EXPECT_EQ(this->getValue(0), this->Map.at(this->getKey(0))); EXPECT_EQ(this->getValue(1), this->Map.at(this->getKey(1))); EXPECT_EQ(this->getValue(2), this->Map.at(this->getKey(2))); + + this->Map.at(this->getKey(0)) = this->getValue(1); + EXPECT_EQ(this->getValue(1), this->Map.at(this->getKey(0))); + + const auto &ConstMap = this->Map; + EXPECT_EQ(this->getValue(1), ConstMap.at(this->getKey(0))); + EXPECT_EQ(this->getValue(1), ConstMap.at(this->getKey(1))); + EXPECT_EQ(this->getValue(2), ConstMap.at(this->getKey(2))); } // Test clear() method diff --git a/llvm/unittests/ADT/MapVectorTest.cpp b/llvm/unittests/ADT/MapVectorTest.cpp index 639e1a14e8178..e0589445e3271 100644 --- a/llvm/unittests/ADT/MapVectorTest.cpp +++ b/llvm/unittests/ADT/MapVectorTest.cpp @@ -292,6 +292,21 @@ TEST(MapVectorTest, GetArrayRef) { EXPECT_TRUE(MV.getArrayRef().equals({std::pair(100, 99), std::pair(99, 98)})); } +TEST(MapVectorTest, AtTest) { + MapVector<int, int> MV; + MV[0] = 10; + MV[1] = 11; + EXPECT_EQ(MV.at(0), 10); + EXPECT_EQ(MV.at(1), 11); + + MV.at(1) = 12; + EXPECT_EQ(MV.at(1), 12); + + const auto &ConstMV = MV; + EXPECT_EQ(ConstMV.at(0), 10); + EXPECT_EQ(ConstMV.at(1), 12); +} + template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test { using int_type = IntType; }; |
kuhar approved these changes Nov 22, 2025
aadeshps-mcw pushed a commit to aadeshps-mcw/llvm-project that referenced this pull request Nov 26, 2025
Make it easier to use these containers as drop-in replacements for std::map.
Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Nov 26, 2025
Make it easier to use these containers as drop-in replacements for std::map.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Make it easier to use these containers as drop-in replacements for
std::map.