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