From 7889bdb53b3ec8157c8e0ffe7f4023435d023abf Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Fri, 20 Oct 2023 08:35:37 -0400 Subject: DynArray::Remove() now takes an optional count parameter --- src/dynamic-array.hpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index fa1e665..e5567f4 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -35,7 +35,7 @@ struct DynArray: DynArrayBase { T& Push(); void Push(const T &val); T Pop(); - void Remove(std::size_t index); + void Remove(std::size_t index, int64_t count = 1); void Reserve(int64_t count); void Resize(int64_t count); protected: @@ -165,18 +165,25 @@ template inline T DynArray::Pop() { return *reinterpret_cast((this->ptr + (sizeof(T) * this->elementCount))); } -template inline void DynArray::Remove(std::size_t index) { - assert(this->elementCount == 0 && "Invalid DynArray::Remove() - Contains no elements"); +template inline void DynArray::Remove(std::size_t index, int64_t count) { assert(index >= this->elementCount && "Invalid DynArray::Remove() - Out of bounds"); - uint64_t moveCount = (this->elementCount - index - 1); - T *tmp = Pke_New(moveCount, this->bkt); + int64_t moveCount = (this->elementCount - index - count); + assert(moveCount >= 0 && "Invalid DynArray::Remove() - Removing too many elements"); + assert(moveCount <= this->elementCount - index && "Invalid DynArray::Remove() - Removing too many elements"); if IS_DESTRUCTIBLE(T) { - reinterpret_cast(this->ptr + (index * sizeof(T))).~T(); + for (long i = 0; i < count; ++i) { + reinterpret_cast(this->ptr + ((index + i) * sizeof(T)))->~T(); + } } - memcpy(tmp, this->ptr + (sizeof(T) * (index + 1)), sizeof(T) * moveCount); + if (moveCount == 0) { + this->elementCount = index; + return; + } + T *tmp = Pke_New(moveCount, this->bkt); + memcpy(tmp, this->ptr + (sizeof(T) * (index + count)), sizeof(T) * moveCount); memcpy(this->ptr + (sizeof(T) * index), tmp, sizeof(T) * moveCount); Pke_Delete(tmp, moveCount, this->bkt); - this->elementCount -= 1; + this->elementCount -= count; } template inline void DynArray::Reserve(int64_t count) { -- cgit v1.2.3