diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-30 10:59:39 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-06 17:19:09 -0400 |
| commit | 2bdb644e2f4f821a367713ad951369013be8b611 (patch) | |
| tree | 4cd9cbe93eedb0783adc74958847b09e94562c7a /src/dynamic-array.hpp | |
| parent | 431434e6bae6cc3ee00b53e87c33331afd3b2b7f (diff) | |
DynArray add Count(), Has(), and reserveCount ctor
Diffstat (limited to 'src/dynamic-array.hpp')
| -rw-r--r-- | src/dynamic-array.hpp | 23 |
1 files changed, 21 insertions, 2 deletions
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 <typename T> 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 <typename T> inline DynArray<T>::DynArray(int64_t count) { + this->elementSize = sizeof(T); + this->Reserve(count); +} template <typename T> inline DynArray<T>::DynArray() { this->elementSize = sizeof(T); @@ -55,6 +63,17 @@ template <typename T> inline T *DynArray<T>::GetPtr() { return reinterpret_cast<T *>(reinterpret_cast<void *>(this->ptr)); } +template <typename T> inline const int64_t DynArray<T>::Count() { + return this->elementCount; +} + +template <typename T> inline bool DynArray<T>::Has(const T &val) { + for (long i = 0; i < this->elementCount; ++i) { + if ((*this)[i] == val) return true; + } + return false; +} + 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)); |
