Plasma Engine  2.0
Loading...
Searching...
No Matches
List.h
1#pragma once
2
3#include <Foundation/Containers/Deque.h>
4
10template <typename T>
12{
13private:
14 struct ListElement;
15
16 struct ListElementBase
17 {
18 ListElementBase();
19
20 ListElement* m_pPrev;
21 ListElement* m_pNext;
22 };
23
25 struct ListElement : public ListElementBase
26 {
27 ListElement()
28 : ListElementBase()
29 {
30 }
31 explicit ListElement(const T& data);
32
33 T m_Data = {};
34 };
35
37 struct ConstIterator
38 {
39 PL_DECLARE_POD_TYPE();
40
42 ConstIterator()
43 : m_pElement(nullptr)
44 {
45 } // [tested]
46
48 bool operator==(typename plListBase<T>::ConstIterator it2) const { return (m_pElement == it2.m_pElement); } // [tested]
49 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(typename plListBase<T>::ConstIterator);
50
52 const T& operator*() const { return (m_pElement->m_Data); } // [tested]
53
55 const T* operator->() const { return (&m_pElement->m_Data); } // [tested]
56
58 void Next() { m_pElement = m_pElement->m_pNext; } // [tested]
59
61 void Prev() { m_pElement = m_pElement->m_pPrev; } // [tested]
62
64 bool IsValid() const { return ((m_pElement != nullptr) && (m_pElement->m_pPrev != nullptr) && (m_pElement->m_pNext != nullptr)); } // [tested]
65
67 void operator++() { Next(); } // [tested]
68
70 void operator--() { Prev(); } // [tested]
71
72 private:
73 friend class plListBase<T>;
74
75 ConstIterator(ListElement* pInit)
76 : m_pElement(pInit)
77 {
78 }
79
80 ListElement* m_pElement;
81 };
82
83public:
85 struct Iterator : public ConstIterator
86 {
87 // this is required to pull in the const version of this function
88 using ConstIterator::operator*;
89 using ConstIterator::operator->;
90
91 PL_DECLARE_POD_TYPE();
92
95 : ConstIterator()
96 {
97 } // [tested]
98
100 T& operator*() { return (this->m_pElement->m_Data); } // [tested]
101
103 T* operator->() { return (&this->m_pElement->m_Data); } // [tested]
104
105 private:
106 friend class plListBase<T>;
107
108 explicit Iterator(ListElement* pInit)
109 : ConstIterator(pInit)
110 {
111 }
112 };
113
114protected:
116 explicit plListBase(plAllocator* pAllocator); // [tested]
117
119 plListBase(const plListBase<T>& cc, plAllocator* pAllocator); // [tested]
120
122 ~plListBase(); // [tested]
123
125 void operator=(const plListBase<T>& cc); // [tested]
126
127public:
129 void Clear(); // [tested]
130
132 void Compact();
133
135 plUInt32 GetCount() const; // [tested]
136
138 bool IsEmpty() const; // [tested]
139
141 const T& PeekFront() const; // [tested]
142
144 const T& PeekBack() const; // [tested]
145
147 T& PeekFront(); // [tested]
148
150 T& PeekBack(); // [tested]
151
153 T& PushBack(); // [tested]
154
156 void PushBack(const T& element); // [tested]
157
159 void PopBack(); // [tested]
160
162 T& PushFront(); // [tested]
163
165 void PushFront(const T& element); // [tested]
166
168 void PopFront(); // [tested]
169
171 void SetCount(plUInt32 uiNewSize); // [tested]
172
174 Iterator Insert(const Iterator& pos, const T& data); // [tested]
175
177 void Insert(const Iterator& pos, ConstIterator first, const ConstIterator& last);
178
180 Iterator Insert(const Iterator& pos);
181
183 Iterator Remove(const Iterator& pos); // [tested]
184
186 Iterator Remove(Iterator first, const Iterator& last);
187
189 Iterator GetIterator(); // [tested]
190
192 Iterator GetEndIterator(); // [tested]
193
195 ConstIterator GetIterator() const; // [tested]
196
198 ConstIterator GetEndIterator() const; // [tested]
199
201 plAllocator* GetAllocator() const { return m_Elements.GetAllocator(); }
202
204 bool operator==(const plListBase<T>& rhs) const; // [tested]
205 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plListBase<T>&);
206
208 plUInt64 GetHeapMemoryUsage() const { return m_Elements.GetHeapMemoryUsage(); } // [tested]
209
210private:
212 ListElementBase m_First;
213
215 ListElementBase m_Last;
216
217 // \brief Small hack to get around const problems.
218 Iterator m_End;
219
221 plUInt32 m_uiCount;
222
224 ListElement* AcquireNode();
225
227 void ReleaseNode(ListElement* pNode);
228
231
233 ListElement* m_pFreeElementStack;
234};
235
237template <typename T, typename AllocatorWrapper = plDefaultAllocatorWrapper>
238class plList : public plListBase<T>
239{
240public:
241 plList();
242 explicit plList(plAllocator* pAllocator);
243
245 plList(const plListBase<T>& other);
246
247 void operator=(const plList<T, AllocatorWrapper>& rhs);
248 void operator=(const plListBase<T>& rhs);
249};
250
251#include <Foundation/Containers/Implementation/List_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition Deque_inl.h:970
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition Deque.h:168
Definition Deque.h:270
A List-class, similar to STL::list.
Definition List.h:12
plUInt32 GetCount() const
Returns the number of elements in the list. O(1) operation.
Definition List_inl.h:127
~plListBase()
Destroys the list and all its content.
Definition List_inl.h:47
void SetCount(plUInt32 uiNewSize)
Sets the number of elements that are in the list.
Definition List_inl.h:307
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition List.h:201
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition List.h:208
Iterator Remove(const Iterator &pos)
Erases the element pointed to by the iterator.
Definition List_inl.h:275
plListBase(plAllocator *pAllocator)
Initializes the list to be empty.
Definition List_inl.h:23
void PopFront()
Removes the very first element from the list.
Definition List_inl.h:220
T & PushFront()
Appends a default-constructed element to the front of the list and returns a reference to it.
Definition List_inl.h:200
void PopBack()
Removes the very last element from the list.
Definition List_inl.h:212
const T & PeekBack() const
Returns the very last element in the list.
Definition List_inl.h:179
void Compact()
See plDeque::Compact()
Definition List_inl.h:149
T & PushBack()
Appends a default-constructed element to the list and returns a reference to it.
Definition List_inl.h:188
void operator=(const plListBase< T > &cc)
Copies the list cc into this list.
Definition List_inl.h:53
const T & PeekFront() const
Returns the very first element in the list.
Definition List_inl.h:171
void Clear()
Clears the list, afterwards it is empty.
Definition List_inl.h:139
Iterator Insert(const Iterator &pos, const T &data)
Inserts one element before the position defined by the iterator.
Definition List_inl.h:245
bool IsEmpty() const
Returns whether size == 0. O(1) operation.
Definition List_inl.h:133
Iterator GetEndIterator()
Returns an iterator pointing behind the last element. Necessary if one wants to insert elements at th...
Definition List_inl.h:109
Iterator GetIterator()
Returns an iterator to the first list-element.
Definition List_inl.h:103
bool operator==(const plListBase< T > &rhs) const
Comparison operator.
Definition List_inl.h:317
Definition List.h:239
A forward-iterator. Allows sequential access from front-to-back.
Definition List.h:86
T * operator->()
Accesses the element stored in the node.
Definition List.h:103
Iterator()
Constructor.
Definition List.h:94
T & operator*()
Accesses the element stored in the node.
Definition List.h:100