TrinityCore
LinkedList.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _LINKEDLIST
19#define _LINKEDLIST
20
21#include "Define.h"
22#include <iterator>
23
24//============================================
25class LinkedListHead;
26
28{
29 private:
30 friend class LinkedListHead;
31
34
35 public:
36 LinkedListElement() : iNext(nullptr), iPrev(nullptr) { }
37
38 bool hasNext() const { return (iNext && iNext->iNext != nullptr); }
39 bool hasPrev() const { return (iPrev && iPrev->iPrev != nullptr); }
40 bool isInList() const { return (iNext != nullptr && iPrev != nullptr); }
41
42 LinkedListElement * next() { return hasNext() ? iNext : nullptr; }
43 LinkedListElement const* next() const { return hasNext() ? iNext : nullptr; }
44 LinkedListElement * prev() { return hasPrev() ? iPrev : nullptr; }
45 LinkedListElement const* prev() const { return hasPrev() ? iPrev : nullptr; }
46
48 LinkedListElement const* nocheck_next() const { return iNext; }
50 LinkedListElement const* nocheck_prev() const { return iPrev; }
51
52 void delink()
53 {
54 if (!isInList())
55 return;
56
57 iNext->iPrev = iPrev;
58 iPrev->iNext = iNext;
59 iNext = nullptr;
60 iPrev = nullptr;
61 }
62
64 {
65 pElem->iNext = this;
66 pElem->iPrev = iPrev;
67 iPrev->iNext = pElem;
68 iPrev = pElem;
69 }
70
72 {
73 pElem->iPrev = this;
74 pElem->iNext = iNext;
75 iNext->iPrev = pElem;
76 iNext = pElem;
77 }
78
79 private:
84
85 protected:
87 {
88 delink();
89 }
90};
91
92//============================================
93
95{
96 private:
100
101 public:
103 {
104 // create empty list
105
106 iFirst.iNext = &iLast;
107 iLast.iPrev = &iFirst;
108 }
109
110 bool isEmpty() const { return(!iFirst.iNext->isInList()); }
111
112 LinkedListElement * getFirst() { return (isEmpty() ? nullptr : iFirst.iNext); }
113 LinkedListElement const* getFirst() const { return (isEmpty() ? nullptr : iFirst.iNext); }
114
115 LinkedListElement * getLast() { return(isEmpty() ? nullptr : iLast.iPrev); }
116 LinkedListElement const* getLast() const { return(isEmpty() ? nullptr : iLast.iPrev); }
117
119 {
120 iFirst.insertAfter(pElem);
121 }
122
124 {
125 iLast.insertBefore(pElem);
126 }
127
129 {
130 if (!iSize)
131 {
132 uint32 result = 0;
133 LinkedListElement const* e = getFirst();
134 while (e)
135 {
136 ++result;
137 e = e->next();
138 }
139 return result;
140 }
141 else
142 return iSize;
143 }
144
145 void incSize() { ++iSize; }
146 void decSize() { --iSize; }
147
148 template<class _Ty>
150 {
151 public:
152 typedef std::bidirectional_iterator_tag iterator_category;
153 typedef _Ty value_type;
154 typedef ptrdiff_t difference_type;
155 typedef ptrdiff_t distance_type;
156 typedef _Ty* pointer;
157 typedef _Ty const* const_pointer;
158 typedef _Ty& reference;
159 typedef _Ty const & const_reference;
160
161 Iterator() : _Ptr(nullptr)
162 { // construct with null node pointer
163 }
164
165 explicit Iterator(pointer _Pnode) : _Ptr(_Pnode)
166 { // construct with node pointer _Pnode
167 }
168
170 {
171 _Ptr = pointer(_Right);
172 return *this;
173 }
174
176 { // return designated value
177 return *_Ptr;
178 }
179
181 { // return pointer to class object
182 return _Ptr;
183 }
184
186 { // preincrement
187 _Ptr = _Ptr->next();
188 return (*this);
189 }
190
192 { // postincrement
193 iterator _Tmp = *this;
194 ++*this;
195 return (_Tmp);
196 }
197
199 { // predecrement
200 _Ptr = _Ptr->prev();
201 return (*this);
202 }
203
205 { // postdecrement
206 iterator _Tmp = *this;
207 --*this;
208 return (_Tmp);
209 }
210
211 bool operator==(Iterator const& _Right) const = default;
212 // test for iterator equality
213
214 protected:
215 pointer _Ptr; // pointer to node
216 };
217
219
220 private:
225
226 protected:
228};
229
230//============================================
231#endif
uint32_t uint32
Definition: Define.h:142
LinkedListElement & operator=(LinkedListElement const &)=delete
LinkedListElement * nocheck_next()
Definition: LinkedList.h:47
LinkedListElement * iPrev
Definition: LinkedList.h:33
void insertAfter(LinkedListElement *pElem)
Definition: LinkedList.h:71
bool hasNext() const
Definition: LinkedList.h:38
LinkedListElement(LinkedListElement &&)=delete
LinkedListElement * prev()
Definition: LinkedList.h:44
LinkedListElement const * next() const
Definition: LinkedList.h:43
LinkedListElement(LinkedListElement const &)=delete
bool hasPrev() const
Definition: LinkedList.h:39
LinkedListElement const * nocheck_next() const
Definition: LinkedList.h:48
LinkedListElement const * prev() const
Definition: LinkedList.h:45
LinkedListElement & operator=(LinkedListElement &&)=delete
LinkedListElement * nocheck_prev()
Definition: LinkedList.h:49
void insertBefore(LinkedListElement *pElem)
Definition: LinkedList.h:63
bool isInList() const
Definition: LinkedList.h:40
LinkedListElement * next()
Definition: LinkedList.h:42
LinkedListElement * iNext
Definition: LinkedList.h:32
LinkedListElement const * nocheck_prev() const
Definition: LinkedList.h:50
_Ty const & const_reference
Definition: LinkedList.h:159
bool operator==(Iterator const &_Right) const =default
Iterator operator++(int)
Definition: LinkedList.h:191
Iterator & operator=(const_pointer const &_Right)
Definition: LinkedList.h:169
Iterator operator--(int)
Definition: LinkedList.h:204
std::bidirectional_iterator_tag iterator_category
Definition: LinkedList.h:152
_Ty const * const_pointer
Definition: LinkedList.h:157
Iterator & operator++()
Definition: LinkedList.h:185
Iterator(pointer _Pnode)
Definition: LinkedList.h:165
Iterator & operator--()
Definition: LinkedList.h:198
void insertLast(LinkedListElement *pElem)
Definition: LinkedList.h:123
LinkedListElement const * getFirst() const
Definition: LinkedList.h:113
LinkedListHead & operator=(LinkedListHead const &)=delete
LinkedListElement * getFirst()
Definition: LinkedList.h:112
void incSize()
Definition: LinkedList.h:145
LinkedListElement const * getLast() const
Definition: LinkedList.h:116
LinkedListElement * getLast()
Definition: LinkedList.h:115
Iterator< LinkedListElement > iterator
Definition: LinkedList.h:218
LinkedListElement iLast
Definition: LinkedList.h:98
uint32 iSize
Definition: LinkedList.h:99
bool isEmpty() const
Definition: LinkedList.h:110
LinkedListHead & operator=(LinkedListHead &&)=delete
uint32 getSize() const
Definition: LinkedList.h:128
LinkedListHead(LinkedListHead &&)=delete
void decSize()
Definition: LinkedList.h:146
LinkedListElement iFirst
Definition: LinkedList.h:97
void insertFirst(LinkedListElement *pElem)
Definition: LinkedList.h:118
LinkedListHead(LinkedListHead const &)=delete