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>* firstEl; |
---|
68 | int counter; |
---|
69 | }; |
---|
70 | |
---|
71 | |
---|
72 | template<class T> |
---|
73 | inline tIterator<T>::tIterator (listElement<T>* startElement) |
---|
74 | { |
---|
75 | this->currentEl = startElement; |
---|
76 | this->firstEl = startElement; |
---|
77 | this->counter = -1; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | template<class T> |
---|
82 | tIterator<T>::~tIterator () |
---|
83 | { |
---|
84 | this->currentEl = NULL; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | template<class T> |
---|
89 | inline T* tIterator<T>::nextElement () |
---|
90 | { |
---|
91 | this->counter++; |
---|
92 | if( this->counter == 0) |
---|
93 | return this->firstEl->curr; |
---|
94 | |
---|
95 | if( this->currentEl->next == NULL || this->currentEl == NULL) |
---|
96 | return NULL; |
---|
97 | |
---|
98 | this->currentEl = this->currentEl->next; |
---|
99 | return this->currentEl->curr; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | template<class T> class tList |
---|
105 | { |
---|
106 | |
---|
107 | public: |
---|
108 | tList (); |
---|
109 | ~tList (); |
---|
110 | |
---|
111 | void add(T* entity); |
---|
112 | void remove(T* entity); |
---|
113 | void destroy(); |
---|
114 | T* firstElement(); |
---|
115 | bool isEmpty(); |
---|
116 | int getSize(); |
---|
117 | T* enumerate(); |
---|
118 | tIterator<T>* getIterator(); |
---|
119 | T* nextElement(); |
---|
120 | T* nextElement(T* toEntity); |
---|
121 | T* toArray(); |
---|
122 | void debug(); |
---|
123 | |
---|
124 | private: |
---|
125 | Uint32 size; |
---|
126 | listElement<T>* first; |
---|
127 | listElement<T>* last; |
---|
128 | listElement<T>* currentEl; |
---|
129 | }; |
---|
130 | |
---|
131 | |
---|
132 | template<class T> |
---|
133 | tList<T>::tList () |
---|
134 | { |
---|
135 | this->first = NULL; |
---|
136 | this->last = NULL; |
---|
137 | this->size = 0; |
---|
138 | } |
---|
139 | |
---|
140 | template<class T> |
---|
141 | tList<T>::~tList () |
---|
142 | { |
---|
143 | this->currentEl = this->first; |
---|
144 | while(this->currentEl != NULL) |
---|
145 | { |
---|
146 | listElement<T>* le = this->currentEl->next; |
---|
147 | //delete this->currentEl->curr; |
---|
148 | delete this->currentEl; |
---|
149 | this->currentEl = le; |
---|
150 | } |
---|
151 | this->first = NULL; |
---|
152 | this->last = NULL; |
---|
153 | this->size = 0; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | template<class T> |
---|
158 | inline void tList<T>::add(T* entity) |
---|
159 | { |
---|
160 | if( entity == NULL) return; |
---|
161 | listElement<T>* el = new listElement<T>; |
---|
162 | el->prev = this->last; |
---|
163 | el->curr = entity; |
---|
164 | el->next = NULL; |
---|
165 | |
---|
166 | this->last = el; |
---|
167 | |
---|
168 | if(el->prev == NULL) this->first = el; /* if first element */ |
---|
169 | else el->prev->next = el; |
---|
170 | this->size++; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | template<class T> |
---|
175 | inline void tList<T>::remove(T* entity) |
---|
176 | { |
---|
177 | if( entity == NULL) return; |
---|
178 | this->currentEl = this->first; |
---|
179 | listElement<T>* te; |
---|
180 | while( this->currentEl != NULL) |
---|
181 | { |
---|
182 | if( this->currentEl->curr == entity) |
---|
183 | { |
---|
184 | if( this->currentEl->prev == NULL ) this->first = this->currentEl->next; |
---|
185 | else this->currentEl->prev->next = this->currentEl->next; |
---|
186 | |
---|
187 | if( this->currentEl->next == NULL) this->last = this->currentEl->prev; |
---|
188 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
189 | |
---|
190 | te = this->currentEl->next; // for what am i doing this? |
---|
191 | delete this->currentEl; |
---|
192 | this->currentEl = te; |
---|
193 | this->size--; |
---|
194 | return; |
---|
195 | } |
---|
196 | this->currentEl = this->currentEl->next; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | template<class T> |
---|
202 | void tList<T>::destroy() |
---|
203 | { |
---|
204 | this->currentEl = this->first; |
---|
205 | while(this->currentEl != NULL) |
---|
206 | { |
---|
207 | listElement<T>* le = this->currentEl->next; |
---|
208 | //delete this->currentEl->curr; |
---|
209 | delete this->currentEl; |
---|
210 | this->currentEl = le; |
---|
211 | } |
---|
212 | this->first = NULL; |
---|
213 | this->last = NULL; |
---|
214 | this->size = 0; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | template<class T> |
---|
219 | T* tList<T>::firstElement() |
---|
220 | { |
---|
221 | return this->first->curr; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | template<class T> |
---|
226 | bool tList<T>::isEmpty() |
---|
227 | { |
---|
228 | return (this->size==0)?true:false; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | template<class T> |
---|
233 | int tList<T>::getSize() |
---|
234 | { |
---|
235 | return this->size; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | template<class T> |
---|
240 | T* tList<T>::enumerate() |
---|
241 | { |
---|
242 | //if( this->last == this->first == NULL) return NULL; |
---|
243 | if( this->size == 0) return NULL; |
---|
244 | this->currentEl = this->first; |
---|
245 | return this->currentEl->curr; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | template<class T> |
---|
250 | inline tIterator<T>* tList<T>::getIterator() |
---|
251 | { |
---|
252 | tIterator<T>* iterator = new tIterator<T>(this->first); |
---|
253 | return iterator; |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | template<class T> |
---|
258 | T* tList<T>::nextElement() |
---|
259 | { |
---|
260 | // if( this->last == this->first == NULL) return NULL; |
---|
261 | if( this->size == 0) return NULL; |
---|
262 | this->currentEl = this->currentEl->next; |
---|
263 | if( this->currentEl == NULL) return NULL; |
---|
264 | return this->currentEl->curr; |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | /** |
---|
269 | \brief this returns the next element after toEntity or the first if toEntity is last |
---|
270 | */ |
---|
271 | template<class T> |
---|
272 | T* tList<T>::nextElement(T* toEntity) |
---|
273 | { |
---|
274 | //if( this->last == this->first == NULL) return NULL; |
---|
275 | if(this->size == 0) return NULL; |
---|
276 | if( toEntity == NULL) return this->first->curr; |
---|
277 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
278 | this->currentEl = this->first; |
---|
279 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
280 | { |
---|
281 | this->currentEl = this->currentEl->next; |
---|
282 | } |
---|
283 | if(this->currentEl == NULL) return NULL; |
---|
284 | return this->currentEl->next->curr; |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | template<class T> |
---|
289 | T* tList<T>::toArray() |
---|
290 | {} |
---|
291 | |
---|
292 | #endif /* _LIST_H */ |
---|