TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LinkedList.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _LINKEDLIST
20 #define _LINKEDLIST
21 
22 #include "Define.h"
23 #include <iterator>
24 
25 //============================================
26 class LinkedListHead;
27 
29 {
30  private:
31  friend class LinkedListHead;
32 
35  public:
36  LinkedListElement() : iNext(NULL), iPrev(NULL) { }
37  virtual ~LinkedListElement() { delink(); }
38 
39  bool hasNext() const { return(iNext && iNext->iNext != NULL); }
40  bool hasPrev() const { return(iPrev && iPrev->iPrev != NULL); }
41  bool isInList() const { return(iNext != NULL && iPrev != NULL); }
42 
43  LinkedListElement * next() { return hasNext() ? iNext : NULL; }
44  LinkedListElement const* next() const { return hasNext() ? iNext : NULL; }
45  LinkedListElement * prev() { return hasPrev() ? iPrev : NULL; }
46  LinkedListElement const* prev() const { return hasPrev() ? iPrev : NULL; }
47 
49  LinkedListElement const* nocheck_next() const { return iNext; }
51  LinkedListElement const* nocheck_prev() const { return iPrev; }
52 
53  void delink()
54  {
55  if (isInList())
56  {
57  iNext->iPrev = iPrev; iPrev->iNext = iNext; iNext = NULL; iPrev = NULL;
58  }
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:
80 };
81 
82 //============================================
83 
85 {
86  private:
90 
91  public:
92  LinkedListHead(): iSize(0)
93  {
94  // create empty list
95 
96  iFirst.iNext = &iLast;
97  iLast.iPrev = &iFirst;
98  }
99 
100  virtual ~LinkedListHead() { }
101 
102  bool isEmpty() const { return(!iFirst.iNext->isInList()); }
103 
104  LinkedListElement * getFirst() { return(isEmpty() ? NULL : iFirst.iNext); }
105  LinkedListElement const* getFirst() const { return(isEmpty() ? NULL : iFirst.iNext); }
106 
107  LinkedListElement * getLast() { return(isEmpty() ? NULL : iLast.iPrev); }
108  LinkedListElement const* getLast() const { return(isEmpty() ? NULL : iLast.iPrev); }
109 
111  {
112  iFirst.insertAfter(pElem);
113  }
114 
116  {
117  iLast.insertBefore(pElem);
118  }
119 
120  uint32 getSize() const
121  {
122  if (!iSize)
123  {
124  uint32 result = 0;
125  LinkedListElement const* e = getFirst();
126  while (e)
127  {
128  ++result;
129  e = e->next();
130  }
131  return result;
132  }
133  else
134  return iSize;
135  }
136 
137  void incSize() { ++iSize; }
138  void decSize() { --iSize; }
139 
140  template<class _Ty>
141  class Iterator
142  {
143  public:
144  typedef std::bidirectional_iterator_tag iterator_category;
145  typedef _Ty value_type;
146  typedef ptrdiff_t difference_type;
147  typedef ptrdiff_t distance_type;
148  typedef _Ty* pointer;
149  typedef _Ty const* const_pointer;
150  typedef _Ty& reference;
151  typedef _Ty const & const_reference;
152 
153  Iterator() : _Ptr(nullptr)
154  { // construct with null node pointer
155  }
156 
157  Iterator(pointer _Pnode) : _Ptr(_Pnode)
158  { // construct with node pointer _Pnode
159  }
160 
161  Iterator& operator=(Iterator const &_Right)
162  {
163  _Ptr = _Right._Ptr;
164  return *this;
165  }
166 
167  Iterator& operator=(const_pointer const &_Right)
168  {
169  _Ptr = pointer(_Right);
170  return *this;
171  }
172 
173  reference operator*()
174  { // return designated value
175  return *_Ptr;
176  }
177 
178  pointer operator->()
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
210  { // test for iterator equality
211  return (_Ptr == _Right._Ptr);
212  }
213 
214  bool operator!=(Iterator const &_Right) const
215  { // test for iterator inequality
216  return (!(*this == _Right));
217  }
218 
219  bool operator==(pointer const &_Right) const
220  { // test for pointer equality
221  return (_Ptr != _Right);
222  }
223 
224  bool operator!=(pointer const &_Right) const
225  { // test for pointer equality
226  return (!(*this == _Right));
227  }
228 
229  bool operator==(const_reference _Right) const
230  { // test for reference equality
231  return (_Ptr == &_Right);
232  }
233 
234  bool operator!=(const_reference _Right) const
235  { // test for reference equality
236  return (_Ptr != &_Right);
237  }
238 
239  pointer _Mynode()
240  { // return node pointer
241  return (_Ptr);
242  }
243 
244  protected:
245  pointer _Ptr; // pointer to node
246  };
247 
249 
250  private:
253 };
254 
255 //============================================
256 #endif
LinkedListElement * getLast()
Definition: LinkedList.h:107
bool operator==(Iterator const &_Right) const
Definition: LinkedList.h:209
Definition: LinkedList.h:28
void insertLast(LinkedListElement *pElem)
Definition: LinkedList.h:115
bool hasNext() const
Definition: LinkedList.h:39
LinkedListElement const * getLast() const
Definition: LinkedList.h:108
LinkedListElement iLast
Definition: LinkedList.h:88
LinkedListHead()
Definition: LinkedList.h:92
Iterator< LinkedListElement > iterator
Definition: LinkedList.h:248
std::bidirectional_iterator_tag iterator_category
Definition: LinkedList.h:144
pointer _Mynode()
Definition: LinkedList.h:239
LinkedListElement * iNext
Definition: LinkedList.h:33
uint32 getSize() const
Definition: LinkedList.h:120
bool operator==(const_reference _Right) const
Definition: LinkedList.h:229
LinkedListElement const * getFirst() const
Definition: LinkedList.h:105
arena_t NULL
Definition: jemalloc_internal.h:624
void incSize()
Definition: LinkedList.h:137
LinkedListElement * getFirst()
Definition: LinkedList.h:104
pointer operator->()
Definition: LinkedList.h:178
LinkedListElement * next()
Definition: LinkedList.h:43
ptrdiff_t difference_type
Definition: LinkedList.h:146
reference operator*()
Definition: LinkedList.h:173
Iterator & operator--()
Definition: LinkedList.h:196
bool isInList() const
Definition: LinkedList.h:41
Iterator(pointer _Pnode)
Definition: LinkedList.h:157
void decSize()
Definition: LinkedList.h:138
pointer _Ptr
Definition: LinkedList.h:245
ptrdiff_t distance_type
Definition: LinkedList.h:147
Iterator()
Definition: LinkedList.h:153
Iterator operator++(int)
Definition: LinkedList.h:189
bool operator!=(Iterator const &_Right) const
Definition: LinkedList.h:214
void delink()
Definition: LinkedList.h:53
uint32 iSize
Definition: LinkedList.h:89
Iterator & operator++()
Definition: LinkedList.h:183
LinkedListElement iFirst
Definition: LinkedList.h:87
_Ty const & const_reference
Definition: LinkedList.h:151
virtual ~LinkedListHead()
Definition: LinkedList.h:100
LinkedListElement()
Definition: LinkedList.h:36
LinkedListElement * nocheck_next()
Definition: LinkedList.h:48
LinkedListElement const * nocheck_prev() const
Definition: LinkedList.h:51
_Ty const * const_pointer
Definition: LinkedList.h:149
LinkedListElement * nocheck_prev()
Definition: LinkedList.h:50
uint32_t uint32
Definition: Define.h:150
Iterator & operator=(Iterator const &_Right)
Definition: LinkedList.h:161
Definition: LinkedList.h:141
LinkedListElement * prev()
Definition: LinkedList.h:45
bool operator!=(const_reference _Right) const
Definition: LinkedList.h:234
bool isEmpty() const
Definition: LinkedList.h:102
_Ty & reference
Definition: LinkedList.h:150
void insertFirst(LinkedListElement *pElem)
Definition: LinkedList.h:110
void insertBefore(LinkedListElement *pElem)
Definition: LinkedList.h:61
_Ty value_type
Definition: LinkedList.h:145
LinkedListElement const * next() const
Definition: LinkedList.h:44
Iterator operator--(int)
Definition: LinkedList.h:202
bool operator==(pointer const &_Right) const
Definition: LinkedList.h:219
Definition: LinkedList.h:84
bool hasPrev() const
Definition: LinkedList.h:40
Iterator & operator=(const_pointer const &_Right)
Definition: LinkedList.h:167
LinkedListElement const * prev() const
Definition: LinkedList.h:46
virtual ~LinkedListElement()
Definition: LinkedList.h:37
LinkedListHead & operator=(LinkedListHead const &)
void insertAfter(LinkedListElement *pElem)
Definition: LinkedList.h:69
LinkedListElement & operator=(LinkedListElement const &)
bool operator!=(pointer const &_Right) const
Definition: LinkedList.h:224
_Ty * pointer
Definition: LinkedList.h:148
LinkedListElement const * nocheck_next() const
Definition: LinkedList.h:49
LinkedListElement * iPrev
Definition: LinkedList.h:34