Skip to content

Commit

Permalink
[flat_map] Add a non-const fm.values() accessor
Browse files Browse the repository at this point in the history
Thanks to Sodalit (via cpplang Slack) for the feature request!
  • Loading branch information
Quuxplusone committed Feb 26, 2023
1 parent 33be24d commit 2200f21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/sg14/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,10 @@ class flat_map {
return c_.keys;
}

MappedContainer& values() {
return c_.values;
}

const MappedContainer& values() const {
return c_.values;
}
Expand Down
18 changes: 18 additions & 0 deletions test/flat_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,21 @@ TEST(flat_map, Iterators)
EXPECT_TRUE(std::equal(fm.rbegin(), fm.rend(), expected.rbegin(), expected.rend()));
EXPECT_TRUE(std::equal(fm.crbegin(), fm.crend(), cexpected.rbegin(), cexpected.rend()));
}

TEST(flat_map, ContainerAccessors)
{
// The C++23 flat_map provides only `ctrs = fm.extract()` and `fm.replace(ctrs)`,
// but the SG14 version provides direct access to the containers in place.
sg14::flat_map<int, char> fm = {{3,'3'}, {1,'1'}, {4,'4'}, {2,'2'}};
const auto& cfm = fm;
static_assert(std::is_same<decltype(fm.keys()), const std::vector<int>&>::value, "");
static_assert(std::is_same<decltype(fm.values()), std::vector<char>&>::value, "");
static_assert(std::is_same<decltype(cfm.keys()), const std::vector<int>&>::value, "");
static_assert(std::is_same<decltype(cfm.values()), const std::vector<char>&>::value, "");
std::vector<int> expected_keys = {1, 2, 3, 4};
std::vector<char> expected_values = {'1', '2', '3', '4'};
EXPECT_EQ(fm.keys(), expected_keys);
EXPECT_EQ(fm.values(), expected_values);
EXPECT_EQ(cfm.keys(), expected_keys);
EXPECT_EQ(cfm.values(), expected_values);
}

0 comments on commit 2200f21

Please sign in to comment.