Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: started cleaning up the list classes

File size: 5.7 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
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
93template<class T> class tList
94{
95 public:
96  tList ();
97  ~tList ();
98
99  void add(T* entity);
100  void remove(T* entity);
101  void destroy();
102  T* firstElement();
103  T* lastElement();
104  bool isEmpty();
105  int getSize();
106  //T* enumerate();
107  tIterator<T>* getIterator();
108  T* nextElement();
109  T* nextElement(T* toEntity);
110  T* toArray();
111  void debug();
112
113 private:
114  unsigned int size;
115  listElement<T>* first;
116  listElement<T>* last;
117  listElement<T>* currentEl;
118};
119
120
121template<class T>
122inline tList<T>::tList () 
123{
124  this->first = NULL;
125  this->last = NULL;
126  this->size = 0;
127}
128
129template<class T>
130inline tList<T>::~tList () 
131{
132  this->currentEl = this->first;
133  while(this->currentEl != NULL)
134    {
135      listElement<T>* le = this->currentEl->next;
136      //delete this->currentEl->curr;
137      delete this->currentEl;
138      this->currentEl = le;
139    }
140  this->first = NULL;
141  this->last = NULL;
142  this->size = 0;
143}
144
145
146template<class T>
147inline void tList<T>::add(T* entity)
148{
149  if( unlikely(entity == NULL)) return;
150  listElement<T>* el = new listElement<T>;
151  el->prev = this->last;
152  el->curr = entity;
153  el->next = NULL;
154
155  this->last = el;
156
157  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
158  else el->prev->next = el;
159  this->size++;
160}
161
162
163template<class T>
164inline void tList<T>::remove(T* entity)
165{
166  this->currentEl = this->first;
167  listElement<T>* te;
168  while( this->currentEl != NULL)
169    {
170      if( unlikely(this->currentEl->curr == entity))
171        { 
172          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
173          else this->currentEl->prev->next = this->currentEl->next;
174
175          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
176          else this->currentEl->next->prev = this->currentEl->prev;
177
178          delete this->currentEl;
179          this->size--;
180          return;
181        }
182      this->currentEl = this->currentEl->next;
183    }
184}
185
186
187template<class T>
188inline void tList<T>::destroy()
189{
190  this->currentEl = this->first;
191  while(this->currentEl != NULL)
192    {
193      listElement<T>* le = this->currentEl->next;
194      //delete this->currentEl->curr;
195      delete this->currentEl;
196      this->currentEl = le;
197    }
198  this->first = NULL;
199  this->last = NULL;
200  this->size = 0;
201}
202
203
204template<class T>
205inline T* tList<T>::firstElement()
206{
207  return this->first->curr;
208}
209
210
211template<class T>
212inline T* tList<T>::lastElement()
213{
214  return this->last->curr;
215}
216
217
218template<class T>
219inline bool tList<T>::isEmpty()
220{
221  return (this->size==0)?true:false;
222}
223
224
225template<class T>
226inline int tList<T>::getSize()
227{
228  return this->size;
229}
230
231
232/* deprecated */
233/*
234template<class T>
235T* tList<T>::enumerate()
236{
237  //if( this->last == this->first == NULL) return NULL;
238  if( this->size == 0) return NULL;
239  this->currentEl = this->first;
240  return this->currentEl->curr;
241}
242*/
243
244template<class T>
245inline tIterator<T>* tList<T>::getIterator()
246{
247  tIterator<T>* iterator = new tIterator<T>(this->first);
248  return iterator;
249}
250
251
252/* deprecated */
253template<class T>
254T* tList<T>::nextElement()
255{
256  // if( this->last == this->first == NULL) return NULL;
257  if( this->size == 0) return NULL;
258  this->currentEl = this->currentEl->next;
259  if( this->currentEl == NULL) return NULL;
260  return this->currentEl->curr;
261}
262
263
264/**
265   \brief this returns the next element after toEntity or the first if toEntity is last
266*/
267template<class T>
268inline T* tList<T>::nextElement(T* toEntity)
269{
270  //if( this->last == this->first == NULL) return NULL;
271  if(this->size == 0) return NULL;
272  if( toEntity == NULL) return this->first->curr;
273  if( toEntity == this->last->curr ) return this->first->curr;
274  this->currentEl = this->first;
275  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
276    {
277      this->currentEl = this->currentEl->next;
278    }
279  if(this->currentEl == NULL) return NULL;
280  return this->currentEl->next->curr;
281}
282
283
284template<class T>
285T* tList<T>::toArray()
286{}
287
288#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.