Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: changed the behaviour of the iterator.
Now it is soon possible to walk through a List from front and back, and tell the Iterator from where to seek

@patrick: i had to disable your Collision-Detection algorithms, because they had a seekElement inside, that i did not entirely grasp the meaning of….
trying to fix this now

File size: 9.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
[5110]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{
[5110]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();
[5103]42  T* firstElement() const;
43  T* lastElement() const;
44  bool isEmpty() const;
45  unsigned int getSize() const;
[4508]46  bool inList(T* entity);
[5103]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>
[5103]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>
[5103]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>
[5103]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>
[5103]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>
[5103]279inline tIterator<T>* tList<T>::getIterator() const
[3652]280{
[5110]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
[5110]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();
335    T* nextElement();
336    T* seekElement(T* element);
337
338  private:
339    listElement<T>*    currentEl;                      //!< pointer to the current list element in the iterator
340    listElement<T>*    tmpEl;                          //!< temp listElemnt pointer
341    const tList<T>*    list;                           //!< The List, that we want to iterate through
342};
343
344
345/**
346 *  iterator constructor
347 * @param startElement:  the first list element from the tList
348
349   normaly you will use it like this:
350
351   tIterator<char>* nameIterator = nameList->getIterator();
352                char name* = nameIterator->nextElement();
353                while( name != NULL)
354{
355                PRINTF(3)("found name: %s in list\n", name);
356                name = nameIterator->nextElement();
357}
358                delete nameIterator;
359 */
360                template<class T>
361                inline tIterator<T>::tIterator (const tList<T>* list)
362{
363  this->currentEl = list->first;
364  this->tmpEl = NULL;
365  this->list = list;
366}
367
368
369/**
370 *  the destructor
371 */
372template<class T>
373    inline tIterator<T>::~tIterator ()
374{
375  this->currentEl = NULL;
376}
377
378template<class T>
379    inline T* tIterator<T>::firstElement ()
380{
381  this->currentEl = this->list->first;
382  if (this->currentEl == NULL)
383    return NULL;
384  else
385    return this->currentEl->curr;
386}
387
388
389/**
390 *  use it to iterate through the list
391 * @returns next list element
392 */
393template<class T>
394    inline T* tIterator<T>::nextElement ()
395{
396  if( this->currentEl == NULL || this->currentEl->next == NULL)
397  {
398    this->currentEl = NULL;
399    return NULL;
400  }
401
402  this->currentEl = this->currentEl->next;
403  return this->currentEl->curr;
404}
405
406/**
407 *  gets the element after the selected one, sets the iterator to this point in the list
408 * @param element the element to seek
409 * @returns next list element
410
411  Attention: if you seek an element, the iterator pointer will point to the NEXT listelement after the argument!
412 */
413template<class T>
414    inline T* tIterator<T>::seekElement (T* element)
415{
416  for(this->tmpEl = this->list->first; this->tmpEl != NULL; this->tmpEl = this->tmpEl->next)
417  {
418    if( unlikely(this->tmpEl->curr == element))
419    {
420      if( this->tmpEl->next != NULL)
421      {
422        this->currentEl = this->tmpEl;
423        return this->tmpEl->next->curr;
424      }
425      return NULL;
426    }
427  }
428  return NULL;
429}
430
[3224]431#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.