Plasma Engine  2.0
Loading...
Searching...
No Matches
plHybridArray< T, Size, AllocatorWrapper > Class Template Reference

A hybrid array uses in-place storage to handle the first few elements without any allocation. It dynamically resizes when more elements are needed. More...

#include <HybridArray.h>

Inheritance diagram for plHybridArray< T, Size, AllocatorWrapper >:

Classes

struct  alignas
 The fixed size array. More...
 

Public Member Functions

 plHybridArray ()
 Creates an empty array. Does not allocate any data yet.
 
 plHybridArray (plAllocator *pAllocator)
 Creates an empty array. Does not allocate any data yet.
 
 plHybridArray (const plHybridArray< T, Size, AllocatorWrapper > &other)
 Creates a copy of the given array.
 
 plHybridArray (const plArrayPtr< const T > &other)
 Creates a copy of the given array.
 
 plHybridArray (plHybridArray< T, Size, AllocatorWrapper > &&other) noexcept
 Moves the given array.
 
void operator= (const plHybridArray< T, Size, AllocatorWrapper > &rhs)
 Copies the data from some other contiguous array into this one.
 
void operator= (const plArrayPtr< const T > &rhs)
 Copies the data from some other contiguous array into this one.
 
void operator= (plHybridArray< T, Size, AllocatorWrapper > &&rhs) noexcept
 Moves the data from some other contiguous array into this one.
 
- Public Member Functions inherited from plDynamicArray< T, plDefaultAllocatorWrapper >
 PL_DECLARE_MEM_RELOCATABLE_TYPE ()
 
 plDynamicArray (plAllocator *pAllocator)
 
 plDynamicArray (const plDynamicArray< T, plDefaultAllocatorWrapper > &other)
 
 plDynamicArray (const plDynamicArrayBase< T > &other)
 
 plDynamicArray (const plArrayPtr< const T > &other)
 
 plDynamicArray (plDynamicArray< T, plDefaultAllocatorWrapper > &&other)
 
 plDynamicArray (plDynamicArrayBase< T > &&other)
 
void operator= (const plDynamicArray< T, plDefaultAllocatorWrapper > &rhs)
 
void operator= (const plDynamicArrayBase< T > &rhs)
 
void operator= (const plArrayPtr< const T > &rhs)
 
void operator= (plDynamicArray< T, plDefaultAllocatorWrapper > &&rhs) noexcept
 
void operator= (plDynamicArrayBase< T > &&rhs) noexcept
 
- Public Member Functions inherited from plDynamicArrayBase< T >
void Reserve (plUInt32 uiCapacity)
 Expands the array so it can at least store the given capacity.
 
void Compact ()
 Tries to compact the array to avoid wasting memory. The resulting capacity is at least 'GetCount' (no elements get removed). Will deallocate all data, if the array is empty.
 
plAllocatorGetAllocator () const
 Returns the allocator that is used by this instance.
 
plUInt64 GetHeapMemoryUsage () const
 Returns the amount of bytes that are currently allocated on the heap.
 
void Swap (plDynamicArrayBase< T > &other)
 swaps the contents of this array with another one
 
- Public Member Functions inherited from plArrayBase< T, plDynamicArrayBase< T > >
 plArrayBase ()
 Constructor.
 
 ~plArrayBase ()
 Destructor.
 
void operator= (const plArrayPtr< const T > &rhs)
 Copies the data from some other contiguous array into this one.
 
 operator plArrayPtr< const T > () const
 Conversion to const plArrayPtr.
 
 operator plArrayPtr< T > ()
 Conversion to plArrayPtr.
 
bool operator== (const plArrayBase< T, plDynamicArrayBase< T > > &rhs) const
 Compares this array to another contiguous array type.
 
 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL (const plArrayBase< T, plDynamicArrayBase< T > > &)
 
bool operator< (const plArrayBase< T, plDynamicArrayBase< T > > &rhs) const
 Compares this array to another contiguous array type.
 
bool operator< (const plArrayPtr< const T > &rhs) const
 Compares this array to another contiguous array type.
 
const T & operator[] (plUInt32 uiIndex) const
 Returns the element at the given index. Does bounds checks in debug builds.
 
T & operator[] (plUInt32 uiIndex)
 Returns the element at the given index. Does bounds checks in debug builds.
 
void SetCount (plUInt32 uiCount)
 Resizes the array to have exactly uiCount elements. Default constructs extra elements if the array is grown.
 
void SetCount (plUInt32 uiCount, const T &fillValue)
 Resizes the array to have exactly uiCount elements. Constructs all new elements by copying the FillValue.
 
void SetCountUninitialized (plUInt32 uiCount)
 Resizes the array to have exactly uiCount elements. Extra elements might be uninitialized.
 
void EnsureCount (plUInt32 uiCount)
 Ensures the container has at least uiCount elements. Ie. calls SetCount() if the container has fewer elements, does nothing otherwise.
 
plUInt32 GetCount () const
 Returns the number of active elements in the array.
 
bool IsEmpty () const
 Returns true, if the array does not contain any elements.
 
void Clear ()
 Clears the array.
 
bool Contains (const T &value) const
 Checks whether the given value can be found in the array. O(n) complexity.
 
void InsertAt (plUInt32 uiIndex, const T &value)
 Inserts value at index by shifting all following elements.
 
void InsertAt (plUInt32 uiIndex, T &&value)
 Inserts value at index by shifting all following elements.
 
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.
 
bool RemoveAndCopy (const T &value)
 Removes the first occurrence of value and fills the gap by shifting all following elements.
 
bool RemoveAndSwap (const T &value)
 Removes the first occurrence of value and fills the gap by swapping in the last element.
 
void RemoveAtAndCopy (plUInt32 uiIndex, plUInt32 uiNumElements=1)
 Removes the element at index and fills the gap by shifting all following elements.
 
void RemoveAtAndSwap (plUInt32 uiIndex, plUInt32 uiNumElements=1)
 Removes the element at index and fills the gap by swapping in the last element.
 
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 found.
 
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 found.
 
T & ExpandAndGetRef ()
 Grows the array by one element and returns a reference to the newly created element.
 
T * ExpandBy (plUInt32 uiNumNewItems)
 Expands the array by N new items and returns a pointer to the first new one.
 
void PushBack (const T &value)
 Pushes value at the end of the array.
 
void PushBack (T &&value)
 Pushes value at the end of the array.
 
void PushBackUnchecked (const T &value)
 Pushes value at the end of the array. Does NOT ensure capacity.
 
void PushBackUnchecked (T &&value)
 Pushes value at the end of the array. Does NOT ensure capacity.
 
void PushBackRange (const plArrayPtr< const T > &range)
 Pushes all elements in range at the end of the array. Increases the capacity if necessary.
 
void PopBack (plUInt32 uiCountToRemove=1)
 Removes count elements from the end of the array.
 
T & PeekBack ()
 Returns the last element of the array.
 
const T & PeekBack () const
 Returns the last element of the array.
 
void Sort (const Comparer &comparer)
 Sort with explicit comparer.
 
void Sort ()
 Sort with default comparer.
 
T * GetData ()
 Returns a pointer to the array data, or nullptr if the array is empty.
 
const T * GetData () const
 Returns a pointer to the array data, or nullptr if the array is empty.
 
plArrayPtr< T > GetArrayPtr ()
 Returns an array pointer to the array data, or an empty array pointer if the array is empty.
 
plArrayPtr< const T > GetArrayPtr () const
 Returns an array pointer to the array data, or an empty array pointer if the array is empty.
 
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.
 
plArrayPtr< typename plArrayPtr< const T >::ByteType > GetByteArrayPtr () const
 Returns a byte array pointer to the array data, or an empty array pointer if the array is empty.
 
plUInt32 GetCapacity () const
 Returns the reserved number of elements that the array can hold without reallocating.
 

Protected Member Functions

PL_ALWAYS_INLINE T * GetStaticArray ()
 
PL_ALWAYS_INLINE const T * GetStaticArray () const
 
- Protected Member Functions inherited from plDynamicArray< T, plDefaultAllocatorWrapper >
 plDynamicArray (T *pInplaceStorage, plUInt32 uiCapacity, plAllocator *pAllocator)
 
- Protected Member Functions inherited from plDynamicArrayBase< T >
 plDynamicArrayBase (plAllocator *pAllocator)
 Creates an empty array. Does not allocate any data yet.
 
 plDynamicArrayBase (T *pInplaceStorage, plUInt32 uiCapacity, plAllocator *pAllocator)
 
 plDynamicArrayBase (const plDynamicArrayBase< T > &other, plAllocator *pAllocator)
 Creates a copy of the given array.
 
 plDynamicArrayBase (plDynamicArrayBase< T > &&other, plAllocator *pAllocator)
 Moves the given array into this one.
 
 plDynamicArrayBase (const plArrayPtr< const T > &other, plAllocator *pAllocator)
 Creates a copy of the given array.
 
 ~plDynamicArrayBase ()
 Destructor.
 
void operator= (const plDynamicArrayBase< T > &rhs)
 Copies the data from some other contiguous array into this one.
 
void operator= (plDynamicArrayBase< T > &&rhs) noexcept
 Moves the data from some other contiguous array into this one.
 
T * GetElementsPtr ()
 
const T * GetElementsPtr () const
 
- Protected Member Functions inherited from plArrayBase< T, plDynamicArrayBase< T > >
void DoSwap (plArrayBase< T, plDynamicArrayBase< T > > &other)
 

Additional Inherited Members

- Public Types inherited from plArrayBase< T, plDynamicArrayBase< T > >
using const_iterator
 
using const_reverse_iterator
 
using iterator
 
using reverse_iterator
 
- Protected Attributes inherited from plArrayBase< T, plDynamicArrayBase< T > >
T * m_pElements
 Element-type access to m_Data.
 
plUInt32 m_uiCount
 The number of elements used from the array.
 
plUInt32 m_uiCapacity
 The number of elements which can be stored in the array without re-allocating.
 

Detailed Description

template<typename T, plUInt32 Size, typename AllocatorWrapper = plDefaultAllocatorWrapper>
class plHybridArray< T, Size, AllocatorWrapper >

A hybrid array uses in-place storage to handle the first few elements without any allocation. It dynamically resizes when more elements are needed.

It is often more efficient to use a hybrid array, rather than a dynamic array, when the number of needed elements is typically low or when the array is used only temporarily. In this case costly allocations can often be prevented entirely. However, if the number of elements is unpredictable or usually very large, prefer a dynamic array, to avoid wasting (stack) memory for a hybrid array that is rarely large enough to be used. The plHybridArray is derived from plDynamicArray and can therefore be passed to functions that expect an plDynamicArray, even for output.


The documentation for this class was generated from the following files: