Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/MapVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class MapVector {
return try_emplace_impl(Key).first->second;
}

[[nodiscard]] inline auto keys() { return make_first_range(Vector); }
[[nodiscard]] inline auto keys() const { return make_first_range(Vector); }
[[nodiscard]] inline auto values() { return make_second_range(Vector); }
[[nodiscard]] inline auto values() const { return make_second_range(Vector); }

// Returns a copy of the value. Only allowed if ValueT is copyable.
[[nodiscard]] ValueT lookup(const KeyT &Key) const {
static_assert(std::is_copy_constructible_v<ValueT>,
Expand Down
18 changes: 18 additions & 0 deletions llvm/unittests/ADT/MapVectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,24 @@ TEST(MapVectorTest, AtTest) {
EXPECT_EQ(ConstMV.at(1), 12);
}

TEST(MapVectorTest, KeysValuesIterator) {
MapVector<int, int> MV;

MV.insert(std::make_pair(1, 11));
MV.insert(std::make_pair(2, 12));
MV.insert(std::make_pair(3, 13));
MV.insert(std::make_pair(4, 14));
MV.insert(std::make_pair(5, 15));
MV.insert(std::make_pair(6, 16));

EXPECT_THAT(MV.keys(), testing::ElementsAre(1, 2, 3, 4, 5, 6));
EXPECT_THAT(MV.values(), testing::ElementsAre(11, 12, 13, 14, 15, 16));

const MapVector<int, int> &ConstMV = MV;
EXPECT_THAT(ConstMV.keys(), testing::ElementsAre(1, 2, 3, 4, 5, 6));
EXPECT_THAT(ConstMV.values(), testing::ElementsAre(11, 12, 13, 14, 15, 16));
}

template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test {
using int_type = IntType;
};
Expand Down