Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/list.h @ 5331

Last change on this file since 5331 was 5285, checked in by bensch, 19 years ago

orxonox/trunk: nicer quit-modi
TextEngine is now deleted by GraphicsEngine
trying to fix errors in the Element2D deletion

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