Skip to content

Commit

Permalink
to_dynarray()
Browse files Browse the repository at this point in the history
  • Loading branch information
OleErikPeistorpet committed Jun 6, 2024
1 parent 7a366ce commit 4d35adf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
14 changes: 14 additions & 0 deletions auxi/dynarray_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,18 @@ namespace oel::_detail
Ptr end;
Ptr reservEnd;
};



template< typename Alloc >
struct ToDynarrPartial
{
Alloc _a;

template< typename Range >
friend auto operator |(Range && r, ToDynarrPartial t)
{
return dynarray(static_cast<Range &&>(r), std::move(t)._a);
}
};
}
19 changes: 16 additions & 3 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,27 @@
namespace oel
{

//! dynarray is trivially relocatable if Alloc is
template< typename T, typename Alloc >
is_trivially_relocatable<Alloc> specify_trivial_relocate(dynarray<T, Alloc>);
struct _toDynarrayFn
{
template< typename Alloc = allocator<> >
constexpr auto operator()(Alloc a = {}) const
{
return _detail::ToDynarrPartial<Alloc>{std::move(a)};
}
};
//! Equivalent to `std::ranges::to<dynarray>` (piped)
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); }


//! dynarray is trivially relocatable if Alloc is
template< typename T, typename Alloc >
is_trivially_relocatable<Alloc> specify_trivial_relocate(dynarray<T, Alloc>);

#if OEL_MEM_BOUND_DEBUG_LVL
inline namespace debug
{
Expand Down
2 changes: 1 addition & 1 deletion unit_test/dynarray_construct_assignop_swap_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ TEST_F(dynarrayConstructTest, deductionGuides)
}
ar;

auto d = dynarray(ar, StatefulAllocator<int>{});
auto d = ar | to_dynarray(StatefulAllocator<int>{});
static_assert(std::is_same< decltype(d)::allocator_type, StatefulAllocator<int> >());
EXPECT_EQ(d.size(), ar.size());

Expand Down
4 changes: 2 additions & 2 deletions unit_test/view_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ TEST(viewTest, viewTransformSizedRange)
int src[] {1, 2};
auto tv = src | view::transform([](int & i) { return i++; });
auto tsr = view::subrange(tv.begin(), tv.end());
oel::dynarray<int> dest(tsr);
auto dest = tsr | oel::to_dynarray();
EXPECT_EQ(2U, tsr.size());
EXPECT_EQ(2U, dest.size());
EXPECT_EQ(1, dest[0]);
Expand All @@ -196,7 +196,7 @@ TEST(viewTest, viewTransformSizedRange)
TEST(viewTest, viewTransformNonSizedRange)
{
std::forward_list<int> const li{-2, -3};
oel::dynarray<int> dest( view::transform(li, Square{}) );
auto dest = view::transform(li, Square{}) | oel::to_dynarray();
EXPECT_EQ(2U, dest.size());
EXPECT_EQ(4, dest[0]);
EXPECT_EQ(9, dest[1]);
Expand Down

0 comments on commit 4d35adf

Please sign in to comment.