Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: PNode debug info much better now (recursive)

File size: 7.4 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                                       //!< this will define NULL
12#endif
13
14
15
16//! a list element of the tList,
17template<class T> struct listElement
18{
19  listElement*        prev;                          //!< pointer to the previous listElement in the list
20  T*                  curr;                          //!< pointer to the list payload/container
21  listElement*        next;                          //!< pointer to the next listElement
22};
23
24/**
25   \brief an iterator class
26
27   this enables the user to iterate through a list very easely
28*/
29template<class T> class tIterator
30{
31 public:
32  tIterator(listElement<T>* startElement);
33  ~tIterator();
34
35  T* nextElement();
36
37 private:
38  listElement<T>*    currentEl;                      //!< pointer to the current list element in the iterator
39  listElement<T>*    tmpEl;                          //!< temp listElemnt pointer
40};
41
42
43/**
44   \brief iterator constructor
45   \param startElement:  the first list element from the tList
46
47   normaly you will use it like this:
48
49   tIterator<char>* nameIterator = nameList->getIterator();
50   char name* = nameIterator->nextElement();
51   while( name != NULL)
52   {
53     PRINTF(3)("found name: %s in list\n", name);
54     name = nameIterator->nextElement();
55   }
56   delete nameIterator;
57*/
58template<class T>
59inline tIterator<T>::tIterator (listElement<T>* startElement)
60{
61  this->currentEl = startElement;
62  this->tmpEl = NULL;
63}
64
65
66/**
67   \brief the destructor
68*/
69template<class T>
70inline tIterator<T>::~tIterator ()
71{
72  this->currentEl = NULL;
73}
74
75
76/**
77   \brief use it to iterate through the list
78   \returns next list element
79*/
80template<class T>
81inline T* tIterator<T>::nextElement ()
82{
83  if( this->currentEl == NULL)
84    return NULL;
85
86  this->tmpEl = this->currentEl;
87  this->currentEl = this->currentEl->next;
88  return this->tmpEl->curr;
89}
90
91
92
93/**
94   \brief the list template class
95
96   you will use this as a generic list for all type of objects
97*/
98template<class T> class tList
99{
100 public:
101  tList ();
102  ~tList ();
103
104  void add(T* entity);
105  void remove(T* entity);
106  void flush();
107  T* firstElement();
108  T* lastElement();
109  bool isEmpty();
110  int getSize();
111  //T* enumerate();
112  bool inList(T* entity);
113  tIterator<T>* getIterator();
114  T* nextElement(T* toEntity);
115  T* toArray();
116
117 private:
118  unsigned int       size;             //!< the size (lenght) of the list
119  listElement<T>*    first;            //!< pointer to the first element
120  listElement<T>*    last;             //!< pointer to the last element
121  listElement<T>*    currentEl;        //!< pointer to the current element
122};
123
124
125/**
126   \brief the constructor
127*/
128template<class T>
129inline tList<T>::tList ()
130{
131  this->first = NULL;
132  this->last = NULL;
133  this->size = 0;
134}
135
136
137/**
138   \brief the deconstructor
139
140   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will
141   not be deleted
142*/
143template<class T>
144inline tList<T>::~tList ()
145{
146  this->currentEl = this->first;
147  while(this->currentEl != NULL)
148    {
149      listElement<T>* le = this->currentEl->next;
150      //delete this->currentEl->curr;
151      delete this->currentEl;
152      this->currentEl = le;
153    }
154  this->first = NULL;
155  this->last = NULL;
156  this->size = 0;
157}
158
159
160/**
161   \brief add an entity to the list
162   \param entity: the entity to add
163*/
164template<class T>
165inline void tList<T>::add(T* entity)
166{
167  if( unlikely(entity == NULL)) return;
168  listElement<T>* el = new listElement<T>;
169  el->prev = this->last;
170  el->curr = entity;
171  el->next = NULL;
172
173  this->last = el;
174
175  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
176  else el->prev->next = el;
177  this->size++;
178}
179
180
181/**
182   \brief remove an entity from the list
183   \param entity: the entity to be removed
184*/
185template<class T>
186inline void tList<T>::remove(T* entity)
187{
188  this->currentEl = this->first;
189  listElement<T>* te;
190  while( this->currentEl != NULL)
191    {
192      if( unlikely(this->currentEl->curr == entity))
193        {
194          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
195          else this->currentEl->prev->next = this->currentEl->next;
196
197          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
198          else this->currentEl->next->prev = this->currentEl->prev;
199
200          delete this->currentEl;
201          this->size--;
202          return;
203        }
204      this->currentEl = this->currentEl->next;
205    }
206}
207
208
209/**
210   \brief this will deletes the objects from the list
211*/
212template<class T>
213inline void tList<T>::flush()
214{
215  this->currentEl = this->first;
216  while(this->currentEl != NULL)
217    {
218      listElement<T>* le = this->currentEl->next;
219      delete this->currentEl->curr;
220      delete this->currentEl;
221      this->currentEl = le;
222    }
223  this->first = NULL;
224  this->last = NULL;
225  this->size = 0;
226}
227
228
229/**
230   \brief returns the first element of the list
231   \returns first element
232*/
233template<class T>
234inline T* tList<T>::firstElement()
235{
236  return this->first->curr;
237}
238
239
240/**
241   \brief function returns the last element of the list
242   \returns the last element
243*/
244template<class T>
245inline T* tList<T>::lastElement()
246{
247  return this->last->curr;
248}
249
250
251/**
252   \brief returns true if the list is empty
253   \returns true if the list is empty
254*/
255template<class T>
256inline bool tList<T>::isEmpty()
257{
258  return (this->size==0)?true:false;
259}
260
261/**
262   \brief checks if an entity is in the List
263   \param entity The entity to check for in the entire List.
264   \returns true if it is, false otherwise
265*/
266template<class T>
267inline bool tList<T>::inList(T* entity)
268{
269  // pre checks
270  if(this->size == 0) return false;
271  if( entity == NULL) return false;
272
273  // search in the List
274  this->currentEl = this->first;
275  while(this->currentEl->curr != entity && this->currentEl != NULL)
276    this->currentEl = this->currentEl->next;
277
278  // post checks
279  if(this->currentEl == NULL)
280    return false;
281  else
282    return true;
283}
284
285/**
286   \brief this returns the number of elements in the list
287   \returns number of elements
288*/
289template<class T>
290inline int tList<T>::getSize()
291{
292  return this->size;
293}
294
295
296/**
297   \brief creates an itereator object and returns it
298   \returns the iterator object to this list
299
300   You will use this, if you want to iterate through the list
301*/
302template<class T>
303inline tIterator<T>* tList<T>::getIterator()
304{
305  tIterator<T>* iterator = new tIterator<T>(this->first);
306  return iterator;
307}
308
309
310
311/**
312   \brief this returns the next element after toEntity or the first if toEntity is last
313   \param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase
314   \returns the element after toEntity
315*/
316template<class T>
317inline T* tList<T>::nextElement(T* toEntity)
318{
319  //if( this->last == this->first == NULL) return NULL;
320  if(this->size == 0) return NULL;
321  if( toEntity == NULL) return this->first->curr;
322  if( toEntity == this->last->curr ) return this->first->curr;
323  this->currentEl = this->first;
324  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
325    {
326      this->currentEl = this->currentEl->next;
327    }
328  if(this->currentEl == NULL) return NULL;
329  return this->currentEl->next->curr;
330}
331
332
333/**
334   \brief creates an array out of the list (ATTENTION: not implemented)
335   \returns pointer to the array beginning
336
337   ATTENTION: function is not implemented and wont do anything
338*/
339template<class T>
340T* tList<T>::toArray()
341{
342  return NULL;
343}
344
345#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.