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
Opcode::SAP_PairData Class Reference

#include <Opcode.h>

Public Member Functions

 SAP_PairData ()
 
 ~SAP_PairData ()
 
bool Init (udword nb_objects)
 
void AddPair (udword id1, udword id2)
 
void RemovePair (udword id1, udword id2)
 
void DumpPairs (Pairs &pairs) const
 
void DumpPairs (PairCallback callback, void *user_data) const
 

Detailed Description

Definition at line 39 of file Opcode.h.

Constructor & Destructor Documentation

SAP_PairData::SAP_PairData ( )

Constructor.

Definition at line 109 of file OPC_SweepAndPrune.cpp.

109  :
110  mNbElements (0),
111  mNbUsedElements (0),
112  mElementPool (null),
113  mFirstFree (null),
114  mNbObjects (0),
115  mArray (null)
116 {
117 }
SAP_PairData::~SAP_PairData ( )

Destructor.

Definition at line 124 of file OPC_SweepAndPrune.cpp.

125 {
126  Release();
127 }

Member Function Documentation

void SAP_PairData::AddPair ( udword  id1,
udword  id2 
)

Definition at line 242 of file OPC_SweepAndPrune.cpp.

References Delta(), Opcode::SAP_Element::mID, Opcode::SAP_Element::mNext, null, OPASSERT, Remap(), and Sort().

Referenced by Opcode::SweepAndPrune::Init(), and Opcode::SweepAndPrune::UpdateObject().

243 {
244  // Order the ids
245  Sort(id1, id2);
246 
247  OPASSERT(id1<mNbObjects);
248  if(id1>=mNbObjects) return;
249 
250  // Select the right list from "mArray".
251  SAP_Element* Current = mArray[id1];
252 
253  if(!Current)
254  {
255  // Empty slot => create new element
256  mArray[id1] = GetFreeElem(id2, null);
257  }
258  else if(Current->mID>id2)
259  {
260  // The list is not empty but all elements are greater than id2 => insert id2 in the front.
261  mArray[id1] = GetFreeElem(id2, mArray[id1]);
262  }
263  else
264  {
265  // Else find the correct location in the sorted list (ascending order) and insert id2 there.
266  while(Current->mNext)
267  {
268  if(Current->mNext->mID > id2) break;
269 
270  Current = Current->mNext;
271  }
272 
273  if(Current->mID==id2) return; // The pair already exists
274 
275 // Current->mNext = GetFreeElem(id2, Current->mNext);
276  udword Delta;
277  SAP_Element* E = GetFreeElem(id2, Current->mNext, &Delta);
278  if(Delta) Remap(Current, Delta);
279  Current->mNext = E;
280  }
281 }
void SAP_PairData::DumpPairs ( Pairs pairs) const

Definition at line 325 of file OPC_SweepAndPrune.cpp.

References Pairs::AddPair(), Opcode::SAP_Element::mID, Opcode::SAP_Element::mNext, and OPASSERT.

Referenced by Opcode::SweepAndPrune::GetPairs().

326 {
327  // ### Ugly and slow
328  for(udword i=0;i<mNbObjects;i++)
329  {
330  SAP_Element* Current = mArray[i];
331  while(Current)
332  {
333  OPASSERT(Current->mID<mNbObjects);
334 
335  pairs.AddPair(i, Current->mID);
336  Current = Current->mNext;
337  }
338  }
339 }
void SAP_PairData::DumpPairs ( PairCallback  callback,
void *  user_data 
) const

Definition at line 341 of file OPC_SweepAndPrune.cpp.

References Opcode::SAP_Element::mID, Opcode::SAP_Element::mNext, and OPASSERT.

342 {
343  if(!callback) return;
344 
345  // ### Ugly and slow
346  for(udword i=0;i<mNbObjects;i++)
347  {
348  SAP_Element* Current = mArray[i];
349  while(Current)
350  {
351  OPASSERT(Current->mID<mNbObjects);
352 
353  if(!(callback)(i, Current->mID, user_data)) return;
354  Current = Current->mNext;
355  }
356  }
357 }
bool SAP_PairData::Init ( udword  nb_objects)

Initializes.

Parameters
nb_objects[in]
Returns
true if success

Definition at line 145 of file OPC_SweepAndPrune.cpp.

Referenced by Opcode::SweepAndPrune::Init().

146 {
147  // Make sure everything has been released
148  Release();
149  if(!nb_objects) return false;
150 
151  mArray = new SAP_Element*[nb_objects];
152  CHECKALLOC(mArray);
153  ZeroMemory(mArray, nb_objects*sizeof(SAP_Element*));
154  mNbObjects = nb_objects;
155 
156  return true;
157 }
void SAP_PairData::RemovePair ( udword  id1,
udword  id2 
)

Definition at line 284 of file OPC_SweepAndPrune.cpp.

References Opcode::SAP_Element::mID, Opcode::SAP_Element::mNext, and Sort().

Referenced by Opcode::SweepAndPrune::UpdateObject().

285 {
286  // Order the ids.
287  Sort(id1, id2);
288 
289  // Exit if the pair doesn't exist in the set
290  if(id1>=mNbObjects) return;
291 
292  // Otherwise, select the correct list.
293  SAP_Element* Current = mArray[id1];
294 
295  // If this list is empty, the pair doesn't exist.
296  if(!Current) return;
297 
298  // Otherwise, if id2 is the first element, delete it.
299  if(Current->mID==id2)
300  {
301  mArray[id1] = Current->mNext;
302  FreeElem(Current);
303  }
304  else
305  {
306  // If id2 is not the first element, start traversing the sorted list.
307  while(Current->mNext)
308  {
309  // If we have moved too far away without hitting id2, then the pair doesn't exist
310  if(Current->mNext->mID > id2) return;
311 
312  // Otherwise, delete id2.
313  if(Current->mNext->mID == id2)
314  {
315  SAP_Element* Temp = Current->mNext;
316  Current->mNext = Temp->mNext;
317  FreeElem(Temp);
318  return;
319  }
320  Current = Current->mNext;
321  }
322  }
323 }

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