diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-30 21:58:20 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-06 17:19:09 -0400 |
| commit | 4d15ed6a8b7676f3c76a466b367f253ba1794268 (patch) | |
| tree | 0ba497953f9b8b063ba396fa652596d84ee34e5b /src/dynamic-array.hpp | |
| parent | 196ad901b19a4a741b71bc3c88927aa0af69273a (diff) | |
add DynArray<T>::Remove(index)
Diffstat (limited to 'src/dynamic-array.hpp')
| -rw-r--r-- | src/dynamic-array.hpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index ba48b8a..a2d2d77 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -28,6 +28,7 @@ struct DynArray: DynArrayBase { bool Has(const T &val); void Push(const T &val); T Pop(); + void Remove(std::size_t index); void Reserve(int64_t count); void Resize(int64_t count); protected: @@ -89,6 +90,17 @@ 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"); + assert(index >= this->elementCount && "Invalid DynArray<T>::Remove() - Out of bounds"); + uint64_t moveCount = (this->elementCount - index - 1); + auto *tmp = pke::PkeNew(this->elementSize * moveCount); + memcpy(tmp, this->ptr + (this->elementSize * (index + 1)), this->elementSize * moveCount); + memcpy(this->ptr + (this->elementSize * index), tmp, this->elementSize * moveCount); + pke::PkeDelete(tmp, moveCount * this->elementSize); + this->elementCount -= 1; +} + template <typename T> inline void DynArray<T>::Reserve(int64_t count) { if (count > 0) { return DynArrayReserve(this, count); |
