1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRINITYCORE_LINKED_LIST_H
#define TRINITYCORE_LINKED_LIST_H
#include "Define.h"
#include <iterator>
//============================================
class LinkedListHead;
class LinkedListElement
{
private:
friend class LinkedListHead;
LinkedListElement* iNext;
LinkedListElement* iPrev;
public:
LinkedListElement() : iNext(nullptr), iPrev(nullptr) { }
bool isInList() const
{
return iNext != nullptr /*unlinked element*/
&& iNext != this /*list head*/;
}
LinkedListElement * next() { return iNext; }
LinkedListElement const* next() const { return iNext; }
LinkedListElement * prev() { return iPrev; }
LinkedListElement const* prev() const { return iPrev; }
void delink()
{
if (iNext)
iNext->iPrev = iPrev;
if (iPrev)
iPrev->iNext = iNext;
iNext = nullptr;
iPrev = nullptr;
}
void insertBefore(LinkedListElement* pElem)
{
pElem->iNext = this;
pElem->iPrev = iPrev;
iPrev->iNext = pElem;
iPrev = pElem;
}
void insertAfter(LinkedListElement* pElem)
{
pElem->iPrev = this;
pElem->iNext = iNext;
iNext->iPrev = pElem;
iNext = pElem;
}
private:
LinkedListElement(LinkedListElement const&) = delete;
LinkedListElement(LinkedListElement&&) = delete;
LinkedListElement& operator=(LinkedListElement const&) = delete;
LinkedListElement& operator=(LinkedListElement&&) = delete;
protected:
~LinkedListElement()
{
delink();
}
};
//============================================
class LinkedListHead
{
private:
LinkedListElement iHeader;
uint32 iSize;
public:
LinkedListHead(): iSize(0)
{
// create empty list
iHeader.iNext = &iHeader;
iHeader.iPrev = &iHeader;
}
bool empty() const { return iHeader.iNext == &iHeader; }
void push_front(LinkedListElement* pElem)
{
iHeader.iNext->insertBefore(pElem);
}
void push_back(LinkedListElement* pElem)
{
iHeader.insertBefore(pElem);
}
void pop_front()
{
front_impl<LinkedListElement>()->delink();
}
void pop_back()
{
back_impl<LinkedListElement>()->delink();
}
uint32 size() const
{
if (!iSize)
{
uint32 result = 0;
for (auto itr = begin_impl<LinkedListElement>(); itr != end_impl<LinkedListElement>(); ++itr)
++result;
return result;
}
else
return iSize;
}
void incSize() { ++iSize; }
void decSize() { --iSize; }
template <typename _Ty>
class Iterator
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = _Ty;
using difference_type = ptrdiff_t;
using base_pointer = std::conditional_t<std::is_const_v<_Ty>, LinkedListElement const, LinkedListElement>*;
using pointer = _Ty*;
using reference = _Ty&;
Iterator() : _Ptr(nullptr)
{ // construct with null node pointer
}
explicit Iterator(base_pointer _Pnode) : _Ptr(_Pnode)
{ // construct with node pointer _Pnode
}
reference operator*() const
{ // return designated value
return static_cast<reference>(*_Ptr);
}
pointer operator->() const
{ // return pointer to class object
return static_cast<pointer>(_Ptr);
}
base_pointer node() const
{
return _Ptr;
}
Iterator& operator++()
{ // preincrement
_Ptr = _Ptr->next();
return (*this);
}
Iterator operator++(int)
{ // postincrement
Iterator _Tmp = *this;
++*this;
return (_Tmp);
}
Iterator& operator--()
{ // predecrement
_Ptr = _Ptr->prev();
return (*this);
}
Iterator operator--(int)
{ // postdecrement
Iterator _Tmp = *this;
--*this;
return (_Tmp);
}
bool operator==(Iterator const& _Right) const = default;
// test for iterator equality
protected:
base_pointer _Ptr; // pointer to node
};
protected:
template <typename T>
T* front_impl() { return static_cast<T*>(iHeader.iNext); }
template <typename T>
T const* front_impl() const { return static_cast<T const*>(iHeader.iNext); }
template <typename T>
T* back_impl() { return static_cast<T*>(iHeader.iPrev); }
template <typename T>
T const* back_impl() const { return static_cast<T const*>(iHeader.iPrev); }
template <typename T>
Iterator<T> begin_impl() { return Iterator<T>(iHeader.iNext); }
template <typename T>
Iterator<T const> begin_impl() const { return Iterator<T const>(iHeader.iNext); }
template <typename T>
Iterator<T> end_impl() { return Iterator<T>(&iHeader); }
template <typename T>
Iterator<T const> end_impl() const { return Iterator<T const>(&iHeader); }
void splice_impl(LinkedListElement* where, LinkedListElement* first, LinkedListElement* last)
{
LinkedListElement* wherePrev = where->iPrev;
LinkedListElement* firstPrev = first->iPrev;
LinkedListElement* lastPrev = last->iPrev;
lastPrev->iNext = where;
where->iPrev = lastPrev;
firstPrev->iNext = last;
last->iPrev = firstPrev;
wherePrev->iNext = first;
first->iPrev = wherePrev;
}
template <typename T>
void splice_impl(Iterator<T> where, Iterator<T> first, Iterator<T> last)
{
splice_impl(where.node(), first.node(), last.node());
}
private:
LinkedListHead(LinkedListHead const&) = delete;
LinkedListHead(LinkedListHead&&) = delete;
LinkedListHead& operator=(LinkedListHead const&) = delete;
LinkedListHead& operator=(LinkedListHead&&) = delete;
protected:
~LinkedListHead() { }
};
//============================================
#endif
|