From 2bdb644e2f4f821a367713ad951369013be8b611 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Wed, 30 Aug 2023 10:59:39 -0400 Subject: DynArray add Count(), Has(), and reserveCount ctor --- src/dynamic-array.hpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/dynamic-array.hpp') diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index 3fc2d08..703b5ed 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -20,9 +20,12 @@ struct DynArrayBase { template struct DynArray: DynArrayBase { DynArray(); + explicit DynArray(int64_t reserveCount); ~DynArray(); T operator[](std::size_t index); T *GetPtr(); + const int64_t Count(); + bool Has(const T &val); void Push(const T &val); T Pop(); void Reserve(int64_t count); @@ -34,8 +37,13 @@ struct DynArray: DynArrayBase { using DynArrayBase::reservedCount; }; -void inline DynArrayReserve(DynArrayBase *arr, int64_t count); -void inline DynArrayDestroy(DynArrayBase *arr); +void DynArrayReserve(DynArrayBase *arr, int64_t count); +void DynArrayDestroy(DynArrayBase *arr); + +template inline DynArray::DynArray(int64_t count) { + this->elementSize = sizeof(T); + this->Reserve(count); +} template inline DynArray::DynArray() { this->elementSize = sizeof(T); @@ -55,6 +63,17 @@ template inline T *DynArray::GetPtr() { return reinterpret_cast(reinterpret_cast(this->ptr)); } +template inline const int64_t DynArray::Count() { + return this->elementCount; +} + +template inline bool DynArray::Has(const T &val) { + for (long i = 0; i < this->elementCount; ++i) { + if ((*this)[i] == val) return true; + } + return false; +} + template inline void DynArray::Push(const T &val) { if (this->elementCount + 1 > this->reservedCount) { DynArrayReserve(this, int64_t(this->reservedCount * 1.5)); -- cgit v1.2.3