Skip to content

Commit d7af3ce

Browse files
artagnonkcloudy0717
authored andcommitted
[MapVector] Introduce {keys,values} iterators (llvm#169675)
Similar to DenseMap::{keys,values}, introduce MapVector::{keys,values}.
1 parent 82cae0c commit d7af3ce

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/include/llvm/ADT/MapVector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class MapVector {
9999
return try_emplace_impl(Key).first->second;
100100
}
101101

102+
[[nodiscard]] auto keys() { return make_first_range(Vector); }
103+
[[nodiscard]] auto keys() const { return make_first_range(Vector); }
104+
[[nodiscard]] auto values() { return make_second_range(Vector); }
105+
[[nodiscard]] auto values() const { return make_second_range(Vector); }
106+
102107
// Returns a copy of the value. Only allowed if ValueT is copyable.
103108
[[nodiscard]] ValueT lookup(const KeyT &Key) const {
104109
static_assert(std::is_copy_constructible_v<ValueT>,

llvm/unittests/ADT/MapVectorTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,24 @@ TEST(MapVectorTest, AtTest) {
307307
EXPECT_EQ(ConstMV.at(1), 12);
308308
}
309309

310+
TEST(MapVectorTest, KeysValuesIterator) {
311+
MapVector<int, int> MV;
312+
313+
MV.insert(std::make_pair(1, 11));
314+
MV.insert(std::make_pair(2, 12));
315+
MV.insert(std::make_pair(3, 13));
316+
MV.insert(std::make_pair(4, 14));
317+
MV.insert(std::make_pair(5, 15));
318+
MV.insert(std::make_pair(6, 16));
319+
320+
EXPECT_THAT(MV.keys(), testing::ElementsAre(1, 2, 3, 4, 5, 6));
321+
EXPECT_THAT(MV.values(), testing::ElementsAre(11, 12, 13, 14, 15, 16));
322+
323+
const MapVector<int, int> &ConstMV = MV;
324+
EXPECT_THAT(ConstMV.keys(), testing::ElementsAre(1, 2, 3, 4, 5, 6));
325+
EXPECT_THAT(ConstMV.values(), testing::ElementsAre(11, 12, 13, 14, 15, 16));
326+
}
327+
310328
template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test {
311329
using int_type = IntType;
312330
};

0 commit comments

Comments
 (0)