[4486] | 1 | /*! |
---|
[5068] | 2 | * @file list.h |
---|
| 3 | * 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 | |
---|
[5115] | 14 | template<class T> class tIterator; |
---|
[2036] | 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 | * the list template class |
---|
[4497] | 26 | |
---|
| 27 | you will use this as a generic list for all type of objects |
---|
| 28 | */ |
---|
[4574] | 29 | template<class T> class tList |
---|
[2822] | 30 | { |
---|
[5115] | 31 | friend class tIterator<T>; |
---|
| 32 | |
---|
[2822] | 33 | public: |
---|
| 34 | tList (); |
---|
| 35 | ~tList (); |
---|
| 36 | |
---|
[3365] | 37 | void add(T* entity); |
---|
[5074] | 38 | void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM |
---|
[5171] | 39 | void remove(const T* entity); |
---|
[5068] | 40 | void removeLast(); |
---|
[4497] | 41 | void flush(); |
---|
[5113] | 42 | T* firstElement() const; |
---|
| 43 | T* lastElement() const; |
---|
| 44 | bool isEmpty() const; |
---|
| 45 | unsigned int getSize() const; |
---|
[4508] | 46 | bool inList(T* entity); |
---|
[5113] | 47 | tIterator<T>* getIterator() const; |
---|
[3585] | 48 | T* nextElement(T* toEntity); |
---|
[2822] | 49 | T* toArray(); |
---|
[3652] | 50 | |
---|
| 51 | private: |
---|
[4497] | 52 | unsigned int size; //!< the size (lenght) of the list |
---|
| 53 | listElement<T>* first; //!< pointer to the first element |
---|
| 54 | listElement<T>* last; //!< pointer to the last element |
---|
| 55 | listElement<T>* currentEl; //!< pointer to the current element |
---|
[2822] | 56 | }; |
---|
| 57 | |
---|
| 58 | |
---|
[4497] | 59 | /** |
---|
[4836] | 60 | * the constructor |
---|
[4497] | 61 | */ |
---|
[2822] | 62 | template<class T> |
---|
[4574] | 63 | inline tList<T>::tList () |
---|
[2822] | 64 | { |
---|
| 65 | this->first = NULL; |
---|
| 66 | this->last = NULL; |
---|
| 67 | this->size = 0; |
---|
| 68 | } |
---|
| 69 | |
---|
[4497] | 70 | |
---|
| 71 | /** |
---|
[4836] | 72 | * the deconstructor |
---|
[4497] | 73 | |
---|
[4574] | 74 | this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will |
---|
[4497] | 75 | not be deleted |
---|
| 76 | */ |
---|
[2822] | 77 | template<class T> |
---|
[4574] | 78 | inline tList<T>::~tList () |
---|
[3553] | 79 | { |
---|
| 80 | this->currentEl = this->first; |
---|
| 81 | while(this->currentEl != NULL) |
---|
| 82 | { |
---|
[3652] | 83 | listElement<T>* le = this->currentEl->next; |
---|
[5211] | 84 | //delete this->currentEl->curr; //! THIS IS EXTREMELY UNSAFE (the list only stores pointers not instances) // |
---|
[3553] | 85 | delete this->currentEl; |
---|
| 86 | this->currentEl = le; |
---|
| 87 | } |
---|
| 88 | this->first = NULL; |
---|
| 89 | this->last = NULL; |
---|
| 90 | this->size = 0; |
---|
| 91 | } |
---|
[2822] | 92 | |
---|
[3553] | 93 | |
---|
[4497] | 94 | /** |
---|
[4836] | 95 | * add an entity to the list |
---|
| 96 | * @param entity: the entity to add |
---|
[4497] | 97 | */ |
---|
[2822] | 98 | template<class T> |
---|
[3661] | 99 | inline void tList<T>::add(T* entity) |
---|
[2822] | 100 | { |
---|
[3860] | 101 | if( unlikely(entity == NULL)) return; |
---|
[3652] | 102 | listElement<T>* el = new listElement<T>; |
---|
[2822] | 103 | el->prev = this->last; |
---|
| 104 | el->curr = entity; |
---|
| 105 | el->next = NULL; |
---|
| 106 | |
---|
| 107 | this->last = el; |
---|
| 108 | |
---|
[3860] | 109 | if( unlikely(el->prev == NULL)) this->first = el; /* if first element */ |
---|
[2822] | 110 | else el->prev->next = el; |
---|
| 111 | this->size++; |
---|
| 112 | } |
---|
| 113 | |
---|
[5073] | 114 | /** |
---|
| 115 | * add an entity to the list |
---|
| 116 | * @param entity: the entity to add |
---|
| 117 | */ |
---|
| 118 | template<class T> |
---|
[5074] | 119 | inline void tList<T>::addAtBeginning(T* entity) |
---|
[5073] | 120 | { |
---|
| 121 | if( unlikely(entity == NULL)) return; |
---|
| 122 | listElement<T>* el = new listElement<T>; |
---|
| 123 | el->next = this->first; |
---|
| 124 | el->curr = entity; |
---|
| 125 | el->prev = NULL; |
---|
[2822] | 126 | |
---|
[5073] | 127 | this->first = el; |
---|
| 128 | |
---|
| 129 | if( unlikely(el->next == NULL)) this->first = el; /* if first element */ |
---|
| 130 | else el->next->prev = el; |
---|
| 131 | this->size++; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
[4497] | 135 | /** |
---|
[4836] | 136 | * remove an entity from the list |
---|
| 137 | * @param entity: the entity to be removed |
---|
[4497] | 138 | */ |
---|
[2822] | 139 | template<class T> |
---|
[5171] | 140 | inline void tList<T>::remove(const T* entity) |
---|
[2822] | 141 | { |
---|
| 142 | this->currentEl = this->first; |
---|
| 143 | while( this->currentEl != NULL) |
---|
| 144 | { |
---|
[3860] | 145 | if( unlikely(this->currentEl->curr == entity)) |
---|
[4574] | 146 | { |
---|
| 147 | if( unlikely(this->currentEl->prev == NULL)) this->first = this->currentEl->next; |
---|
| 148 | else this->currentEl->prev->next = this->currentEl->next; |
---|
[2822] | 149 | |
---|
[4574] | 150 | if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev; |
---|
| 151 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
[2822] | 152 | |
---|
[4574] | 153 | delete this->currentEl; |
---|
| 154 | this->size--; |
---|
| 155 | return; |
---|
| 156 | } |
---|
[2822] | 157 | this->currentEl = this->currentEl->next; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
[5068] | 161 | /** |
---|
| 162 | * removes the Last Element of the List |
---|
| 163 | */ |
---|
| 164 | template<class T> |
---|
| 165 | inline void tList<T>::removeLast() |
---|
| 166 | { |
---|
| 167 | if (this->last == NULL) |
---|
| 168 | return; |
---|
| 169 | else if (this->last == this->first) |
---|
| 170 | { |
---|
| 171 | delete this->first; |
---|
| 172 | this->first = NULL; |
---|
| 173 | this->last = NULL; |
---|
| 174 | this->size--; |
---|
| 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | listElement<T>* delLast = this->last; |
---|
| 179 | this->last->prev->next = NULL; |
---|
| 180 | this->last = this->last->prev; |
---|
| 181 | delete delLast; |
---|
| 182 | } |
---|
| 183 | } |
---|
[2822] | 184 | |
---|
[4497] | 185 | /** |
---|
[4836] | 186 | * this will deletes the objects from the list |
---|
[4497] | 187 | */ |
---|
[2822] | 188 | template<class T> |
---|
[4497] | 189 | inline void tList<T>::flush() |
---|
[2822] | 190 | { |
---|
| 191 | this->currentEl = this->first; |
---|
| 192 | while(this->currentEl != NULL) |
---|
| 193 | { |
---|
[3652] | 194 | listElement<T>* le = this->currentEl->next; |
---|
[4497] | 195 | delete this->currentEl->curr; |
---|
[2822] | 196 | delete this->currentEl; |
---|
| 197 | this->currentEl = le; |
---|
| 198 | } |
---|
| 199 | this->first = NULL; |
---|
| 200 | this->last = NULL; |
---|
| 201 | this->size = 0; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
[4497] | 205 | /** |
---|
[4836] | 206 | * returns the first element of the list |
---|
| 207 | * @returns first element |
---|
[4497] | 208 | */ |
---|
[2822] | 209 | template<class T> |
---|
[5113] | 210 | inline T* tList<T>::firstElement() const |
---|
[2822] | 211 | { |
---|
| 212 | return this->first->curr; |
---|
| 213 | } |
---|
| 214 | |
---|
[3832] | 215 | |
---|
[4497] | 216 | /** |
---|
[4836] | 217 | * function returns the last element of the list |
---|
| 218 | * @returns the last element |
---|
[4497] | 219 | */ |
---|
[3790] | 220 | template<class T> |
---|
[5113] | 221 | inline T* tList<T>::lastElement() const |
---|
[3790] | 222 | { |
---|
| 223 | return this->last->curr; |
---|
| 224 | } |
---|
[2822] | 225 | |
---|
[3790] | 226 | |
---|
[4497] | 227 | /** |
---|
[4836] | 228 | * returns true if the list is empty |
---|
| 229 | * @returns true if the list is empty |
---|
[4497] | 230 | */ |
---|
[2822] | 231 | template<class T> |
---|
[5113] | 232 | inline bool tList<T>::isEmpty() const |
---|
[2822] | 233 | { |
---|
| 234 | return (this->size==0)?true:false; |
---|
| 235 | } |
---|
| 236 | |
---|
[4508] | 237 | /** |
---|
[4836] | 238 | * checks if an entity is in the List |
---|
| 239 | * @param entity The entity to check for in the entire List. |
---|
| 240 | * @returns true if it is, false otherwise |
---|
[4508] | 241 | */ |
---|
| 242 | template<class T> |
---|
| 243 | inline bool tList<T>::inList(T* entity) |
---|
[4574] | 244 | { |
---|
[4508] | 245 | // pre checks |
---|
| 246 | if(this->size == 0) return false; |
---|
| 247 | if( entity == NULL) return false; |
---|
[2822] | 248 | |
---|
[4508] | 249 | // search in the List |
---|
| 250 | this->currentEl = this->first; |
---|
| 251 | while(this->currentEl->curr != entity && this->currentEl != NULL) |
---|
| 252 | this->currentEl = this->currentEl->next; |
---|
| 253 | |
---|
| 254 | // post checks |
---|
[4574] | 255 | if(this->currentEl == NULL) |
---|
[4508] | 256 | return false; |
---|
| 257 | else |
---|
| 258 | return true; |
---|
| 259 | } |
---|
| 260 | |
---|
[4497] | 261 | /** |
---|
[4836] | 262 | * this returns the number of elements in the list |
---|
| 263 | * @returns number of elements |
---|
[4497] | 264 | */ |
---|
[2822] | 265 | template<class T> |
---|
[5113] | 266 | inline unsigned int tList<T>::getSize() const |
---|
[2822] | 267 | { |
---|
| 268 | return this->size; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
[4497] | 272 | /** |
---|
[4836] | 273 | * creates an itereator object and returns it |
---|
| 274 | * @returns the iterator object to this list |
---|
[4497] | 275 | |
---|
| 276 | You will use this, if you want to iterate through the list |
---|
[3832] | 277 | */ |
---|
[2822] | 278 | template<class T> |
---|
[5113] | 279 | inline tIterator<T>* tList<T>::getIterator() const |
---|
[3652] | 280 | { |
---|
[5115] | 281 | return new tIterator<T>(this); |
---|
[3652] | 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
[2822] | 285 | |
---|
[3585] | 286 | /** |
---|
[4836] | 287 | * this returns the next element after toEntity or the first if toEntity is last |
---|
| 288 | * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase |
---|
| 289 | * @returns the element after toEntity |
---|
[3585] | 290 | */ |
---|
[2822] | 291 | template<class T> |
---|
[3831] | 292 | inline T* tList<T>::nextElement(T* toEntity) |
---|
[3585] | 293 | { |
---|
[3586] | 294 | //if( this->last == this->first == NULL) return NULL; |
---|
| 295 | if(this->size == 0) return NULL; |
---|
[3585] | 296 | if( toEntity == NULL) return this->first->curr; |
---|
| 297 | if( toEntity == this->last->curr ) return this->first->curr; |
---|
| 298 | this->currentEl = this->first; |
---|
| 299 | while(this->currentEl->curr != toEntity && this->currentEl->next != NULL) |
---|
| 300 | { |
---|
| 301 | this->currentEl = this->currentEl->next; |
---|
| 302 | } |
---|
| 303 | if(this->currentEl == NULL) return NULL; |
---|
| 304 | return this->currentEl->next->curr; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | |
---|
[4497] | 308 | /** |
---|
[4836] | 309 | * creates an array out of the list (ATTENTION: not implemented) |
---|
| 310 | * @returns pointer to the array beginning |
---|
[4497] | 311 | |
---|
| 312 | ATTENTION: function is not implemented and wont do anything |
---|
| 313 | */ |
---|
[3585] | 314 | template<class T> |
---|
[2822] | 315 | T* tList<T>::toArray() |
---|
[4497] | 316 | { |
---|
| 317 | return NULL; |
---|
| 318 | } |
---|
[2822] | 319 | |
---|
[5115] | 320 | |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | /** |
---|
| 324 | * an iterator class |
---|
| 325 | |
---|
| 326 | this enables the user to iterate through a list very easely |
---|
| 327 | */ |
---|
| 328 | template<class T> class tIterator |
---|
| 329 | { |
---|
| 330 | public: |
---|
| 331 | tIterator(const tList<T>* list); |
---|
| 332 | ~tIterator(); |
---|
| 333 | |
---|
| 334 | T* firstElement(); |
---|
[5118] | 335 | T* lastElement(); |
---|
[5115] | 336 | T* nextElement(); |
---|
[5118] | 337 | T* prevElement(); |
---|
[5115] | 338 | T* seekElement(T* element); |
---|
| 339 | T* iteratorElement(const tIterator<T>* iterator); |
---|
| 340 | |
---|
| 341 | private: |
---|
| 342 | listElement<T>* currentEl; //!< pointer to the current list element in the iterator |
---|
| 343 | listElement<T>* tmpEl; //!< temp listElemnt pointer |
---|
| 344 | const tList<T>* list; //!< The List, that we want to iterate through |
---|
| 345 | }; |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | /** |
---|
| 349 | * iterator constructor |
---|
| 350 | * @param startElement: the first list element from the tList |
---|
| 351 | * |
---|
| 352 | * normaly you will use it like this: |
---|
| 353 | ********************************************************** |
---|
| 354 | * tIterator<char>* nameIterator = nameList->getIterator(); |
---|
| 355 | * char name* = nameIterator->firstElement(); |
---|
| 356 | * while( name != NULL) |
---|
| 357 | * { |
---|
| 358 | * PRINTF(3)("found name: %s in list\n", name); |
---|
| 359 | * name = nameIterator->nextElement(); |
---|
| 360 | * } |
---|
| 361 | * delete nameIterator; |
---|
| 362 | * ******************************************************** |
---|
| 363 | */ |
---|
| 364 | template<class T> |
---|
| 365 | inline tIterator<T>::tIterator (const tList<T>* list) |
---|
| 366 | { |
---|
[5209] | 367 | this->currentEl = NULL; |
---|
[5115] | 368 | this->tmpEl = NULL; |
---|
| 369 | this->list = list; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | /** |
---|
| 374 | * the destructor |
---|
| 375 | */ |
---|
| 376 | template<class T> |
---|
| 377 | inline tIterator<T>::~tIterator () |
---|
| 378 | { |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | template<class T> |
---|
| 382 | inline T* tIterator<T>::firstElement () |
---|
| 383 | { |
---|
[5131] | 384 | this->tmpEl = this->list->first; |
---|
| 385 | if (this->tmpEl != NULL) |
---|
| 386 | { |
---|
| 387 | this->currentEl = this->tmpEl->next; |
---|
| 388 | return this->tmpEl->curr; |
---|
| 389 | } |
---|
[5118] | 390 | else |
---|
[5115] | 391 | return NULL; |
---|
[5118] | 392 | } |
---|
| 393 | |
---|
| 394 | template<class T> |
---|
| 395 | inline T* tIterator<T>::lastElement () |
---|
| 396 | { |
---|
[5131] | 397 | this->tmpEl = this->list->last; |
---|
| 398 | if (this->tmpEl != NULL) |
---|
| 399 | { |
---|
| 400 | this->currentEl = tmpEl->prev; |
---|
| 401 | return this->tmpEl->curr; |
---|
| 402 | } |
---|
[5115] | 403 | else |
---|
[5118] | 404 | return NULL; |
---|
[5115] | 405 | } |
---|
| 406 | |
---|
| 407 | |
---|
| 408 | /** |
---|
| 409 | * use it to iterate through the list |
---|
| 410 | * @returns next list element |
---|
| 411 | */ |
---|
| 412 | template<class T> |
---|
| 413 | inline T* tIterator<T>::nextElement () |
---|
| 414 | { |
---|
| 415 | if( this->currentEl == NULL) |
---|
| 416 | return NULL; |
---|
| 417 | |
---|
[5131] | 418 | this->tmpEl = this->currentEl; |
---|
[5115] | 419 | this->currentEl = this->currentEl->next; |
---|
[5131] | 420 | |
---|
| 421 | return this->tmpEl->curr; |
---|
[5115] | 422 | } |
---|
| 423 | |
---|
| 424 | /** |
---|
[5118] | 425 | * use it to iterate backwards through the list |
---|
| 426 | * @returns next list element |
---|
| 427 | */ |
---|
| 428 | template<class T> |
---|
| 429 | inline T* tIterator<T>::prevElement () |
---|
| 430 | { |
---|
| 431 | if( this->currentEl == NULL) |
---|
| 432 | return NULL; |
---|
| 433 | |
---|
[5131] | 434 | this->tmpEl = this->currentEl; |
---|
[5118] | 435 | this->currentEl = this->currentEl->prev; |
---|
[5131] | 436 | |
---|
| 437 | return this->tmpEl->curr; |
---|
[5118] | 438 | } |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | /** |
---|
[5115] | 442 | * gets the element after the selected one, sets the iterator to this point in the list |
---|
| 443 | * @param element the element to seek |
---|
| 444 | * @returns current list element |
---|
| 445 | */ |
---|
| 446 | template<class T> |
---|
| 447 | inline T* tIterator<T>::seekElement (T* element) |
---|
| 448 | { |
---|
| 449 | if (element == NULL) |
---|
| 450 | { |
---|
| 451 | this->currentEl = NULL; |
---|
| 452 | return NULL; |
---|
| 453 | } |
---|
| 454 | this->tmpEl = this->list->first; |
---|
| 455 | while( this->tmpEl != NULL) |
---|
| 456 | { |
---|
| 457 | if( unlikely(this->tmpEl->curr == element)) |
---|
| 458 | { |
---|
| 459 | this->currentEl = this->tmpEl; |
---|
| 460 | return this->currentEl->curr; |
---|
| 461 | } |
---|
| 462 | this->tmpEl = this->tmpEl->next; |
---|
| 463 | } |
---|
| 464 | this->currentEl = NULL; |
---|
| 465 | return NULL; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | /** |
---|
| 469 | * grabs the iterator entity from another iterator |
---|
| 470 | * @param iterator the iterator to grab the local currentEl from |
---|
[5120] | 471 | * @returns the grabbed element (current). NULL if not found, or not defined |
---|
| 472 | * |
---|
| 473 | * Both iterators must be from the same List! |
---|
[5115] | 474 | */ |
---|
| 475 | template<class T> |
---|
| 476 | T* tIterator<T>::iteratorElement(const tIterator<T>* iterator) |
---|
| 477 | { |
---|
[5120] | 478 | if (iterator != NULL && iterator->list == this->list) |
---|
[5115] | 479 | { |
---|
| 480 | this->currentEl = iterator->currentEl; |
---|
| 481 | if (this->currentEl != NULL) |
---|
| 482 | return this->currentEl->curr; |
---|
| 483 | else |
---|
| 484 | return NULL; |
---|
| 485 | } |
---|
| 486 | else |
---|
| 487 | { |
---|
| 488 | this->currentEl = NULL; |
---|
| 489 | return NULL; |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | |
---|
[3224] | 494 | #endif /* _LIST_H */ |
---|