diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-09 11:29:03 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-09 11:29:03 -0400 |
| commit | 1dee1034d2f63451962c7ca1027e573a0bbf2d2d (patch) | |
| tree | c1aa33d374af013e2df311d609243cf6767634ec /src/dynamic-array.hpp | |
| parent | 96ea43e609e59a166fd6fe65511db3fe9fe777e9 (diff) | |
DynArray placement new push
Diffstat (limited to 'src/dynamic-array.hpp')
| -rw-r--r-- | src/dynamic-array.hpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index 343f6c1..645f9c8 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -31,6 +31,7 @@ struct DynArray: DynArrayBase { T *GetPtr(); const int64_t Count(); bool Has(const T &val); + void Push(); void Push(const T &val); T Pop(); void Remove(std::size_t index); @@ -126,6 +127,16 @@ template <typename T> inline bool DynArray<T>::Has(const T &val) { return false; } +template <typename T> inline void DynArray<T>::Push() { + if (this->elementCount + 1 > this->reservedCount) { + auto safeReserveCount = this->reservedCount < 2 ? 2 : this->reservedCount; + DynArrayReserve(this, int64_t(safeReserveCount * 1.5)); + } + auto itemPtr = this->ptr + (sizeof(T) * this->elementCount); + const auto &targetItem = new(itemPtr) T{}; + this->elementCount += 1; +} + template <typename T> inline void DynArray<T>::Push(const T &val) { if (this->elementCount + 1 > this->reservedCount) { auto safeReserveCount = this->reservedCount < 2 ? 2 : this->reservedCount; |
