From 1dee1034d2f63451962c7ca1027e573a0bbf2d2d Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Mon, 9 Oct 2023 11:29:03 -0400 Subject: DynArray placement new push --- src/dynamic-array.hpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/dynamic-array.hpp') 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 inline bool DynArray::Has(const T &val) { return false; } +template inline void DynArray::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 inline void DynArray::Push(const T &val) { if (this->elementCount + 1 > this->reservedCount) { auto safeReserveCount = this->reservedCount < 2 ? 2 : this->reservedCount; -- cgit v1.2.3