diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-13 12:02:00 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-13 12:02:00 -0400 |
| commit | 5e67008bc6039c593af4930b3a39242c6fc48355 (patch) | |
| tree | 6c0d62ca2f11a81a990ca55648f8bbf8523a65e7 /src/dynamic-array.hpp | |
| parent | fb5c06777557fc28c0d8e919d9a82bdf51adeea7 (diff) | |
DynArray can specify MemBucket in constructor
Diffstat (limited to 'src/dynamic-array.hpp')
| -rw-r--r-- | src/dynamic-array.hpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index 95ba967..3bf02ee 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -12,6 +12,7 @@ #define BAKE_DYN_ARRAY(T) template struct DynArray<T>; struct DynArrayBase { + mutable MemBucket *bkt = nullptr; mutable char *ptr = nullptr; int64_t elementSize = 0; int64_t elementCount = 0; @@ -20,8 +21,8 @@ struct DynArrayBase { template <typename T> struct DynArray: DynArrayBase { - DynArray(); - explicit DynArray(int64_t reserveCount); + explicit DynArray(MemBucket *bucket = nullptr); + explicit DynArray(int64_t reserveCount, MemBucket *bucket = nullptr); DynArray(const DynArray<T> &other); DynArray(DynArray<T> &&other); DynArray &operator=(const DynArray<T> &other); @@ -38,6 +39,7 @@ struct DynArray: DynArrayBase { void Reserve(int64_t count); void Resize(int64_t count); protected: + using DynArrayBase::bkt; using DynArrayBase::ptr; using DynArrayBase::elementSize; using DynArrayBase::elementCount; @@ -47,7 +49,8 @@ struct DynArray: DynArrayBase { void DynArrayReserve(DynArrayBase *arr, int64_t count); void DynArrayDestroy(DynArrayBase *arr); -template <typename T> inline DynArray<T>::DynArray(int64_t count) { +template <typename T> inline DynArray<T>::DynArray(int64_t count, MemBucket *bucket) { + this->bkt = bucket; this->elementSize = sizeof(T); if (count > 0) DynArrayReserve(this, count); if IS_CONSTRUCTIBLE(T) { @@ -57,11 +60,13 @@ template <typename T> inline DynArray<T>::DynArray(int64_t count) { } } -template <typename T> inline DynArray<T>::DynArray() { +template <typename T> inline DynArray<T>::DynArray(MemBucket *bucket) { + this->bkt = bucket; this->elementSize = sizeof(T); } template <typename T> inline DynArray<T>::DynArray(const DynArray<T> &rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -70,6 +75,7 @@ template <typename T> inline DynArray<T>::DynArray(const DynArray<T> &rh) { } template <typename T> inline DynArray<T>::DynArray(DynArray<T> &&rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -78,6 +84,7 @@ template <typename T> inline DynArray<T>::DynArray(DynArray<T> &&rh) { } template <typename T> inline DynArray<T> &DynArray<T>::operator=(const DynArray<T> &rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -87,6 +94,7 @@ template <typename T> inline DynArray<T> &DynArray<T>::operator=(const DynArray< } template <typename T> inline DynArray<T> &DynArray<T>::operator=(DynArray<T> &&rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -159,13 +167,13 @@ 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_New(sizeof(T) * moveCount); + T *tmp = Pke_New<T>(moveCount, this->bkt); if IS_DESTRUCTIBLE(T) { reinterpret_cast<T>(this->ptr + (index * sizeof(T))).~T(); } memcpy(tmp, this->ptr + (sizeof(T) * (index + 1)), sizeof(T) * moveCount); memcpy(this->ptr + (sizeof(T) * index), tmp, sizeof(T) * moveCount); - Pke_Delete(tmp, moveCount * sizeof(T)); + Pke_Delete<T>(tmp, moveCount, this->bkt); this->elementCount -= 1; } |
