Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5099 was 5076, checked in by bensch, 20 years ago

orxonox/trunk: minor

File size: 9.0 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
14
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 *  an iterator class
[4488]26
27   this enables the user to iterate through a list very easely
28*/
[3652]29template<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]60template<class T>
[4574]61inline 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]72template<class T>
[3831]73inline 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]83template<class T>
84inline 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 */
101template<class T>
102inline 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]126template<class T> class tList
[2822]127{
128 public:
129  tList ();
130  ~tList ();
131
[3365]132  void add(T* entity);
[5074]133  void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM
[3365]134  void remove(T* entity);
[5068]135  void removeLast();
[4497]136  void flush();
[2822]137  T* firstElement();
[3790]138  T* lastElement();
[2822]139  bool isEmpty();
[5076]140  unsigned int getSize();
[4508]141  bool inList(T* entity);
[3652]142  tIterator<T>* getIterator();
[3585]143  T* nextElement(T* toEntity);
[2822]144  T* toArray();
[3652]145
146 private:
[4497]147  unsigned int       size;             //!< the size (lenght) of the list
148  listElement<T>*    first;            //!< pointer to the first element
149  listElement<T>*    last;             //!< pointer to the last element
150  listElement<T>*    currentEl;        //!< pointer to the current element
[2822]151};
152
153
[4497]154/**
[4836]155 *  the constructor
[4497]156*/
[2822]157template<class T>
[4574]158inline tList<T>::tList ()
[2822]159{
160  this->first = NULL;
161  this->last = NULL;
162  this->size = 0;
163}
164
[4497]165
166/**
[4836]167 *  the deconstructor
[4497]168
[4574]169   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will
[4497]170   not be deleted
171*/
[2822]172template<class T>
[4574]173inline tList<T>::~tList ()
[3553]174{
175  this->currentEl = this->first;
176  while(this->currentEl != NULL)
177    {
[3652]178      listElement<T>* le = this->currentEl->next;
[3553]179      //delete this->currentEl->curr;
180      delete this->currentEl;
181      this->currentEl = le;
182    }
183  this->first = NULL;
184  this->last = NULL;
185  this->size = 0;
186}
[2822]187
[3553]188
[4497]189/**
[4836]190 *  add an entity to the list
191 * @param entity: the entity to add
[4497]192*/
[2822]193template<class T>
[3661]194inline void tList<T>::add(T* entity)
[2822]195{
[3860]196  if( unlikely(entity == NULL)) return;
[3652]197  listElement<T>* el = new listElement<T>;
[2822]198  el->prev = this->last;
199  el->curr = entity;
200  el->next = NULL;
201
202  this->last = el;
203
[3860]204  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
[2822]205  else el->prev->next = el;
206  this->size++;
207}
208
[5073]209/**
210 *  add an entity to the list
211 * @param entity: the entity to add
212 */
213template<class T>
[5074]214    inline void tList<T>::addAtBeginning(T* entity)
[5073]215{
216  if( unlikely(entity == NULL)) return;
217  listElement<T>* el = new listElement<T>;
218  el->next = this->first;
219  el->curr = entity;
220  el->prev = NULL;
[2822]221
[5073]222  this->first = el;
223
224  if( unlikely(el->next == NULL)) this->first = el; /* if first element */
225  else el->next->prev = el;
226  this->size++;
227}
228
229
[4497]230/**
[4836]231 *  remove an entity from the list
232 * @param entity: the entity to be removed
[4497]233*/
[2822]234template<class T>
[3661]235inline void tList<T>::remove(T* entity)
[2822]236{
237  this->currentEl = this->first;
238  while( this->currentEl != NULL)
239    {
[3860]240      if( unlikely(this->currentEl->curr == entity))
[4574]241        {
242          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
243          else this->currentEl->prev->next = this->currentEl->next;
[2822]244
[4574]245          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
246          else this->currentEl->next->prev = this->currentEl->prev;
[2822]247
[4574]248          delete this->currentEl;
249          this->size--;
250          return;
251        }
[2822]252      this->currentEl = this->currentEl->next;
253    }
254}
255
[5068]256/**
257 * removes the Last Element of the List
258 */
259 template<class T>
260     inline void tList<T>::removeLast()
261{
262  if (this->last == NULL)
263    return;
264  else if (this->last == this->first)
265  {
266    delete this->first;
267    this->first = NULL;
268    this->last = NULL;
269    this->size--;
270  }
271  else
272  {
273    listElement<T>* delLast = this->last;
274    this->last->prev->next = NULL;
275    this->last = this->last->prev;
276    delete delLast;
277  }
278}
[2822]279
[4497]280/**
[4836]281 *  this will deletes the objects from the list
[4497]282*/
[2822]283template<class T>
[4497]284inline void tList<T>::flush()
[2822]285{
286  this->currentEl = this->first;
287  while(this->currentEl != NULL)
288    {
[3652]289      listElement<T>* le = this->currentEl->next;
[4497]290      delete this->currentEl->curr;
[2822]291      delete this->currentEl;
292      this->currentEl = le;
293    }
294  this->first = NULL;
295  this->last = NULL;
296  this->size = 0;
297}
298
299
[4497]300/**
[4836]301 *  returns the first element of the list
302 * @returns first element
[4497]303*/
[2822]304template<class T>
[3831]305inline T* tList<T>::firstElement()
[2822]306{
307  return this->first->curr;
308}
309
[3832]310
[4497]311/**
[4836]312 *  function returns the last element of the list
313 * @returns the last element
[4497]314*/
[3790]315template<class T>
[3831]316inline T* tList<T>::lastElement()
[3790]317{
318  return this->last->curr;
319}
[2822]320
[3790]321
[4497]322/**
[4836]323 *  returns true if the list is empty
324 * @returns true if the list is empty
[4497]325*/
[2822]326template<class T>
[3831]327inline bool tList<T>::isEmpty()
[2822]328{
329  return (this->size==0)?true:false;
330}
331
[4508]332/**
[4836]333 *  checks if an entity is in the List
334 * @param entity The entity to check for in the entire List.
335 * @returns true if it is, false otherwise
[4508]336*/
337template<class T>
338inline bool tList<T>::inList(T* entity)
[4574]339{
[4508]340  // pre checks
341  if(this->size == 0) return false;
342  if( entity == NULL) return false;
[2822]343
[4508]344  // search in the List
345  this->currentEl = this->first;
346  while(this->currentEl->curr != entity && this->currentEl != NULL)
347    this->currentEl = this->currentEl->next;
348
349  // post checks
[4574]350  if(this->currentEl == NULL)
[4508]351    return false;
352  else
353    return true;
354}
355
[4497]356/**
[4836]357 *  this returns the number of elements in the list
358 * @returns number of elements
[4497]359*/
[2822]360template<class T>
[5076]361inline unsigned int tList<T>::getSize()
[2822]362{
363  return this->size;
364}
365
366
[4497]367/**
[4836]368 *  creates an itereator object and returns it
369 * @returns the iterator object to this list
[4497]370
371   You will use this, if you want to iterate through the list
[3832]372*/
[2822]373template<class T>
[3653]374inline tIterator<T>* tList<T>::getIterator()
[3652]375{
376  tIterator<T>* iterator = new tIterator<T>(this->first);
377  return iterator;
378}
379
380
[2822]381
[3585]382/**
[4836]383 *  this returns the next element after toEntity or the first if toEntity is last
384 * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase
385 * @returns the element after toEntity
[3585]386*/
[2822]387template<class T>
[3831]388inline T* tList<T>::nextElement(T* toEntity)
[3585]389{
[3586]390  //if( this->last == this->first == NULL) return NULL;
391  if(this->size == 0) return NULL;
[3585]392  if( toEntity == NULL) return this->first->curr;
393  if( toEntity == this->last->curr ) return this->first->curr;
394  this->currentEl = this->first;
395  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
396    {
397      this->currentEl = this->currentEl->next;
398    }
399  if(this->currentEl == NULL) return NULL;
400  return this->currentEl->next->curr;
401}
402
403
[4497]404/**
[4836]405 *  creates an array out of the list (ATTENTION: not implemented)
406 * @returns pointer to the array beginning
[4497]407
408   ATTENTION: function is not implemented and wont do anything
409*/
[3585]410template<class T>
[2822]411T* tList<T>::toArray()
[4497]412{
413  return NULL;
414}
[2822]415
[3224]416#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.