root/trunk/src/framework/Utilities/LinkedList.h

Revision 102, 7.1 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef _LINKEDLIST
22#define _LINKEDLIST
23
24#include "Common.h"
25
26//============================================
27class LinkedListHead;
28
29class LinkedListElement
30{
31    private:
32        friend class LinkedListHead;
33
34        LinkedListElement* iNext;
35        LinkedListElement* iPrev;
36    public:
37        LinkedListElement() { iNext = NULL; iPrev = NULL; }
38        ~LinkedListElement() { delink(); }
39
40        bool hasNext() const { return(iNext->iNext != NULL); }
41        bool hasPrev() const { return(iPrev->iPrev != NULL); }
42        bool isInList() const { return(iNext != NULL && iPrev != NULL); }
43
44        LinkedListElement      * next()       { return hasNext() ? iNext : NULL; }
45        LinkedListElement const* next() const { return hasNext() ? iNext : NULL; }
46        LinkedListElement      * prev()       { return hasPrev() ? iPrev : NULL; }
47        LinkedListElement const* prev() const { return hasPrev() ? iPrev : NULL; }
48
49        void delink()
50        {
51            if(isInList())
52            {
53                iNext->iPrev = iPrev; iPrev->iNext = iNext; iNext = NULL; iPrev = NULL;
54            }
55        }
56
57        void insertBefore(LinkedListElement* pElem)
58        {
59            pElem->iNext = this;
60            pElem->iPrev = iPrev;
61            iPrev->iNext = pElem;
62            iPrev = pElem;
63        }
64
65        void insertAfter(LinkedListElement* pElem)
66        {
67            pElem->iPrev = this;
68            pElem->iNext = iNext;
69            iNext->iPrev = pElem;
70            iNext = pElem;
71        }
72};
73
74//============================================
75
76class LinkedListHead
77{
78    private:
79        LinkedListElement iFirst;
80        LinkedListElement iLast;
81        uint32 iSize;
82    public:
83        LinkedListHead()
84        {
85            // create empty list
86
87            iFirst.iNext = &iLast;
88            iLast.iPrev = &iFirst;
89            iSize = 0;
90        }
91
92        bool isEmpty() const { return(!iFirst.iNext->isInList()); }
93
94        LinkedListElement      * getFirst()       { return(isEmpty() ? NULL : iFirst.iNext); }
95        LinkedListElement const* getFirst() const { return(isEmpty() ? NULL : iFirst.iNext); }
96
97        LinkedListElement      * getLast() { return(isEmpty() ? NULL : iLast.iPrev); }
98        LinkedListElement const* getLast() const  { return(isEmpty() ? NULL : iLast.iPrev); }
99
100        void insertFirst(LinkedListElement* pElem)
101        {
102            iFirst.insertAfter(pElem);
103        }
104
105        void insertLast(LinkedListElement* pElem)
106        {
107            iLast.insertBefore(pElem);
108        }
109
110        uint32 getSize() const
111        {
112            if(!iSize)
113            {
114                uint32 result = 0;
115                LinkedListElement const* e = getFirst();
116                while(e)
117                {
118                    ++result;
119                    e = e->next();
120                }
121                return result;
122            }
123            else
124                return iSize;
125        }
126
127        void incSize() { ++iSize; }
128        void decSize() { --iSize; }
129
130        template<class _Ty>
131            class Iterator
132        {
133            public:
134                typedef std::bidirectional_iterator_tag     iterator_category;
135                typedef _Ty                                 value_type;
136                typedef ptrdiff_t                           difference_type;
137                typedef ptrdiff_t                           distance_type;
138                typedef _Ty*                                pointer;
139                typedef _Ty&                                reference;
140
141                Iterator() : _Ptr(0)
142                {                                           // construct with null node pointer
143                }
144
145                Iterator(pointer _Pnode) : _Ptr(_Pnode)
146                {                                           // construct with node pointer _Pnode
147                }
148
149                reference operator*()
150                {                                           // return designated value
151                    return *_Ptr;
152                }
153
154                pointer operator->()
155                {                                           // return pointer to class object
156                    return _Ptr;
157                }
158
159                Iterator& operator++()
160                {                                           // preincrement
161                    _Ptr = _Ptr->next();
162                    return (*this);
163                }
164
165                Iterator operator++(int)
166                {                                           // postincrement
167                    iterator _Tmp = *this;
168                    ++*this;
169                    return (_Tmp);
170                }
171
172                Iterator& operator--()
173                {                                           // predecrement
174                    _Ptr = _Ptr->prev();
175                    return (*this);
176                }
177
178                Iterator operator--(int)
179                {                                           // postdecrement
180                    iterator _Tmp = *this;
181                    --*this;
182                    return (_Tmp);
183                }
184
185                bool operator==(Iterator const &_Right) const
186                {                                           // test for iterator equality
187                    return (_Ptr == _Right._Ptr);
188                }
189
190                bool operator!=(Iterator const &_Right) const
191                {                                           // test for iterator inequality
192                    return (!(*this == _Right));
193                }
194
195                bool operator==(pointer const &_Right) const
196                {                                           // test for pointer equality
197                    return (_Ptr != _Right);
198                }
199
200                bool operator!=(pointer const &_Right) const
201                {                                           // test for pointer equality
202                    return (!(*this == _Right));
203                }
204
205                pointer _Mynode()
206                {                                           // return node pointer
207                    return (_Ptr);
208                }
209
210            protected:
211                pointer _Ptr;                               // pointer to node
212        };
213
214        typedef Iterator<LinkedListElement> iterator;
215};
216
217//============================================
218#endif
Note: See TracBrowser for help on using the browser.