From fcba8f5f646490e5ac15d09499be27e447d41d5c Mon Sep 17 00:00:00 2001 From: Ole Erik Peistorpet Date: Sun, 9 Jun 2024 16:14:46 +0200 Subject: [PATCH] unordered_erase overload for dynarray moved to range_algo.h, simplified to_dynarray Also added ../ to an include. to_dynarray no longer niebloid --- auxi/dynarray_detail.h | 2 +- dynarray.h | 19 +++++-------------- range_algo.h | 3 +++ unit_test/dynarray_mutate_gtest.cpp | 7 ------- unit_test/range_algo_gtest.cpp | 19 +++++++++++++++++++ 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/auxi/dynarray_detail.h b/auxi/dynarray_detail.h index ee4973b2..06d28e90 100644 --- a/auxi/dynarray_detail.h +++ b/auxi/dynarray_detail.h @@ -6,7 +6,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -#include "util.h" // for from_range +#include "../util.h" // for from_range #include // for uintptr_t #include diff --git a/dynarray.h b/dynarray.h index 36b03f41..3d0b2ef7 100644 --- a/dynarray.h +++ b/dynarray.h @@ -21,14 +21,6 @@ namespace oel { -struct _toDynarrayFn -{ - template< typename Alloc = allocator<> > - constexpr auto operator()(Alloc a = {}) const - { - return _detail::ToDynarrPartial{std::move(a)}; - } -}; //! `r | to_dynarray()` is equivalent to `r | std::ranges::to()` /** * Example, convert array of std::bitset to `dynarray`: @@ -36,12 +28,11 @@ struct _toDynarrayFn std::bitset<8> arr[] {3, 5, 7, 11}; auto result = arr | view::transform(OEL_MEMBER_FN(to_string)) | to_dynarray(); @endcode */ -inline constexpr _toDynarrayFn to_dynarray; - -//! Overloads generic unordered_erase(RandomAccessContainer &, Integral) (in range_algo.h) -template< typename T, typename A > inline -void unordered_erase(dynarray & d, ptrdiff_t index) { d.unordered_erase(d.begin() + index); } - +template< typename Alloc = allocator<> > +constexpr auto to_dynarray(Alloc a = {}) + { + return _detail::ToDynarrPartial{std::move(a)}; + } //! dynarray is trivially relocatable if Alloc is template< typename T, typename Alloc > diff --git a/range_algo.h b/range_algo.h index c6ce915f..ac359d60 100644 --- a/range_algo.h +++ b/range_algo.h @@ -29,6 +29,9 @@ constexpr void unordered_erase(RandomAccessContainer & c, Integral index) c[index] = std::move(c.back()); c.pop_back(); } +//! See unordered_erase(RandomAccessContainer &, Integral) or dynarray::unordered_erase +template< typename T, typename A > inline +void unordered_erase(dynarray & d, ptrdiff_t index) { d.unordered_erase(d.begin() + index); } /** * @brief Erase from container all elements for which predicate returns true diff --git a/unit_test/dynarray_mutate_gtest.cpp b/unit_test/dynarray_mutate_gtest.cpp index 4b18bb0d..29052a40 100644 --- a/unit_test/dynarray_mutate_gtest.cpp +++ b/unit_test/dynarray_mutate_gtest.cpp @@ -854,13 +854,6 @@ void testUnorderedErase() EXPECT_EQ(-2, *(*it)); it = d.unordered_erase(it); EXPECT_EQ(end(d), it); - - d.emplace_back(-1); - d.emplace_back(2); - unordered_erase(d, 1); - EXPECT_EQ(-1, *d.back()); - unordered_erase(d, 0); - EXPECT_TRUE(d.empty()); } TEST_F(dynarrayTest, unorderedErase) diff --git a/unit_test/range_algo_gtest.cpp b/unit_test/range_algo_gtest.cpp index 3bfc2047..2176f443 100644 --- a/unit_test/range_algo_gtest.cpp +++ b/unit_test/range_algo_gtest.cpp @@ -27,6 +27,25 @@ TEST(rangeTest, unorderedErase) EXPECT_EQ("aa", d.front()); } +template +void testUnorderedErase() +{ + Container c; + c.emplace_back(-1); + c.emplace_back(2); + + unordered_erase(c, 1); + EXPECT_EQ(-1, *c.back()); + unordered_erase(c, 0); + EXPECT_TRUE(c.empty()); +} + +TEST(rangeTest, unorderedEraseDynarray) +{ + testUnorderedErase< oel::dynarray >(); + testUnorderedErase< oel::dynarray >(); +} + TEST(rangeTest, eraseIf) { using namespace oel;