3#include <Foundation/Memory/MemoryUtils.h>
5#include <Foundation/Containers/Implementation/ArrayIterator.h>
8#include <Foundation/Strings/FormatString.h>
10#include <Foundation/Math/Math.h>
14# define plInvalidIndex 0xFFFFFFFF
17namespace plArrayPtrDetail
28 using type =
const plUInt8;
42 PL_DECLARE_POD_TYPE();
44 static_assert(!std::is_same_v<T, void>,
"plArrayPtr<void> is not allowed (anymore)");
45 static_assert(!std::is_same_v<T, const void>,
"plArrayPtr<void> is not allowed (anymore)");
47 using ByteType =
typename plArrayPtrDetail::ByteTypeHelper<T>::type;
49 using PointerType = T*;
61 m_pPtr = other.m_pPtr;
62 m_uiCount = other.m_uiCount;
71 if (m_pPtr ==
nullptr || m_uiCount == 0)
82 , m_uiCount(static_cast<plUInt32>(N))
89 : m_pPtr(other.m_pPtr)
90 , m_uiCount(other.m_uiCount)
100 m_pPtr = other.m_pPtr;
101 m_uiCount = other.m_uiCount;
111 PL_ALWAYS_INLINE
void operator=(std::nullptr_t)
118 PL_ALWAYS_INLINE PointerType
GetPtr() const
130 PL_ALWAYS_INLINE PointerType
GetEndPtr() {
return m_pPtr + m_uiCount; }
133 PL_ALWAYS_INLINE PointerType
GetEndPtr()
const {
return m_pPtr + m_uiCount; }
151 PL_ASSERT_DEV(uiStart <=
GetCount() && uiStart + uiCount <=
GetCount(),
"uiStart+uiCount ({0}) has to be smaller or equal than the count ({1}).",
160 PL_ASSERT_DEV(uiStart <=
GetCount(),
"uiStart ({0}) has to be smaller or equal than the count ({1}).", uiStart,
GetCount());
175 template <
typename U>
178 static_assert(
sizeof(T) ==
sizeof(U),
"Can only cast with equivalent element size.");
183 template <
typename U>
186 static_assert(
sizeof(T) ==
sizeof(U),
"Can only cast with equivalent element size.");
191 PL_FORCE_INLINE
const ValueType&
operator[](plUInt32 uiIndex)
const
193 PL_ASSERT_DEBUG(uiIndex <
GetCount(),
"Cannot access element {0}, the array only holds {1} elements.", uiIndex,
GetCount());
194 return *
static_cast<const ValueType*
>(
GetPtr() + uiIndex);
200 PL_ASSERT_DEBUG(uiIndex <
GetCount(),
"Cannot access element {0}, the array only holds {1} elements.", uiIndex,
GetCount());
201 return *
static_cast<ValueType*
>(
GetPtr() + uiIndex);
205 template <typename = typename std::enable_if<std::is_const<T>::value ==
false>>
211 if (
GetPtr() == other.GetPtr())
217#if PL_DISABLED(PL_USE_CPP20_OPERATORS)
218 template <typename = typename std::enable_if<std::is_const<T>::value ==
false>>
221 return !(*
this == other);
231 if (
GetPtr() == other.GetPtr())
242 return GetCount() < other.GetCount();
244 for (plUInt32 i = 0; i <
GetCount(); ++i)
246 if (
GetPtr()[i] < other.GetPtr()[i])
249 if (other.GetPtr()[i] <
GetPtr()[i])
271 PL_ALWAYS_INLINE
bool Contains(
const T& value)
const
273 return IndexOf(value) != plInvalidIndex;
277 inline plUInt32
IndexOf(
const T& value, plUInt32 uiStartIndex = 0) const
279 for (plUInt32 i = uiStartIndex; i < m_uiCount; ++i)
285 return plInvalidIndex;
289 inline plUInt32
LastIndexOf(
const T& value, plUInt32 uiStartIndex = plInvalidIndex)
const
291 for (plUInt32 i =
::plMath::Min(uiStartIndex, m_uiCount); i-- > 0;)
296 return plInvalidIndex;
299 using const_iterator =
const T*;
318PL_ALWAYS_INLINE
plArrayPtr<T> plMakeArrayPtr(T* pPtr, plUInt32 uiCount)
324template <
typename T, plUInt32 N>
325PL_ALWAYS_INLINE
plArrayPtr<T> plMakeArrayPtr(T (&staticArray)[N])
339PL_ALWAYS_INLINE
plByteArrayPtr plMakeByteArrayPtr(T* pPtr, plUInt32 uiCount)
341 return plByteArrayPtr(
reinterpret_cast<plUInt8*
>(pPtr), uiCount *
sizeof(T));
345PL_ALWAYS_INLINE
plByteArrayPtr plMakeByteArrayPtr(
void* pPtr, plUInt32 uiBytes)
359typename plArrayPtr<T>::iterator begin(
plArrayPtr<T>& ref_container)
361 return ref_container.
GetPtr();
365typename plArrayPtr<T>::const_iterator begin(
const plArrayPtr<T>& container)
367 return container.
GetPtr();
371typename plArrayPtr<T>::const_iterator cbegin(
const plArrayPtr<T>& container)
373 return container.
GetPtr();
395typename plArrayPtr<T>::iterator end(
plArrayPtr<T>& ref_container)
401typename plArrayPtr<T>::const_iterator end(
const plArrayPtr<T>& container)
407typename plArrayPtr<T>::const_iterator cend(
const plArrayPtr<T>& container)
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()
Returns the pointer to the array.
Definition ArrayPtr.h:124
void CopyFrom(const plArrayPtr< const T > &other)
Copies the data from other into this array. The arrays must have the exact same size.
Definition ArrayPtr.h:257
PL_FORCE_INLINE ValueType & operator[](plUInt32 uiIndex)
Index access.
Definition ArrayPtr.h:198
PL_ALWAYS_INLINE plArrayPtr< ByteType > ToByteArray()
Reinterprets this array as a byte array.
Definition ArrayPtr.h:171
bool operator<(const plArrayPtr< const T > &other) const
Compares the two arrays for less.
Definition ArrayPtr.h:239
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 ArrayPtr.h:277
PL_ALWAYS_INLINE plArrayPtr(const plArrayPtr< U > &other)
Initializes the plArrayPtr to be a copy of other. No memory is allocated or copied.
Definition ArrayPtr.h:88
PL_ALWAYS_INLINE void operator=(const plArrayPtr< T > &other)
Copies the pointer and size of /a other. Does not allocate any data.
Definition ArrayPtr.h:98
bool operator==(const plArrayPtr< T > &other) const
Compares the two arrays for equality.
Definition ArrayPtr.h:226
PL_ALWAYS_INLINE plArrayPtr< U > Cast()
Cast an ArrayPtr to an ArrayPtr to a different, but same size, type.
Definition ArrayPtr.h:176
PL_ALWAYS_INLINE PointerType GetEndPtr()
Returns the pointer behind the last element of the array.
Definition ArrayPtr.h:130
PL_ALWAYS_INLINE bool Contains(const T &value) const
Checks whether the given value can be found in the array. O(n) complexity.
Definition ArrayPtr.h:271
PL_ALWAYS_INLINE void Clear()
Clears the array.
Definition ArrayPtr.h:105
PL_FORCE_INLINE const ValueType & operator[](plUInt32 uiIndex) const
Index access.
Definition ArrayPtr.h:191
PL_ALWAYS_INLINE PointerType GetPtr() const
Returns the pointer to the array.
Definition ArrayPtr.h:118
PL_ALWAYS_INLINE bool IsEmpty() const
Returns whether the array is empty.
Definition ArrayPtr.h:136
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 ArrayPtr.h:289
PL_ALWAYS_INLINE plArrayPtr< const ByteType > ToByteArray() const
Reinterprets this array as a byte array.
Definition ArrayPtr.h:165
PL_FORCE_INLINE plArrayPtr< T > GetSubArray(plUInt32 uiStart, plUInt32 uiCount) const
Creates a sub-array from this array.
Definition ArrayPtr.h:148
PL_ALWAYS_INLINE plArrayPtr< const U > Cast() const
Cast an ArrayPtr to an ArrayPtr to a different, but same size, type.
Definition ArrayPtr.h:184
PL_ALWAYS_INLINE plArrayPtr(const plArrayPtr< T > &other)
Copies the pointer and size of /a other. Does not allocate any data.
Definition ArrayPtr.h:59
PL_ALWAYS_INLINE plArrayPtr()
Initializes the plArrayPtr to be empty.
Definition ArrayPtr.h:52
bool operator==(const plArrayPtr< const T > &other) const
Compares the two arrays for equality.
Definition ArrayPtr.h:206
plArrayPtr(T *pPtr, plUInt32 uiCount)
Initializes the plArrayPtr with the given pointer and number of elements. No memory is allocated or c...
Definition ArrayPtr.h:66
PL_ALWAYS_INLINE plArrayPtr(T(&staticArray)[N])
Initializes the plArrayPtr to encapsulate the given array.
Definition ArrayPtr.h:80
PL_ALWAYS_INLINE PointerType GetEndPtr() const
Returns the pointer behind the last element of the array.
Definition ArrayPtr.h:133
PL_FORCE_INLINE plArrayPtr< T > GetSubArray(plUInt32 uiStart) const
Creates a sub-array from this array.
Definition ArrayPtr.h:158
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 Copy(T *pDestination, const T *pSource, size_t uiCount=1)
Copies objects of type T from pSource to pDestination.
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
Base class for Pointer like reverse iterators.
Definition ArrayIterator.h:152
Non-Const class for Pointer like reverse iterators.
Definition ArrayIterator.h:216