Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better calculation of the Line-Positions in the Shell

File size: 10.6 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
28*/
[4574]29template<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
[3365]39  void remove(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]62template<class T>
[4574]63inline 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]77template<class T>
[4574]78inline tList<T>::~tList ()
[3553]79{
80  this->currentEl = this->first;
81  while(this->currentEl != NULL)
82    {
[3652]83      listElement<T>* le = this->currentEl->next;
[3553]84      //delete this->currentEl->curr;
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]98template<class T>
[3661]99inline 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 */
118template<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]139template<class T>
[3661]140inline void tList<T>::remove(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]188template<class T>
[4497]189inline 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]209template<class T>
[5113]210inline 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]220template<class T>
[5113]221inline 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]231template<class T>
[5113]232inline 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*/
242template<class T>
243inline 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]265template<class T>
[5113]266inline 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]278template<class T>
[5113]279inline 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]291template<class T>
[3831]292inline 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]314template<class T>
[2822]315T* 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 */
328template<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 */
364template<class T>
365  inline tIterator<T>::tIterator (const tList<T>* list)
366{
367  this->currentEl = list->first;
368  this->tmpEl = NULL;
369  this->list = list;
370}
371
372
373/**
374 *  the destructor
375 */
376template<class T>
377    inline tIterator<T>::~tIterator ()
378{
379}
380
381template<class T>
382    inline T* tIterator<T>::firstElement ()
383{
384  this->currentEl = this->list->first;
[5118]385  if (this->currentEl != NULL)
386    return this->currentEl->curr;
387  else
[5115]388    return NULL;
[5118]389}
390
391template<class T>
392    inline T* tIterator<T>::lastElement ()
393{
394  this->currentEl = this->list->last;
395  if (this->currentEl != NULL)
396    return this->currentEl->curr;
[5115]397  else
[5118]398    return NULL;
[5115]399}
400
401
402/**
403 *  use it to iterate through the list
404 * @returns next list element
405 */
406template<class T>
407    inline T* tIterator<T>::nextElement ()
408{
409  if( this->currentEl == NULL)
410    return NULL;
411
412  this->currentEl = this->currentEl->next;
413  if (this->currentEl != NULL)
414    return this->currentEl->curr;
415  else
416    return NULL;
417}
418
419/**
[5118]420 *  use it to iterate backwards through the list
421 * @returns next list element
422 */
423template<class T>
424    inline T* tIterator<T>::prevElement ()
425{
426  if( this->currentEl == NULL)
427    return NULL;
428
429  this->currentEl = this->currentEl->prev;
430  if (this->currentEl != NULL)
431    return this->currentEl->curr;
432  else
433    return NULL;
434}
435
436
437/**
[5115]438 *  gets the element after the selected one, sets the iterator to this point in the list
439 * @param element the element to seek
440 * @returns current list element
441 */
442template<class T>
443    inline T* tIterator<T>::seekElement (T* element)
444{
445  if (element == NULL)
446  {
447    this->currentEl = NULL;
448    return NULL;
449  }
450  this->tmpEl = this->list->first;
451  while( this->tmpEl != NULL)
452  {
453    if( unlikely(this->tmpEl->curr == element))
454    {
455      this->currentEl = this->tmpEl;
456      return this->currentEl->curr;
457    }
458    this->tmpEl = this->tmpEl->next;
459  }
460  this->currentEl = NULL;
461  return NULL;
462}
463
464/**
465 * grabs the iterator entity from another iterator
466 * @param iterator the iterator to grab the local currentEl from
[5120]467 * @returns the grabbed element (current). NULL if not found, or not defined
468 *
469 * Both iterators must be from the same List!
[5115]470 */
471template<class T>
472    T* tIterator<T>::iteratorElement(const tIterator<T>* iterator)
473{
[5120]474  if (iterator != NULL && iterator->list == this->list)
[5115]475  {
476    this->currentEl = iterator->currentEl;
477    if (this->currentEl != NULL)
478      return this->currentEl->curr;
479    else
480      return NULL;
481  }
482  else
483  {
484    this->currentEl = NULL;
485    return NULL;
486  }
487}
488
489
[3224]490#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.