Skip to content

Commit

Permalink
Renamed erase_unstable to unordered_erase
Browse files Browse the repository at this point in the history
Was originally erase_unordered
  • Loading branch information
OleErikPeistorpet committed Sep 29, 2023
1 parent a65cef6 commit 9d1ef72
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ namespace oel
template< typename T, typename Alloc >
is_trivially_relocatable<Alloc> specify_trivial_relocate(dynarray<T, Alloc>);

//! Overloads generic erase_unstable(RandomAccessContainer &, Integral) (in range_algo.h)
//! Overloads generic unordered_erase(RandomAccessContainer &, Integral) (in range_algo.h)
template< typename T, typename A > inline
void erase_unstable(dynarray<T, A> & d, size_t index) { d.erase_unstable(d.begin() + index); }
void unordered_erase(dynarray<T, A> & d, size_t index) { d.unordered_erase(d.begin() + index); }

//! @name GenericContainerInsert
//!@{
Expand Down Expand Up @@ -212,7 +212,7 @@ class dynarray
*
* Constant complexity (compared to linear in the distance between pos and end() for normal erase).
* @return iterator corresponding to the same index in the sequence as pos, same as for std containers. */
iterator erase_unstable(iterator pos) &;
iterator unordered_erase(iterator pos) &;

iterator erase(iterator pos) &;

Expand Down Expand Up @@ -894,7 +894,7 @@ inline void dynarray<T, Alloc>::erase_to_end(iterator first) noexcept
}

template< typename T, typename Alloc >
inline typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::erase_unstable(iterator pos) &
inline typename dynarray<T, Alloc>::iterator dynarray<T, Alloc>::unordered_erase(iterator pos) &
{
if constexpr (is_trivially_relocatable<T>::value)
{
Expand Down
4 changes: 3 additions & 1 deletion range_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ namespace oel
* Constant complexity (compared to linear in the distance between position and last for standard erase).
* The end iterator and any iterator, pointer and reference referring to the last element may become invalid. */
template< typename RandomAccessContainer, typename Integral >
constexpr void erase_unstable(RandomAccessContainer & c, Integral index)
constexpr void unordered_erase(RandomAccessContainer & c, Integral index)
{
c[index] = std::move(c.back());
c.pop_back();
}
template< typename RandomAccessContainer, typename Integral >
[[deprecated]] constexpr void erase_unstable(RandomAccessContainer & c, Integral index) { oel::unordered_erase(c, index); }

/**
* @brief Erase from container all elements for which predicate returns true
Expand Down
24 changes: 12 additions & 12 deletions unit_test/dynarray_mutate_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,43 +798,43 @@ TEST_F(dynarrayTest, erasePrecondCheck)
TEST_F(dynarrayTest, unorderErasePrecondCheck)
{
dynarray<int> di{1};
ASSERT_DEATH( di.erase_unstable(di.end()), "" );
ASSERT_DEATH( di.unordered_erase(di.end()), "" );
}
#endif

template< typename T >
void testEraseUnstable()
void testUnorderedErase()
{
T::clearCount();
{
dynarray<T> d;
d.emplace_back(1);
d.emplace_back(-2);

auto it = d.erase_unstable(d.begin());
auto it = d.unordered_erase(d.begin());
EXPECT_EQ(1U, d.size());
EXPECT_EQ(-2, *(*it));
it = d.erase_unstable(it);
it = d.unordered_erase(it);
EXPECT_EQ(end(d), it);

d.emplace_back(-1);
d.emplace_back(2);
erase_unstable(d, 1);
unordered_erase(d, 1);
EXPECT_EQ(-1, *d.back());
erase_unstable(d, 0);
unordered_erase(d, 0);
EXPECT_TRUE(d.empty());
}
EXPECT_EQ(T::nConstructions, T::nDestruct);
}

TEST_F(dynarrayTest, eraseUnstable)
TEST_F(dynarrayTest, unorderedErase)
{
testEraseUnstable<MoveOnly>();
testUnorderedErase<MoveOnly>();
}

TEST_F(dynarrayTest, eraseUnstableTrivialReloc)
TEST_F(dynarrayTest, unorderedEraseTrivialReloc)
{
testEraseUnstable<TrivialRelocat>();
testUnorderedErase<TrivialRelocat>();
}

TEST_F(dynarrayTest, shrinkToFit)
Expand Down Expand Up @@ -867,8 +867,8 @@ TEST_F(dynarrayTest, overAligned)
for (const auto & v : special)
EXPECT_EQ(0U, reinterpret_cast<std::uintptr_t>(&v) % testAlignment);

special.erase_unstable(special.end() - 1);
special.erase_unstable(special.begin());
special.unordered_erase(special.end() - 1);
special.unordered_erase(special.begin());
special.shrink_to_fit();
EXPECT_TRUE(special.capacity() < 3U);
EXPECT_EQ(0U, reinterpret_cast<std::uintptr_t>(&special.front()) % testAlignment);
Expand Down
6 changes: 2 additions & 4 deletions unit_test/range_algo_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ namespace view = oel::view;

TEST(rangeTest, eraseUnstable)
{
using oel::erase_unstable;

std::deque<std::string> d{"aa", "bb", "cc"};

erase_unstable(d, 1);
oel::unordered_erase(d, 1);
EXPECT_EQ(2U, d.size());
EXPECT_EQ("cc", d.back());
erase_unstable(d, 1);
oel::unordered_erase(d, 1);
EXPECT_EQ(1U, d.size());
EXPECT_EQ("aa", d.front());
}
Expand Down

0 comments on commit 9d1ef72

Please sign in to comment.