BLI: support removing multiple elements from a vector

This commit is contained in:
Jacques Lucke 2020-08-26 10:52:35 +02:00
parent 6a10e69d27
commit 0e50b6529c
2 changed files with 53 additions and 0 deletions

@ -741,6 +741,27 @@ class Vector {
UPDATE_VECTOR_SIZE(this);
}
/**
* Remove a contiguous chunk of elements and move all values coming after it towards the front.
* This takes O(n) time.
*
* This is similar to std::vector::erase.
*/
void remove(const int64_t start_index, const int64_t amount)
{
const int64_t old_size = this->size();
BLI_assert(start_index >= 0);
BLI_assert(amount >= 0);
BLI_assert(start_index + amount <= old_size);
const int64_t move_amount = old_size - start_index - amount;
for (int64_t i = 0; i < move_amount; i++) {
begin_[start_index + i] = std::move(begin_[start_index + amount + i]);
}
destruct_n(end_ - amount, amount);
end_ -= amount;
UPDATE_VECTOR_SIZE(this);
}
/**
* Do a linear search to find the value in the vector.
* When found, return the first index, otherwise return -1.

@ -793,4 +793,36 @@ TEST(vector, RemoveExceptions)
EXPECT_EQ(vec.size(), 10);
}
TEST(vector, RemoveChunk)
{
Vector<int> vec = {2, 3, 4, 5, 6, 7, 8};
EXPECT_EQ(vec.size(), 7);
vec.remove(2, 4);
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], 2);
EXPECT_EQ(vec[1], 3);
EXPECT_EQ(vec[2], 8);
vec.remove(0, 1);
EXPECT_EQ(vec.size(), 2);
EXPECT_EQ(vec[0], 3);
EXPECT_EQ(vec[1], 8);
vec.remove(1, 1);
EXPECT_EQ(vec.size(), 1);
EXPECT_EQ(vec[0], 3);
vec.remove(0, 1);
EXPECT_EQ(vec.size(), 0);
vec.remove(0, 0);
EXPECT_EQ(vec.size(), 0);
}
TEST(vector, RemoveChunkExceptitons)
{
Vector<ExceptionThrower> vec(10);
vec.remove(1, 3);
EXPECT_EQ(vec.size(), 7);
vec[5].throw_during_move = true;
EXPECT_ANY_THROW({ vec.remove(2, 3); });
EXPECT_EQ(vec.size(), 7);
}
} // namespace blender::tests