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.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <vector>
3 #include "collection.h"
4 #include <assert.h>
5 #ifndef LIST_TESTING
6 #include "unit_generic.h"
7 #endif
8 
9 UnitCollection::UnitListNode::UnitListNode( Unit *unit ) : unit( unit )
10  , next( NULL )
11 {
12  if (unit)
13  unit->Ref();
14 }
15 UnitCollection::UnitListNode::UnitListNode( Unit *unit, UnitListNode *next ) : unit( unit )
16  , next( next )
17 {
18  if (unit)
19  unit->Ref();
20 }
21 
22 UnitCollection::UnitListNode::~UnitListNode()
23 {
24  if (NULL != unit)
25  unit->UnRef();
26  unit = NULL;
27  next = NULL;
28 }
29 void UnitCollection::destr()
30 {
31  UnitListNode *tmp;
32  while (u->next) {
33  tmp = u->next;
34  u->next = u->next->next;
35  PushUnusedNode( tmp );
36  }
37  u->unit = NULL;
38  u->next = NULL;
39  PushUnusedNode( u );
40 }
41 
42 void* UnitCollection::PushUnusedNode( UnitListNode *node )
43 {
44  static UnitListNode cat( NULL, NULL );
45  static UnitListNode dog( NULL, &cat );
46  static bool cachunk = true;
47  if (cachunk)
48  cachunk = false;
49  //VSFileSystem::vs_fprintf (stderr,"%x %x",&dog,&cat);
50  static std::vector< UnitCollection::UnitListNode* >dogpile;
51  if (node == NULL) {
52  return &dogpile;
53  } else {
54  node->next = &dog;
55  dogpile.push_back( node );
56  }
57  return NULL;
58 }
60 {
61  static std::vector< UnitCollection::UnitListNode* >bakdogpile;
62  std::vector< UnitCollection::UnitListNode* > *dogpile = (std::vector< UnitCollection::UnitListNode* >*)PushUnusedNode(
63  NULL );
64  bakdogpile.swap( *dogpile );
65  while ( !dogpile->empty() ) {
66  delete dogpile->back();
67  dogpile->pop_back();
68  }
69 }
71 {
72  if (pos->next->unit) {
73  UnitListNode *tmp = pos->next->next;
74  otherList.prepend( pos->next );
75  pos->next = tmp;
76  } else {
77  assert( 0 );
78  }
79 }
81 {
82  UnitListNode *n = u;
83  Unit *tmp;
84  while ( ( tmp = iter->current() ) ) {
85  //iter->current checks for killed()
86  n->next = new UnitListNode( tmp, n->next );
87  iter->advance();
88  }
89 }
91 {
92  UnitListNode *n = u;
93  while (n->next->unit != NULL)
94  n = n->next;
95  Unit *tmp;
96  while ( ( tmp = iter->current() ) ) {
97  n->next = new UnitListNode( tmp, n->next );
98  n = n->next;
99  iter->advance();
100  }
101 }
102 void UnitCollection::append( Unit *unit )
103 {
104  UnitListNode *n = u;
105  while (n->next->unit != NULL)
106  n = n->next;
107  n->next = new UnitListNode( unit, n->next );
108 }
109 void UnitCollection::UnitListNode::PostInsert( Unit *unit )
110 {
111  if (next->unit != NULL)
112  next->next = new UnitListNode( unit, next->next );
113  else
114  next = new UnitListNode( unit, next );
115 }
117 {
118  pos->PostInsert( unit );
119 }
121 {
122  pos->PostInsert( unit );
123 }
124 void UnitCollection::UnitListNode::Remove()
125 {
126  if (next->unit) {
127  UnitListNode *tmp = next->next;
128  //delete next; //takes care of unref! And causes a shitload of bugs
129  //concurrent lists, man
130  PushUnusedNode( next );
131  next = tmp;
132  } else {
133  assert( 0 );
134  }
135 }
137 {
138  pos->Remove();
139 }
141 {
142  pos->Remove();
143 }
144 
145 void UnitCollection::ConstIterator::GetNextValidUnit()
146 {
147  while (pos->next->unit ? pos->next->unit->Killed() : false)
148  pos = pos->next;
149 }
150 
151 const UnitCollection& UnitCollection::operator=( const UnitCollection &uc )
152 {
153 #ifdef _DEBUG
154  printf( "warning could cause problems with concurrent lists. Make sure no one is traversing gotten list" );
155 #endif
156  destr();
157  init();
158  un_iter ui = createIterator();
159  const UnitListNode *n = uc.u;
160  while (n) {
161  if (n->unit) {
162  ui.postinsert( n->unit );
163  ++ui;
164  }
165  n = n->next;
166  }
167  return uc;
168 }
169 UnitCollection::UnitCollection( const UnitCollection &uc ) : u( NULL )
170 {
171  init();
172  un_iter ui = createIterator();
173  const UnitListNode *n = uc.u;
174  while (n) {
175  if (n->unit) {
176  ui.postinsert( n->unit );
177  ++ui;
178  }
179  n = n->next;
180  }
181 }
182 
183 bool UnitCollection::contains( const Unit *unit ) const
184 {
185  if ( empty() ) return false;
187  while ( it.notDone() ) {
188  if (it.current() == unit)
189  return true;
190 
191  else
192  it.advance();
193  }
194  return false;
195 }
196 
197 bool UnitCollection::remove( const Unit *unit )
198 {
199  bool res = false;
200  if ( empty() ) return false;
201  FastIterator it = fastIterator();
202  while ( it.notDone() ) {
203  if (it.current() == unit)
204  it.remove(), res = true;
205 
206  else
207  it.advance();
208  }
209  return res;
210 }
211 
213 {
214  //NOTE: advance() will be cleaning up the list by itself
215  un_iter ui = createIterator();
216  while ( ui.notDone() )
217  ui.advance();
218 }
219