3#include <Foundation/Algorithm/Sorting.h>
4#include <Foundation/Math/Math.h>
5#include <Foundation/Memory/AllocatorWrapper.h>
6#include <Foundation/Types/ArrayPtr.h>
8constexpr plUInt32 plSmallInvalidIndex = 0xFFFF;
15template <
typename T, plUInt16 Size>
20 PL_DECLARE_MEM_RELOCATABLE_TYPE_CONDITIONAL(T);
49#if PL_DISABLED(PL_USE_CPP20_OPERATORS)
66 template <
typename =
void>
104 plUInt32
IndexOf(
const T& value, plUInt32 uiStartIndex = 0)
const;
107 plUInt32
LastIndexOf(
const T& value, plUInt32 uiStartIndex = plSmallInvalidIndex)
const;
128 void PopBack(plUInt32 uiCountToRemove = 1);
137 template <
typename Comparer>
138 void Sort(
const Comparer& comparer);
174 using value_type = T;
175 using const_reference =
const T&;
176 using const_iterator =
const T*;
181 template <
typename U>
182 const U& GetUserData()
const;
184 template <
typename U>
190 CAPACITY_ALIGNMENT = 4
193 void SetCapacity(plUInt16 uiCapacity,
plAllocator* pAllocator);
196 const T* GetElementsPtr()
const;
198 plUInt16 m_uiCount = 0;
199 plUInt16 m_uiCapacity = Size;
201 plUInt32 m_uiUserData = 0;
205 struct alignas(PL_ALIGNMENT_OF(T))
207 plUInt8 m_StaticData[Size *
sizeof(T)];
210 T* m_pElements =
nullptr;
217template <
typename T, plUInt16 Size,
typename AllocatorWrapper = plDefaultAllocatorWrapper>
224 PL_DECLARE_MEM_RELOCATABLE_TYPE_CONDITIONAL(T);
238 void SetCount(plUInt16 uiCount);
239 void SetCount(plUInt16 uiCount,
const T& fillValue);
240 void EnsureCount(plUInt16 uiCount);
242 template <
typename =
void>
243 void SetCountUninitialized(plUInt16 uiCount);
245 void InsertAt(plUInt32 uiIndex,
const T& value);
246 void InsertAt(plUInt32 uiIndex, T&& value);
248 T& ExpandAndGetRef();
249 void PushBack(
const T& value);
250 void PushBack(T&& value);
253 void Reserve(plUInt16 uiCapacity);
257#include <Foundation/Containers/Implementation/SmallArray_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Implementation of a dynamically growing array with in-place storage and small memory overhead.
Definition SmallArray.h:17
void PopBack(plUInt32 uiCountToRemove=1)
Removes count elements from the end of the array.
Definition SmallArray_inl.h:400
void PushBackRange(const plArrayPtr< const T > &range, plAllocator *pAllocator)
Pushes all elements in range at the end of the array. Increases the capacity if necessary.
Definition SmallArray_inl.h:390
void Clear()
Clears the array.
Definition SmallArray_inl.h:222
void SetCount(plUInt16 uiCount, plAllocator *pAllocator)
Resizes the array to have exactly uiCount elements. Default constructs extra elements if the array is...
Definition SmallArray_inl.h:142
void RemoveAtAndSwap(plUInt32 uiIndex, plUInt16 uiNumElements=1)
Removes the element at index and fills the gap by swapping in the last element.
Definition SmallArray_inl.h:292
void MoveFrom(plSmallArrayBase< T, Size > &&other, plAllocator *pAllocator)
Moves the data from some other array into this one.
Definition SmallArray_inl.h:69
void SetCountUninitialized(plUInt16 uiCount, plAllocator *pAllocator)
Resizes the array to have exactly uiCount elements. Extra elements might be uninitialized.
Definition SmallArray_inl.h:190
void Sort()
Sort with default comparer.
Definition SmallArray_inl.h:434
void Compact(plAllocator *pAllocator)
Tries to compact the array to avoid wasting memory. The resulting capacity is at least 'GetCount' (no...
Definition SmallArray_inl.h:502
void Reserve(plUInt16 uiCapacity, plAllocator *pAllocator)
Expands the array so it can at least store the given capacity.
Definition SmallArray_inl.h:486
void Insert(const T &value, plUInt32 uiIndex, plAllocator *pAllocator)
Inserts value at index by shifting all following elements.
Definition SmallArray_inl.h:235
void CopyFrom(const plArrayPtr< const T > &other, plAllocator *pAllocator)
Copies the data from some other array into this one.
Definition SmallArray_inl.h:32
bool RemoveAndCopy(const T &value)
Removes the first occurrence of value and fills the gap by shifting all following elements.
Definition SmallArray_inl.h:257
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 SmallArray_inl.h:474
void EnsureCount(plUInt16 uiCount, plAllocator *pAllocator)
Ensures the container has at least uiCount elements. Ie. calls SetCount() if the container has fewer ...
Definition SmallArray_inl.h:180
T * GetData()
Returns a pointer to the array data, or nullptr if the array is empty.
Definition SmallArray_inl.h:444
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition SmallArray_inl.h:526
T & PeekBack()
Returns the last element of the array.
Definition SmallArray_inl.h:409
T & ExpandAndGetRef(plAllocator *pAllocator)
Grows the array by one element and returns a reference to the newly created element.
Definition SmallArray_inl.h:338
plUInt32 LastIndexOf(const T &value, plUInt32 uiStartIndex=plSmallInvalidIndex) const
Searches for the last occurrence of the given value and returns its index or plInvalidIndex if not fo...
Definition SmallArray_inl.h:325
plArrayPtr< T > GetArrayPtr()
Returns an array pointer to the array data, or an empty array pointer if the array is empty.
Definition SmallArray_inl.h:462
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition SmallArray_inl.h:210
const T & operator[](plUInt32 uiIndex) const
Returns the element at the given index. Does bounds checks in debug builds.
Definition SmallArray_inl.h:128
void RemoveAtAndCopy(plUInt32 uiIndex, plUInt16 uiNumElements=1)
Removes the element at index and fills the gap by shifting all following elements.
Definition SmallArray_inl.h:281
void PushBackUnchecked(const T &value)
Pushes value at the end of the array. Does NOT ensure capacity.
Definition SmallArray_inl.h:372
bool IsEmpty() const
Returns true, if the array does not contain any elements.
Definition SmallArray_inl.h:216
void PushBack(const T &value, plAllocator *pAllocator)
Pushes value at the end of the array.
Definition SmallArray_inl.h:354
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 SmallArray_inl.h:312
bool operator==(const plSmallArrayBase< T, Size > &rhs) const
Compares this array to another contiguous array type.
Definition SmallArray_inl.h:111
bool RemoveAndSwap(const T &value)
Removes the first occurrence of value and fills the gap by swapping in the last element.
Definition SmallArray_inl.h:269
plUInt32 GetCapacity() const
Returns the reserved number of elements that the array can hold without reallocating.
Definition SmallArray.h:169
bool Contains(const T &value) const
Checks whether the given value can be found in the array. O(n) complexity.
Definition SmallArray_inl.h:229
Definition SmallArray.h:219
Base class for Pointer like reverse iterators.
Definition ArrayIterator.h:152
Non-Const class for Pointer like reverse iterators.
Definition ArrayIterator.h:216