Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oldcollection.h
Go to the documentation of this file.
1 /* unitCollection.h
2  *
3  *****/
4 
5 #ifndef _OLDCOLLECTION_H_
6 #define _OLDCOLLECTION_H_
7 #include <stdlib.h>
8 
9 class Unit;
10 
11 class UnitCollection
12 {
13 private:
15  class UnitListNode
16  {
17 #ifdef _TEST_
18  friend void Iterate( UnitCollection &c );
19 #endif
20 public:
21  Unit *unit;
22  UnitListNode *next;
23 
24  UnitListNode( Unit *unit );
26  void PostInsert( Unit *un );
28  void Remove();
29  UnitListNode( Unit *unit, UnitListNode *next );
30  ~UnitListNode();
31 
32 private: UnitListNode();
33  UnitListNode( const UnitListNode& );
34  UnitListNode& operator=( const UnitListNode& );
35  }
36  *u;
38  void destr();
40  void init()
41  {
42  u = new UnitListNode( NULL );
43  u->next = new UnitListNode( NULL, new UnitListNode( NULL ) );
44  }
45 public:
48  {
49  init();
50  }
53  {
54  destr();
55  }
56  class UnitIterator
57  {
58 #ifdef _TEST_
59  friend void Iterate( UnitCollection &c );
60 #endif
61 private:
63  UnitListNode *pos;
65  void GetNextValidUnit();
66 public: UnitIterator() : pos( NULL ) {}
68  UnitIterator( UnitListNode *start ) : pos( start )
69  {
70  GetNextValidUnit();
71  }
72  UnitIterator( const UnitIterator &orig ) : pos( orig.pos ) {}
74  {
75  pos = orig.pos;
76  return *this;
77  }
79  {
80  pos = NULL;
81  }
82 
83  bool isDone() const
84  {
85  return pos->next->unit == NULL;
86  }
87  bool notDone() const
88  {
89  return pos->next->unit != NULL;
90  }
92  void remove();
93  void moveBefore( UnitCollection &otherList );
95  void preinsert( Unit *unit )
96  {
97  pos->next = new UnitListNode( unit, pos->next );
98  }
100  void postinsert( Unit *unit );
103  {
104  return pos->next->unit;
105  }
107  Unit * next()
108  {
109  advance();
110  return current();
111  }
112  void advance()
113  {
114  pos = pos->next;
115  GetNextValidUnit();
116  }
117  inline Unit* operator++( int )
118  {
119  Unit *un = current();
120  advance();
121  return un;
122  }
123  inline Unit* operator++()
124  {
125  advance();
126  return current();
127  }
128  inline Unit* operator*()
129  {
130  return current();
131  }
132  };
133  class ConstIterator
134  {
135 private:
136  const UnitListNode *pos;
137  void GetNextValidUnit();
138 public: ConstIterator() : pos( NULL ) {}
139  ConstIterator( const ConstIterator &orig ) : pos( orig.pos ) {}
140  ConstIterator( const UnitListNode *start ) : pos( start )
141  {
142  GetNextValidUnit();
143  }
145  {
146  pos = orig.pos;
147  return *this;
148  }
150  {
151  pos = NULL;
152  }
153  const Unit * next()
154  {
155  advance();
156  return current();
157  }
158  const Unit * current() const
159  {
160  return pos->next->unit;
161  }
162  bool isDone() const
163  {
164  return current() == NULL;
165  }
166  bool notDone() const
167  {
168  return current() != NULL;
169  }
170  void advance()
171  {
172  pos = pos->next;
173  GetNextValidUnit();
174  }
175  inline const Unit* operator++()
176  {
177  advance();
178  return current();
179  }
180  inline const Unit* operator++( int )
181  {
182  const Unit *un = current();
183  advance();
184  return un;
185  }
186  inline const Unit* operator*() const
187  {
188  return current();
189  }
190  };
191 
193  {
194 private:
195  const UnitListNode *pos;
196 public: ConstFastIterator() : pos( NULL ) {}
197  ConstFastIterator( const ConstFastIterator &orig ) : pos( orig.pos ) {}
198  ConstFastIterator( const UnitListNode *start ) : pos( start ) {}
200  {
201  pos = NULL;
202  }
203  const Unit * current() const
204  {
205  return pos->next->unit;
206  }
207  void advance()
208  {
209  pos = pos->next;
210  }
211  inline const Unit* operator++()
212  {
213  advance();
214  return current();
215  }
216  inline const Unit* operator++( int )
217  {
218  const Unit *un = current();
219  advance();
220  return un;
221  }
222  inline const Unit* operator*() const
223  {
224  return current();
225  }
226  const Unit * next()
227  {
228  advance();
229  return current();
230  }
231  bool isDone() const
232  {
233  return pos->next->unit == NULL;
234  }
235  bool notDone() const
236  {
237  return pos->next->unit != NULL;
238  }
239 private:
240  ConstFastIterator&operator=( const ConstFastIterator& );
241  };
242 
244  {
245 private:
246  UnitListNode *pos;
247 public:
248 
250  void remove();
252  void preinsert( Unit *unit )
253  {
254  pos->next = new UnitListNode( unit, pos->next );
255  }
257  void postinsert( Unit *unit );
258  FastIterator() : pos( NULL ) {}
259  FastIterator( const FastIterator &orig ) : pos( orig.pos ) {}
260  FastIterator( UnitListNode *start ) : pos( start ) {}
262  {
263  pos = NULL;
264  }
266  {
267  return pos->next->unit;
268  }
269  void advance()
270  {
271  pos = pos->next;
272  }
273  inline Unit* operator++( int )
274  {
275  Unit *un = current();
276  advance();
277  return un;
278  }
279  inline Unit* operator++()
280  {
281  advance();
282  return current();
283  }
284  inline Unit* operator*()
285  {
286  return current();
287  }
288  Unit * next()
289  {
290  advance();
291  return current();
292  }
293  bool isDone() const
294  {
295  return pos->next->unit == NULL;
296  }
297  bool notDone() const
298  {
299  return pos->next->unit != NULL;
300  }
301 private:
302  FastIterator&operator=( const FastIterator& );
303  };
304  static void FreeUnusedNodes(); //not allowed to happen if any lists are traversing
305  static void * PushUnusedNode( UnitListNode *node );
306 #ifdef _TEST_
307  friend void Iterate( UnitCollection &c );
308 #endif
309 //could be empty and this returns false...but usually correct...never has units when it returns true
310  bool empty() const
311  {
312  return u->next->unit == NULL;
313  }
315  {
316  return UnitIterator( u );
317  }
319  {
320  return ConstIterator( u );
321  }
323  {
324  return FastIterator( u );
325  }
327  {
328  return ConstFastIterator( u );
329  }
330  void insert_unique( Unit *un )
331  {
332  for (UnitListNode *i = u->next; i != NULL; i = i->next)
333  if (i->unit == un) return;
334  prepend( un );
335  }
336  void prepend( Unit *unit )
337  {
338  u->next = new UnitListNode( unit, u->next );
339  }
340  void prepend( UnitListNode *unitlistnode )
341  {
342  unitlistnode->next = u->next;
343  u->next = unitlistnode;
344  }
345  void prepend( UnitIterator *iter );
346  void append( Unit *unit );
347  void append( UnitIterator *iter );
348  void clear()
349  {
350  destr();
351  init();
352  }
353  bool contains( const Unit *unit ) const;
354  bool remove( const Unit *unit );
355  void cleanup();
357  {
358  return *createIterator();
359  }
360  const Unit * front() const
361  {
362  return *constIterator();
363  }
364  UnitCollection( const UnitCollection &c );
365  const UnitCollection& operator=( const UnitCollection &c );
366 };
371 
372 #endif
373