Skip to content

Commit

Permalink
unordered_erase overload for dynarray moved to range_algo.h, simplifi…
Browse files Browse the repository at this point in the history
…ed to_dynarray

Also added ../ to an include.
to_dynarray no longer niebloid
  • Loading branch information
OleErikPeistorpet committed Jun 9, 2024
1 parent b43ba7a commit fcba8f5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion auxi/dynarray_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint> // for uintptr_t
#include <stdexcept>
Expand Down
19 changes: 5 additions & 14 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,18 @@
namespace oel
{

struct _toDynarrayFn
{
template< typename Alloc = allocator<> >
constexpr auto operator()(Alloc a = {}) const
{
return _detail::ToDynarrPartial<Alloc>{std::move(a)};
}
};
//! `r | to_dynarray()` is equivalent to `r | std::ranges::to<dynarray>()`
/**
* Example, convert array of std::bitset to `dynarray<std::string>`:
@code
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<T, A> & d, ptrdiff_t index) { d.unordered_erase(d.begin() + index); }

template< typename Alloc = allocator<> >
constexpr auto to_dynarray(Alloc a = {})
{
return _detail::ToDynarrPartial<Alloc>{std::move(a)};
}

//! dynarray is trivially relocatable if Alloc is
template< typename T, typename Alloc >
Expand Down
3 changes: 3 additions & 0 deletions range_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, A> & d, ptrdiff_t index) { d.unordered_erase(d.begin() + index); }

/**
* @brief Erase from container all elements for which predicate returns true
Expand Down
7 changes: 0 additions & 7 deletions unit_test/dynarray_mutate_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions unit_test/range_algo_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ TEST(rangeTest, unorderedErase)
EXPECT_EQ("aa", d.front());
}

template<typename Container>
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<MoveOnly> >();
testUnorderedErase< oel::dynarray<TrivialRelocat> >();
}

TEST(rangeTest, eraseIf)
{
using namespace oel;
Expand Down

0 comments on commit fcba8f5

Please sign in to comment.