Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added Shell Class to the utils.
This class should enable us to print all the output from orxonox directly onto the screen.
Also it should allow inputting some little Commands, so that we have more force over our little project :)

Also added a new Function to tList, removeLast, that is very quick in deleting the last element, as this is used inside of the Shell (deleting the Last Element is a major issue)

File size: 8.5 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 remove(T* entity);
134  void removeLast();
135  void flush();
136  T* firstElement();
137  T* lastElement();
138  bool isEmpty();
139  int getSize();
140  bool inList(T* entity);
141  tIterator<T>* getIterator();
142  T* nextElement(T* toEntity);
143  T* toArray();
144
145 private:
146  unsigned int       size;             //!< the size (lenght) of the list
147  listElement<T>*    first;            //!< pointer to the first element
148  listElement<T>*    last;             //!< pointer to the last element
149  listElement<T>*    currentEl;        //!< pointer to the current element
150};
151
152
153/**
154 *  the constructor
155*/
156template<class T>
157inline tList<T>::tList ()
158{
159  this->first = NULL;
160  this->last = NULL;
161  this->size = 0;
162}
163
164
165/**
166 *  the deconstructor
167
168   this will delete only the list. ATTENTION: the list is deleted, but the objects in the list will
169   not be deleted
170*/
171template<class T>
172inline tList<T>::~tList ()
173{
174  this->currentEl = this->first;
175  while(this->currentEl != NULL)
176    {
177      listElement<T>* le = this->currentEl->next;
178      //delete this->currentEl->curr;
179      delete this->currentEl;
180      this->currentEl = le;
181    }
182  this->first = NULL;
183  this->last = NULL;
184  this->size = 0;
185}
186
187
188/**
189 *  add an entity to the list
190 * @param entity: the entity to add
191*/
192template<class T>
193inline void tList<T>::add(T* entity)
194{
195  if( unlikely(entity == NULL)) return;
196  listElement<T>* el = new listElement<T>;
197  el->prev = this->last;
198  el->curr = entity;
199  el->next = NULL;
200
201  this->last = el;
202
203  if( unlikely(el->prev == NULL)) this->first = el; /* if first element */
204  else el->prev->next = el;
205  this->size++;
206}
207
208
209/**
210 *  remove an entity from the list
211 * @param entity: the entity to be removed
212*/
213template<class T>
214inline void tList<T>::remove(T* entity)
215{
216  this->currentEl = this->first;
217  while( this->currentEl != NULL)
218    {
219      if( unlikely(this->currentEl->curr == entity))
220        {
221          if( unlikely(this->currentEl->prev  == NULL)) this->first = this->currentEl->next;
222          else this->currentEl->prev->next = this->currentEl->next;
223
224          if( unlikely(this->currentEl->next == NULL)) this->last = this->currentEl->prev;
225          else this->currentEl->next->prev = this->currentEl->prev;
226
227          delete this->currentEl;
228          this->size--;
229          return;
230        }
231      this->currentEl = this->currentEl->next;
232    }
233}
234
235/**
236 * removes the Last Element of the List
237 */
238 template<class T>
239     inline void tList<T>::removeLast()
240{
241  if (this->last == NULL)
242    return;
243  else if (this->last == this->first)
244  {
245    delete this->first;
246    this->first = NULL;
247    this->last = NULL;
248    this->size--;
249  }
250  else
251  {
252    listElement<T>* delLast = this->last;
253    this->last->prev->next = NULL;
254    this->last = this->last->prev;
255    delete delLast;
256  }
257}
258
259/**
260 *  this will deletes the objects from the list
261*/
262template<class T>
263inline void tList<T>::flush()
264{
265  this->currentEl = this->first;
266  while(this->currentEl != NULL)
267    {
268      listElement<T>* le = this->currentEl->next;
269      delete this->currentEl->curr;
270      delete this->currentEl;
271      this->currentEl = le;
272    }
273  this->first = NULL;
274  this->last = NULL;
275  this->size = 0;
276}
277
278
279/**
280 *  returns the first element of the list
281 * @returns first element
282*/
283template<class T>
284inline T* tList<T>::firstElement()
285{
286  return this->first->curr;
287}
288
289
290/**
291 *  function returns the last element of the list
292 * @returns the last element
293*/
294template<class T>
295inline T* tList<T>::lastElement()
296{
297  return this->last->curr;
298}
299
300
301/**
302 *  returns true if the list is empty
303 * @returns true if the list is empty
304*/
305template<class T>
306inline bool tList<T>::isEmpty()
307{
308  return (this->size==0)?true:false;
309}
310
311/**
312 *  checks if an entity is in the List
313 * @param entity The entity to check for in the entire List.
314 * @returns true if it is, false otherwise
315*/
316template<class T>
317inline bool tList<T>::inList(T* entity)
318{
319  // pre checks
320  if(this->size == 0) return false;
321  if( entity == NULL) return false;
322
323  // search in the List
324  this->currentEl = this->first;
325  while(this->currentEl->curr != entity && this->currentEl != NULL)
326    this->currentEl = this->currentEl->next;
327
328  // post checks
329  if(this->currentEl == NULL)
330    return false;
331  else
332    return true;
333}
334
335/**
336 *  this returns the number of elements in the list
337 * @returns number of elements
338*/
339template<class T>
340inline int tList<T>::getSize()
341{
342  return this->size;
343}
344
345
346/**
347 *  creates an itereator object and returns it
348 * @returns the iterator object to this list
349
350   You will use this, if you want to iterate through the list
351*/
352template<class T>
353inline tIterator<T>* tList<T>::getIterator()
354{
355  tIterator<T>* iterator = new tIterator<T>(this->first);
356  return iterator;
357}
358
359
360
361/**
362 *  this returns the next element after toEntity or the first if toEntity is last
363 * @param toEntity: the entity after which is an entity, that has to be returned, sorry, terrible phrase
364 * @returns the element after toEntity
365*/
366template<class T>
367inline T* tList<T>::nextElement(T* toEntity)
368{
369  //if( this->last == this->first == NULL) return NULL;
370  if(this->size == 0) return NULL;
371  if( toEntity == NULL) return this->first->curr;
372  if( toEntity == this->last->curr ) return this->first->curr;
373  this->currentEl = this->first;
374  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
375    {
376      this->currentEl = this->currentEl->next;
377    }
378  if(this->currentEl == NULL) return NULL;
379  return this->currentEl->next->curr;
380}
381
382
383/**
384 *  creates an array out of the list (ATTENTION: not implemented)
385 * @returns pointer to the array beginning
386
387   ATTENTION: function is not implemented and wont do anything
388*/
389template<class T>
390T* tList<T>::toArray()
391{
392  return NULL;
393}
394
395#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.