Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3686 was 3669, checked in by patrick, 20 years ago

orxonox/trunk: now garbage collection is working correctlymake

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