Skip to content

Commit a54edaf

Browse files
authored
ADT: Complete the at() methods for DenseMap and MapVector (llvm#169147)
Make it easier to use these containers as drop-in replacements for std::map.
1 parent 0332af2 commit a54edaf

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ class DenseMapBase : public DebugEpochBase {
219219
return Default;
220220
}
221221

222+
/// at - Return the entry for the specified key, or abort if no such
223+
/// entry exists.
224+
[[nodiscard]] ValueT &at(const_arg_type_t<KeyT> Val) {
225+
auto Iter = this->find(std::move(Val));
226+
assert(Iter != this->end() && "DenseMap::at failed due to a missing key");
227+
return Iter->second;
228+
}
229+
222230
/// at - Return the entry for the specified key, or abort if no such
223231
/// entry exists.
224232
[[nodiscard]] const ValueT &at(const_arg_type_t<KeyT> Val) const {

llvm/include/llvm/ADT/MapVector.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,28 @@ class MapVector {
148148

149149
[[nodiscard]] iterator find(const KeyT &Key) {
150150
typename MapType::const_iterator Pos = Map.find(Key);
151-
return Pos == Map.end()? Vector.end() :
152-
(Vector.begin() + Pos->second);
151+
return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
153152
}
154153

155154
[[nodiscard]] const_iterator find(const KeyT &Key) const {
156155
typename MapType::const_iterator Pos = Map.find(Key);
157-
return Pos == Map.end()? Vector.end() :
158-
(Vector.begin() + Pos->second);
156+
return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
157+
}
158+
159+
/// at - Return the entry for the specified key, or abort if no such
160+
/// entry exists.
161+
[[nodiscard]] ValueT &at(const KeyT &Key) {
162+
auto Pos = Map.find(Key);
163+
assert(Pos != Map.end() && "MapVector::at failed due to a missing key");
164+
return Vector[Pos->second].second;
165+
}
166+
167+
/// at - Return the entry for the specified key, or abort if no such
168+
/// entry exists.
169+
[[nodiscard]] const ValueT &at(const KeyT &Key) const {
170+
auto Pos = Map.find(Key);
171+
assert(Pos != Map.end() && "MapVector::at failed due to a missing key");
172+
return Vector[Pos->second].second;
159173
}
160174

161175
/// Remove the last element from the vector.

llvm/unittests/ADT/DenseMapTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ TYPED_TEST(DenseMapTest, AtTest) {
200200
EXPECT_EQ(this->getValue(0), this->Map.at(this->getKey(0)));
201201
EXPECT_EQ(this->getValue(1), this->Map.at(this->getKey(1)));
202202
EXPECT_EQ(this->getValue(2), this->Map.at(this->getKey(2)));
203+
204+
this->Map.at(this->getKey(0)) = this->getValue(1);
205+
EXPECT_EQ(this->getValue(1), this->Map.at(this->getKey(0)));
206+
207+
const auto &ConstMap = this->Map;
208+
EXPECT_EQ(this->getValue(1), ConstMap.at(this->getKey(0)));
209+
EXPECT_EQ(this->getValue(1), ConstMap.at(this->getKey(1)));
210+
EXPECT_EQ(this->getValue(2), ConstMap.at(this->getKey(2)));
203211
}
204212

205213
// Test clear() method

llvm/unittests/ADT/MapVectorTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ TEST(MapVectorTest, GetArrayRef) {
292292
EXPECT_TRUE(MV.getArrayRef().equals({std::pair(100, 99), std::pair(99, 98)}));
293293
}
294294

295+
TEST(MapVectorTest, AtTest) {
296+
MapVector<int, int> MV;
297+
MV[0] = 10;
298+
MV[1] = 11;
299+
EXPECT_EQ(MV.at(0), 10);
300+
EXPECT_EQ(MV.at(1), 11);
301+
302+
MV.at(1) = 12;
303+
EXPECT_EQ(MV.at(1), 12);
304+
305+
const auto &ConstMV = MV;
306+
EXPECT_EQ(ConstMV.at(0), 10);
307+
EXPECT_EQ(ConstMV.at(1), 12);
308+
}
309+
295310
template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test {
296311
using int_type = IntType;
297312
};

0 commit comments

Comments
 (0)