Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3831 was 3831, checked in by patrick, 19 years ago

orxonox/trunk: implemente animation sine function

File size: 5.5 KB
RevLine 
[2636]1
[3224]2#ifndef _LIST_H
3#define _LIST_H
[2036]4
[2816]5#include "stdincl.h"
[2036]6
[2077]7//! An enum to list all the modes available when adding an object to a List
[2816]8//enum ADDMODE {LIST_ADD_FIRST, LIST_ADD_LAST};
[2077]9//! An enum to list the two searching directions available when removing an object from a List
[2816]10//enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW};
[2036]11
12
[2077]13
[2816]14class WorldEntity;
[2036]15
[2816]16class List {
[2036]17
[2816]18 public:
19  List ();
20  ~List ();
[2036]21
[2816]22  void add(WorldEntity* entity);
23  void remove(WorldEntity* entity);
[3194]24  void destroy();
[2816]25  WorldEntity* firstElement();
26  bool isEmpty();
27  int getSize();
28  WorldEntity* enumerate();
29  WorldEntity* nextElement();
30  WorldEntity* toArray();
31  void debug();
[2077]32
[2816]33 private:
34  struct listElement
[2077]35  {
[2816]36    listElement* prev;
37    WorldEntity* curr;
38    listElement* next;
39  };
40  Uint32 size;
41  listElement* first;
42  listElement* last;
43  listElement* currentEl;
[2077]44
45
[2816]46};
[2077]47
[3652]48
49
50template<class T> struct listElement
[2077]51{
[3652]52  listElement* prev;
53  T* curr;
54  listElement* next;
55};
[2077]56
[3652]57template<class T> class tIterator
58{
[2816]59 public:
[3652]60  tIterator(listElement<T>* startElement);
61  ~tIterator();
62 
63  T* nextElement();
[2077]64
[2816]65 private:
[3652]66  listElement<T>* currentEl;
[3669]67  listElement<T>* tmpEl;
[2816]68};
69
[2822]70
[3652]71template<class T>
[3653]72inline tIterator<T>::tIterator (listElement<T>* startElement) 
[3652]73{
74  this->currentEl = startElement;
[3669]75  this->tmpEl = NULL;
[3652]76}
77
78
79template<class T>
[3831]80inline tIterator<T>::~tIterator ()
[3652]81{
82  this->currentEl = NULL;
83}
84
85
86template<class T>
87inline T* tIterator<T>::nextElement ()
88{
[3668]89  if( this->currentEl == NULL)
[3661]90    return NULL;
91
[3669]92  this->tmpEl = this->currentEl;
[3653]93  this->currentEl = this->currentEl->next;
[3669]94  return this->tmpEl->curr;
[3652]95}
96
97
98
[2822]99template<class T> class tList
100{
101 public:
102  tList ();
103  ~tList ();
104
[3365]105  void add(T* entity);
106  void remove(T* entity);
[3194]107  void destroy();
[2822]108  T* firstElement();
[3790]109  T* lastElement();
[2822]110  bool isEmpty();
111  int getSize();
112  T* enumerate();
[3652]113  tIterator<T>* getIterator();
[2822]114  T* nextElement();
[3585]115  T* nextElement(T* toEntity);
[2822]116  T* toArray();
117  void debug();
[3652]118
119 private:
120  Uint32 size;
121  listElement<T>* first;
122  listElement<T>* last;
123  listElement<T>* currentEl;
[2822]124};
125
126
127template<class T>
[3831]128inline tList<T>::tList () 
[2822]129{
130  this->first = NULL;
131  this->last = NULL;
132  this->size = 0;
133}
134
135template<class T>
[3831]136inline tList<T>::~tList () 
[3553]137{
138  this->currentEl = this->first;
139  while(this->currentEl != NULL)
140    {
[3652]141      listElement<T>* le = this->currentEl->next;
[3553]142      //delete this->currentEl->curr;
143      delete this->currentEl;
144      this->currentEl = le;
145    }
146  this->first = NULL;
147  this->last = NULL;
148  this->size = 0;
149}
[2822]150
[3553]151
[2822]152template<class T>
[3661]153inline void tList<T>::add(T* entity)
[2822]154{
[3831]155  __UNLIKELY_IF( entity == NULL) return;
[3652]156  listElement<T>* el = new listElement<T>;
[2822]157  el->prev = this->last;
158  el->curr = entity;
159  el->next = NULL;
160
161  this->last = el;
162
[3831]163  __UNLIKELY_IF(el->prev == NULL) this->first = el; /* if first element */
[2822]164  else el->prev->next = el;
165  this->size++;
166}
167
168
169template<class T>
[3661]170inline void tList<T>::remove(T* entity)
[2822]171{
[3831]172  //__UNLIKELY( entity == NULL) return;
[2822]173  this->currentEl = this->first;
[3652]174  listElement<T>* te;
[2822]175  while( this->currentEl != NULL)
176    {
177      if( this->currentEl->curr == entity)
[3221]178        { 
[2822]179          if( this->currentEl->prev  == NULL ) this->first = this->currentEl->next;
180          else this->currentEl->prev->next = this->currentEl->next;
181
182          if( this->currentEl->next == NULL) this->last = this->currentEl->prev;
183          else this->currentEl->next->prev = this->currentEl->prev;
184
[3668]185          //te = this->currentEl->next;  // for what am i doing this?
[2822]186          delete this->currentEl;
[3668]187          //this->currentEl = te;
188          this->currentEl = NULL;
[3534]189          this->size--;
[2822]190          return;
191        }
192      this->currentEl = this->currentEl->next;
193    }
194}
195
196
197template<class T>
[3831]198inline void tList<T>::destroy()
[2822]199{
200  this->currentEl = this->first;
201  while(this->currentEl != NULL)
202    {
[3652]203      listElement<T>* le = this->currentEl->next;
[3553]204      //delete this->currentEl->curr;
[2822]205      delete this->currentEl;
206      this->currentEl = le;
207    }
208  this->first = NULL;
209  this->last = NULL;
210  this->size = 0;
211}
212
213
214template<class T>
[3831]215inline T* tList<T>::firstElement()
[2822]216{
217  return this->first->curr;
218}
219
[3790]220template<class T>
[3831]221inline T* tList<T>::lastElement()
[3790]222{
223  return this->last->curr;
224}
[2822]225
[3790]226
[2822]227template<class T>
[3831]228inline bool tList<T>::isEmpty()
[2822]229{
230  return (this->size==0)?true:false;
231}
232
233
234template<class T>
[3831]235inline int tList<T>::getSize()
[2822]236{
237  return this->size;
238}
239
240
[3831]241/* deprecated */
[2822]242template<class T>
243T* tList<T>::enumerate()
244{
[3586]245  //if( this->last == this->first == NULL) return NULL;
[3652]246  if( this->size == 0) return NULL;
[2822]247  this->currentEl = this->first;
248  return this->currentEl->curr;
249}
250
251
252template<class T>
[3653]253inline tIterator<T>* tList<T>::getIterator()
[3652]254{
255  tIterator<T>* iterator = new tIterator<T>(this->first);
256  return iterator;
257}
258
259
[3831]260/* deprecated */
[3652]261template<class T>
[2822]262T* tList<T>::nextElement()
263{
[3586]264  // if( this->last == this->first == NULL) return NULL;
[3652]265  if( this->size == 0) return NULL;
[2822]266  this->currentEl = this->currentEl->next;
[3652]267  if( this->currentEl == NULL) return NULL;
[2822]268  return this->currentEl->curr;
269}
270
271
[3585]272/**
273   \brief this returns the next element after toEntity or the first if toEntity is last
274*/
[2822]275template<class T>
[3831]276inline T* tList<T>::nextElement(T* toEntity)
[3585]277{
[3586]278  //if( this->last == this->first == NULL) return NULL;
279  if(this->size == 0) return NULL;
[3585]280  if( toEntity == NULL) return this->first->curr;
281  if( toEntity == this->last->curr ) return this->first->curr;
282  this->currentEl = this->first;
283  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
284    {
285      this->currentEl = this->currentEl->next;
286    }
287  if(this->currentEl == NULL) return NULL;
288  return this->currentEl->next->curr;
289}
290
291
292template<class T>
[2822]293T* tList<T>::toArray()
294{}
295
[3224]296#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.