Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: made list more performant and less vulnerable to segfaults. changed the benchmark function to only display list attributes for now. changed pnode to iterator

File size: 5.4 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  int counter;
69};
70
71
72template<class T>
73inline tIterator<T>::tIterator (listElement<T>* startElement) 
74{
75  this->currentEl = startElement;
76  this->firstEl = startElement;
77  if( this->firstEl != NULL)
78    this->counter = -1; 
79  else 
80    this->counter = 0; 
81}
82
83
84template<class T>
85tIterator<T>::~tIterator ()
86{
87  this->currentEl = NULL;
88}
89
90
91template<class T>
92inline T* tIterator<T>::nextElement ()
93{
94  /*
95  this->counter++;
96  if( this->counter == 0)
97    return this->firstEl->curr;
98  */
99
100  if( this->currentEl == NULL)
101    return NULL;
102
103  listElement<T>* tmp = this->currentEl;
104  this->currentEl = this->currentEl->next;
105  return tmp->curr;
106}
107
108
109
110template<class T> class tList
111{
112 public:
113  tList ();
114  ~tList ();
115
116  void add(T* entity);
117  void remove(T* entity);
118  void destroy();
119  T* firstElement();
120  bool isEmpty();
121  int getSize();
122  T* enumerate();
123  tIterator<T>* getIterator();
124  T* nextElement();
125  T* nextElement(T* toEntity);
126  T* toArray();
127  void debug();
128
129 private:
130  Uint32 size;
131  listElement<T>* first;
132  listElement<T>* last;
133  listElement<T>* currentEl;
134};
135
136
137template<class T>
138tList<T>::tList () 
139{
140  this->first = NULL;
141  this->last = NULL;
142  this->size = 0;
143}
144
145template<class T>
146tList<T>::~tList () 
147{
148  this->currentEl = this->first;
149  while(this->currentEl != NULL)
150    {
151      listElement<T>* le = this->currentEl->next;
152      //delete this->currentEl->curr;
153      delete this->currentEl;
154      this->currentEl = le;
155    }
156  this->first = NULL;
157  this->last = NULL;
158  this->size = 0;
159}
160
161
162template<class T>
163inline void tList<T>::add(T* entity)
164{
165  if( entity == NULL) return;
166  listElement<T>* el = new listElement<T>;
167  el->prev = this->last;
168  el->curr = entity;
169  el->next = NULL;
170
171  this->last = el;
172
173  if(el->prev == NULL) this->first = el; /* if first element */
174  else el->prev->next = el;
175  this->size++;
176}
177
178
179template<class T>
180inline void tList<T>::remove(T* entity)
181{
182  if( entity == NULL) return;
183  this->currentEl = this->first;
184  listElement<T>* te;
185  while( this->currentEl != NULL)
186    {
187      if( this->currentEl->curr == entity)
188        { 
189          if( this->currentEl->prev  == NULL ) this->first = this->currentEl->next;
190          else this->currentEl->prev->next = this->currentEl->next;
191
192          if( this->currentEl->next == NULL) this->last = this->currentEl->prev;
193          else this->currentEl->next->prev = this->currentEl->prev;
194
195          //te = this->currentEl->next;  // for what am i doing this?
196          delete this->currentEl;
197          //this->currentEl = te;
198          this->currentEl = NULL;
199          this->size--;
200          return;
201        }
202      this->currentEl = this->currentEl->next;
203    }
204}
205
206
207template<class T>
208void tList<T>::destroy()
209{
210  this->currentEl = this->first;
211  while(this->currentEl != NULL)
212    {
213      listElement<T>* le = this->currentEl->next;
214      //delete this->currentEl->curr;
215      delete this->currentEl;
216      this->currentEl = le;
217    }
218  this->first = NULL;
219  this->last = NULL;
220  this->size = 0;
221}
222
223
224template<class T>
225T* tList<T>::firstElement()
226{
227  return this->first->curr;
228}
229
230
231template<class T>
232bool tList<T>::isEmpty()
233{
234  return (this->size==0)?true:false;
235}
236
237
238template<class T>
239int tList<T>::getSize()
240{
241  return this->size;
242}
243
244
245template<class T>
246T* tList<T>::enumerate()
247{
248  //if( this->last == this->first == NULL) return NULL;
249  if( this->size == 0) return NULL;
250  this->currentEl = this->first;
251  return this->currentEl->curr;
252}
253
254
255template<class T>
256inline tIterator<T>* tList<T>::getIterator()
257{
258  tIterator<T>* iterator = new tIterator<T>(this->first);
259  return iterator;
260}
261
262
263template<class T>
264T* tList<T>::nextElement()
265{
266  // if( this->last == this->first == NULL) return NULL;
267  if( this->size == 0) return NULL;
268  this->currentEl = this->currentEl->next;
269  if( this->currentEl == NULL) return NULL;
270  return this->currentEl->curr;
271}
272
273
274/**
275   \brief this returns the next element after toEntity or the first if toEntity is last
276*/
277template<class T>
278T* tList<T>::nextElement(T* toEntity)
279{
280  //if( this->last == this->first == NULL) return NULL;
281  if(this->size == 0) return NULL;
282  if( toEntity == NULL) return this->first->curr;
283  if( toEntity == this->last->curr ) return this->first->curr;
284  this->currentEl = this->first;
285  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
286    {
287      this->currentEl = this->currentEl->next;
288    }
289  if(this->currentEl == NULL) return NULL;
290  return this->currentEl->next->curr;
291}
292
293
294template<class T>
295T* tList<T>::toArray()
296{}
297
298#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.