From 795a17e53042fbf30c8ef8e2e71a7f19ee5cbd99 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Wed, 12 Jul 2023 16:18:24 -0400 Subject: [PATCH] [flat_set] Add a const `fm.keys()` accessor 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. --- include/sg14/flat_set.h | 4 ++++ test/flat_map_test.cpp | 15 ++++++++------- test/flat_set_test.cpp | 13 +++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/sg14/flat_set.h b/include/sg14/flat_set.h index af1c166..e623bed 100644 --- a/include/sg14/flat_set.h +++ b/include/sg14/flat_set.h @@ -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)) { diff --git a/test/flat_map_test.cpp b/test/flat_map_test.cpp index edc6ae4..acf1afa 100644 --- a/test/flat_map_test.cpp +++ b/test/flat_map_test.cpp @@ -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> underlying = {{1,'1'}, {2,'2'}, {3,'3'}, {4,'4'}}; - std::vector> expected(underlying.begin(), underlying.end()); - std::vector> 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> 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) diff --git a/test/flat_set_test.cpp b/test/flat_set_test.cpp index 8edebb9..8059671 100644 --- a/test/flat_set_test.cpp +++ b/test/flat_set_test.cpp @@ -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 fs = {3, 1, 4, 2}; + const auto& cfs = fs; + static_assert(std::is_same&>::value, ""); + static_assert(std::is_same&>::value, ""); + std::vector expected_keys = {1, 2, 3, 4}; + EXPECT_EQ(fs.keys(), expected_keys); + EXPECT_EQ(cfs.keys(), expected_keys); +}