Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now using iterator in world instead of old construct. on my pc, there is now a speedup of about 1 fps! (instead of 1fps i ve now 2fps :)) )

File size: 5.5 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>
72inline tIterator<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 == this->firstEl)
95    {
96      this->currentEl = this->currentEl->next;
97      return this->firstEl->curr;
98    }
99  if( this->currentEl->next == NULL)
100    return NULL;
101  this->currentEl = this->currentEl->next;
102  return this->currentEl->curr;
103}
104
105
106
107template<class T> class tList
108{
109 
110 public:
111  tList ();
112  ~tList ();
113
114  void add(T* entity);
115  void remove(T* entity);
116  void destroy();
117  T* firstElement();
118  bool isEmpty();
119  int getSize();
120  T* enumerate();
121  tIterator<T>* getIterator();
122  T* nextElement();
123  T* nextElement(T* toEntity);
124  T* toArray();
125  void debug();
126
127 private:
128
129  Uint32 size;
130  listElement<T>* first;
131  listElement<T>* last;
132  listElement<T>* currentEl;
133
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>
163void 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>
180void 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->size--;
199          return;
200        }
201      this->currentEl = this->currentEl->next;
202    }
203}
204
205
206template<class T>
207void tList<T>::destroy()
208{
209  this->currentEl = this->first;
210  while(this->currentEl != NULL)
211    {
212      listElement<T>* le = this->currentEl->next;
213      //delete this->currentEl->curr;
214      delete this->currentEl;
215      this->currentEl = le;
216    }
217  this->first = NULL;
218  this->last = NULL;
219  this->size = 0;
220}
221
222
223template<class T>
224T* tList<T>::firstElement()
225{
226  return this->first->curr;
227}
228
229
230template<class T>
231bool tList<T>::isEmpty()
232{
233  return (this->size==0)?true:false;
234}
235
236
237template<class T>
238int tList<T>::getSize()
239{
240  return this->size;
241}
242
243
244template<class T>
245T* tList<T>::enumerate()
246{
247  //if( this->last == this->first == NULL) return NULL;
248  if( this->size == 0) return NULL;
249  this->currentEl = this->first;
250  return this->currentEl->curr;
251}
252
253
254template<class T>
255inline tIterator<T>* tList<T>::getIterator()
256{
257  if( this->size == 0) return NULL;
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.