1 | |
---|
2 | #ifndef _LIST_H |
---|
3 | #define _LIST_H |
---|
4 | |
---|
5 | #include "stdincl.h" |
---|
6 | |
---|
7 | //! An enum to list all the modes available when adding an object to a List |
---|
8 | //enum ADDMODE {LIST_ADD_FIRST, LIST_ADD_LAST}; |
---|
9 | //! An enum to list the two searching directions available when removing an object from a List |
---|
10 | //enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW}; |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | class WorldEntity; |
---|
15 | |
---|
16 | class List { |
---|
17 | |
---|
18 | public: |
---|
19 | List (); |
---|
20 | ~List (); |
---|
21 | |
---|
22 | void add(WorldEntity* entity); |
---|
23 | void remove(WorldEntity* entity); |
---|
24 | void destroy(); |
---|
25 | WorldEntity* firstElement(); |
---|
26 | bool isEmpty(); |
---|
27 | int getSize(); |
---|
28 | WorldEntity* enumerate(); |
---|
29 | WorldEntity* nextElement(); |
---|
30 | WorldEntity* toArray(); |
---|
31 | void debug(); |
---|
32 | |
---|
33 | private: |
---|
34 | struct listElement |
---|
35 | { |
---|
36 | listElement* prev; |
---|
37 | WorldEntity* curr; |
---|
38 | listElement* next; |
---|
39 | }; |
---|
40 | Uint32 size; |
---|
41 | listElement* first; |
---|
42 | listElement* last; |
---|
43 | listElement* currentEl; |
---|
44 | |
---|
45 | |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | template<class T> struct listElement |
---|
51 | { |
---|
52 | listElement* prev; |
---|
53 | T* curr; |
---|
54 | listElement* next; |
---|
55 | }; |
---|
56 | |
---|
57 | template<class T> class tIterator |
---|
58 | { |
---|
59 | public: |
---|
60 | tIterator(listElement<T>* startElement); |
---|
61 | ~tIterator(); |
---|
62 | |
---|
63 | T* nextElement(); |
---|
64 | |
---|
65 | private: |
---|
66 | listElement<T>* currentEl; |
---|
67 | listElement<T>* tmpEl; |
---|
68 | }; |
---|
69 | |
---|
70 | |
---|
71 | template<class T> |
---|
72 | inline tIterator<T>::tIterator (listElement<T>* startElement) |
---|
73 | { |
---|
74 | this->currentEl = startElement; |
---|
75 | this->tmpEl = NULL; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | template<class T> |
---|
80 | tIterator<T>::~tIterator () |
---|
81 | { |
---|
82 | this->currentEl = NULL; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | template<class T> |
---|
87 | inline T* tIterator<T>::nextElement () |
---|
88 | { |
---|
89 | if( this->currentEl == NULL) |
---|
90 | return NULL; |
---|
91 | |
---|
92 | this->tmpEl = this->currentEl; |
---|
93 | this->currentEl = this->currentEl->next; |
---|
94 | return this->tmpEl->curr; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | template<class T> class tList |
---|
100 | { |
---|
101 | public: |
---|
102 | tList (); |
---|
103 | ~tList (); |
---|
104 | |
---|
105 | void add(T* entity); |
---|
106 | void remove(T* entity); |
---|
107 | void destroy(); |
---|
108 | T* firstElement(); |
---|
109 | T* lastElement(); |
---|
110 | bool isEmpty(); |
---|
111 | int getSize(); |
---|
112 | T* enumerate(); |
---|
113 | tIterator<T>* getIterator(); |
---|
114 | T* nextElement(); |
---|
115 | T* nextElement(T* toEntity); |
---|
116 | T* toArray(); |
---|
117 | void debug(); |
---|
118 | |
---|
119 | private: |
---|
120 | Uint32 size; |
---|
121 | listElement<T>* first; |
---|
122 | listElement<T>* last; |
---|
123 | listElement<T>* currentEl; |
---|
124 | }; |
---|
125 | |
---|
126 | |
---|
127 | template<class T> |
---|
128 | tList<T>::tList () |
---|
129 | { |
---|
130 | this->first = NULL; |
---|
131 | this->last = NULL; |
---|
132 | this->size = 0; |
---|
133 | } |
---|
134 | |
---|
135 | template<class T> |
---|
136 | tList<T>::~tList () |
---|
137 | { |
---|
138 | this->currentEl = this->first; |
---|
139 | while(this->currentEl != NULL) |
---|
140 | { |
---|
141 | listElement<T>* le = this->currentEl->next; |
---|
142 | //delete this->currentEl->curr; |
---|
143 | delete this->currentEl; |
---|
144 | this->currentEl = le; |
---|
145 | } |
---|
146 | this->first = NULL; |
---|
147 | this->last = NULL; |
---|
148 | this->size = 0; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | template<class T> |
---|
153 | inline void tList<T>::add(T* entity) |
---|
154 | { |
---|
155 | if( entity == NULL) return; |
---|
156 | listElement<T>* el = new listElement<T>; |
---|
157 | el->prev = this->last; |
---|
158 | el->curr = entity; |
---|
159 | el->next = NULL; |
---|
160 | |
---|
161 | this->last = el; |
---|
162 | |
---|
163 | if(el->prev == NULL) this->first = el; /* if first element */ |
---|
164 | else el->prev->next = el; |
---|
165 | this->size++; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | template<class T> |
---|
170 | inline void tList<T>::remove(T* entity) |
---|
171 | { |
---|
172 | if( entity == NULL) return; |
---|
173 | this->currentEl = this->first; |
---|
174 | listElement<T>* te; |
---|
175 | while( this->currentEl != NULL) |
---|
176 | { |
---|
177 | if( this->currentEl->curr == entity) |
---|
178 | { |
---|
179 | if( this->currentEl->prev == NULL ) this->first = this->currentEl->next; |
---|
180 | else this->currentEl->prev->next = this->currentEl->next; |
---|
181 | |
---|
182 | if( this->currentEl->next == NULL) this->last = this->currentEl->prev; |
---|
183 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
184 | |
---|
185 | //te = this->currentEl->next; // for what am i doing this? |
---|
186 | delete this->currentEl; |
---|
187 | //this->currentEl = te; |
---|
188 | this->currentEl = NULL; |
---|
189 | this->size--; |
---|
190 | return; |
---|
191 | } |
---|
192 | this->currentEl = this->currentEl->next; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | template<class T> |
---|
198 | void tList<T>::destroy() |
---|
199 | { |
---|
200 | this->currentEl = this->first; |
---|
201 | while(this->currentEl != NULL) |
---|
202 | { |
---|
203 | listElement<T>* le = this->currentEl->next; |
---|
204 | //delete this->currentEl->curr; |
---|
205 | delete this->currentEl; |
---|
206 | this->currentEl = le; |
---|
207 | } |
---|
208 | this->first = NULL; |
---|
209 | this->last = NULL; |
---|
210 | this->size = 0; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | template<class T> |
---|
215 | T* tList<T>::firstElement() |
---|
216 | { |
---|
217 | return this->first->curr; |
---|
218 | } |
---|
219 | |
---|
220 | template<class T> |
---|
221 | T* tList<T>::lastElement() |
---|
222 | { |
---|
223 | return this->last->curr; |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | template<class T> |
---|
228 | bool tList<T>::isEmpty() |
---|
229 | { |
---|
230 | return (this->size==0)?true:false; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | template<class T> |
---|
235 | int tList<T>::getSize() |
---|
236 | { |
---|
237 | return this->size; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | template<class T> |
---|
242 | T* tList<T>::enumerate() |
---|
243 | { |
---|
244 | //if( this->last == this->first == NULL) return NULL; |
---|
245 | if( this->size == 0) return NULL; |
---|
246 | this->currentEl = this->first; |
---|
247 | return this->currentEl->curr; |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | template<class T> |
---|
252 | inline tIterator<T>* tList<T>::getIterator() |
---|
253 | { |
---|
254 | tIterator<T>* iterator = new tIterator<T>(this->first); |
---|
255 | return iterator; |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | template<class T> |
---|
260 | T* tList<T>::nextElement() |
---|
261 | { |
---|
262 | // if( this->last == this->first == NULL) return NULL; |
---|
263 | if( this->size == 0) return NULL; |
---|
264 | this->currentEl = this->currentEl->next; |
---|
265 | if( this->currentEl == NULL) return NULL; |
---|
266 | return this->currentEl->curr; |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | /** |
---|
271 | \brief this returns the next element after toEntity or the first if toEntity is last |
---|
272 | */ |
---|
273 | template<class T> |
---|
274 | T* tList<T>::nextElement(T* toEntity) |
---|
275 | { |
---|
276 | //if( this->last == this->first == NULL) return NULL; |
---|
277 | if(this->size == 0) return NULL; |
---|
278 | if( toEntity == NULL) return this->first->curr; |
---|
279 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
280 | this->currentEl = this->first; |
---|
281 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
282 | { |
---|
283 | this->currentEl = this->currentEl->next; |
---|
284 | } |
---|
285 | if(this->currentEl == NULL) return NULL; |
---|
286 | return this->currentEl->next->curr; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | template<class T> |
---|
291 | T* tList<T>::toArray() |
---|
292 | {} |
---|
293 | |
---|
294 | #endif /* _LIST_H */ |
---|