2template <
typename T,
typename Derived>
5template <
typename T,
typename Derived>
8 PL_ASSERT_DEBUG(m_uiCount == 0,
"The derived class did not destruct all objects. Count is {0}.", m_uiCount);
9 PL_ASSERT_DEBUG(m_pElements ==
nullptr,
"The derived class did not free its memory.");
12template <
typename T,
typename Derived>
15 if (this->GetData() == rhs.
GetPtr())
20 PL_ASSERT_DEV(m_uiCount > rhs.
GetCount(),
"Dangling array pointer. The given array pointer points to invalid memory.");
21 T* pElements =
static_cast<Derived*
>(
this)->GetElementsPtr();
27 const plUInt32 uiOldCount = m_uiCount;
30 if (uiNewCount > uiOldCount)
32 static_cast<Derived*
>(
this)->Reserve(uiNewCount);
33 T* pElements =
static_cast<Derived*
>(
this)->GetElementsPtr();
39 T* pElements =
static_cast<Derived*
>(
this)->GetElementsPtr();
44 m_uiCount = uiNewCount;
47template <
typename T,
typename Derived>
53template <
typename T,
typename Derived>
56 return plArrayPtr<T>(
static_cast<Derived*
>(
this)->GetElementsPtr(), m_uiCount);
59template <
typename T,
typename Derived>
68template <
typename T,
typename Derived>
74#if PL_DISABLED(PL_USE_CPP20_OPERATORS)
75template <
typename T,
typename Derived>
85template <
typename T,
typename Derived>
88 return GetArrayPtr() < rhs;
91template <
typename T,
typename Derived>
94 PL_ASSERT_DEBUG(uiIndex < m_uiCount,
"Out of bounds access. Array has {0} elements, trying to access element at index {1}.", m_uiCount, uiIndex);
95 return static_cast<const Derived*
>(
this)->GetElementsPtr()[uiIndex];
98template <
typename T,
typename Derived>
101 PL_ASSERT_DEBUG(uiIndex < m_uiCount,
"Out of bounds access. Array has {0} elements, trying to access element at index {1}.", m_uiCount, uiIndex);
102 return static_cast<Derived*
>(
this)->GetElementsPtr()[uiIndex];
105template <
typename T,
typename Derived>
108 const plUInt32 uiOldCount = m_uiCount;
109 const plUInt32 uiNewCount = uiCount;
111 if (uiNewCount > uiOldCount)
113 static_cast<Derived*
>(
this)->Reserve(uiNewCount);
116 else if (uiNewCount < uiOldCount)
124template <
typename T,
typename Derived>
127 const plUInt32 uiOldCount = m_uiCount;
128 const plUInt32 uiNewCount = uiCount;
130 if (uiNewCount > uiOldCount)
132 static_cast<Derived*
>(
this)->Reserve(uiNewCount);
135 else if (uiNewCount < uiOldCount)
143template <
typename T,
typename Derived>
146 if (uiCount > m_uiCount)
152template <
typename T,
typename Derived>
157 static_assert(
plIsPodType<T>::value == plTypeIsPod::value,
"SetCountUninitialized is only supported for POD types.");
158 const plUInt32 uiOldCount = m_uiCount;
159 const plUInt32 uiNewCount = uiCount;
161 if (uiNewCount > uiOldCount)
163 static_cast<Derived*
>(
this)->Reserve(uiNewCount);
171template <
typename T,
typename Derived>
177template <
typename T,
typename Derived>
180 return m_uiCount == 0;
183template <
typename T,
typename Derived>
190template <
typename T,
typename Derived>
193 return IndexOf(value) != plInvalidIndex;
196template <
typename T,
typename Derived>
199 PL_ASSERT_DEV(uiIndex <= m_uiCount,
"Invalid index. Array has {0} elements, trying to insert element at index {1}.", m_uiCount, uiIndex);
201 static_cast<Derived*
>(
this)->Reserve(m_uiCount + 1);
203 plMemoryUtils::Prepend(
static_cast<Derived*
>(
this)->GetElementsPtr() + uiIndex, value, m_uiCount - uiIndex);
207template <
typename T,
typename Derived>
210 PL_ASSERT_DEV(uiIndex <= m_uiCount,
"Invalid index. Array has {0} elements, trying to insert element at index {1}.", m_uiCount, uiIndex);
212 static_cast<Derived*
>(
this)->Reserve(m_uiCount + 1);
214 plMemoryUtils::Prepend(
static_cast<Derived*
>(
this)->GetElementsPtr() + uiIndex, std::move(value), m_uiCount - uiIndex);
218template <
typename T,
typename Derived>
221 const plUInt32 uiRangeCount = range.
GetCount();
222 static_cast<Derived*
>(
this)->Reserve(m_uiCount + uiRangeCount);
225 m_uiCount += uiRangeCount;
228template <
typename T,
typename Derived>
231 plUInt32 uiIndex = IndexOf(value);
233 if (uiIndex == plInvalidIndex)
236 RemoveAtAndCopy(uiIndex);
240template <
typename T,
typename Derived>
243 plUInt32 uiIndex = IndexOf(value);
245 if (uiIndex == plInvalidIndex)
248 RemoveAtAndSwap(uiIndex);
252template <
typename T,
typename Derived>
255 PL_ASSERT_DEV(uiIndex + uiNumElements <= m_uiCount,
"Out of bounds access. Array has {0} elements, trying to remove element at index {1}.", m_uiCount, uiIndex + uiNumElements - 1);
257 T* pElements =
static_cast<Derived*
>(
this)->GetElementsPtr();
259 m_uiCount -= uiNumElements;
263template <
typename T,
typename Derived>
266 PL_ASSERT_DEV(uiIndex + uiNumElements <= m_uiCount,
"Out of bounds access. Array has {0} elements, trying to remove element at index {1}.", m_uiCount, uiIndex + uiNumElements - 1);
268 T* pElements =
static_cast<Derived*
>(
this)->GetElementsPtr();
270 for (plUInt32 i = 0; i < uiNumElements; ++i)
274 if (m_uiCount != uiIndex)
276 pElements[uiIndex] = std::move(pElements[m_uiCount]);
283template <
typename T,
typename Derived>
286 const T* pElements =
static_cast<const Derived*
>(
this)->GetElementsPtr();
288 for (plUInt32 i = uiStartIndex; i < m_uiCount; i++)
293 return plInvalidIndex;
296template <
typename T,
typename Derived>
299 const T* pElements =
static_cast<const Derived*
>(
this)->GetElementsPtr();
301 for (plUInt32 i =
plMath::Min(uiStartIndex, m_uiCount); i-- > 0;)
306 return plInvalidIndex;
309template <
typename T,
typename Derived>
312 static_cast<Derived*
>(
this)->Reserve(m_uiCount + 1);
314 T* pElements =
static_cast<Derived*
>(
this)->GetElementsPtr();
318 T& ReturnRef = *(pElements + m_uiCount);
325template <
typename T,
typename Derived>
328 this->SetCount(this->GetCount() + uiNumNewItems);
329 return GetArrayPtr().GetEndPtr() - uiNumNewItems;
332template <
typename T,
typename Derived>
335 static_cast<Derived*
>(
this)->Reserve(m_uiCount + 1);
341template <
typename T,
typename Derived>
344 static_cast<Derived*
>(
this)->Reserve(m_uiCount + 1);
350template <
typename T,
typename Derived>
353 PL_ASSERT_DEBUG(m_uiCount < m_uiCapacity,
"Appending unchecked to array with insufficient capacity.");
359template <
typename T,
typename Derived>
362 PL_ASSERT_DEBUG(m_uiCount < m_uiCapacity,
"Appending unchecked to array with insufficient capacity.");
368template <
typename T,
typename Derived>
371 const plUInt32 uiRangeCount = range.
GetCount();
372 static_cast<Derived*
>(
this)->Reserve(m_uiCount + uiRangeCount);
375 m_uiCount += uiRangeCount;
378template <
typename T,
typename Derived>
381 PL_ASSERT_DEV(m_uiCount >= uiCountToRemove,
"Out of bounds access. Array has {0} elements, trying to pop {1} elements.", m_uiCount, uiCountToRemove);
383 m_uiCount -= uiCountToRemove;
387template <
typename T,
typename Derived>
390 PL_ASSERT_DEBUG(m_uiCount > 0,
"Out of bounds access. Trying to peek into an empty array.");
391 return static_cast<Derived*
>(
this)->GetElementsPtr()[m_uiCount - 1];
394template <
typename T,
typename Derived>
397 PL_ASSERT_DEBUG(m_uiCount > 0,
"Out of bounds access. Trying to peek into an empty array.");
398 return static_cast<const Derived*
>(
this)->GetElementsPtr()[m_uiCount - 1];
401template <
typename T,
typename Derived>
402template <
typename Comparer>
412template <
typename T,
typename Derived>
422template <
typename T,
typename Derived>
428 return static_cast<Derived*
>(
this)->GetElementsPtr();
431template <
typename T,
typename Derived>
437 return static_cast<const Derived*
>(
this)->GetElementsPtr();
440template <
typename T,
typename Derived>
446template <
typename T,
typename Derived>
452template <
typename T,
typename Derived>
458template <
typename T,
typename Derived>
464template <
typename T,
typename Derived>
Base class for all array containers. Implements all the basic functionality that only requires a poin...
Definition ArrayBase.h:19
~plArrayBase()
Destructor.
Definition ArrayBase_inl.h:6
bool Contains(const T &value) const
Checks whether the given value can be found in the array. O(n) complexity.
Definition ArrayBase_inl.h:191
T & ExpandAndGetRef()
Grows the array by one element and returns a reference to the newly created element.
Definition ArrayBase_inl.h:310
void RemoveAtAndCopy(plUInt32 uiIndex, plUInt32 uiNumElements=1)
Removes the element at index and fills the gap by shifting all following elements.
Definition ArrayBase_inl.h:253
const T & operator[](plUInt32 uiIndex) const
Returns the element at the given index. Does bounds checks in debug builds.
Definition ArrayBase_inl.h:92
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
T * GetData()
Returns a pointer to the array data, or nullptr if the array is empty.
Definition ArrayBase_inl.h:423
bool operator==(const plArrayBase< T, Derived > &rhs) const
Compares this array to another contiguous array type.
Definition ArrayBase_inl.h:60
void Sort()
Sort with default comparer.
Definition ArrayBase_inl.h:413
void Clear()
Clears the array.
Definition ArrayBase_inl.h:184
void InsertAt(plUInt32 uiIndex, const T &value)
Inserts value at index by shifting all following elements.
Definition ArrayBase_inl.h:197
plUInt32 m_uiCount
The number of elements used from the array.
Definition ArrayBase.h:203
void SetCountUninitialized(plUInt32 uiCount)
Resizes the array to have exactly uiCount elements. Extra elements might be uninitialized.
Definition ArrayBase_inl.h:155
void PushBackRange(const plArrayPtr< const T > &range)
Pushes all elements in range at the end of the array. Increases the capacity if necessary.
Definition ArrayBase_inl.h:369
void SetCount(plUInt32 uiCount)
Resizes the array to have exactly uiCount elements. Default constructs extra elements if the array is...
Definition ArrayBase_inl.h:106
plUInt32 m_uiCapacity
The number of elements which can be stored in the array without re-allocating.
Definition ArrayBase.h:206
plArrayBase()
Constructor.
void PopBack(plUInt32 uiCountToRemove=1)
Removes count elements from the end of the array.
Definition ArrayBase_inl.h:379
bool RemoveAndCopy(const T &value)
Removes the first occurrence of value and fills the gap by shifting all following elements.
Definition ArrayBase_inl.h:229
void EnsureCount(plUInt32 uiCount)
Ensures the container has at least uiCount elements. Ie. calls SetCount() if the container has fewer ...
Definition ArrayBase_inl.h:144
plUInt32 LastIndexOf(const T &value, plUInt32 uiStartIndex=plInvalidIndex) const
Searches for the last occurrence of the given value and returns its index or plInvalidIndex if not fo...
Definition ArrayBase_inl.h:297
T * ExpandBy(plUInt32 uiNumNewItems)
Expands the array by N new items and returns a pointer to the first new one.
Definition ArrayBase_inl.h:326
void InsertRange(const plArrayPtr< const T > &range, plUInt32 uiIndex)
Inserts all elements in the range starting at the given index, shifting the elements after the index.
Definition ArrayBase_inl.h:219
bool operator<(const plArrayBase< T, Derived > &rhs) const
Compares this array to another contiguous array type.
Definition ArrayBase_inl.h:69
void operator=(const plArrayPtr< const T > &rhs)
Copies the data from some other contiguous array into this one.
Definition ArrayBase_inl.h:13
T & PeekBack()
Returns the last element of the array.
Definition ArrayBase_inl.h:388
plArrayPtr< typename plArrayPtr< T >::ByteType > GetByteArrayPtr()
Returns a byte array pointer to the array data, or an empty array pointer if the array is empty.
Definition ArrayBase_inl.h:453
bool RemoveAndSwap(const T &value)
Removes the first occurrence of value and fills the gap by swapping in the last element.
Definition ArrayBase_inl.h:241
void PushBackUnchecked(const T &value)
Pushes value at the end of the array. Does NOT ensure capacity.
Definition ArrayBase_inl.h:351
T * m_pElements
Element-type access to m_Data.
Definition ArrayBase.h:200
plUInt32 IndexOf(const T &value, plUInt32 uiStartIndex=0) const
Searches for the first occurrence of the given value and returns its index or plInvalidIndex if not f...
Definition ArrayBase_inl.h:284
bool IsEmpty() const
Returns true, if the array does not contain any elements.
Definition ArrayBase_inl.h:178
plArrayPtr< T > GetArrayPtr()
Returns an array pointer to the array data, or an empty array pointer if the array is empty.
Definition ArrayBase_inl.h:441
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
void RemoveAtAndSwap(plUInt32 uiIndex, plUInt32 uiNumElements=1)
Removes the element at index and fills the gap by swapping in the last element.
Definition ArrayBase_inl.h:264
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
PL_ALWAYS_INLINE plUInt32 GetCount() const
Returns the number of elements in the array.
Definition ArrayPtr.h:142
PL_ALWAYS_INLINE PointerType GetPtr() const
Returns the pointer to the array.
Definition ArrayPtr.h:118
PL_ALWAYS_INLINE plArrayPtr< const ByteType > ToByteArray() const
Reinterprets this array as a byte array.
Definition ArrayPtr.h:165
static void CopyConstruct(Destination *pDestination, const Source ©, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination, by creating uiCount copies of c...
static void CopyConstructArray(T *pDestination, const T *pSource, size_t uiCount)
Constructs uiCount objects of type T in a raw buffer at pDestination from an existing array of object...
static bool IsEqual(const T *a, const T *b, size_t uiCount=1)
Tests if objects of type T from pSource and pDestination are equal.
static void Construct(T *pDestination, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination.
static void Prepend(T *pDestination, const T &source, size_t uiCount)
Moves uiCount objects in pDestination by one object and copies source to the free space.
static void Copy(T *pDestination, const T *pSource, size_t uiCount=1)
Copies objects of type T from pSource to pDestination.
static void MoveConstruct(T *pDestination, T &&source)
Constructs an object of type T in a raw buffer at pDestination, by using move construction from sourc...
static void RelocateOverlapped(T *pDestination, T *pSource, size_t uiCount=1)
Moves objects of type T from pSource to pDestination.
static void Destruct(T *pDestination, size_t uiCount=1)
Destructs uiCount objects of type T at pDestination.
static void QuickSort(Container &inout_container, const Comparer &comparer=Comparer())
Sorts the elements in container using a in-place quick sort implementation (not stable).
Definition Sorting_inl.h:3
constexpr PL_ALWAYS_INLINE T Min(T f1, T f2)
Returns the smaller value, f1 or f2.
Definition Math_inl.h:27
PL_ALWAYS_INLINE void Swap(T &ref_f1, T &ref_f2)
Swaps the values in the two variables f1 and f2.
Definition Math_inl.h:224
A comparer object is used in sorting algorithms to compare to objects of the same type.
Definition Comparer.h:7
If there is an % operator which takes a TypeIsPod and returns a CompileTimeTrueType T is Pod....
Definition TypeTraits.h:43