Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3848 was 3832, checked in by patrick, 20 years ago

orxonox/trunk: animation class functions implemented, list enhanced

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>* 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>
80inline tIterator<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>
128inline tList<T>::tList () 
129{
130  this->first = NULL;
131  this->last = NULL;
132  this->size = 0;
133}
134
135template<class T>
136inline tList<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  __UNLIKELY_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  __UNLIKELY_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  this->currentEl = this->first;
173  listElement<T>* te;
174  while( this->currentEl != NULL)
175    {
176      __UNLIKELY_IF( this->currentEl->curr == entity)
177        { 
178          __UNLIKELY_IF( this->currentEl->prev  == NULL ) this->first = this->currentEl->next;
179          else this->currentEl->prev->next = this->currentEl->next;
180
181          __UNLIKELY_IF( this->currentEl->next == NULL) this->last = this->currentEl->prev;
182          else this->currentEl->next->prev = this->currentEl->prev;
183
184          delete this->currentEl;
185          this->size--;
186          return;
187        }
188      this->currentEl = this->currentEl->next;
189    }
190}
191
192
193template<class T>
194inline void tList<T>::destroy()
195{
196  this->currentEl = this->first;
197  while(this->currentEl != NULL)
198    {
199      listElement<T>* le = this->currentEl->next;
200      //delete this->currentEl->curr;
201      delete this->currentEl;
202      this->currentEl = le;
203    }
204  this->first = NULL;
205  this->last = NULL;
206  this->size = 0;
207}
208
209
210template<class T>
211inline T* tList<T>::firstElement()
212{
213  return this->first->curr;
214}
215
216
217template<class T>
218inline T* tList<T>::lastElement()
219{
220  return this->last->curr;
221}
222
223
224template<class T>
225inline bool tList<T>::isEmpty()
226{
227  return (this->size==0)?true:false;
228}
229
230
231template<class T>
232inline int tList<T>::getSize()
233{
234  return this->size;
235}
236
237
238/* deprecated */
239/*
240template<class T>
241T* tList<T>::enumerate()
242{
243  //if( this->last == this->first == NULL) return NULL;
244  if( this->size == 0) return NULL;
245  this->currentEl = this->first;
246  return this->currentEl->curr;
247}
248*/
249
250template<class T>
251inline tIterator<T>* tList<T>::getIterator()
252{
253  tIterator<T>* iterator = new tIterator<T>(this->first);
254  return iterator;
255}
256
257
258/* deprecated */
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>
274inline T* 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.