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 | |
---|
14 | class WorldEntity; |
---|
15 | |
---|
16 | class 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 | |
---|
48 | class Iterator |
---|
49 | { |
---|
50 | |
---|
51 | public: |
---|
52 | bool hasNext(); |
---|
53 | WorldEntity* next(); |
---|
54 | |
---|
55 | private: |
---|
56 | |
---|
57 | }; |
---|
58 | |
---|
59 | |
---|
60 | template<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(WorldEntity* entity); |
---|
81 | void remove(WorldEntity* 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 | |
---|
93 | template<class T> |
---|
94 | tList<T>::tList () |
---|
95 | { |
---|
96 | this->first = NULL; |
---|
97 | this->last = NULL; |
---|
98 | this->size = 0; |
---|
99 | } |
---|
100 | |
---|
101 | template<class T> |
---|
102 | tList<T>::~tList () |
---|
103 | {} |
---|
104 | |
---|
105 | template<class T> |
---|
106 | void tList<T>::add(WorldEntity* entity) |
---|
107 | { |
---|
108 | printf("tList<T>::add() \n"); |
---|
109 | listElement* el = new listElement; |
---|
110 | el->prev = this->last; |
---|
111 | el->curr = entity; |
---|
112 | el->next = NULL; |
---|
113 | |
---|
114 | this->last = el; |
---|
115 | |
---|
116 | if(this->size == 0) this->first = el; |
---|
117 | else el->prev->next = el; |
---|
118 | this->size++; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | template<class T> |
---|
123 | void tList<T>::remove(WorldEntity* entity) |
---|
124 | { |
---|
125 | this->currentEl = this->first; |
---|
126 | listElement* te; |
---|
127 | while( this->currentEl != NULL) |
---|
128 | { |
---|
129 | if( this->currentEl->curr == entity) |
---|
130 | { |
---|
131 | printf("tList<T>::remove() - object found\n"); |
---|
132 | |
---|
133 | if( this->currentEl->prev == NULL ) this->first = this->currentEl->next; |
---|
134 | else this->currentEl->prev->next = this->currentEl->next; |
---|
135 | |
---|
136 | if( this->currentEl->next == NULL) this->last = this->currentEl->prev; |
---|
137 | else this->currentEl->next->prev = this->currentEl->prev; |
---|
138 | |
---|
139 | te = this->currentEl->next; |
---|
140 | delete this->currentEl; |
---|
141 | this->currentEl = te; |
---|
142 | return; |
---|
143 | } |
---|
144 | this->currentEl = this->currentEl->next; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | template<class T> |
---|
150 | void tList<T>::destroy() |
---|
151 | { |
---|
152 | printf("tList<T>::clear() - clearing all world objects, releasing mem\n"); |
---|
153 | this->currentEl = this->first; |
---|
154 | while(this->currentEl != NULL) |
---|
155 | { |
---|
156 | listElement* le = this->currentEl->next; |
---|
157 | delete this->currentEl->curr; |
---|
158 | delete this->currentEl; |
---|
159 | this->currentEl = le; |
---|
160 | } |
---|
161 | this->first = NULL; |
---|
162 | this->last = NULL; |
---|
163 | this->size = 0; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | template<class T> |
---|
168 | T* tList<T>::firstElement() |
---|
169 | { |
---|
170 | return this->first->curr; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | template<class T> |
---|
175 | bool tList<T>::isEmpty() |
---|
176 | { |
---|
177 | return (this->size==0)?true:false; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | template<class T> |
---|
182 | int tList<T>::getSize() |
---|
183 | { |
---|
184 | return this->size; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | template<class T> |
---|
189 | T* tList<T>::enumerate() |
---|
190 | { |
---|
191 | if(this->size == 0) return NULL; |
---|
192 | this->currentEl = this->first; |
---|
193 | return this->currentEl->curr; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | template<class T> |
---|
198 | T* tList<T>::nextElement() |
---|
199 | { |
---|
200 | if(this->size == 0) return NULL; |
---|
201 | this->currentEl = this->currentEl->next; |
---|
202 | if(this->currentEl == NULL) return NULL; |
---|
203 | return this->currentEl->curr; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | template<class T> |
---|
208 | T* tList<T>::toArray() |
---|
209 | {} |
---|
210 | |
---|
211 | #endif |
---|