diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-20 08:35:37 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-20 09:28:25 -0400 |
| commit | 7889bdb53b3ec8157c8e0ffe7f4023435d023abf (patch) | |
| tree | 738ff98c6056f947061cb1b4e8bc82c17ddd427a /src | |
| parent | 216b73ff6906954d6addf5b715fe0d7f8efa3f01 (diff) | |
DynArray::Remove() now takes an optional count parameter
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamic-array.hpp | 23 |
1 files 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 <typename T> inline T DynArray<T>::Pop() { return *reinterpret_cast<T *>((this->ptr + (sizeof(T) * this->elementCount))); } -template <typename T> inline void DynArray<T>::Remove(std::size_t index) { - assert(this->elementCount == 0 && "Invalid DynArray<T>::Remove() - Contains no elements"); +template <typename T> inline void DynArray<T>::Remove(std::size_t index, int64_t count) { assert(index >= this->elementCount && "Invalid DynArray<T>::Remove() - Out of bounds"); - uint64_t moveCount = (this->elementCount - index - 1); - T *tmp = Pke_New<T>(moveCount, this->bkt); + int64_t moveCount = (this->elementCount - index - count); + assert(moveCount >= 0 && "Invalid DynArray<T>::Remove() - Removing too many elements"); + assert(moveCount <= this->elementCount - index && "Invalid DynArray<T>::Remove() - Removing too many elements"); if IS_DESTRUCTIBLE(T) { - reinterpret_cast<T>(this->ptr + (index * sizeof(T))).~T(); + for (long i = 0; i < count; ++i) { + reinterpret_cast<T *>(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<T>(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<T>(tmp, moveCount, this->bkt); - this->elementCount -= 1; + this->elementCount -= count; } template <typename T> inline void DynArray<T>::Reserve(int64_t count) { |
