Plasma Engine  2.0
Loading...
Searching...
No Matches
Deque.h
1#pragma once
2
3#include <Foundation/Algorithm/Sorting.h>
4#include <Foundation/Containers/Implementation/ArrayIterator.h>
5#include <Foundation/Memory/Allocator.h>
6#include <Foundation/Memory/AllocatorWrapper.h>
7
9#ifndef plInvalidIndex
10# define plInvalidIndex 0xFFFFFFFF
11#endif
12
25template <typename T, bool Construct>
27{
28protected:
30 explicit plDequeBase(plAllocator* pAllocator); // [tested]
31
33 plDequeBase(const plDequeBase<T, Construct>& rhs, plAllocator* pAllocator); // [tested]
34
36 plDequeBase(plDequeBase<T, Construct>&& rhs, plAllocator* pAllocator); // [tested]
37
39 ~plDequeBase(); // [tested]
40
42 void operator=(const plDequeBase<T, Construct>& rhs); // [tested]
43
45 void operator=(plDequeBase<T, Construct>&& rhs); // [tested]
46
47public:
49 void Clear(); // [tested]
50
60 void Reserve(plUInt32 uiCount); // [tested]
61
68 void Compact(); // [tested]
69
71 void Swap(plDequeBase<T, Construct>& other); // [tested]
72
75 void SetCount(plUInt32 uiCount); // [tested]
76
78 template <typename = void> // Template is used to only conditionally compile this function in when it is actually used.
79 void SetCountUninitialized(plUInt32 uiCount); // [tested]
80
83 void EnsureCount(plUInt32 uiCount); // [tested]
84
86 T& operator[](plUInt32 uiIndex); // [tested]
87
89 const T& operator[](plUInt32 uiIndex) const; // [tested]
90
92 T& ExpandAndGetRef(); // [tested]
93
95 void PushBack(); // [tested]
96
98 void PushBack(const T& element); // [tested]
99
101 void PushBack(T&& value); // [tested]
102
104 void PopBack(plUInt32 uiElements = 1); // [tested]
105
107 void PushFront(const T& element); // [tested]
108
110 void PushFront(T&& element); // [tested]
111
113 void PushFront(); // [tested]
114
116 void PopFront(plUInt32 uiElements = 1); // [tested]
117
119 bool IsEmpty() const; // [tested]
120
122 plUInt32 GetCount() const; // [tested]
123
125 const T& PeekFront() const; // [tested]
126
128 T& PeekFront(); // [tested]
129
131 const T& PeekBack() const; // [tested]
132
134 T& PeekBack(); // [tested]
135
137 bool Contains(const T& value) const; // [tested]
138
140 plUInt32 IndexOf(const T& value, plUInt32 uiStartIndex = 0) const; // [tested]
141
143 plUInt32 LastIndexOf(const T& value, plUInt32 uiStartIndex = plInvalidIndex) const; // [tested]
144
146 void RemoveAtAndSwap(plUInt32 uiIndex); // [tested]
147
149 void RemoveAtAndCopy(plUInt32 uiIndex); // [tested]
150
152 bool RemoveAndCopy(const T& value); // [tested]
153
155 bool RemoveAndSwap(const T& value); // [tested]
156
158 void InsertAt(plUInt32 uiIndex, const T& value); // [tested]
159
161 template <typename Comparer>
162 void Sort(const Comparer& comparer); // [tested]
163
165 void Sort(); // [tested]
166
168 plAllocator* GetAllocator() const { return m_pAllocator; }
169
170 using const_iterator = const_iterator_base<plDequeBase<T, Construct>, T, false>;
171 using const_reverse_iterator = const_iterator_base<plDequeBase<T, Construct>, T, true>;
172 using iterator = iterator_base<plDequeBase<T, Construct>, T, false>;
173 using reverse_iterator = iterator_base<plDequeBase<T, Construct>, T, true>;
174
178 plUInt32 GetContiguousRange(plUInt32 uiStartIndex) const; // [tested]
179
181 bool operator==(const plDequeBase<T, Construct>& rhs) const; // [tested]
182
183 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plDequeBase<T, Construct>&);
184
186 plUInt64 GetHeapMemoryUsage() const; // [tested]
187
188private:
190 void Constructor(plAllocator* pAllocator);
191
193 void CompactIndexArray(plUInt32 uiMinChunksToKeep);
194
196 void MoveIndexChunksLeft(plUInt32 uiChunkDiff);
197
199 void MoveIndexChunksRight(plUInt32 uiChunkDiff);
200
202 plUInt32 GetFirstUsedChunk() const;
203
206 plUInt32 GetLastUsedChunk(plUInt32 uiAtSize) const;
207
209 plUInt32 GetLastUsedChunk() const;
210
213 plUInt32 GetRequiredChunks(plUInt32 uiAtSize) const;
214
216 void DeallocateUnusedChunks(plUInt32 uiMaxChunks);
217
219 void ResetReduceSizeCounter();
220
236 void ReduceSize(plInt32 iReduction);
237
239 plUInt32 GetCurMaxCount() const;
240
242 T* GetUnusedChunk();
243
246 T& ElementAt(plUInt32 uiIndex);
247
249 void DeallocateAll();
250
251 plAllocator* m_pAllocator;
252 T** m_pChunks;
253 plUInt32 m_uiChunks;
254 plUInt32 m_uiFirstElement;
255 plUInt32 m_uiCount;
256 plUInt32 m_uiAllocatedChunks;
257 plInt32 m_iReduceSizeTimer;
259 plUInt32 m_uiMaxCount;
261
262#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
263 plUInt32 m_uiChunkSize; // needed for debugger visualization
264#endif
265};
266
268template <typename T, typename AllocatorWrapper = plDefaultAllocatorWrapper, bool Construct = true>
269class plDeque : public plDequeBase<T, Construct>
270{
271public:
272 plDeque();
273 plDeque(plAllocator* pAllocator);
274
277
280
281 void operator=(const plDeque<T, AllocatorWrapper, Construct>& rhs);
282 void operator=(const plDequeBase<T, Construct>& rhs);
283
284 void operator=(plDeque<T, AllocatorWrapper, Construct>&& rhs);
285 void operator=(plDequeBase<T, Construct>&& rhs);
286};
287
288template <typename T, bool Construct>
290{
291 return typename plDequeBase<T, Construct>::iterator(ref_container, (size_t)0);
292}
293
294template <typename T, bool Construct>
296{
297 return typename plDequeBase<T, Construct>::const_iterator(container, (size_t)0);
298}
299
300template <typename T, bool Construct>
302{
303 return typename plDequeBase<T, Construct>::const_iterator(container, (size_t)0);
304}
305
306template <typename T, bool Construct>
308{
309 return typename plDequeBase<T, Construct>::reverse_iterator(ref_container, (size_t)0);
310}
311
312template <typename T, bool Construct>
314{
315 return typename plDequeBase<T, Construct>::const_reverse_iterator(container, (size_t)0);
316}
317
318template <typename T, bool Construct>
320{
321 return typename plDequeBase<T, Construct>::const_reverse_iterator(container, (size_t)0);
322}
323
324template <typename T, bool Construct>
326{
327 return typename plDequeBase<T, Construct>::iterator(ref_container, (size_t)ref_container.GetCount());
328}
329
330template <typename T, bool Construct>
332{
333 return typename plDequeBase<T, Construct>::const_iterator(container, (size_t)container.GetCount());
334}
335
336template <typename T, bool Construct>
338{
339 return typename plDequeBase<T, Construct>::const_iterator(container, (size_t)container.GetCount());
340}
341
342template <typename T, bool Construct>
344{
345 return typename plDequeBase<T, Construct>::reverse_iterator(ref_container, (size_t)ref_container.GetCount());
346}
347
348template <typename T, bool Construct>
350{
351 return typename plDequeBase<T, Construct>::const_reverse_iterator(container, (size_t)container.GetCount());
352}
353
354template <typename T, bool Construct>
356{
357 return typename plDequeBase<T, Construct>::const_reverse_iterator(container, (size_t)container.GetCount());
358}
359
360#include <Foundation/Containers/Implementation/Deque_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
A double ended queue container.
Definition Deque.h:27
void Swap(plDequeBase< T, Construct > &other)
swaps the contents of this deque with another one
Definition Deque_inl.h:264
~plDequeBase()
Destructor.
Definition Deque_inl.h:67
T & operator[](plUInt32 uiIndex)
Accesses the n-th element in the deque.
Definition Deque_inl.h:447
void PopBack(plUInt32 uiElements=1)
Removes the last element from the deque.
Definition Deque_inl.h:525
void Clear()
Destructs all elements and sets the count to zero. Does not deallocate any data.
Definition Deque_inl.h:130
void Sort()
Sort with default comparer.
Definition Deque_inl.h:963
plUInt32 IndexOf(const T &value, plUInt32 uiStartIndex=0) const
Returns the first index at which an element with the given value could be found or plInvalidIndex if ...
Definition Deque_inl.h:643
void Reserve(plUInt32 uiCount)
Rearranges the internal data structures such that the amount of reserved elements can be appended wit...
Definition Deque_inl.h:158
void RemoveAtAndSwap(plUInt32 uiIndex)
Removes the element at the given index and fills the gap with the last element in the deque.
Definition Deque_inl.h:666
plUInt32 LastIndexOf(const T &value, plUInt32 uiStartIndex=plInvalidIndex) const
Returns the last index at which an element with the given value could be found or plInvalidIndex if n...
Definition Deque_inl.h:655
void PushFront()
Adds one default constructed element to the front of the deque.
Definition Deque_inl.h:566
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition Deque_inl.h:970
plUInt32 GetCount() const
Returns the number of active elements in the deque.
Definition Deque_inl.h:607
bool operator==(const plDequeBase< T, Construct > &rhs) const
Comparison operator.
Definition Deque_inl.h:115
void InsertAt(plUInt32 uiIndex, const T &value)
Inserts value at index by shifting all following elements. Valid insert positions are [0; GetCount].
Definition Deque_inl.h:937
plUInt32 GetContiguousRange(plUInt32 uiStartIndex) const
Returns the number of elements after uiStartIndex that are stored in contiguous memory.
Definition Deque_inl.h:432
void Compact()
This function deallocates as much memory as possible to shrink the deque to the bare minimum size tha...
Definition Deque_inl.h:246
T & ExpandAndGetRef()
Grows the deque by one element and returns a reference to the newly created element.
Definition Deque_inl.h:473
void SetCountUninitialized(plUInt32 uiCount)
\Same as SetCount(), but new elements do not get default constructed.
Definition Deque_inl.h:389
void RemoveAtAndCopy(plUInt32 uiIndex)
Removes the element at index and fills the gap by shifting all following elements.
Definition Deque_inl.h:894
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition Deque.h:168
void PushBack()
Adds one default constructed element to the back of the deque.
Definition Deque_inl.h:489
void EnsureCount(plUInt32 uiCount)
Ensures the container has at least uiCount elements. Ie. calls SetCount() if the container has fewer ...
Definition Deque_inl.h:423
bool RemoveAndSwap(const T &value)
Removes the first occurrence of value and fills the gap with the last element in the deque.
Definition Deque_inl.h:923
bool Contains(const T &value) const
Checks whether there is any element in the deque with the given value.
Definition Deque_inl.h:637
const T & PeekFront() const
Returns the first element.
Definition Deque_inl.h:613
plDequeBase(plAllocator *pAllocator)
No memory is allocated during construction.
Definition Deque_inl.h:41
void PopFront(plUInt32 uiElements=1)
Removes the first element from the deque.
Definition Deque_inl.h:581
void operator=(const plDequeBase< T, Construct > &rhs)
Assignment operator.
Definition Deque_inl.h:73
const T & PeekBack() const
Returns the last element.
Definition Deque_inl.h:625
bool IsEmpty() const
Checks whether no elements are active in the deque.
Definition Deque_inl.h:601
bool RemoveAndCopy(const T &value)
Removes the first occurrence of value and fills the gap by shifting all following elements.
Definition Deque_inl.h:909
void SetCount(plUInt32 uiCount)
Sets the number of active elements in the deque. All new elements are default constructed....
Definition Deque_inl.h:346
Definition Deque.h:270
Base class for STL like random access iterators.
Definition ArrayIterator.h:9
Non-const STL like iterators.
Definition ArrayIterator.h:91