Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4499 was 4499, checked in by patrick, 19 years ago

orxonox/trunk: list is now correctly commented

File size: 6.8 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  tIterator<T>* getIterator();
113  T* nextElement(T* toEntity);
114  T* toArray();
115
116 private:
117  unsigned int       size;             //!< the size (lenght) of the list
118  listElement<T>*    first;            //!< pointer to the first element
119  listElement<T>*    last;             //!< pointer to the last element
120  listElement<T>*    currentEl;        //!< pointer to the current element
121};
122
123
124/**
125   \brief the constructor
126*/
127template<class T>
128inline tList<T>::tList () 
129{
130  this->first = NULL;
131  this->last = NULL;
132  this->size = 0;
133}
134
135
136/**
137   \brief the deconstructor
138
139   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will
140   not be deleted
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
159/**
160   \brief add an entity to the list
161   \param entity: the entity to add
162*/
163template<class T>
164inline void tList<T>::add(T* entity)
165{
166  if( unlikely(entity == NULL)) return;
167  listElement<T>* el = new listElement<T>;
168  el->prev = this->last;
169  el->curr = entity;
170  el->next = NULL;
171
172  this->last = el;
173
174  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
175  else el->prev->next = el;
176  this->size++;
177}
178
179
180/**
181   \brief remove an entity from the list
182   \param entity: the entity to be removed
183*/
184template<class T>
185inline void tList<T>::remove(T* entity)
186{
187  this->currentEl = this->first;
188  listElement<T>* te;
189  while( this->currentEl != NULL)
190    {
191      if( unlikely(this->currentEl->curr == entity))
192        { 
193          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
194          else this->currentEl->prev->next = this->currentEl->next;
195
196          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
197          else this->currentEl->next->prev = this->currentEl->prev;
198
199          delete this->currentEl;
200          this->size--;
201          return;
202        }
203      this->currentEl = this->currentEl->next;
204    }
205}
206
207
208/**
209   \brief this will deletes the objects from the list
210*/
211template<class T>
212inline void tList<T>::flush()
213{
214  this->currentEl = this->first;
215  while(this->currentEl != NULL)
216    {
217      listElement<T>* le = this->currentEl->next;
218      delete this->currentEl->curr;
219      delete this->currentEl;
220      this->currentEl = le;
221    }
222  this->first = NULL;
223  this->last = NULL;
224  this->size = 0;
225}
226
227
228/**
229   \brief returns the first element of the list
230   \returns first element
231*/
232template<class T>
233inline T* tList<T>::firstElement()
234{
235  return this->first->curr;
236}
237
238
239/**
240   \brief function returns the last element of the list
241   \returns the last element
242*/
243template<class T>
244inline T* tList<T>::lastElement()
245{
246  return this->last->curr;
247}
248
249
250/**
251   \brief returns true if the list is empty
252   \returns true if the list is empty
253*/
254template<class T>
255inline bool tList<T>::isEmpty()
256{
257  return (this->size==0)?true:false;
258}
259
260
261/**
262   \brief this returns the number of elements in the list
263   \returns number of elements
264*/
265template<class T>
266inline int tList<T>::getSize()
267{
268  return this->size;
269}
270
271
272/**
273   \brief creates an itereator object and returns it
274   \returns the iterator object to this list
275
276   You will use this, if you want to iterate through the list
277*/
278template<class T>
279inline tIterator<T>* tList<T>::getIterator()
280{
281  tIterator<T>* iterator = new tIterator<T>(this->first);
282  return iterator;
283}
284
285
286
287/**
288   \brief this returns the next element after toEntity or the first if toEntity is last
289   \param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase
290   \returns the element after toEntity
291*/
292template<class T>
293inline T* tList<T>::nextElement(T* toEntity)
294{
295  //if( this->last == this->first == NULL) return NULL;
296  if(this->size == 0) return NULL;
297  if( toEntity == NULL) return this->first->curr;
298  if( toEntity == this->last->curr ) return this->first->curr;
299  this->currentEl = this->first;
300  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
301    {
302      this->currentEl = this->currentEl->next;
303    }
304  if(this->currentEl == NULL) return NULL;
305  return this->currentEl->next->curr;
306}
307
308
309/**
310   \brief creates an array out of the list (ATTENTION: not implemented)
311   \returns pointer to the array beginning
312
313   ATTENTION: function is not implemented and wont do anything
314*/
315template<class T>
316T* tList<T>::toArray()
317{
318  return NULL;
319}
320
321#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.