[4486] | 1 | /*! |
---|
| 2 | \file list.h |
---|
| 3 | \brief a File that includes a List-template |
---|
| 4 | */ |
---|
[2636] | 5 | |
---|
[3224] | 6 | #ifndef _LIST_H |
---|
| 7 | #define _LIST_H |
---|
[2036] | 8 | |
---|
[3860] | 9 | #include "compiler.h" |
---|
| 10 | #ifndef NULL |
---|
[4499] | 11 | #define NULL 0 //!< this will define NULL |
---|
[3860] | 12 | #endif |
---|
[2036] | 13 | |
---|
| 14 | |
---|
| 15 | |
---|
[4574] | 16 | //! a list element of the tList, |
---|
[3652] | 17 | template<class T> struct listElement |
---|
[2077] | 18 | { |
---|
[4488] | 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 |
---|
[3652] | 22 | }; |
---|
[2077] | 23 | |
---|
[4488] | 24 | /** |
---|
[4836] | 25 | * an iterator class |
---|
[4488] | 26 | |
---|
| 27 | this enables the user to iterate through a list very easely |
---|
| 28 | */ |
---|
[3652] | 29 | template<class T> class tIterator |
---|
| 30 | { |
---|
[2816] | 31 | public: |
---|
[3652] | 32 | tIterator(listElement<T>* startElement); |
---|
| 33 | ~tIterator(); |
---|
[4574] | 34 | |
---|
[3652] | 35 | T* nextElement(); |
---|
[4694] | 36 | T* seekElement(T* element); |
---|
[2077] | 37 | |
---|
[2816] | 38 | private: |
---|
[4488] | 39 | listElement<T>* currentEl; //!< pointer to the current list element in the iterator |
---|
| 40 | listElement<T>* tmpEl; //!< temp listElemnt pointer |
---|
[4694] | 41 | listElement<T>* startElement; //!< pointer to the start of the list |
---|
[2816] | 42 | }; |
---|
| 43 | |
---|
[2822] | 44 | |
---|
[4488] | 45 | /** |
---|
[4836] | 46 | * iterator constructor |
---|
| 47 | * @param startElement: the first list element from the tList |
---|
[4488] | 48 | |
---|
[4574] | 49 | normaly you will use it like this: |
---|
[4488] | 50 | |
---|
| 51 | tIterator<char>* nameIterator = nameList->getIterator(); |
---|
[4574] | 52 | char name* = nameIterator->nextElement(); |
---|
[4488] | 53 | while( name != NULL) |
---|
| 54 | { |
---|
| 55 | PRINTF(3)("found name: %s in list\n", name); |
---|
[4574] | 56 | name = nameIterator->nextElement(); |
---|
[4488] | 57 | } |
---|
[4574] | 58 | delete nameIterator; |
---|
[4488] | 59 | */ |
---|
[3652] | 60 | template<class T> |
---|
[4574] | 61 | inline tIterator<T>::tIterator (listElement<T>* startElement) |
---|
[3652] | 62 | { |
---|
| 63 | this->currentEl = startElement; |
---|
[3669] | 64 | this->tmpEl = NULL; |
---|
[4694] | 65 | this->startElement = startElement; |
---|
[3652] | 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
[4488] | 69 | /** |
---|
[4836] | 70 | * the destructor |
---|
[4488] | 71 | */ |
---|
[3652] | 72 | template<class T> |
---|
[3831] | 73 | inline tIterator<T>::~tIterator () |
---|
[3652] | 74 | { |
---|
| 75 | this->currentEl = NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
[4488] | 79 | /** |
---|
[4836] | 80 | * use it to iterate through the list |
---|
| 81 | * @returns next list element |
---|
[4488] | 82 | */ |
---|
[3652] | 83 | template<class T> |
---|
| 84 | inline T* tIterator<T>::nextElement () |
---|
| 85 | { |
---|
[3668] | 86 | if( this->currentEl == NULL) |
---|
[3661] | 87 | return NULL; |
---|
| 88 | |
---|
[3669] | 89 | this->tmpEl = this->currentEl; |
---|
[3653] | 90 | this->currentEl = this->currentEl->next; |
---|
[3669] | 91 | return this->tmpEl->curr; |
---|
[3652] | 92 | } |
---|
| 93 | |
---|
[4694] | 94 | /** |
---|
[4836] | 95 | * gets the element after the selected one, sets the iterator to this point in the list |
---|
| 96 | * @param element the element to seek |
---|
| 97 | * @returns next list element |
---|
[3652] | 98 | |
---|
[4694] | 99 | Attention: if you seek an element, the iterator pointer will point to the NEXT listelement after the argument! |
---|
| 100 | */ |
---|
| 101 | template<class T> |
---|
| 102 | inline T* tIterator<T>::seekElement (T* element) |
---|
| 103 | { |
---|
| 104 | for(this->tmpEl = this->startElement; this->tmpEl != NULL; this->tmpEl = this->tmpEl->next) |
---|
| 105 | { |
---|
| 106 | if( unlikely(this->tmpEl->curr == element)) |
---|
| 107 | { |
---|
| 108 | if( this->tmpEl->next != NULL) |
---|
| 109 | { |
---|
| 110 | this->currentEl = this->tmpEl->next->next; |
---|
| 111 | return this->tmpEl->next->curr; |
---|
| 112 | } |
---|
| 113 | return NULL; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | return NULL; |
---|
| 117 | } |
---|
[3652] | 118 | |
---|
[4694] | 119 | |
---|
| 120 | |
---|
[4497] | 121 | /** |
---|
[4836] | 122 | * the list template class |
---|
[4497] | 123 | |
---|
| 124 | you will use this as a generic list for all type of objects |
---|
| 125 | */ |
---|
[4574] | 126 | template<class T> class tList |
---|
[2822] | 127 | { |
---|
| 128 | public: |
---|
| 129 | tList (); |
---|
| 130 | ~tList (); |
---|
| 131 | |
---|
[3365] | 132 | void add(T* entity); |
---|
| 133 | void remove(T* entity); |
---|
[4497] | 134 | void flush(); |
---|
[2822] | 135 | T* firstElement(); |
---|
[3790] | 136 | T* lastElement(); |
---|
[2822] | 137 | bool isEmpty(); |
---|
| 138 | int getSize(); |
---|
[4508] | 139 | bool inList(T* entity); |
---|
[3652] | 140 | tIterator<T>* getIterator(); |
---|
[3585] | 141 | T* nextElement(T* toEntity); |
---|
[2822] | 142 | T* toArray(); |
---|
[3652] | 143 | |
---|
| 144 | private: |
---|
[4497] | 145 | unsigned int size; //!< the size (lenght) of the list |
---|
| 146 | listElement<T>* first; //!< pointer to the first element |
---|
| 147 | listElement<T>* last; //!< pointer to the last element |
---|
| 148 | listElement<T>* currentEl; //!< pointer to the current element |
---|
[2822] | 149 | }; |
---|
| 150 | |
---|
| 151 | |
---|
[4497] | 152 | /** |
---|
[4836] | 153 | * the constructor |
---|
[4497] | 154 | */ |
---|
[2822] | 155 | template<class T> |
---|
[4574] | 156 | inline tList<T>::tList () |
---|
[2822] | 157 | { |
---|
| 158 | this->first = NULL; |
---|
| 159 | this->last = NULL; |
---|
| 160 | this->size = 0; |
---|
| 161 | } |
---|
| 162 | |
---|
[4497] | 163 | |
---|
| 164 | /** |
---|
[4836] | 165 | * the deconstructor |
---|
[4497] | 166 | |
---|
[4574] | 167 | this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will |
---|
[4497] | 168 | not be deleted |
---|
| 169 | */ |
---|
[2822] | 170 | template<class T> |
---|
[4574] | 171 | inline tList<T>::~tList () |
---|
[3553] | 172 | { |
---|
| 173 | this->currentEl = this->first; |
---|
| 174 | while(this->currentEl != NULL) |
---|
| 175 | { |
---|
[3652] | 176 | listElement<T>* le = this->currentEl->next; |
---|
[3553] | 177 | //delete this->currentEl->curr; |
---|
| 178 | delete this->currentEl; |
---|
| 179 | this->currentEl = le; |
---|
| 180 | } |
---|
| 181 | this->first = NULL; |
---|
| 182 | this->last = NULL; |
---|
| 183 | this->size = 0; |
---|
| 184 | } |
---|
[2822] | 185 | |
---|
[3553] | 186 | |
---|
[4497] | 187 | /** |
---|
[4836] | 188 | * add an entity to the list |
---|
| 189 | * @param entity: the entity to add |
---|
[4497] | 190 | */ |
---|
[2822] | 191 | template<class T> |
---|
[3661] | 192 | inline void tList<T>::add(T* entity) |
---|
[2822] | 193 | { |
---|
[3860] | 194 | if( unlikely(entity == NULL)) return; |
---|
[3652] | 195 | listElement<T>* el = new listElement<T>; |
---|
[2822] | 196 | el->prev = this->last; |
---|
| 197 | el->curr = entity; |
---|
| 198 | el->next = NULL; |
---|
| 199 | |
---|
| 200 | this->last = el; |
---|
| 201 | |
---|
[3860] | 202 | if( unlikely(el->prev == NULL)) this->first = el; /* if first element */ |
---|
[2822] | 203 | else el->prev->next = el; |
---|
| 204 | this->size++; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
[4497] | 208 | /** |
---|
[4836] | 209 | * remove an entity from the list |
---|
| 210 | * @param entity: the entity to be removed |
---|
[4497] | 211 | */ |
---|
[2822] | 212 | template<class T> |
---|
[3661] | 213 | inline void tList<T>::remove(T* entity) |
---|
[2822] | 214 | { |
---|
| 215 | this->currentEl = this->first; |
---|
[3652] | 216 | listElement<T>* te; |
---|
[2822] | 217 | while( this->currentEl != NULL) |
---|
| 218 | { |
---|
[3860] | 219 | if( unlikely(this->currentEl->curr == entity)) |
---|
[4574] | 220 | { |
---|
| 221 | if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; |
---|
| 222 | else this->currentEl->prev->next = this->currentEl->next; |
---|
[2822] | 223 | |
---|
[4574] | 224 | if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; |
---|
| 225 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
[2822] | 226 | |
---|
[4574] | 227 | delete this->currentEl; |
---|
| 228 | this->size--; |
---|
| 229 | return; |
---|
| 230 | } |
---|
[2822] | 231 | this->currentEl = this->currentEl->next; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | |
---|
[4497] | 236 | /** |
---|
[4836] | 237 | * this will deletes the objects from the list |
---|
[4497] | 238 | */ |
---|
[2822] | 239 | template<class T> |
---|
[4497] | 240 | inline void tList<T>::flush() |
---|
[2822] | 241 | { |
---|
| 242 | this->currentEl = this->first; |
---|
| 243 | while(this->currentEl != NULL) |
---|
| 244 | { |
---|
[3652] | 245 | listElement<T>* le = this->currentEl->next; |
---|
[4497] | 246 | delete this->currentEl->curr; |
---|
[2822] | 247 | delete this->currentEl; |
---|
| 248 | this->currentEl = le; |
---|
| 249 | } |
---|
| 250 | this->first = NULL; |
---|
| 251 | this->last = NULL; |
---|
| 252 | this->size = 0; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | |
---|
[4497] | 256 | /** |
---|
[4836] | 257 | * returns the first element of the list |
---|
| 258 | * @returns first element |
---|
[4497] | 259 | */ |
---|
[2822] | 260 | template<class T> |
---|
[3831] | 261 | inline T* tList<T>::firstElement() |
---|
[2822] | 262 | { |
---|
| 263 | return this->first->curr; |
---|
| 264 | } |
---|
| 265 | |
---|
[3832] | 266 | |
---|
[4497] | 267 | /** |
---|
[4836] | 268 | * function returns the last element of the list |
---|
| 269 | * @returns the last element |
---|
[4497] | 270 | */ |
---|
[3790] | 271 | template<class T> |
---|
[3831] | 272 | inline T* tList<T>::lastElement() |
---|
[3790] | 273 | { |
---|
| 274 | return this->last->curr; |
---|
| 275 | } |
---|
[2822] | 276 | |
---|
[3790] | 277 | |
---|
[4497] | 278 | /** |
---|
[4836] | 279 | * returns true if the list is empty |
---|
| 280 | * @returns true if the list is empty |
---|
[4497] | 281 | */ |
---|
[2822] | 282 | template<class T> |
---|
[3831] | 283 | inline bool tList<T>::isEmpty() |
---|
[2822] | 284 | { |
---|
| 285 | return (this->size==0)?true:false; |
---|
| 286 | } |
---|
| 287 | |
---|
[4508] | 288 | /** |
---|
[4836] | 289 | * checks if an entity is in the List |
---|
| 290 | * @param entity The entity to check for in the entire List. |
---|
| 291 | * @returns true if it is, false otherwise |
---|
[4508] | 292 | */ |
---|
| 293 | template<class T> |
---|
| 294 | inline bool tList<T>::inList(T* entity) |
---|
[4574] | 295 | { |
---|
[4508] | 296 | // pre checks |
---|
| 297 | if(this->size == 0) return false; |
---|
| 298 | if( entity == NULL) return false; |
---|
[2822] | 299 | |
---|
[4508] | 300 | // search in the List |
---|
| 301 | this->currentEl = this->first; |
---|
| 302 | while(this->currentEl->curr != entity && this->currentEl != NULL) |
---|
| 303 | this->currentEl = this->currentEl->next; |
---|
| 304 | |
---|
| 305 | // post checks |
---|
[4574] | 306 | if(this->currentEl == NULL) |
---|
[4508] | 307 | return false; |
---|
| 308 | else |
---|
| 309 | return true; |
---|
| 310 | } |
---|
| 311 | |
---|
[4497] | 312 | /** |
---|
[4836] | 313 | * this returns the number of elements in the list |
---|
| 314 | * @returns number of elements |
---|
[4497] | 315 | */ |
---|
[2822] | 316 | template<class T> |
---|
[3831] | 317 | inline int tList<T>::getSize() |
---|
[2822] | 318 | { |
---|
| 319 | return this->size; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | |
---|
[4497] | 323 | /** |
---|
[4836] | 324 | * creates an itereator object and returns it |
---|
| 325 | * @returns the iterator object to this list |
---|
[4497] | 326 | |
---|
| 327 | You will use this, if you want to iterate through the list |
---|
[3832] | 328 | */ |
---|
[2822] | 329 | template<class T> |
---|
[3653] | 330 | inline tIterator<T>* tList<T>::getIterator() |
---|
[3652] | 331 | { |
---|
| 332 | tIterator<T>* iterator = new tIterator<T>(this->first); |
---|
| 333 | return iterator; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | |
---|
[2822] | 337 | |
---|
[3585] | 338 | /** |
---|
[4836] | 339 | * this returns the next element after toEntity or the first if toEntity is last |
---|
| 340 | * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase |
---|
| 341 | * @returns the element after toEntity |
---|
[3585] | 342 | */ |
---|
[2822] | 343 | template<class T> |
---|
[3831] | 344 | inline T* tList<T>::nextElement(T* toEntity) |
---|
[3585] | 345 | { |
---|
[3586] | 346 | //if( this->last == this->first == NULL) return NULL; |
---|
| 347 | if(this->size == 0) return NULL; |
---|
[3585] | 348 | if( toEntity == NULL) return this->first->curr; |
---|
| 349 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
| 350 | this->currentEl = this->first; |
---|
| 351 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
| 352 | { |
---|
| 353 | this->currentEl = this->currentEl->next; |
---|
| 354 | } |
---|
| 355 | if(this->currentEl == NULL) return NULL; |
---|
| 356 | return this->currentEl->next->curr; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | |
---|
[4497] | 360 | /** |
---|
[4836] | 361 | * creates an array out of the list (ATTENTION: not implemented) |
---|
| 362 | * @returns pointer to the array beginning |
---|
[4497] | 363 | |
---|
| 364 | ATTENTION: function is not implemented and wont do anything |
---|
| 365 | */ |
---|
[3585] | 366 | template<class T> |
---|
[2822] | 367 | T* tList<T>::toArray() |
---|
[4497] | 368 | { |
---|
| 369 | return NULL; |
---|
| 370 | } |
---|
[2822] | 371 | |
---|
[3224] | 372 | #endif /* _LIST_H */ |
---|