Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor

File size: 9.0 KB
Line 
1/*!
2 * @file list.h
3 * 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 *  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  T* seekElement(T* element);
37
38 private:
39  listElement<T>*    currentEl;                      //!< pointer to the current list element in the iterator
40  listElement<T>*    tmpEl;                          //!< temp listElemnt pointer
41  listElement<T>*    startElement;                   //!< pointer to the start of the list
42};
43
44
45/**
46 *  iterator constructor
47 * @param startElement:  the first list element from the tList
48
49   normaly you will use it like this:
50
51   tIterator<char>* nameIterator = nameList->getIterator();
52   char name* = nameIterator->nextElement();
53   while( name != NULL)
54   {
55     PRINTF(3)("found name: %s in list\n", name);
56     name = nameIterator->nextElement();
57   }
58   delete nameIterator;
59*/
60template<class T>
61inline tIterator<T>::tIterator (listElement<T>* startElement)
62{
63  this->currentEl = startElement;
64  this->tmpEl = NULL;
65  this->startElement = startElement;
66}
67
68
69/**
70 *  the destructor
71*/
72template<class T>
73inline tIterator<T>::~tIterator ()
74{
75  this->currentEl = NULL;
76}
77
78
79/**
80 *  use it to iterate through the list
81 * @returns next list element
82*/
83template<class T>
84inline T* tIterator<T>::nextElement ()
85{
86  if( this->currentEl == NULL)
87    return NULL;
88
89  this->tmpEl = this->currentEl;
90  this->currentEl = this->currentEl->next;
91  return this->tmpEl->curr;
92}
93
94/**
95 *  gets the element after the selected one, sets the iterator to this point in the list
96 * @param element the element to seek
97 * @returns next list element
98
99  Attention: if you seek an element, the iterator pointer will point to the NEXT listelement after the argument!
100 */
101template<class T>
102inline T* tIterator<T>::seekElement (T* element)
103{
104  for(this->tmpEl = this->startElement; this->tmpEl != NULL; this->tmpEl = this->tmpEl->next)
105  {
106    if( unlikely(this->tmpEl->curr == element))
107    {
108      if( this->tmpEl->next != NULL)
109      {
110        this->currentEl = this->tmpEl->next->next;
111        return this->tmpEl->next->curr;
112      }
113      return NULL;
114    }
115  }
116  return NULL;
117}
118
119
120
121/**
122 *  the list template class
123
124   you will use this as a generic list for all type of objects
125*/
126template<class T> class tList
127{
128 public:
129  tList ();
130  ~tList ();
131
132  void add(T* entity);
133  void addAtBeginning(T* entity); //!< @todo This should be made with an ENUM
134  void remove(T* entity);
135  void removeLast();
136  void flush();
137  T* firstElement();
138  T* lastElement();
139  bool isEmpty();
140  unsigned int getSize();
141  bool inList(T* entity);
142  tIterator<T>* getIterator();
143  T* nextElement(T* toEntity);
144  T* toArray();
145
146 private:
147  unsigned int       size;             //!< the size (lenght) of the list
148  listElement<T>*    first;            //!< pointer to the first element
149  listElement<T>*    last;             //!< pointer to the last element
150  listElement<T>*    currentEl;        //!< pointer to the current element
151};
152
153
154/**
155 *  the constructor
156*/
157template<class T>
158inline tList<T>::tList ()
159{
160  this->first = NULL;
161  this->last = NULL;
162  this->size = 0;
163}
164
165
166/**
167 *  the deconstructor
168
169   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will
170   not be deleted
171*/
172template<class T>
173inline tList<T>::~tList ()
174{
175  this->currentEl = this->first;
176  while(this->currentEl != NULL)
177    {
178      listElement<T>* le = this->currentEl->next;
179      //delete this->currentEl->curr;
180      delete this->currentEl;
181      this->currentEl = le;
182    }
183  this->first = NULL;
184  this->last = NULL;
185  this->size = 0;
186}
187
188
189/**
190 *  add an entity to the list
191 * @param entity: the entity to add
192*/
193template<class T>
194inline void tList<T>::add(T* entity)
195{
196  if( unlikely(entity == NULL)) return;
197  listElement<T>* el = new listElement<T>;
198  el->prev = this->last;
199  el->curr = entity;
200  el->next = NULL;
201
202  this->last = el;
203
204  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
205  else el->prev->next = el;
206  this->size++;
207}
208
209/**
210 *  add an entity to the list
211 * @param entity: the entity to add
212 */
213template<class T>
214    inline void tList<T>::addAtBeginning(T* entity)
215{
216  if( unlikely(entity == NULL)) return;
217  listElement<T>* el = new listElement<T>;
218  el->next = this->first;
219  el->curr = entity;
220  el->prev = NULL;
221
222  this->first = el;
223
224  if( unlikely(el->next == NULL)) this->first = el; /* if first element */
225  else el->next->prev = el;
226  this->size++;
227}
228
229
230/**
231 *  remove an entity from the list
232 * @param entity: the entity to be removed
233*/
234template<class T>
235inline void tList<T>::remove(T* entity)
236{
237  this->currentEl = this->first;
238  while( this->currentEl != NULL)
239    {
240      if( unlikely(this->currentEl->curr == entity))
241        {
242          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
243          else this->currentEl->prev->next = this->currentEl->next;
244
245          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
246          else this->currentEl->next->prev = this->currentEl->prev;
247
248          delete this->currentEl;
249          this->size--;
250          return;
251        }
252      this->currentEl = this->currentEl->next;
253    }
254}
255
256/**
257 * removes the Last Element of the List
258 */
259 template<class T>
260     inline void tList<T>::removeLast()
261{
262  if (this->last == NULL)
263    return;
264  else if (this->last == this->first)
265  {
266    delete this->first;
267    this->first = NULL;
268    this->last = NULL;
269    this->size--;
270  }
271  else
272  {
273    listElement<T>* delLast = this->last;
274    this->last->prev->next = NULL;
275    this->last = this->last->prev;
276    delete delLast;
277  }
278}
279
280/**
281 *  this will deletes the objects from the list
282*/
283template<class T>
284inline void tList<T>::flush()
285{
286  this->currentEl = this->first;
287  while(this->currentEl != NULL)
288    {
289      listElement<T>* le = this->currentEl->next;
290      delete this->currentEl->curr;
291      delete this->currentEl;
292      this->currentEl = le;
293    }
294  this->first = NULL;
295  this->last = NULL;
296  this->size = 0;
297}
298
299
300/**
301 *  returns the first element of the list
302 * @returns first element
303*/
304template<class T>
305inline T* tList<T>::firstElement()
306{
307  return this->first->curr;
308}
309
310
311/**
312 *  function returns the last element of the list
313 * @returns the last element
314*/
315template<class T>
316inline T* tList<T>::lastElement()
317{
318  return this->last->curr;
319}
320
321
322/**
323 *  returns true if the list is empty
324 * @returns true if the list is empty
325*/
326template<class T>
327inline bool tList<T>::isEmpty()
328{
329  return (this->size==0)?true:false;
330}
331
332/**
333 *  checks if an entity is in the List
334 * @param entity The entity to check for in the entire List.
335 * @returns true if it is, false otherwise
336*/
337template<class T>
338inline bool tList<T>::inList(T* entity)
339{
340  // pre checks
341  if(this->size == 0) return false;
342  if( entity == NULL) return false;
343
344  // search in the List
345  this->currentEl = this->first;
346  while(this->currentEl->curr != entity && this->currentEl != NULL)
347    this->currentEl = this->currentEl->next;
348
349  // post checks
350  if(this->currentEl == NULL)
351    return false;
352  else
353    return true;
354}
355
356/**
357 *  this returns the number of elements in the list
358 * @returns number of elements
359*/
360template<class T>
361inline unsigned int tList<T>::getSize()
362{
363  return this->size;
364}
365
366
367/**
368 *  creates an itereator object and returns it
369 * @returns the iterator object to this list
370
371   You will use this, if you want to iterate through the list
372*/
373template<class T>
374inline tIterator<T>* tList<T>::getIterator()
375{
376  tIterator<T>* iterator = new tIterator<T>(this->first);
377  return iterator;
378}
379
380
381
382/**
383 *  this returns the next element after toEntity or the first if toEntity is last
384 * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase
385 * @returns the element after toEntity
386*/
387template<class T>
388inline T* tList<T>::nextElement(T* toEntity)
389{
390  //if( this->last == this->first == NULL) return NULL;
391  if(this->size == 0) return NULL;
392  if( toEntity == NULL) return this->first->curr;
393  if( toEntity == this->last->curr ) return this->first->curr;
394  this->currentEl = this->first;
395  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
396    {
397      this->currentEl = this->currentEl->next;
398    }
399  if(this->currentEl == NULL) return NULL;
400  return this->currentEl->next->curr;
401}
402
403
404/**
405 *  creates an array out of the list (ATTENTION: not implemented)
406 * @returns pointer to the array beginning
407
408   ATTENTION: function is not implemented and wont do anything
409*/
410template<class T>
411T* tList<T>::toArray()
412{
413  return NULL;
414}
415
416#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.