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 | |
---|
15 | |
---|
16 | //! a list element of the tList, |
---|
17 | template<class T> struct listElement |
---|
18 | { |
---|
19 | listElement* prev; //!< pointer to the previous listElement in the list |
---|
20 | T* curr; //!< pointer to the list payload/container |
---|
21 | listElement* next; //!< pointer to the next listElement |
---|
22 | }; |
---|
23 | |
---|
24 | /** |
---|
25 | \brief an iterator class |
---|
26 | |
---|
27 | this enables the user to iterate through a list very easely |
---|
28 | */ |
---|
29 | template<class T> class tIterator |
---|
30 | { |
---|
31 | public: |
---|
32 | tIterator(listElement<T>* startElement); |
---|
33 | ~tIterator(); |
---|
34 | |
---|
35 | T* nextElement(); |
---|
36 | |
---|
37 | private: |
---|
38 | listElement<T>* currentEl; //!< pointer to the current list element in the iterator |
---|
39 | listElement<T>* tmpEl; //!< temp listElemnt pointer |
---|
40 | }; |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | \brief iterator constructor |
---|
45 | \param startElement: the first list element from the tList |
---|
46 | |
---|
47 | normaly you will use it like this: |
---|
48 | |
---|
49 | tIterator<char>* nameIterator = nameList->getIterator(); |
---|
50 | char name* = nameIterator->nextElement(); |
---|
51 | while( name != NULL) |
---|
52 | { |
---|
53 | PRINTF(3)("found name: %s in list\n", name); |
---|
54 | name = nameIterator->nextElement(); |
---|
55 | } |
---|
56 | delete nameIterator; |
---|
57 | */ |
---|
58 | template<class T> |
---|
59 | inline tIterator<T>::tIterator (listElement<T>* startElement) |
---|
60 | { |
---|
61 | this->currentEl = startElement; |
---|
62 | this->tmpEl = NULL; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | /** |
---|
67 | \brief the destructor |
---|
68 | */ |
---|
69 | template<class T> |
---|
70 | inline tIterator<T>::~tIterator () |
---|
71 | { |
---|
72 | this->currentEl = NULL; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | \brief use it to iterate through the list |
---|
78 | \returns next list element |
---|
79 | */ |
---|
80 | template<class T> |
---|
81 | inline T* tIterator<T>::nextElement () |
---|
82 | { |
---|
83 | if( this->currentEl == NULL) |
---|
84 | return NULL; |
---|
85 | |
---|
86 | this->tmpEl = this->currentEl; |
---|
87 | this->currentEl = this->currentEl->next; |
---|
88 | return this->tmpEl->curr; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | \brief the list template class |
---|
95 | |
---|
96 | you will use this as a generic list for all type of objects |
---|
97 | */ |
---|
98 | template<class T> class tList |
---|
99 | { |
---|
100 | public: |
---|
101 | tList (); |
---|
102 | ~tList (); |
---|
103 | |
---|
104 | void add(T* entity); |
---|
105 | void remove(T* entity); |
---|
106 | void flush(); |
---|
107 | T* firstElement(); |
---|
108 | T* lastElement(); |
---|
109 | bool isEmpty(); |
---|
110 | int getSize(); |
---|
111 | //T* enumerate(); |
---|
112 | tIterator<T>* getIterator(); |
---|
113 | T* nextElement(); |
---|
114 | T* nextElement(T* toEntity); |
---|
115 | T* toArray(); |
---|
116 | void debug(); |
---|
117 | |
---|
118 | private: |
---|
119 | unsigned int size; //!< the size (lenght) of the list |
---|
120 | listElement<T>* first; //!< pointer to the first element |
---|
121 | listElement<T>* last; //!< pointer to the last element |
---|
122 | listElement<T>* currentEl; //!< pointer to the current element |
---|
123 | }; |
---|
124 | |
---|
125 | |
---|
126 | /** |
---|
127 | \brief the constructor |
---|
128 | */ |
---|
129 | template<class T> |
---|
130 | inline tList<T>::tList () |
---|
131 | { |
---|
132 | this->first = NULL; |
---|
133 | this->last = NULL; |
---|
134 | this->size = 0; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | /** |
---|
139 | \brief the deconstructor |
---|
140 | |
---|
141 | this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will |
---|
142 | not be deleted |
---|
143 | */ |
---|
144 | template<class T> |
---|
145 | inline tList<T>::~tList () |
---|
146 | { |
---|
147 | this->currentEl = this->first; |
---|
148 | while(this->currentEl != NULL) |
---|
149 | { |
---|
150 | listElement<T>* le = this->currentEl->next; |
---|
151 | //delete this->currentEl->curr; |
---|
152 | delete this->currentEl; |
---|
153 | this->currentEl = le; |
---|
154 | } |
---|
155 | this->first = NULL; |
---|
156 | this->last = NULL; |
---|
157 | this->size = 0; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | /** |
---|
162 | \brief add an entity to the list |
---|
163 | \param entity: the entity to add |
---|
164 | */ |
---|
165 | template<class T> |
---|
166 | inline void tList<T>::add(T* entity) |
---|
167 | { |
---|
168 | if( unlikely(entity == NULL)) return; |
---|
169 | listElement<T>* el = new listElement<T>; |
---|
170 | el->prev = this->last; |
---|
171 | el->curr = entity; |
---|
172 | el->next = NULL; |
---|
173 | |
---|
174 | this->last = el; |
---|
175 | |
---|
176 | if( unlikely(el->prev == NULL)) this->first = el; /* if first element */ |
---|
177 | else el->prev->next = el; |
---|
178 | this->size++; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /** |
---|
183 | \brief remove an entity from the list |
---|
184 | \param entity: the entity to be removed |
---|
185 | */ |
---|
186 | template<class T> |
---|
187 | inline void tList<T>::remove(T* entity) |
---|
188 | { |
---|
189 | this->currentEl = this->first; |
---|
190 | listElement<T>* te; |
---|
191 | while( this->currentEl != NULL) |
---|
192 | { |
---|
193 | if( unlikely(this->currentEl->curr == entity)) |
---|
194 | { |
---|
195 | if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; |
---|
196 | else this->currentEl->prev->next = this->currentEl->next; |
---|
197 | |
---|
198 | if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; |
---|
199 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
200 | |
---|
201 | delete this->currentEl; |
---|
202 | this->size--; |
---|
203 | return; |
---|
204 | } |
---|
205 | this->currentEl = this->currentEl->next; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | /** |
---|
211 | \brief this will deletes the objects from the list |
---|
212 | */ |
---|
213 | template<class T> |
---|
214 | inline void tList<T>::flush() |
---|
215 | { |
---|
216 | this->currentEl = this->first; |
---|
217 | while(this->currentEl != NULL) |
---|
218 | { |
---|
219 | listElement<T>* le = this->currentEl->next; |
---|
220 | delete this->currentEl->curr; |
---|
221 | delete this->currentEl; |
---|
222 | this->currentEl = le; |
---|
223 | } |
---|
224 | this->first = NULL; |
---|
225 | this->last = NULL; |
---|
226 | this->size = 0; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | \brief returns the first element of the list |
---|
232 | \returns first element |
---|
233 | */ |
---|
234 | template<class T> |
---|
235 | inline T* tList<T>::firstElement() |
---|
236 | { |
---|
237 | return this->first->curr; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | /** |
---|
242 | \brief function returns the last element of the list |
---|
243 | \returns the last element |
---|
244 | */ |
---|
245 | template<class T> |
---|
246 | inline T* tList<T>::lastElement() |
---|
247 | { |
---|
248 | return this->last->curr; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | /** |
---|
253 | \brief returns true if the list is empty |
---|
254 | \returns true if the list is empty |
---|
255 | */ |
---|
256 | template<class T> |
---|
257 | inline bool tList<T>::isEmpty() |
---|
258 | { |
---|
259 | return (this->size==0)?true:false; |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | /** |
---|
264 | \brief this returns the number of elements in the list |
---|
265 | \returns number of elements |
---|
266 | */ |
---|
267 | template<class T> |
---|
268 | inline int tList<T>::getSize() |
---|
269 | { |
---|
270 | return this->size; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /** |
---|
275 | \brief creates an itereator object and returns it |
---|
276 | \returns the iterator object to this list |
---|
277 | |
---|
278 | You will use this, if you want to iterate through the list |
---|
279 | */ |
---|
280 | template<class T> |
---|
281 | inline tIterator<T>* tList<T>::getIterator() |
---|
282 | { |
---|
283 | tIterator<T>* iterator = new tIterator<T>(this->first); |
---|
284 | return iterator; |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | /** |
---|
290 | \brief this returns the next element after toEntity or the first if toEntity is last |
---|
291 | \param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase |
---|
292 | \returns the element after toEntity |
---|
293 | */ |
---|
294 | template<class T> |
---|
295 | inline T* tList<T>::nextElement(T* toEntity) |
---|
296 | { |
---|
297 | //if( this->last == this->first == NULL) return NULL; |
---|
298 | if(this->size == 0) return NULL; |
---|
299 | if( toEntity == NULL) return this->first->curr; |
---|
300 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
301 | this->currentEl = this->first; |
---|
302 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
303 | { |
---|
304 | this->currentEl = this->currentEl->next; |
---|
305 | } |
---|
306 | if(this->currentEl == NULL) return NULL; |
---|
307 | return this->currentEl->next->curr; |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | /** |
---|
312 | \brief creates an array out of the list (ATTENTION: not implemented) |
---|
313 | \returns pointer to the array beginning |
---|
314 | |
---|
315 | ATTENTION: function is not implemented and wont do anything |
---|
316 | */ |
---|
317 | template<class T> |
---|
318 | T* tList<T>::toArray() |
---|
319 | { |
---|
320 | return NULL; |
---|
321 | } |
---|
322 | |
---|
323 | #endif /* _LIST_H */ |
---|