Skip to content

Commit

Permalink
[flat_set] Add a const fm.keys() accessor
Browse files Browse the repository at this point in the history
As proposed in P2767R0.
Also, fix a unit test that was apparently too sneaky for C++23;
I didn't look far into what was going wrong with it.
  • Loading branch information
Quuxplusone committed Jul 12, 2023
1 parent 2200f21 commit 795a17e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions include/sg14/flat_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ class flat_set {
Compare key_comp() const { return compare_; }
Compare value_comp() const { return compare_; }

const KeyContainer& keys() const {
return c_;
}

iterator find(const Key& t) {
auto it = this->lower_bound(t);
if (it == this->end() || compare_(t, *it)) {
Expand Down
15 changes: 8 additions & 7 deletions test/flat_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,14 @@ TEST(flat_map, Iterators)
EXPECT_EQ(fm.end(), fm.cend());
EXPECT_EQ(fm.rbegin(), fm.crbegin());
EXPECT_EQ(fm.rend(), fm.crend());
std::vector<std::pair<int, char>> underlying = {{1,'1'}, {2,'2'}, {3,'3'}, {4,'4'}};
std::vector<std::pair<const int&, char&>> expected(underlying.begin(), underlying.end());
std::vector<std::pair<const int&, const char&>> cexpected(underlying.begin(), underlying.end());
EXPECT_TRUE(std::equal(fm.begin(), fm.end(), expected.begin(), expected.end()));
EXPECT_TRUE(std::equal(fm.cbegin(), fm.cend(), cexpected.begin(), cexpected.end()));
EXPECT_TRUE(std::equal(fm.rbegin(), fm.rend(), expected.rbegin(), expected.rend()));
EXPECT_TRUE(std::equal(fm.crbegin(), fm.crend(), cexpected.rbegin(), cexpected.rend()));
std::vector<std::pair<int, char>> expected = {{1,'1'}, {2,'2'}, {3,'3'}, {4,'4'}};
auto eq = [](auto&& p1, auto&& p2) {
return (p1.first == p2.first) && (p1.second == p2.second);
};
EXPECT_TRUE(std::equal(fm.begin(), fm.end(), expected.begin(), expected.end(), eq));
EXPECT_TRUE(std::equal(fm.cbegin(), fm.cend(), expected.cbegin(), expected.cend(), eq));
EXPECT_TRUE(std::equal(fm.rbegin(), fm.rend(), expected.rbegin(), expected.rend(), eq));
EXPECT_TRUE(std::equal(fm.crbegin(), fm.crend(), expected.crbegin(), expected.crend(), eq));
}

TEST(flat_map, ContainerAccessors)
Expand Down
13 changes: 13 additions & 0 deletions test/flat_set_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,16 @@ TEST(flat_set, Iterators)
EXPECT_TRUE(std::equal(fs.rbegin(), fs.rend(), expected.rbegin(), expected.rend()));
EXPECT_TRUE(std::equal(fs.crbegin(), fs.crend(), expected.rbegin(), expected.rend()));
}

TEST(flat_set, ContainerAccessors)
{
// The C++23 flat_set provides only `ctr = fs.extract()` and `fs.replace(ctr)`,
// but the SG14 version provides direct access to the container in place.
sg14::flat_set<int> fs = {3, 1, 4, 2};
const auto& cfs = fs;
static_assert(std::is_same<decltype(fs.keys()), const std::vector<int>&>::value, "");
static_assert(std::is_same<decltype(cfs.keys()), const std::vector<int>&>::value, "");
std::vector<int> expected_keys = {1, 2, 3, 4};
EXPECT_EQ(fs.keys(), expected_keys);
EXPECT_EQ(cfs.keys(), expected_keys);
}

0 comments on commit 795a17e

Please sign in to comment.