Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
LinkedList.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef _LINKEDLIST
7#define _LINKEDLIST
8
9#include "Define.h"
10#include <iterator>
11
12//============================================
13class LinkedListHead;
14
16{
17private:
18 friend class LinkedListHead;
19
22public:
23 LinkedListElement() : iNext(NULL), iPrev(NULL) { }
24 virtual ~LinkedListElement() { delink(); }
25
26 bool hasNext() const { return(iNext && iNext->iNext != NULL); }
27 bool hasPrev() const { return(iPrev && iPrev->iPrev != NULL); }
28 bool isInList() const { return(iNext != NULL && iPrev != NULL); }
29
30 LinkedListElement* next() { return hasNext() ? iNext : NULL; }
31 LinkedListElement const* next() const { return hasNext() ? iNext : NULL; }
32 LinkedListElement* prev() { return hasPrev() ? iPrev : NULL; }
33 LinkedListElement const* prev() const { return hasPrev() ? iPrev : NULL; }
34
36 LinkedListElement const* nocheck_next() const { return iNext; }
38 LinkedListElement const* nocheck_prev() const { return iPrev; }
39
40 void delink()
41 {
42 if (isInList())
43 {
44 iNext->iPrev = iPrev; iPrev->iNext = iNext; iNext = NULL; iPrev = NULL;
45 }
46 }
47
49 {
50 pElem->iNext = this;
51 pElem->iPrev = iPrev;
52 iPrev->iNext = pElem;
53 iPrev = pElem;
54 }
55
57 {
58 pElem->iPrev = this;
59 pElem->iNext = iNext;
60 iNext->iPrev = pElem;
61 iNext = pElem;
62 }
63private:
66};
67
68//============================================
69
71{
72private:
76public:
78 {
79 // create empty list
80
81 iFirst.iNext = &iLast;
82 iLast.iPrev = &iFirst;
83 }
84
85 virtual ~LinkedListHead() { }
86 bool isEmpty() const { return(!iFirst.iNext->isInList()); }
87
88 LinkedListElement* getFirst() { return(isEmpty() ? NULL : iFirst.iNext); }
89 LinkedListElement const* getFirst() const { return(isEmpty() ? NULL : iFirst.iNext); }
90
91 LinkedListElement* getLast() { return(isEmpty() ? NULL : iLast.iPrev); }
92 LinkedListElement const* getLast() const { return(isEmpty() ? NULL : iLast.iPrev); }
93
95 {
96 iFirst.insertAfter(pElem);
97 }
98
100 {
101 iLast.insertBefore(pElem);
102 }
103
105 {
106 if (!iSize)
107 {
108 uint32 result = 0;
109 LinkedListElement const* e = getFirst();
110 while (e)
111 {
112 ++result;
113 e = e->next();
114 }
115 return result;
116 }
117 else
118 return iSize;
119 }
120
121 void incSize() { ++iSize; }
122 void decSize() { --iSize; }
123
124 template<class _Ty>
126 {
127 public:
128 typedef std::bidirectional_iterator_tag iterator_category;
129 typedef _Ty value_type;
130 typedef ptrdiff_t difference_type;
131 typedef ptrdiff_t distance_type;
132 typedef _Ty* pointer;
133 typedef _Ty const* const_pointer;
134 typedef _Ty& reference;
135 typedef _Ty const& const_reference;
136
138 { // construct with null node pointer
139 }
140
141 Iterator(pointer _Pnode) : _Ptr(_Pnode)
142 { // construct with node pointer _Pnode
143 }
144
146 {
147 _Ptr = _Right._Ptr;
148 return *this;
149 }
150
152 {
153 _Ptr = pointer(_Right);
154 return *this;
155 }
156
158 { // return designated value
159 return *_Ptr;
160 }
161
163 { // return pointer to class object
164 return _Ptr;
165 }
166
168 { // preincrement
169 _Ptr = _Ptr->next();
170 return (*this);
171 }
172
174 { // postincrement
175 iterator _Tmp = *this;
176 ++*this;
177 return (_Tmp);
178 }
179
181 { // predecrement
182 _Ptr = _Ptr->prev();
183 return (*this);
184 }
185
187 { // postdecrement
188 iterator _Tmp = *this;
189 --*this;
190 return (_Tmp);
191 }
192
193 bool operator==(Iterator const& _Right) const
194 { // test for iterator equality
195 return (_Ptr == _Right._Ptr);
196 }
197
198 bool operator!=(Iterator const& _Right) const
199 { // test for iterator inequality
200 return (!(*this == _Right));
201 }
202
203 bool operator==(pointer const& _Right) const
204 { // test for pointer equality
205 return (_Ptr != _Right);
206 }
207
208 bool operator!=(pointer const& _Right) const
209 { // test for pointer equality
210 return (!(*this == _Right));
211 }
212
213 bool operator==(const_reference _Right) const
214 { // test for reference equality
215 return (_Ptr == &_Right);
216 }
217
218 bool operator!=(const_reference _Right) const
219 { // test for reference equality
220 return (_Ptr != &_Right);
221 }
222
224 { // return node pointer
225 return (_Ptr);
226 }
227
228 protected:
229 pointer _Ptr; // pointer to node
230 };
231
233
234private:
237};
238
239//============================================
240#endif
std::uint32_t uint32
Definition Define.h:77
virtual ~LinkedListElement()
Definition LinkedList.h:24
LinkedListElement(LinkedListElement const &)
LinkedListElement * nocheck_next()
Definition LinkedList.h:35
LinkedListElement * iPrev
Definition LinkedList.h:21
friend class LinkedListHead
Definition LinkedList.h:18
void insertAfter(LinkedListElement *pElem)
Definition LinkedList.h:56
bool hasNext() const
Definition LinkedList.h:26
LinkedListElement * prev()
Definition LinkedList.h:32
LinkedListElement const * next() const
Definition LinkedList.h:31
bool hasPrev() const
Definition LinkedList.h:27
LinkedListElement & operator=(LinkedListElement const &)
LinkedListElement const * nocheck_next() const
Definition LinkedList.h:36
LinkedListElement const * prev() const
Definition LinkedList.h:33
LinkedListElement * nocheck_prev()
Definition LinkedList.h:37
void insertBefore(LinkedListElement *pElem)
Definition LinkedList.h:48
bool isInList() const
Definition LinkedList.h:28
LinkedListElement * next()
Definition LinkedList.h:30
LinkedListElement * iNext
Definition LinkedList.h:20
LinkedListElement const * nocheck_prev() const
Definition LinkedList.h:38
GridReference< OBJECT > const & const_reference
Definition LinkedList.h:135
Iterator operator++(int)
Definition LinkedList.h:173
bool operator==(const_reference _Right) const
Definition LinkedList.h:213
Iterator & operator=(const_pointer const &_Right)
Definition LinkedList.h:151
bool operator!=(Iterator const &_Right) const
Definition LinkedList.h:198
bool operator==(pointer const &_Right) const
Definition LinkedList.h:203
Iterator operator--(int)
Definition LinkedList.h:186
std::bidirectional_iterator_tag iterator_category
Definition LinkedList.h:128
Iterator & operator=(Iterator const &_Right)
Definition LinkedList.h:145
GridReference< OBJECT > const * const_pointer
Definition LinkedList.h:133
bool operator!=(pointer const &_Right) const
Definition LinkedList.h:208
bool operator==(Iterator const &_Right) const
Definition LinkedList.h:193
bool operator!=(const_reference _Right) const
Definition LinkedList.h:218
Iterator(pointer _Pnode)
Definition LinkedList.h:141
LinkedListHead(LinkedListHead const &)
LinkedListHead & operator=(LinkedListHead const &)
void insertLast(LinkedListElement *pElem)
Definition LinkedList.h:99
LinkedListElement const * getFirst() const
Definition LinkedList.h:89
LinkedListElement * getFirst()
Definition LinkedList.h:88
LinkedListElement const * getLast() const
Definition LinkedList.h:92
LinkedListElement * getLast()
Definition LinkedList.h:91
Iterator< LinkedListElement > iterator
Definition LinkedList.h:232
LinkedListElement iLast
Definition LinkedList.h:74
virtual ~LinkedListHead()
Definition LinkedList.h:85
bool isEmpty() const
Definition LinkedList.h:86
uint32 getSize() const
Definition LinkedList.h:104
LinkedListElement iFirst
Definition LinkedList.h:73
void insertFirst(LinkedListElement *pElem)
Definition LinkedList.h:94