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