Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
irrList.hpp
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in nirtcpp/nirtcpp.hpp
4
5#ifndef NIRT_LIST_HPP_INCLUDED
6#define NIRT_LIST_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/nirt_types.hpp>
9#include <nirtcpp/core/engine/irrAllocator.hpp>
10#include <nirtcpp/core/engine/irrMath.hpp>
11
12namespace nirt
13{
14namespace core
15{
16
17
19template <class T>
20class list
21{
22private:
23
25 class SKListNode
26 {
27 public:
28 SKListNode(const T& e) : Next(0), Prev(0), Element(e) {}
29
30 SKListNode* Next;
31 SKListNode* Prev;
32 T Element;
33 };
34
35public:
36 class ConstIterator;
37
40 {
41 public:
42 Iterator() : Current(0) {}
43
44 Iterator& operator ++() { Current = Current->Next; return *this; }
45 Iterator& operator --() { Current = Current->Prev; return *this; }
46 Iterator operator ++(s32) { Iterator tmp = *this; Current = Current->Next; return tmp; }
47 Iterator operator --(s32) { Iterator tmp = *this; Current = Current->Prev; return tmp; }
48
50 {
51 if(num > 0)
52 {
53 while (num-- && this->Current != 0) ++(*this);
54 }
55 else
56 {
57 while(num++ && this->Current != 0) --(*this);
58 }
59 return *this;
60 }
61
62 Iterator operator + (s32 num) const { Iterator tmp = *this; return tmp += num; }
63 Iterator& operator -=(s32 num) { return (*this)+=(-num); }
64 Iterator operator - (s32 num) const { return (*this)+ (-num); }
65
66 bool operator ==(const Iterator& other) const { return Current == other.Current; }
67 bool operator !=(const Iterator& other) const { return Current != other.Current; }
68 bool operator ==(const ConstIterator& other) const { return Current == other.Current; }
69 bool operator !=(const ConstIterator& other) const { return Current != other.Current; }
70
71 T & operator * () { return Current->Element; }
72 T * operator ->() { return &Current->Element; }
73
74 private:
75 explicit Iterator(SKListNode* begin) : Current(begin) {}
76
77 SKListNode* Current;
78
79 friend class list<T>;
80 friend class ConstIterator;
81 };
82
85 {
86 public:
87
88 ConstIterator() : Current(0) {}
89 ConstIterator(const Iterator& iter) : Current(iter.Current) {}
90
91 ConstIterator& operator ++() { Current = Current->Next; return *this; }
92 ConstIterator& operator --() { Current = Current->Prev; return *this; }
93 ConstIterator operator ++(s32) { ConstIterator tmp = *this; Current = Current->Next; return tmp; }
94 ConstIterator operator --(s32) { ConstIterator tmp = *this; Current = Current->Prev; return tmp; }
95
97 {
98 if(num > 0)
99 {
100 while(num-- && this->Current != 0) ++(*this);
101 }
102 else
103 {
104 while(num++ && this->Current != 0) --(*this);
105 }
106 return *this;
107 }
108
109 ConstIterator operator + (s32 num) const { ConstIterator tmp = *this; return tmp += num; }
110 ConstIterator& operator -=(s32 num) { return (*this)+=(-num); }
111 ConstIterator operator - (s32 num) const { return (*this)+ (-num); }
112
113 bool operator ==(const ConstIterator& other) const { return Current == other.Current; }
114 bool operator !=(const ConstIterator& other) const { return Current != other.Current; }
115 bool operator ==(const Iterator& other) const { return Current == other.Current; }
116 bool operator !=(const Iterator& other) const { return Current != other.Current; }
117
118 const T & operator * () { return Current->Element; }
119 const T * operator ->() { return &Current->Element; }
120
121 ConstIterator & operator =(const Iterator & iterator) { Current = iterator.Current; return *this; }
122
123 private:
124 explicit ConstIterator(SKListNode* begin) : Current(begin) {}
125
126 SKListNode* Current;
127
128 friend class Iterator;
129 friend class list<T>;
130 };
131
134 : First(0), Last(0), Size(0) {}
135
136
138 list(const list<T>& other) : First(0), Last(0), Size(0)
139 {
140 *this = other;
141 }
142
143
146 {
147 clear();
148 }
149
150
153 {
154 if(&other == this)
155 {
156 return;
157 }
158
159 clear();
160
161 SKListNode* node = other.First;
162 while(node)
163 {
164 push_back(node->Element);
165 node = node->Next;
166 }
167 }
168
169
171
172 u32 size() const
173 {
174 return Size;
175 }
176 u32 getSize() const
177 {
178 return Size;
179 }
180
181
183
184 void clear()
185 {
186 while(First)
187 {
188 SKListNode * next = First->Next;
189 allocator.destruct(First);
190 allocator.deallocate(First);
191 First = next;
192 }
193
194 //First = 0; handled by loop
195 Last = 0;
196 Size = 0;
197 }
198
199
201
202 bool empty() const
203 {
204 return (First == 0);
205 }
206
207
209
210 void push_back(const T& element)
211 {
212 SKListNode* node = allocator.allocate(1);
213 allocator.construct(node, element);
214
215 ++Size;
216
217 if (First == 0)
218 First = node;
219
220 node->Prev = Last;
221
222 if (Last != 0)
223 Last->Next = node;
224
225 Last = node;
226 }
227
228
230
231 void push_front(const T& element)
232 {
233 SKListNode* node = allocator.allocate(1);
234 allocator.construct(node, element);
235
236 ++Size;
237
238 if (First == 0)
239 {
240 Last = node;
241 First = node;
242 }
243 else
244 {
245 node->Next = First;
246 First->Prev = node;
247 First = node;
248 }
249 }
250
251
253
255 {
256 return Iterator(First);
257 }
258
259
261
263 {
264 return ConstIterator(First);
265 }
266
267
269
271 {
272 return Iterator(0);
273 }
274
275
277
279 {
280 return ConstIterator(0);
281 }
282
283
285
287 {
288 return Iterator(Last);
289 }
290
291
293
295 {
296 return ConstIterator(Last);
297 }
298
299
301
305 void insert_after(const Iterator& it, const T& element)
306 {
307 SKListNode* node = allocator.allocate(1);
308 allocator.construct(node, element);
309
310 node->Next = it.Current->Next;
311
312 if (it.Current->Next)
313 it.Current->Next->Prev = node;
314
315 node->Prev = it.Current;
316 it.Current->Next = node;
317 ++Size;
318
319 if (it.Current == Last)
320 Last = node;
321 }
322
323
325
329 void insert_before(const Iterator& it, const T& element)
330 {
331 SKListNode* node = allocator.allocate(1);
332 allocator.construct(node, element);
333
334 node->Prev = it.Current->Prev;
335
336 if (it.Current->Prev)
337 it.Current->Prev->Next = node;
338
339 node->Next = it.Current;
340 it.Current->Prev = node;
341 ++Size;
342
343 if (it.Current == First)
344 First = node;
345 }
346
347
349
352 {
353 // suggest changing this to a const Iterator& and
354 // working around line: it.Current = 0 (possibly with a mutable, or just let it be garbage?)
355
358
359 if(it.Current == First)
360 {
361 First = it.Current->Next;
362 }
363 else
364 {
365 it.Current->Prev->Next = it.Current->Next;
366 }
367
368 if(it.Current == Last)
369 {
370 Last = it.Current->Prev;
371 }
372 else
373 {
374 it.Current->Next->Prev = it.Current->Prev;
375 }
376
377 allocator.destruct(it.Current);
378 allocator.deallocate(it.Current);
379 it.Current = 0;
380 --Size;
381
382 return returnIterator;
383 }
384
386
391 {
392 core::swap(First, other.First);
393 core::swap(Last, other.Last);
394 core::swap(Size, other.Size);
395 core::swap(allocator, other.allocator); // memory is still released by the same allocator used for allocation
396 }
397
398 using value_type = T;
399 using size_type = u32;
400
401private:
402
403 SKListNode* First;
404 SKListNode* Last;
405 u32 Size;
406 irrAllocator<SKListNode> allocator;
407
408};
409
410
411} // end namespace core
412}// end namespace nirt
413
414#endif
415
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
void deallocate(T *ptr)
Deallocate memory for an array of objects.
Definition irrAllocator.hpp:39
T * allocate(size_t cnt)
Allocate memory for an array of objects.
Definition irrAllocator.hpp:33
void destruct(T *ptr)
Destruct an element.
Definition irrAllocator.hpp:51
void construct(T *ptr, const T &e)
Construct an element.
Definition irrAllocator.hpp:45
List iterator for const access.
Definition irrList.hpp:85
List iterator.
Definition irrList.hpp:40
Doubly linked list template.
Definition irrList.hpp:21
list(const list< T > &other)
Copy constructor.
Definition irrList.hpp:138
list()
Default constructor for empty list.
Definition irrList.hpp:133
void insert_before(const Iterator &it, const T &element)
Inserts an element before an element.
Definition irrList.hpp:329
ConstIterator end() const
Gets end node.
Definition irrList.hpp:278
ConstIterator begin() const
Gets first node.
Definition irrList.hpp:262
Iterator erase(Iterator &it)
Erases an element.
Definition irrList.hpp:351
Iterator end()
Gets end node.
Definition irrList.hpp:270
void clear()
Clears the list, deletes all elements in the list.
Definition irrList.hpp:184
Iterator getLast()
Gets last element.
Definition irrList.hpp:286
ConstIterator getLast() const
Gets last element.
Definition irrList.hpp:294
void push_back(const T &element)
Adds an element at the end of the list.
Definition irrList.hpp:210
void push_front(const T &element)
Adds an element at the begin of the list.
Definition irrList.hpp:231
Iterator begin()
Gets first node.
Definition irrList.hpp:254
bool empty() const
Checks for empty list.
Definition irrList.hpp:202
u32 size() const
Returns amount of elements in list.
Definition irrList.hpp:172
~list()
Destructor.
Definition irrList.hpp:145
void insert_after(const Iterator &it, const T &element)
Inserts an element after an element.
Definition irrList.hpp:305
void swap(list< T > &other)
Swap the content of this list container with the content of another list.
Definition irrList.hpp:390
void operator=(const list< T > &other)
Assignment operator.
Definition irrList.hpp:152
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.hpp:175
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64

Nirtcpp    @cppfx.xyz

Esvcpp    esv::print