Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/common/list.h @ 4486

Last change on this file since 4486 was 4486, checked in by bensch, 19 years ago

orxonox/trunk: more documentation in util

File size: 5.5 KB
Line 
1/*!
2  \file list.h
3  \brief a File that includes a List-template
4*/
5
6#ifndef _LIST_H
7#define _LIST_H
8
9#include "compiler.h"
10#ifndef NULL
11#define NULL 0
12#endif
13
14//! An enum to list all the modes available when adding an object to a List
15//enum ADDMODE {LIST_ADD_FIRST, LIST_ADD_LAST};
16//! An enum to list the two searching directions available when removing an object from a List
17//enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW};
18
19
20
21class WorldEntity;
22
23class List {
24
25 public:
26  List ();
27  ~List ();
28
29  void add(WorldEntity* entity);
30  void remove(WorldEntity* entity);
31  void destroy();
32  WorldEntity* firstElement();
33  bool isEmpty();
34  int getSize();
35  WorldEntity* enumerate();
36  WorldEntity* nextElement();
37  WorldEntity* toArray();
38  void debug();
39
40 private:
41  struct listElement
42  {
43    listElement* prev;
44    WorldEntity* curr;
45    listElement* next;
46  };
47  unsigned int size;
48  listElement* first;
49  listElement* last;
50  listElement* currentEl;
51
52
53};
54
55
56
57template<class T> struct listElement
58{
59  listElement* prev;
60  T* curr;
61  listElement* next;
62};
63
64template<class T> class tIterator
65{
66 public:
67  tIterator(listElement<T>* startElement);
68  ~tIterator();
69 
70  T* nextElement();
71
72 private:
73  listElement<T>* currentEl;
74  listElement<T>* tmpEl;
75};
76
77
78template<class T>
79inline tIterator<T>::tIterator (listElement<T>* startElement) 
80{
81  this->currentEl = startElement;
82  this->tmpEl = NULL;
83}
84
85
86template<class T>
87inline tIterator<T>::~tIterator ()
88{
89  this->currentEl = NULL;
90}
91
92
93template<class T>
94inline T* tIterator<T>::nextElement ()
95{
96  if( this->currentEl == NULL)
97    return NULL;
98
99  this->tmpEl = this->currentEl;
100  this->currentEl = this->currentEl->next;
101  return this->tmpEl->curr;
102}
103
104
105
106template<class T> class tList
107{
108 public:
109  tList ();
110  ~tList ();
111
112  void add(T* entity);
113  void remove(T* entity);
114  void destroy();
115  T* firstElement();
116  T* lastElement();
117  bool isEmpty();
118  int getSize();
119  //T* enumerate();
120  tIterator<T>* getIterator();
121  T* nextElement();
122  T* nextElement(T* toEntity);
123  T* toArray();
124  void debug();
125
126 private:
127  unsigned int size;
128  listElement<T>* first;
129  listElement<T>* last;
130  listElement<T>* currentEl;
131};
132
133
134template<class T>
135inline tList<T>::tList () 
136{
137  this->first = NULL;
138  this->last = NULL;
139  this->size = 0;
140}
141
142template<class T>
143inline tList<T>::~tList () 
144{
145  this->currentEl = this->first;
146  while(this->currentEl != NULL)
147    {
148      listElement<T>* le = this->currentEl->next;
149      //delete this->currentEl->curr;
150      delete this->currentEl;
151      this->currentEl = le;
152    }
153  this->first = NULL;
154  this->last = NULL;
155  this->size = 0;
156}
157
158
159template<class T>
160inline void tList<T>::add(T* entity)
161{
162  if( unlikely(entity == NULL)) return;
163  listElement<T>* el = new listElement<T>;
164  el->prev = this->last;
165  el->curr = entity;
166  el->next = NULL;
167
168  this->last = el;
169
170  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
171  else el->prev->next = el;
172  this->size++;
173}
174
175
176template<class T>
177inline void tList<T>::remove(T* entity)
178{
179  this->currentEl = this->first;
180  listElement<T>* te;
181  while( this->currentEl != NULL)
182    {
183      if( unlikely(this->currentEl->curr == entity))
184        { 
185          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
186          else this->currentEl->prev->next = this->currentEl->next;
187
188          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
189          else this->currentEl->next->prev = this->currentEl->prev;
190
191          delete this->currentEl;
192          this->size--;
193          return;
194        }
195      this->currentEl = this->currentEl->next;
196    }
197}
198
199
200template<class T>
201inline void tList<T>::destroy()
202{
203  this->currentEl = this->first;
204  while(this->currentEl != NULL)
205    {
206      listElement<T>* le = this->currentEl->next;
207      //delete this->currentEl->curr;
208      delete this->currentEl;
209      this->currentEl = le;
210    }
211  this->first = NULL;
212  this->last = NULL;
213  this->size = 0;
214}
215
216
217template<class T>
218inline T* tList<T>::firstElement()
219{
220  return this->first->curr;
221}
222
223
224template<class T>
225inline T* tList<T>::lastElement()
226{
227  return this->last->curr;
228}
229
230
231template<class T>
232inline bool tList<T>::isEmpty()
233{
234  return (this->size==0)?true:false;
235}
236
237
238template<class T>
239inline int tList<T>::getSize()
240{
241  return this->size;
242}
243
244
245/* deprecated */
246/*
247template<class T>
248T* tList<T>::enumerate()
249{
250  //if( this->last == this->first == NULL) return NULL;
251  if( this->size == 0) return NULL;
252  this->currentEl = this->first;
253  return this->currentEl->curr;
254}
255*/
256
257template<class T>
258inline tIterator<T>* tList<T>::getIterator()
259{
260  tIterator<T>* iterator = new tIterator<T>(this->first);
261  return iterator;
262}
263
264
265/* deprecated */
266template<class T>
267T* tList<T>::nextElement()
268{
269  // if( this->last == this->first == NULL) return NULL;
270  if( this->size == 0) return NULL;
271  this->currentEl = this->currentEl->next;
272  if( this->currentEl == NULL) return NULL;
273  return this->currentEl->curr;
274}
275
276
277/**
278   \brief this returns the next element after toEntity or the first if toEntity is last
279*/
280template<class T>
281inline T* tList<T>::nextElement(T* toEntity)
282{
283  //if( this->last == this->first == NULL) return NULL;
284  if(this->size == 0) return NULL;
285  if( toEntity == NULL) return this->first->curr;
286  if( toEntity == this->last->curr ) return this->first->curr;
287  this->currentEl = this->first;
288  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
289    {
290      this->currentEl = this->currentEl->next;
291    }
292  if(this->currentEl == NULL) return NULL;
293  return this->currentEl->next->curr;
294}
295
296
297template<class T>
298T* tList<T>::toArray()
299{}
300
301#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.