cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TList.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
27
28namespace cppcore {
29
30//-------------------------------------------------------------------------------------------------
35//-------------------------------------------------------------------------------------------------
36template <class T, class TAlloc = TDefaultAllocator<T>>
37class TList {
38 // Forward declarations
39 class Node;
40
41public:
42 class Iterator;
43
45 TList() noexcept = default;
46
49 TList(const TList<T, TAlloc> &rhs);
50
52 ~TList();
53
56 void copyFrom(const TList<T, TAlloc> &rOther);
57
61 Iterator addFront(const T &item);
62
66 Iterator addBack(const T &item);
67
69 void removeFront();
70
72 void removeBack();
73
76 T &front() const;
77
80 T &back() const;
81
84 size_t size() const;
85
88 bool isEmpty() const;
89
91 void clear();
92
95 Iterator begin() const;
96
99 Iterator end() const;
100
102 bool operator == (const TList<T, TAlloc> &rOther) const;
103
105 TList<T, TAlloc> &operator = (const TList<T, TAlloc> &rOther);
106
107 //---------------------------------------------------------------------------------------------
110 //---------------------------------------------------------------------------------------------
111 class Iterator {
112 public:
114 Iterator();
116 Iterator(Node *pNode);
118 Iterator(const Iterator &rOther);
120 ~Iterator() = default;
122 Iterator &operator=(const Iterator &rOther);
124 bool operator==(const Iterator &rOther) const;
126 bool operator!=(const Iterator &rOther) const;
128 const Iterator &operator++(int);
132 const Iterator &operator--(int);
136 T *operator->() const;
138 T &operator*() const;
139
140 private:
141 Node *mNode{nullptr};
142 };
143
144private:
145 class Node {
146 public:
147 T mItem{};
148 Node *mPrev{nullptr};
149 Node *mNext{nullptr};
150
151 Node(const T &rItem);
152 void setNext(Node *pNode);
153 Node *getNext() const;
154 void setPrev(Node *pNode);
155 Node *getPrev() const;
156 T &getItem() const;
157 };
158
159 Node *mFist{nullptr};
160 Node *mLast{nullptr};
161 size_t mSize{0};
162};
163
164template <class T, class TAlloc>
166 mFist(nullptr),
167 mLast(nullptr),
168 mSize(0) {
169 copyFrom(rhs);
170}
171
172template <class T, class TAlloc>
174 clear();
175}
176
177template <class T, class TAlloc>
179 clear();
180 Node *current = rhs.mFist;
181 if (nullptr == current) {
182 return;
183 }
184
185 Node *prevNode = nullptr;
186 for (Iterator it = rhs.begin(); it != rhs.end(); ++it) {
187 mLast = new Node(current->mItem);
188 if (!mFist) {
189 mFist = mLast;
190 }
191
192 if (nullptr != prevNode) {
193 mLast->setPrev(prevNode);
194 prevNode->setNext(mLast);
195 }
196 prevNode = mLast;
197 current = current->getNext();
198 }
199
200 mSize = rhs.mSize;
201}
202
203template <class T, class TAlloc>
205 Node *node = new Node(item);
206 if (nullptr == mFist) {
207 mFist = node;
208 mLast = node;
209 } else {
210 node->setNext(mFist);
211 mFist->setPrev(node);
212 mFist = node;
213 }
214 ++mSize;
215
216 return Iterator(node);
217}
218
219template <class T, class TAlloc>
221 Node *node = new Node(item);
222 if (nullptr == mFist) {
223 mFist = node;
224 mLast = node;
225 } else {
226 node->setPrev(mLast);
227 mLast->setNext(node);
228 mLast = node;
229 }
230 ++mSize;
231
232 return Iterator(node);
233}
234
235template <class T, class TAlloc>
237 Node *tmp = mFist->getNext();
238 if (tmp) {
239 tmp->setPrev(nullptr);
240 }
241
242 delete mFist;
243 mFist = tmp;
244 --mSize;
245}
246
247template <class T, class TAlloc>
249 Node *last = mLast->getPrev();
250 last->setNext(nullptr);
251 delete mLast;
252 mLast = last;
253 --mSize;
254}
255
256template <class T, class TAlloc>
257inline T &TList<T, TAlloc>::front() const {
258 assert(nullptr != mFist);
259
260 return (mFist->mItem);
261}
262
263template <class T, class TAlloc>
264inline T &TList<T, TAlloc>::back() const {
265 assert(nullptr != mLast);
266
267 return (mLast->mItem);
268}
269
270template <class T, class TAlloc>
271inline size_t TList<T, TAlloc>::size() const {
272 return mSize;
273}
274
275template <class T, class TAlloc>
276inline bool TList<T, TAlloc>::isEmpty() const {
277 return (0u == mSize);
278}
279
280template <class T, class TAlloc>
282 if (isEmpty()) {
283 return;
284 }
285
286 Node *current = mFist;
287 while (mFist->getNext()) {
288 current = mFist;
289 mFist = mFist->getNext();
290 delete current;
291 }
292 delete mFist;
293 mFist = nullptr;
294 mLast = nullptr;
295 mSize = 0;
296}
297
298template <class T, class TAlloc>
300 if (isEmpty()) {
301 return end();
302 }
303
304 return Iterator(mFist);
305}
306
307template <class T, class TAlloc>
309 return 0;
310}
311
312template <class T, class TAlloc>
314 if (mSize != rhs.mSize) {
315 return false;
316 }
317
318 Iterator otherIt = rhs.begin();
319 for (Iterator it = begin(); it != end(); ++it) {
320 if (!(*it == *otherIt)) {
321 return false;
322 }
323 }
324
325 return true;
326}
327
328template <class T, class TAlloc>
330 if (*this == rhs) {
331 return *this;
332 }
333
334 clear();
335 for (Iterator it = rhs.begin(); it != rhs.end(); ++it) {
336 addBack(*it);
337 }
338
339 return *this;
340}
341
342template <class T, class TAlloc>
343inline TList<T, TAlloc>::Node::Node(const T &item) :
344 mItem(item),
345 mPrev(nullptr),
346 mNext(nullptr) {
347 // empty
348}
349
350template <class T, class TAlloc>
351inline void TList<T, TAlloc>::Node::setNext(Node *pNode) {
352 mNext = pNode;
353}
354
355template <class T, class TAlloc>
357 return mNext;
358}
359
360template <class T, class TAlloc>
361inline void TList<T, TAlloc>::Node::setPrev(Node *node) {
362 mPrev = node;
363}
364
365template <class T, class TAlloc>
367 return mPrev;
368}
369
370template <class T, class TAlloc>
371inline T &TList<T, TAlloc>::Node::getItem() const {
372 return mItem;
373}
374
375template <class T, class TAlloc>
377 mNode(nullptr) {
378 // empty
379}
380
381template <class T, class TAlloc>
383 mNode(node) {
384 // empty
385}
386
387template <class T, class TAlloc>
389 mNode(rhs.mNode) {
390 // empty
391}
392
393template <class T, class TAlloc>
395 if (*this == rhs) {
396 return *this;
397 }
398
399 mNode = rhs.mNode;
400
401 return *this;
402}
403
404template <class T, class TAlloc>
406 return (mNode == rhs.mNode);
407}
408
409template <class T, class TAlloc>
411 return (mNode != rhs.mNode);
412}
413
414template <class T, class TAlloc>
416 assert(nullptr != mNode);
417
418 Iterator inst(mNode);
419 mNode = mNode->getNext();
420
421 return inst;
422}
423
424template <class T, class TAlloc>
426 assert(nullptr != mNode);
427
428 mNode = mNode->getNext();
429
430 return *this;
431}
432
433template <class T, class TAlloc>
435 assert(nullptr != mNode);
436
437 Iterator inst(mNode);
438 mNode = mNode->getPrev();
439
440 return inst;
441}
442
443template <class T, class TAlloc>
445 assert(nullptr != mNode);
446
447 mNode = mNode->getPrev();
448
449 return *this;
450}
451
452template <class T, class TAlloc>
454 assert(nullptr != mNode);
455
456 return &(mNode->mItem);
457}
458
459template <class T, class TAlloc>
461 assert(nullptr != mNode);
462
463 return mNode->mItem;
464}
465
466} // Namespace cppcore
Access implementation class.
Definition TList.h:111
Iterator & operator--()
The pre-decrement operator.
Definition TList.h:444
bool operator==(const Iterator &rOther) const
The compare operator.
Definition TList.h:405
~Iterator()=default
The destructor, non virtual.
bool operator!=(const Iterator &rOther) const
The not equal operator.
Definition TList.h:410
T & operator*() const
The dereference operator.
Definition TList.h:460
Iterator & operator=(const Iterator &rOther)
The assignment operator.
Definition TList.h:394
const Iterator & operator++(int)
The post increment operator.
Definition TList.h:415
T * operator->() const
The -> operator.
Definition TList.h:453
Iterator()
The default class constructor.
Definition TList.h:376
Iterator & operator++()
The pre-increment operator.
Definition TList.h:425
const Iterator & operator--(int)
The post decrement operator.
Definition TList.h:434
Iterator begin() const
Returns the iterator showing to the first entry.
Definition TList.h:299
Iterator addBack(const T &item)
Adds a new item at the back of the list.
Definition TList.h:220
TList< T, TAlloc > & operator=(const TList< T, TAlloc > &rOther)
Assignment operator.
Definition TList.h:329
void removeFront()
The first item will be removed.
Definition TList.h:236
Iterator end() const
Returns the iterator showing to the last entry.
Definition TList.h:308
T & front() const
Returns the first item.
Definition TList.h:257
void removeBack()
The hast item will be removed.
Definition TList.h:248
T & back() const
Returns the last item.
Definition TList.h:264
Iterator addFront(const T &item)
Adds a new item at the front of the list.
Definition TList.h:204
bool operator==(const TList< T, TAlloc > &rOther) const
Compare operator.
Definition TList.h:313
TList() noexcept=default
The class default constructor.
void clear()
The list will be cleared.
Definition TList.h:281
~TList()
The class destructor.
Definition TList.h:173
bool isEmpty() const
Returns true, if the list is empty.
Definition TList.h:276
void copyFrom(const TList< T, TAlloc > &rOther)
Copy the data from a given instance.
Definition TList.h:178
size_t size() const
The number of stored items will be returned.
Definition TList.h:271