From 4d15ed6a8b7676f3c76a466b367f253ba1794268 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Wed, 30 Aug 2023 21:58:20 -0400 Subject: add DynArray::Remove(index) --- src/dynamic-array.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') 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 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"); + assert(index >= this->elementCount && "Invalid DynArray::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 inline void DynArray::Reserve(int64_t count) { if (count > 0) { return DynArrayReserve(this, count); -- cgit v1.2.3