Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: altered the list function to make it more performant the world now uses iterators everywhere.

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