Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3623 was 3586, checked in by patrick, 20 years ago

orxonox/trunk: fixed movement bug (was in the lists), modified the player

File size: 4.5 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* nextElement(T* toEntity);
89  T* toArray();
90  void debug();
91};
92
93
94template<class T>
95tList<T>::tList () 
96{
97  this->first = NULL;
98  this->last = NULL;
99  this->size = 0;
100}
101
102template<class T>
103tList<T>::~tList () 
104{
105  this->currentEl = this->first;
106  while(this->currentEl != NULL)
107    {
108      listElement* le = this->currentEl->next;
109      //delete this->currentEl->curr;
110      delete this->currentEl;
111      this->currentEl = le;
112    }
113  this->first = NULL;
114  this->last = NULL;
115  this->size = 0;
116}
117
118
119template<class T>
120void tList<T>::add(T* entity)
121{
122  if( entity == NULL) return;
123  listElement* el = new listElement;
124  el->prev = this->last;
125  el->curr = entity;
126  el->next = NULL;
127
128  this->last = el;
129
130  if(el->prev == NULL) this->first = el; /* if first element */
131  else el->prev->next = el;
132  this->size++;
133}
134
135
136template<class T>
137void tList<T>::remove(T* entity)
138{
139  if( entity == NULL) return;
140  this->currentEl = this->first;
141  listElement* te;
142  while( this->currentEl != NULL)
143    {
144      if( this->currentEl->curr == entity)
145        { 
146          if( this->currentEl->prev  == NULL ) this->first = this->currentEl->next;
147          else this->currentEl->prev->next = this->currentEl->next;
148
149          if( this->currentEl->next == NULL) this->last = this->currentEl->prev;
150          else this->currentEl->next->prev = this->currentEl->prev;
151
152          te = this->currentEl->next;  // for what am i doing this?
153          delete this->currentEl;
154          this->currentEl = te;
155          this->size--;
156          return;
157        }
158      this->currentEl = this->currentEl->next;
159    }
160}
161
162
163template<class T>
164void tList<T>::destroy()
165{
166  this->currentEl = this->first;
167  while(this->currentEl != NULL)
168    {
169      listElement* le = this->currentEl->next;
170      //delete this->currentEl->curr;
171      delete this->currentEl;
172      this->currentEl = le;
173    }
174  this->first = NULL;
175  this->last = NULL;
176  this->size = 0;
177}
178
179
180template<class T>
181T* tList<T>::firstElement()
182{
183  return this->first->curr;
184}
185
186
187template<class T>
188bool tList<T>::isEmpty()
189{
190  return (this->size==0)?true:false;
191}
192
193
194template<class T>
195int tList<T>::getSize()
196{
197  return this->size;
198}
199
200
201template<class T>
202T* tList<T>::enumerate()
203{
204  //if( this->last == this->first == NULL) return NULL;
205  if(this->size == 0) return NULL;
206  this->currentEl = this->first;
207  return this->currentEl->curr;
208}
209
210
211template<class T>
212T* tList<T>::nextElement()
213{
214  // if( this->last == this->first == NULL) return NULL;
215  if(this->size == 0) return NULL;
216  this->currentEl = this->currentEl->next;
217  if(this->currentEl == NULL) return NULL;
218  return this->currentEl->curr;
219}
220
221
222/**
223   \brief this returns the next element after toEntity or the first if toEntity is last
224*/
225template<class T>
226T* tList<T>::nextElement(T* toEntity)
227{
228  //if( this->last == this->first == NULL) return NULL;
229  if(this->size == 0) return NULL;
230  if( toEntity == NULL) return this->first->curr;
231  if( toEntity == this->last->curr ) return this->first->curr;
232  this->currentEl = this->first;
233  while(this->currentEl->curr != toEntity && this->currentEl->next != NULL)
234    {
235      this->currentEl = this->currentEl->next;
236    }
237  if(this->currentEl == NULL) return NULL;
238  return this->currentEl->next->curr;
239}
240
241
242template<class T>
243T* tList<T>::toArray()
244{}
245
246#endif /* _LIST_H */
Note: See TracBrowser for help on using the repository browser.