Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added list iterator template. this is a fix in the list framework, that should I should have made long time ago. modified the list to make it more performant — benchmarking rulez

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