TrinityCore
Loading...
Searching...
No Matches
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 TRINITYCORE_LINKED_LIST_H
19#define TRINITYCORE_LINKED_LIST_H
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 isInList() const
39 {
40 return iNext != nullptr /*unlinked element*/
41 && iNext != this /*list head*/;
42 }
43
45 LinkedListElement const* next() const { return iNext; }
47 LinkedListElement const* prev() const { return iPrev; }
48
49 void delink()
50 {
51 if (iNext)
52 iNext->iPrev = iPrev;
53
54 if (iPrev)
55 iPrev->iNext = iNext;
56
57 iNext = nullptr;
58 iPrev = nullptr;
59 }
60
62 {
63 pElem->iNext = this;
64 pElem->iPrev = iPrev;
65 iPrev->iNext = pElem;
66 iPrev = pElem;
67 }
68
70 {
71 pElem->iPrev = this;
72 pElem->iNext = iNext;
73 iNext->iPrev = pElem;
74 iNext = pElem;
75 }
76
77 private:
82
83 protected:
85 {
86 delink();
87 }
88};
89
90//============================================
91
93{
94 private:
97
98 public:
100 {
101 // create empty list
102
105 }
106
107 bool empty() const { return iHeader.iNext == &iHeader; }
108
110 {
111 iHeader.iNext->insertBefore(pElem);
112 }
113
115 {
116 iHeader.insertBefore(pElem);
117 }
118
120 {
121 front_impl<LinkedListElement>()->delink();
122 }
123
124 void pop_back()
125 {
126 back_impl<LinkedListElement>()->delink();
127 }
128
129 uint32 size() const
130 {
131 if (!iSize)
132 {
133 uint32 result = 0;
134 for (auto itr = begin_impl<LinkedListElement>(); itr != end_impl<LinkedListElement>(); ++itr)
135 ++result;
136
137 return result;
138 }
139 else
140 return iSize;
141 }
142
143 void incSize() { ++iSize; }
144 void decSize() { --iSize; }
145
146 template <typename _Ty>
148 {
149 public:
150 using iterator_category = std::bidirectional_iterator_tag;
151 using value_type = _Ty;
152 using difference_type = ptrdiff_t;
153 using base_pointer = std::conditional_t<std::is_const_v<_Ty>, LinkedListElement const, LinkedListElement>*;
154 using pointer = _Ty*;
155 using reference = _Ty&;
156
157 Iterator() : _Ptr(nullptr)
158 { // construct with null node pointer
159 }
160
161 explicit Iterator(base_pointer _Pnode) : _Ptr(_Pnode)
162 { // construct with node pointer _Pnode
163 }
164
166 { // return designated value
167 return static_cast<reference>(*_Ptr);
168 }
169
171 { // return pointer to class object
172 return static_cast<pointer>(_Ptr);
173 }
174
176 {
177 return _Ptr;
178 }
179
181 { // preincrement
182 _Ptr = _Ptr->next();
183 return (*this);
184 }
185
187 { // postincrement
188 Iterator _Tmp = *this;
189 ++*this;
190 return (_Tmp);
191 }
192
194 { // predecrement
195 _Ptr = _Ptr->prev();
196 return (*this);
197 }
198
200 { // postdecrement
201 Iterator _Tmp = *this;
202 --*this;
203 return (_Tmp);
204 }
205
206 bool operator==(Iterator const& _Right) const = default;
207 // test for iterator equality
208
209 protected:
210 base_pointer _Ptr; // pointer to node
211 };
212
213 protected:
214 template <typename T>
215 T* front_impl() { return static_cast<T*>(iHeader.iNext); }
216
217 template <typename T>
218 T const* front_impl() const { return static_cast<T const*>(iHeader.iNext); }
219
220 template <typename T>
221 T* back_impl() { return static_cast<T*>(iHeader.iPrev); }
222
223 template <typename T>
224 T const* back_impl() const { return static_cast<T const*>(iHeader.iPrev); }
225
226 template <typename T>
228
229 template <typename T>
231
232 template <typename T>
234
235 template <typename T>
237
239 {
240 LinkedListElement* wherePrev = where->iPrev;
241 LinkedListElement* firstPrev = first->iPrev;
242 LinkedListElement* lastPrev = last->iPrev;
243 lastPrev->iNext = where;
244 where->iPrev = lastPrev;
245 firstPrev->iNext = last;
246 last->iPrev = firstPrev;
247 wherePrev->iNext = first;
248 first->iPrev = wherePrev;
249 }
250
251 template <typename T>
253 {
254 splice_impl(where.node(), first.node(), last.node());
255 }
256
257 private:
262
263 protected:
265};
266
267//============================================
268#endif
uint32_t uint32
Definition Define.h:154
LinkedListElement & operator=(LinkedListElement const &)=delete
LinkedListElement * iPrev
Definition LinkedList.h:33
void insertAfter(LinkedListElement *pElem)
Definition LinkedList.h:69
LinkedListElement(LinkedListElement &&)=delete
LinkedListElement * prev()
Definition LinkedList.h:46
LinkedListElement const * next() const
Definition LinkedList.h:45
LinkedListElement(LinkedListElement const &)=delete
LinkedListElement const * prev() const
Definition LinkedList.h:47
LinkedListElement & operator=(LinkedListElement &&)=delete
void insertBefore(LinkedListElement *pElem)
Definition LinkedList.h:61
bool isInList() const
Definition LinkedList.h:38
LinkedListElement * next()
Definition LinkedList.h:44
LinkedListElement * iNext
Definition LinkedList.h:32
bool operator==(Iterator const &_Right) const =default
std::bidirectional_iterator_tag iterator_category
Definition LinkedList.h:150
Iterator operator++(int)
Definition LinkedList.h:186
base_pointer node() const
Definition LinkedList.h:175
Iterator(base_pointer _Pnode)
Definition LinkedList.h:161
std::conditional_t< std::is_const_v< _Ty >, LinkedListElement const, LinkedListElement > * base_pointer
Definition LinkedList.h:153
Iterator operator--(int)
Definition LinkedList.h:199
reference operator*() const
Definition LinkedList.h:165
pointer operator->() const
Definition LinkedList.h:170
uint32 size() const
Definition LinkedList.h:129
Iterator< T const > begin_impl() const
Definition LinkedList.h:230
LinkedListHead & operator=(LinkedListHead const &)=delete
LinkedListElement iHeader
Definition LinkedList.h:95
void splice_impl(Iterator< T > where, Iterator< T > first, Iterator< T > last)
Definition LinkedList.h:252
T const * back_impl() const
Definition LinkedList.h:224
Iterator< T > begin_impl()
Definition LinkedList.h:227
Iterator< T > end_impl()
Definition LinkedList.h:233
bool empty() const
Definition LinkedList.h:107
LinkedListHead & operator=(LinkedListHead &&)=delete
void push_back(LinkedListElement *pElem)
Definition LinkedList.h:114
void splice_impl(LinkedListElement *where, LinkedListElement *first, LinkedListElement *last)
Definition LinkedList.h:238
Iterator< T const > end_impl() const
Definition LinkedList.h:236
void push_front(LinkedListElement *pElem)
Definition LinkedList.h:109
LinkedListHead(LinkedListHead &&)=delete
T const * front_impl() const
Definition LinkedList.h:218
LinkedListHead(LinkedListHead const &)=delete