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 _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:
82
83 protected:
85 {
86 delink();
87 }
88};
89
90//============================================
91
93{
94 private:
98
99 public:
101 {
102 // create empty list
103
104 iFirst.iNext = &iLast;
105 iLast.iPrev = &iFirst;
106 }
107
108 bool isEmpty() const { return(!iFirst.iNext->isInList()); }
109
110 LinkedListElement * getFirst() { return (isEmpty() ? nullptr : iFirst.iNext); }
111 LinkedListElement const* getFirst() const { return (isEmpty() ? nullptr : iFirst.iNext); }
112
113 LinkedListElement * getLast() { return(isEmpty() ? nullptr : iLast.iPrev); }
114 LinkedListElement const* getLast() const { return(isEmpty() ? nullptr : iLast.iPrev); }
115
117 {
118 iFirst.insertAfter(pElem);
119 }
120
122 {
123 iLast.insertBefore(pElem);
124 }
125
127 {
128 if (!iSize)
129 {
130 uint32 result = 0;
131 LinkedListElement const* e = getFirst();
132 while (e)
133 {
134 ++result;
135 e = e->next();
136 }
137 return result;
138 }
139 else
140 return iSize;
141 }
142
143 void incSize() { ++iSize; }
144 void decSize() { --iSize; }
145
146 template<class _Ty>
148 {
149 public:
150 typedef std::bidirectional_iterator_tag iterator_category;
151 typedef _Ty value_type;
152 typedef ptrdiff_t difference_type;
153 typedef ptrdiff_t distance_type;
154 typedef _Ty* pointer;
155 typedef _Ty const* const_pointer;
156 typedef _Ty& reference;
157 typedef _Ty const & const_reference;
158
159 Iterator() : _Ptr(nullptr)
160 { // construct with null node pointer
161 }
162
163 explicit Iterator(pointer _Pnode) : _Ptr(_Pnode)
164 { // construct with node pointer _Pnode
165 }
166
168 {
169 _Ptr = pointer(_Right);
170 return *this;
171 }
172
174 { // return designated value
175 return *_Ptr;
176 }
177
179 { // return pointer to class object
180 return _Ptr;
181 }
182
184 { // preincrement
185 _Ptr = _Ptr->next();
186 return (*this);
187 }
188
190 { // postincrement
191 iterator _Tmp = *this;
192 ++*this;
193 return (_Tmp);
194 }
195
197 { // predecrement
198 _Ptr = _Ptr->prev();
199 return (*this);
200 }
201
203 { // postdecrement
204 iterator _Tmp = *this;
205 --*this;
206 return (_Tmp);
207 }
208
209 bool operator==(Iterator const& _Right) const = default;
210 // test for iterator equality
211
212 protected:
213 pointer _Ptr; // pointer to node
214 };
215
217
218 private:
221
222 protected:
224};
225
226//============================================
227#endif
uint32_t uint32
Definition Define.h:133
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 * 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 * 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:157
bool operator==(Iterator const &_Right) const =default
Iterator operator++(int)
Definition LinkedList.h:189
Iterator & operator=(const_pointer const &_Right)
Definition LinkedList.h:167
Iterator operator--(int)
Definition LinkedList.h:202
std::bidirectional_iterator_tag iterator_category
Definition LinkedList.h:150
Iterator(pointer _Pnode)
Definition LinkedList.h:163
void insertLast(LinkedListElement *pElem)
Definition LinkedList.h:121
LinkedListElement const * getFirst() const
Definition LinkedList.h:111
LinkedListHead & operator=(LinkedListHead const &)=delete
LinkedListElement * getFirst()
Definition LinkedList.h:110
LinkedListElement const * getLast() const
Definition LinkedList.h:114
LinkedListElement * getLast()
Definition LinkedList.h:113
Iterator< LinkedListElement > iterator
Definition LinkedList.h:216
LinkedListElement iLast
Definition LinkedList.h:96
bool isEmpty() const
Definition LinkedList.h:108
uint32 getSize() const
Definition LinkedList.h:126
LinkedListElement iFirst
Definition LinkedList.h:95
void insertFirst(LinkedListElement *pElem)
Definition LinkedList.h:116
LinkedListHead(LinkedListHead const &)=delete