Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: resolved a list deleting problem in tlist.h.

File size: 3.7 KB
Line 
1
2#ifndef _LIST_H
3#define _LIST_H
4
5#include "stdincl.h"
6
7//! An enum to list all the modes available when adding an object to a List
8//enum ADDMODE {LIST_ADD_FIRST, LIST_ADD_LAST};
9//! An enum to list the two searching directions available when removing an object from a List
10//enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW};
11
12
13
14class WorldEntity;
15
16class List {
17
18 public:
19  List ();
20  ~List ();
21
22  void add(WorldEntity* entity);
23  void remove(WorldEntity* entity);
24  void destroy();
25  WorldEntity* firstElement();
26  bool isEmpty();
27  int getSize();
28  WorldEntity* enumerate();
29  WorldEntity* nextElement();
30  WorldEntity* toArray();
31  void debug();
32
33 private:
34  struct listElement
35  {
36    listElement* prev;
37    WorldEntity* curr;
38    listElement* next;
39  };
40  Uint32 size;
41  listElement* first;
42  listElement* last;
43  listElement* currentEl;
44
45
46};
47
48class Iterator
49{
50
51 public:
52  bool hasNext();
53  WorldEntity* next();
54
55 private:
56
57};
58
59
60template<class T> class tList
61{
62 private:
63  struct listElement
64  {
65    listElement* prev;
66    T* curr;
67    listElement* next;
68  };
69
70  Uint32 size;
71  listElement* first;
72  listElement* last;
73  listElement* currentEl;
74 
75 public:
76  tList ();
77  ~tList ();
78 
79
80  void add(T* entity);
81  void remove(T* entity);
82  void destroy();
83  T* firstElement();
84  bool isEmpty();
85  int getSize();
86  T* enumerate();
87  T* nextElement();
88  T* toArray();
89  void debug();
90};
91
92
93template<class T>
94tList<T>::tList () 
95{
96  this->first = NULL;
97  this->last = NULL;
98  this->size = 0;
99}
100
101template<class T>
102tList<T>::~tList () 
103{
104  this->currentEl = this->first;
105  while(this->currentEl != NULL)
106    {
107      listElement* le = this->currentEl->next;
108      //delete this->currentEl->curr;
109      delete this->currentEl;
110      this->currentEl = le;
111    }
112  this->first = NULL;
113  this->last = NULL;
114  this->size = 0;
115}
116
117
118template<class T>
119void tList<T>::add(T* entity)
120{
121  listElement* el = new listElement;
122  el->prev = this->last;
123  el->curr = entity;
124  el->next = NULL;
125
126  this->last = el;
127
128  if(el->prev == NULL) this->first = el; /* if first element */
129  else el->prev->next = el;
130  this->size++;
131}
132
133
134template<class T>
135void tList<T>::remove(T* entity)
136{
137  this->currentEl = this->first;
138  listElement* te;
139  while( this->currentEl != NULL)
140    {
141      if( this->currentEl->curr == entity)
142        { 
143          if( this->currentEl->prev  == NULL ) this->first = this->currentEl->next;
144          else this->currentEl->prev->next = this->currentEl->next;
145
146          if( this->currentEl->next == NULL) this->last = this->currentEl->prev;
147          else this->currentEl->next->prev = this->currentEl->prev;
148
149          te = this->currentEl->next;  // for what am i doing this?
150          delete this->currentEl;
151          this->currentEl = te;
152          this->size--;
153          return;
154        }
155      this->currentEl = this->currentEl->next;
156    }
157}
158
159
160template<class T>
161void tList<T>::destroy()
162{
163  this->currentEl = this->first;
164  while(this->currentEl != NULL)
165    {
166      listElement* le = this->currentEl->next;
167      //delete this->currentEl->curr;
168      delete this->currentEl;
169      this->currentEl = le;
170    }
171  this->first = NULL;
172  this->last = NULL;
173  this->size = 0;
174}
175
176
177template<class T>
178T* tList<T>::firstElement()
179{
180  return this->first->curr;
181}
182
183
184template<class T>
185bool tList<T>::isEmpty()
186{
187  return (this->size==0)?true:false;
188}
189
190
191template<class T>
192int tList<T>::getSize()
193{
194  return this->size;
195}
196
197
198template<class T>
199T* tList<T>::enumerate()
200{
201  if(this->size == 0) return NULL;
202  this->currentEl = this->first;
203  return this->currentEl->curr;
204}
205
206
207template<class T>
208T* tList<T>::nextElement()
209{
210  if(this->size == 0) return NULL;
211  this->currentEl = this->currentEl->next;
212  if(this->currentEl == NULL) return NULL;
213  return this->currentEl->curr;
214}
215
216
217template<class T>
218T* tList<T>::toArray()
219{}
220
221#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.