diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-30 21:57:45 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-06 17:19:09 -0400 |
| commit | 196ad901b19a4a741b71bc3c88927aa0af69273a (patch) | |
| tree | e0f38b2d44dc7e4f0b54f5df5d4c74151a9e2a8e /src/dynamic-array.hpp | |
| parent | ea218cad0ee862964e12bb7f15d442acb7de6c43 (diff) | |
DynArray gracefully handle attempting to reserve size of 0
Diffstat (limited to 'src/dynamic-array.hpp')
| -rw-r--r-- | src/dynamic-array.hpp | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index 703b5ed..ba48b8a 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -42,7 +42,7 @@ void DynArrayDestroy(DynArrayBase *arr); template <typename T> inline DynArray<T>::DynArray(int64_t count) { this->elementSize = sizeof(T); - this->Reserve(count); + if (count > 0) DynArrayReserve(this, count); } template <typename T> inline DynArray<T>::DynArray() { @@ -76,7 +76,8 @@ template <typename T> inline bool DynArray<T>::Has(const T &val) { template <typename T> inline void DynArray<T>::Push(const T &val) { if (this->elementCount + 1 > this->reservedCount) { - DynArrayReserve(this, int64_t(this->reservedCount * 1.5)); + auto safeReserveCount = this->reservedCount == 0 ? 1 : this->reservedCount; + DynArrayReserve(this, int64_t(safeReserveCount * 1.5)); } std::memcpy(this->ptr + (sizeof(T) * this->elementCount), &val, sizeof(T)); this->elementCount += 1; @@ -89,11 +90,16 @@ template <typename T> inline T DynArray<T>::Pop() { } template <typename T> inline void DynArray<T>::Reserve(int64_t count) { - return DynArrayReserve(this, count); + if (count > 0) { + return DynArrayReserve(this, count); + } + this->elementCount = this->elementCount > count ? count : this->elementCount; } template <typename T> inline void DynArray<T>::Resize(int64_t count) { - DynArrayReserve(this, count); + if (count > 0) { + DynArrayReserve(this, count); + } this->elementCount = count; } |
