Skip to content

Commit 8602a65

Browse files
authored
[DenseMap] Introduce keys, values iterators (#138848)
1 parent f4e7ba0 commit 8602a65

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/ADL.h"
1818
#include "llvm/ADT/DenseMapInfo.h"
1919
#include "llvm/ADT/EpochTracker.h"
20+
#include "llvm/ADT/STLExtras.h"
2021
#include "llvm/Support/AlignOf.h"
2122
#include "llvm/Support/Compiler.h"
2223
#include "llvm/Support/MathExtras.h"
@@ -96,6 +97,24 @@ class DenseMapBase : public DebugEpochBase {
9697
return makeConstIterator(getBucketsEnd(), getBucketsEnd(), *this, true);
9798
}
9899

100+
// Return an iterator to iterate over keys in the map.
101+
inline auto keys() {
102+
return map_range(*this, [](const BucketT &P) { return P.getFirst(); });
103+
}
104+
105+
// Return an iterator to iterate over values in the map.
106+
inline auto values() {
107+
return map_range(*this, [](const BucketT &P) { return P.getSecond(); });
108+
}
109+
110+
inline auto keys() const {
111+
return map_range(*this, [](const BucketT &P) { return P.getFirst(); });
112+
}
113+
114+
inline auto values() const {
115+
return map_range(*this, [](const BucketT &P) { return P.getSecond(); });
116+
}
117+
99118
[[nodiscard]] bool empty() const { return getNumEntries() == 0; }
100119
unsigned size() const { return getNumEntries(); }
101120

llvm/unittests/ADT/DenseMapTest.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "CountCopyAndMove.h"
1111
#include "llvm/ADT/DenseMapInfo.h"
1212
#include "llvm/ADT/DenseMapInfoVariant.h"
13+
#include "llvm/ADT/SmallSet.h"
1314
#include "llvm/ADT/StringRef.h"
1415
#include "gmock/gmock.h"
1516
#include "gtest/gtest.h"
@@ -359,6 +360,51 @@ TYPED_TEST(DenseMapTest, ConstIteratorTest) {
359360
EXPECT_TRUE(cit == cit2);
360361
}
361362

363+
TYPED_TEST(DenseMapTest, KeysValuesIterator) {
364+
SmallSet<typename TypeParam::key_type, 10> Keys;
365+
SmallSet<typename TypeParam::mapped_type, 10> Values;
366+
for (int I = 0; I < 10; ++I) {
367+
auto K = this->getKey(I);
368+
auto V = this->getValue(I);
369+
Keys.insert(K);
370+
Values.insert(V);
371+
this->Map[K] = V;
372+
}
373+
374+
SmallSet<typename TypeParam::key_type, 10> ActualKeys;
375+
SmallSet<typename TypeParam::mapped_type, 10> ActualValues;
376+
for (auto K : this->Map.keys())
377+
ActualKeys.insert(K);
378+
for (auto V : this->Map.values())
379+
ActualValues.insert(V);
380+
381+
EXPECT_EQ(Keys, ActualKeys);
382+
EXPECT_EQ(Values, ActualValues);
383+
}
384+
385+
TYPED_TEST(DenseMapTest, ConstKeysValuesIterator) {
386+
SmallSet<typename TypeParam::key_type, 10> Keys;
387+
SmallSet<typename TypeParam::mapped_type, 10> Values;
388+
for (int I = 0; I < 10; ++I) {
389+
auto K = this->getKey(I);
390+
auto V = this->getValue(I);
391+
Keys.insert(K);
392+
Values.insert(V);
393+
this->Map[K] = V;
394+
}
395+
396+
const TypeParam &ConstMap = this->Map;
397+
SmallSet<typename TypeParam::key_type, 10> ActualKeys;
398+
SmallSet<typename TypeParam::mapped_type, 10> ActualValues;
399+
for (auto K : ConstMap.keys())
400+
ActualKeys.insert(K);
401+
for (auto V : ConstMap.values())
402+
ActualValues.insert(V);
403+
404+
EXPECT_EQ(Keys, ActualKeys);
405+
EXPECT_EQ(Values, ActualValues);
406+
}
407+
362408
// Test initializer list construction.
363409
TEST(DenseMapCustomTest, InitializerList) {
364410
DenseMap<int, int> M({{0, 0}, {0, 1}, {1, 2}});

0 commit comments

Comments
 (0)