Plasma Engine  2.0
Loading...
Searching...
No Matches
ArrayBase.h
1#pragma once
2
3#include <Foundation/Algorithm/Sorting.h>
4#include <Foundation/Math/Math.h>
5#include <Foundation/Types/ArrayPtr.h>
6
7#if PL_ENABLED(PL_INTEROP_STL_SPAN)
8# include <span>
9#endif
10
12#ifndef plInvalidIndex
13# define plInvalidIndex 0xFFFFFFFF
14#endif
15
17template <typename T, typename Derived>
19{
20public:
22 plArrayBase(); // [tested]
23
25 ~plArrayBase(); // [tested]
26
28 void operator=(const plArrayPtr<const T>& rhs); // [tested]
29
31 operator plArrayPtr<const T>() const; // [tested]
32
34 operator plArrayPtr<T>(); // [tested]
35
37 bool operator==(const plArrayBase<T, Derived>& rhs) const; // [tested]
38 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plArrayBase<T, Derived>&);
39
41 bool operator<(const plArrayBase<T, Derived>& rhs) const; // [tested]
42
43#if PL_DISABLED(PL_USE_CPP20_OPERATORS)
45 bool operator==(const plArrayPtr<const T>& rhs) const; // [tested]
46 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plArrayPtr<const T>&);
47#endif
48
50 bool operator<(const plArrayPtr<const T>& rhs) const; // [tested]
51
53 const T& operator[](plUInt32 uiIndex) const; // [tested]
54
56 T& operator[](plUInt32 uiIndex); // [tested]
57
59 void SetCount(plUInt32 uiCount); // [tested]
60
62 void SetCount(plUInt32 uiCount, const T& fillValue); // [tested]
63
65 template <typename = void> // Template is used to only conditionally compile this function in when it is actually used.
66 void SetCountUninitialized(plUInt32 uiCount); // [tested]
67
70 void EnsureCount(plUInt32 uiCount); // [tested]
71
73 plUInt32 GetCount() const; // [tested]
74
76 bool IsEmpty() const; // [tested]
77
79 void Clear(); // [tested]
80
82 bool Contains(const T& value) const; // [tested]
83
85 void InsertAt(plUInt32 uiIndex, const T& value); // [tested]
86
88 void InsertAt(plUInt32 uiIndex, T&& value); // [tested]
89
91 void InsertRange(const plArrayPtr<const T>& range, plUInt32 uiIndex); // [tested]
92
94 bool RemoveAndCopy(const T& value); // [tested]
95
97 bool RemoveAndSwap(const T& value); // [tested]
98
100 void RemoveAtAndCopy(plUInt32 uiIndex, plUInt32 uiNumElements = 1); // [tested]
101
103 void RemoveAtAndSwap(plUInt32 uiIndex, plUInt32 uiNumElements = 1); // [tested]
104
106 plUInt32 IndexOf(const T& value, plUInt32 uiStartIndex = 0) const; // [tested]
107
109 plUInt32 LastIndexOf(const T& value, plUInt32 uiStartIndex = plInvalidIndex) const; // [tested]
110
112 T& ExpandAndGetRef(); // [tested]
113
115 T* ExpandBy(plUInt32 uiNumNewItems);
116
118 void PushBack(const T& value); // [tested]
119
121 void PushBack(T&& value); // [tested]
122
124 void PushBackUnchecked(const T& value); // [tested]
125
127 void PushBackUnchecked(T&& value); // [tested]
128
130 void PushBackRange(const plArrayPtr<const T>& range); // [tested]
131
133 void PopBack(plUInt32 uiCountToRemove = 1); // [tested]
134
136 T& PeekBack(); // [tested]
137
139 const T& PeekBack() const; // [tested]
140
142 template <typename Comparer>
143 void Sort(const Comparer& comparer); // [tested]
144
146 void Sort(); // [tested]
147
149 T* GetData();
150
152 const T* GetData() const;
153
155 plArrayPtr<T> GetArrayPtr(); // [tested]
156
158 plArrayPtr<const T> GetArrayPtr() const; // [tested]
159
162
165
167 plUInt32 GetCapacity() const { return m_uiCapacity; }
168
169 using const_iterator = const T*;
170 using const_reverse_iterator = const_reverse_pointer_iterator<T>;
171 using iterator = T*;
172 using reverse_iterator = reverse_pointer_iterator<T>;
173
174#if PL_ENABLED(PL_INTEROP_STL_SPAN)
175 operator std::span<const T>() const
176 {
177 return std::span(GetData(), static_cast<size_t>(GetCount()));
178 }
179
180 operator std::span<T>()
181 {
182 return std::span(GetData(), static_cast<size_t>(GetCount()));
183 }
184
185 std::span<T> GetSpan()
186 {
187 return std::span(GetData(), static_cast<size_t>(GetCount()));
188 }
189
190 std::span<const T> GetSpan() const
191 {
192 return std::span(GetData(), static_cast<size_t>(GetCount()));
193 }
194#endif
195
196protected:
197 void DoSwap(plArrayBase<T, Derived>& other);
198
200 T* m_pElements = nullptr;
201
203 plUInt32 m_uiCount = 0;
204
206 plUInt32 m_uiCapacity = 0;
207};
208
209template <typename T, typename Derived>
210typename plArrayBase<T, Derived>::iterator begin(plArrayBase<T, Derived>& ref_container)
211{
212 return ref_container.GetData();
213}
214
215template <typename T, typename Derived>
216typename plArrayBase<T, Derived>::const_iterator begin(const plArrayBase<T, Derived>& container)
217{
218 return container.GetData();
219}
220
221template <typename T, typename Derived>
222typename plArrayBase<T, Derived>::const_iterator cbegin(const plArrayBase<T, Derived>& container)
223{
224 return container.GetData();
225}
226
227template <typename T, typename Derived>
229{
230 return typename plArrayBase<T, Derived>::reverse_iterator(ref_container.GetData() + ref_container.GetCount() - 1);
231}
232
233template <typename T, typename Derived>
235{
236 return typename plArrayBase<T, Derived>::const_reverse_iterator(container.GetData() + container.GetCount() - 1);
237}
238
239template <typename T, typename Derived>
241{
242 return typename plArrayBase<T, Derived>::const_reverse_iterator(container.GetData() + container.GetCount() - 1);
243}
244
245template <typename T, typename Derived>
246typename plArrayBase<T, Derived>::iterator end(plArrayBase<T, Derived>& ref_container)
247{
248 return ref_container.GetData() + ref_container.GetCount();
249}
250
251template <typename T, typename Derived>
252typename plArrayBase<T, Derived>::const_iterator end(const plArrayBase<T, Derived>& container)
253{
254 return container.GetData() + container.GetCount();
255}
256
257template <typename T, typename Derived>
258typename plArrayBase<T, Derived>::const_iterator cend(const plArrayBase<T, Derived>& container)
259{
260 return container.GetData() + container.GetCount();
261}
262
263template <typename T, typename Derived>
265{
266 return typename plArrayBase<T, Derived>::reverse_iterator(ref_container.GetData() - 1);
267}
268
269template <typename T, typename Derived>
271{
272 return typename plArrayBase<T, Derived>::const_reverse_iterator(container.GetData() - 1);
273}
274
275template <typename T, typename Derived>
277{
278 return typename plArrayBase<T, Derived>::const_reverse_iterator(container.GetData() - 1);
279}
280
281#include <Foundation/Containers/Implementation/ArrayBase_inl.h>
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
plUInt32 GetCapacity() const
Returns the reserved number of elements that the array can hold without reallocating.
Definition ArrayBase.h:167
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
Base class for Pointer like reverse iterators.
Definition ArrayIterator.h:152
Non-Const class for Pointer like reverse iterators.
Definition ArrayIterator.h:216