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
UnitCollection Class Reference

#include <collection.h>

Classes

class  ConstFastIterator
 
class  ConstIterator
 
class  FastIterator
 
class  UnitIterator
 

Public Types

typedef ConstIterator ConstFastIterator
 
typedef UnitIterator FastIterator
 

Public Member Functions

 UnitCollection ()
 
 UnitCollection (const UnitCollection &)
 
 ~UnitCollection ()
 
UnitIterator createIterator ()
 
FastIterator fastIterator ()
 
ConstIterator constIterator () const
 
ConstFastIterator constFastIterator () const
 
void insert_unique (Unit *)
 
bool empty () const
 
void prepend (Unit *)
 
void prepend (UnitIterator *)
 
void append (class Unit *)
 
void append (UnitIterator *)
 
void insert (std::list< Unit * >::iterator &, Unit *)
 
void clear ()
 
bool contains (const class Unit *) const
 
void erase (std::list< class Unit * >::iterator &)
 
bool remove (const class Unit *)
 
const int size () const
 
Unitback ()
 
Unitfront ()
 
 UnitCollection ()
 Initislizes the first unit and then calls init;. More...
 
 ~UnitCollection ()
 destroys the list permanently More...
 
bool empty () const
 
UnitIterator createIterator ()
 
ConstIterator constIterator () const
 
FastIterator fastIterator ()
 
ConstFastIterator constFastIterator () const
 
void insert_unique (Unit *un)
 
void prepend (Unit *unit)
 
void prepend (UnitListNode *unitlistnode)
 
void prepend (UnitIterator *iter)
 
void append (Unit *unit)
 
void append (UnitIterator *iter)
 
void clear ()
 
bool contains (const Unit *unit) const
 
bool remove (const Unit *unit)
 
void cleanup ()
 
Unitfront ()
 
const Unitfront () const
 
 UnitCollection (const UnitCollection &c)
 
const UnitCollectionoperator= (const UnitCollection &c)
 

Static Public Member Functions

static void FreeUnusedNodes ()
 
static void * PushUnusedNode (UnitListNode *node)
 

Friends

class UnitIterator
 
class ConstIterator
 

Detailed Description

Definition at line 29 of file collection.h.

Member Typedef Documentation

Definition at line 143 of file collection.h.

Definition at line 144 of file collection.h.

Constructor & Destructor Documentation

UnitCollection::UnitCollection ( )

Definition at line 176 of file collection.cpp.

177 {
178  activeIters.reserve( 20 );
179 }
UnitCollection::UnitCollection ( const UnitCollection uc)

Definition at line 181 of file collection.cpp.

References append().

182 {
183  list< Unit* >::const_iterator in = uc.u.begin();
184  while ( in != uc.u.end() ) {
185  append( *in );
186  ++in;
187  }
188 }
UnitCollection::~UnitCollection ( )
inline

Definition at line 148 of file collection.h.

149  {
150  destr();
151  }
UnitCollection::UnitCollection ( )
inline

Initislizes the first unit and then calls init;.

Definition at line 47 of file oldcollection.h.

48  {
49  init();
50  }
UnitCollection::~UnitCollection ( )
inline

destroys the list permanently

Definition at line 52 of file oldcollection.h.

53  {
54  destr();
55  }
UnitCollection::UnitCollection ( const UnitCollection c)

Member Function Documentation

void UnitCollection::append ( class Unit un)

Definition at line 222 of file collection.cpp.

References Unit::Ref().

Referenced by Planet::PlanetIterator::advance(), Planet::PlanetIterator::PlanetIterator(), and UnitCollection().

223 {
224  if (un) {
225  un->Ref();
226  u.push_back( un );
227  }
228 }
void UnitCollection::append ( UnitIterator it)

Definition at line 230 of file collection.cpp.

References UnitCollection::UnitIterator::advance(), and Unit::Ref().

231 {
232  if (!it) return;
233  Unit *tmp = NULL;
234  while ( (tmp = **it) ) {
235  tmp->Ref();
236  u.push_back( tmp );
237  it->advance();
238  }
239 }
void UnitCollection::append ( Unit unit)
void UnitCollection::append ( UnitIterator iter)
Unit* UnitCollection::back ( )
inline

Definition at line 219 of file collection.h.

220  {
221  for (std::list< Unit* >::reverse_iterator it = u.rbegin(); it != u.rend(); ++it)
222  if (*it)
223  return *it;
224  return NULL;
225  }
void UnitCollection::cleanup ( )

Definition at line 212 of file oldcollection.cpp.

References UnitCollection::UnitIterator::advance(), createIterator(), and UnitCollection::UnitIterator::notDone().

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 }
void UnitCollection::clear ( )

Definition at line 250 of file collection.cpp.

References fprintf.

Referenced by UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Clear(), GamePlanet::Kill(), Planet::Kill(), and UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::SwapHugeAccum().

251 {
252  if ( !activeIters.empty() ) {
253  fprintf(stderr, "WARNING! Attempting to clear a collection with active iterators!\n" );
254  return;
255  }
256 
257  for (list< Unit* >::iterator it = u.begin(); it != u.end(); ++it) {
258  (*it)->UnRef();
259  (*it) = NULL;
260  }
261  u.clear();
262 }
void UnitCollection::clear ( )
inline

Definition at line 348 of file oldcollection.h.

349  {
350  destr();
351  init();
352  }
ConstFastIterator UnitCollection::constFastIterator ( ) const
inline

Definition at line 166 of file collection.h.

Referenced by Unit::querySphere().

167  {
168  return ConstFastIterator( this );
169  }
ConstFastIterator UnitCollection::constFastIterator ( ) const
inline

Definition at line 326 of file oldcollection.h.

327  {
328  return ConstFastIterator( u );
329  }
ConstIterator UnitCollection::constIterator ( ) const
inline

Definition at line 162 of file collection.h.

References ConstIterator.

Referenced by Radar::Sensor::FindTracksInRange(), findUnitInStarsystem(), front(), NetServer::sendCargoSnapshot(), and Unit::Target().

163  {
164  return ConstIterator( this );
165  }
ConstIterator UnitCollection::constIterator ( ) const
inline

Definition at line 318 of file oldcollection.h.

References ConstIterator.

319  {
320  return ConstIterator( u );
321  }
bool UnitCollection::contains ( const class Unit ) const
bool UnitCollection::contains ( const Unit unit) const

Definition at line 275 of file collection.cpp.

References Unit::Killed().

276 {
277  if (u.empty() || !unit)
278  return false;
279  for (list< Unit* >::const_iterator it = u.begin(); it != u.end(); ++it)
280  if ( (*it) == unit && !(*it)->Killed() )
281  return true;
282  return false;
283 }
UnitIterator UnitCollection::createIterator ( )
inline
UnitIterator UnitCollection::createIterator ( )
inline

Definition at line 314 of file oldcollection.h.

References UnitIterator.

315  {
316  return UnitIterator( u );
317  }
bool UnitCollection::empty ( ) const
inline

Definition at line 310 of file oldcollection.h.

311  {
312  return u->next->unit == NULL;
313  }
void UnitCollection::erase ( std::list< class Unit * >::iterator &  )
inline

Definition at line 285 of file collection.cpp.

References i.

Referenced by remove().

286 {
287  if ( !(*it2) ) {
288  ++it2;
289  return;
290  }
291  //If we have more than 4 iterators, just push node onto vector.
292  if (activeIters.size() > 3) {
293  removedIters.push_back( it2 );
294  (*it2)->UnRef();
295  (*it2) = NULL;
296  ++it2;
297  return;
298  }
299  //If we have between 2 and 4 iterators, see if any are actually
300  //on the node we want to remove, if so, just push onto vector.
301  //Purpose : This special case is to reduce the size of the list in the
302  //situation where removedIters isn't being processed.
303  if (activeIters.size() > 1)
304  for (vector<UnitCollection::UnitIterator*>::size_type i = 0; i < activeIters.size(); ++i)
305  if (activeIters[i]->it == it2) {
306  removedIters.push_back( it2 );
307  (*it2)->UnRef();
308  (*it2) = NULL;
309  ++it2;
310  return;
311  }
312  //If we have 1 iterator, or none of the iterators are currently on the
313  //requested node to be removed, then remove it right away.
314  (*it2)->UnRef();
315  (*it2) = NULL;
316  it2 = u.erase( it2 );
317 }
FastIterator UnitCollection::fastIterator ( )
inline

Definition at line 158 of file collection.h.

Referenced by CalculateNearestWarpUnit(), and Unit::rayCollide().

159  {
160  return FastIterator( this );
161  }
FastIterator UnitCollection::fastIterator ( )
inline

Definition at line 322 of file oldcollection.h.

323  {
324  return FastIterator( u );
325  }
void UnitCollection::FreeUnusedNodes ( )
static

Definition at line 59 of file oldcollection.cpp.

References PushUnusedNode().

Referenced by main().

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 }
Unit* UnitCollection::front ( )
inline

Definition at line 228 of file collection.h.

229  {
230  for (std::list< Unit* >::iterator it = u.begin(); it != u.end(); ++it)
231  if (*it)
232  return *it;
233  return NULL;
234  }
Unit* UnitCollection::front ( )
inline

Definition at line 356 of file oldcollection.h.

References createIterator().

357  {
358  return *createIterator();
359  }
const Unit* UnitCollection::front ( ) const
inline

Definition at line 360 of file oldcollection.h.

References constIterator().

361  {
362  return *constIterator();
363  }
void UnitCollection::insert ( std::list< Unit * >::iterator &  ,
Unit  
)

Definition at line 241 of file collection.cpp.

References Unit::Ref().

242 {
243  if (unit) {
244  unit->Ref();
245  temp = u.insert( temp, unit );
246  }
247  temp = u.end();
248 }
void UnitCollection::insert_unique ( Unit unit)

Definition at line 190 of file collection.cpp.

References Unit::Ref().

191 {
192  if (unit) {
193  for (list< Unit* >::iterator it = u.begin(); it != u.end(); ++it)
194  if (*it == unit)
195  return;
196  unit->Ref();
197  u.push_front( unit );
198  }
199 }
void UnitCollection::insert_unique ( Unit un)
inline

Definition at line 330 of file oldcollection.h.

References i, and prepend().

331  {
332  for (UnitListNode *i = u->next; i != NULL; i = i->next)
333  if (i->unit == un) return;
334  prepend( un );
335  }
const UnitCollection& UnitCollection::operator= ( const UnitCollection c)
void UnitCollection::prepend ( UnitIterator it)

Definition at line 209 of file collection.cpp.

References UnitCollection::UnitIterator::advance(), and Unit::Ref().

210 {
211  Unit *tmp = NULL;
212  if (!it) return;
213  list< Unit* >::iterator tmpI = u.begin();
214  while ( (tmp = **it) ) {
215  tmp->Ref();
216  u.insert( tmpI, tmp );
217  ++tmpI;
218  it->advance();
219  }
220 }
void UnitCollection::prepend ( Unit unit)
inline

Definition at line 336 of file oldcollection.h.

337  {
338  u->next = new UnitListNode( unit, u->next );
339  }
void UnitCollection::prepend ( UnitListNode *  unitlistnode)
inline

Definition at line 340 of file oldcollection.h.

341  {
342  unitlistnode->next = u->next;
343  u->next = unitlistnode;
344  }
void UnitCollection::prepend ( UnitIterator iter)
void * UnitCollection::PushUnusedNode ( UnitListNode *  node)
static

Definition at line 42 of file oldcollection.cpp.

Referenced by FreeUnusedNodes().

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 }
bool UnitCollection::remove ( const class Unit )
bool UnitCollection::remove ( const Unit unit)

Definition at line 319 of file collection.cpp.

References erase().

320 {
321  bool res = false;
322  if (u.empty() || !unit)
323  return false;
324  for (list< Unit* >::iterator it = u.begin(); it != u.end();) {
325  if ( (*it) == unit ) {
326  erase( it );
327  res = true;
328  } else {
329  ++it;
330  }
331  }
332  return res;
333 }
const int UnitCollection::size ( ) const
inline

Definition at line 213 of file collection.h.

Referenced by UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Clear().

214  {
215  return u.size()-removedIters.size();
216  }

Friends And Related Function Documentation

friend class ConstIterator
friend

Definition at line 238 of file collection.h.

Referenced by constIterator().

friend class UnitIterator
friend

Definition at line 237 of file collection.h.

Referenced by createIterator().


The documentation for this class was generated from the following files: