Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3814 was 3790, checked in by bensch, 20 years ago

orxonox/trunk: merged the textEngine back into the trunk.
merged with command:
svn merge -r 3681:HEAD branches/textEngine/ trunk/

conflicts in:
world.cc/h orxonox.cc NEWS
changed in favor of the trunk

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