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

#include <IceContainer.h>

Inheritance diagram for Container:
CollisionFaces Opcode::CollisionFaces Opcode::TriangleList Opcode::TriList Pairs TriangleList TriList

Public Member Functions

 Container ()
 
 Container (const Container &object)
 
 Container (udword size, float growth_factor)
 
 ~Container ()
 
inline_ ContainerAdd (udword entry)
 
inline_ ContainerAdd (const udword *entries, udword nb)
 
inline_ ContainerAdd (float entry)
 
inline_ ContainerAdd (const float *entries, udword nb)
 
inline_ ContainerAddUnique (udword entry)
 Add unique [slow]. More...
 
ContainerEmpty ()
 
inline_ void Reset ()
 
inline_ void ForceSize (udword size)
 
bool SetSize (udword nb)
 
bool Refit ()
 
bool Contains (udword entry, udword *location=null) const
 
bool Delete (udword entry)
 
bool DeleteKeepingOrder (udword entry)
 
inline_ void DeleteLastEntry ()
 Deletes the very last entry. More...
 
inline_ void DeleteIndex (udword index)
 Deletes the entry whose index is given. More...
 
ContainerFindNext (udword &entry, FindMode find_mode=FIND_CLAMP)
 
ContainerFindPrev (udword &entry, FindMode find_mode=FIND_CLAMP)
 
inline_ udword GetNbEntries () const
 Returns the current number of entries. More...
 
inline_ udword GetEntry (udword i) const
 Returns ith entry. More...
 
inline_ udwordGetEntries () const
 Returns the list of entries. More...
 
inline_ udword GetFirst () const
 
inline_ udword GetLast () const
 
inline_ float GetGrowthFactor () const
 Returns the growth factor. More...
 
inline_ void SetGrowthFactor (float growth)
 Sets the growth factor. More...
 
inline_ bool IsFull () const
 Checks the container is full. More...
 
inline_ BOOL IsNotEmpty () const
 Checks the container is empty. More...
 
inline_ udword operator[] (udword i) const
 Read-access as an array. More...
 
inline_ udwordoperator[] (udword i)
 Write-access as an array. More...
 
udword GetUsedRam () const
 
void operator= (const Container &object)
 Operator for "Container A = Container B". More...
 
inline_ udword GetNbContainers () const
 
inline_ udword GetTotalBytes () const
 

Detailed Description

Contains a list of 32-bits values. Use this class when you need to store an unknown number of values. The list is automatically resized and can contains 32-bits entities (dwords or floats)

Author
Pierre Terdiman
Version
1.0
Date
08.15.98

Definition at line 25 of file IceContainer.h.

Constructor & Destructor Documentation

Container::Container ( )

Constructor. No entries allocated there.

Definition at line 41 of file IceContainer.cpp.

41  : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
42 {
43 #ifdef CONTAINER_STATS
44  mNbContainers++;
45  mUsedRam+=sizeof(Container);
46 #endif
47 }
Container::Container ( const Container object)

Copy constructor.

Definition at line 68 of file IceContainer.cpp.

68  : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
69 {
70 #ifdef CONTAINER_STATS
71  mNbContainers++;
72  mUsedRam+=sizeof(Container);
73 #endif
74  *this = object;
75 }
Container::Container ( udword  size,
float  growth_factor 
)

Constructor. Also allocates a given number of entries.

Definition at line 54 of file IceContainer.cpp.

54  : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(growth_factor)
55 {
56 #ifdef CONTAINER_STATS
57  mNbContainers++;
58  mUsedRam+=sizeof(Container);
59 #endif
60  SetSize(size);
61 }
Container::~Container ( )

Destructor. Frees everything and leaves.

Definition at line 82 of file IceContainer.cpp.

83 {
84  Empty();
85 #ifdef CONTAINER_STATS
86  mNbContainers--;
87  mUsedRam-=GetUsedRam();
88 #endif
89 }

Member Function Documentation

inline_ Container& Container::Add ( udword  entry)
inline

A O(1) method to add a value in the container. The container is automatically resized if needed. The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation costs a lot more than the call overhead...

Parameters
entry[in] a udword to store in the container
See Also
Add(float entry)
Empty()
Contains(udword entry)
Returns
Self-Reference

Definition at line 47 of file IceContainer.h.

Referenced by CollisionFaces::AddFace(), Pairs::AddPair(), TriList::AddTri(), and TriangleList::AddTriangle().

48  {
49  // Resize if needed
50  if(mCurNbEntries==mMaxNbEntries) Resize();
51 
52  // Add new entry
53  mEntries[mCurNbEntries++] = entry;
54  return *this;
55  }
inline_ Container& Container::Add ( const udword entries,
udword  nb 
)
inline

Definition at line 57 of file IceContainer.h.

References CopyMemory().

58  {
59  // Resize if needed
60  if(mCurNbEntries+nb>mMaxNbEntries) Resize(nb);
61 
62  // Add new entry
63  CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(udword));
64  mCurNbEntries+=nb;
65  return *this;
66  }
inline_ Container& Container::Add ( float  entry)
inline

A O(1) method to add a value in the container. The container is automatically resized if needed. The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation costs a lot more than the call overhead...

Parameters
entry[in] a float to store in the container
See Also
Add(udword entry)
Empty()
Contains(udword entry)
Returns
Self-Reference

Definition at line 81 of file IceContainer.h.

82  {
83  // Resize if needed
84  if(mCurNbEntries==mMaxNbEntries) Resize();
85 
86  // Add new entry
87  mEntries[mCurNbEntries++] = IR(entry);
88  return *this;
89  }
inline_ Container& Container::Add ( const float entries,
udword  nb 
)
inline

Definition at line 91 of file IceContainer.h.

References CopyMemory().

92  {
93  // Resize if needed
94  if(mCurNbEntries+nb>mMaxNbEntries) Resize(nb);
95 
96  // Add new entry
97  CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(float));
98  mCurNbEntries+=nb;
99  return *this;
100  }
inline_ Container& Container::AddUnique ( udword  entry)
inline

Add unique [slow].

Definition at line 103 of file IceContainer.h.

104  {
105  if(!Contains(entry)) Add(entry);
106  return *this;
107  }
bool Container::Contains ( udword  entry,
udword location = null 
) const

Checks whether the container already contains a given value.

Parameters
entry[in] the value to look for in the container
location[out] a possible pointer to store the entry location
See Also
Add(udword entry)
Add(float entry)
Empty()
Returns
true if the value has been found in the container, else false.

Definition at line 225 of file IceContainer.cpp.

226 {
227  // Look for the entry
228  for(udword i=0;i<mCurNbEntries;i++)
229  {
230  if(mEntries[i]==entry)
231  {
232  if(location) *location = i;
233  return true;
234  }
235  }
236  return false;
237 }
bool Container::Delete ( udword  entry)

Deletes an entry. If the container contains such an entry, it's removed.

Parameters
entry[in] the value to delete.
Returns
true if the value has been found in the container, else false.
Warning
This method is arbitrary slow (O(n)) and should be used carefully. Insertion order is not preserved.

Definition at line 247 of file IceContainer.cpp.

248 {
249  // Look for the entry
250  for(udword i=0;i<mCurNbEntries;i++)
251  {
252  if(mEntries[i]==entry)
253  {
254  // Entry has been found at index i. The strategy is to copy the last current entry at index i, and decrement the current number of entries.
255  DeleteIndex(i);
256  return true;
257  }
258  }
259  return false;
260 }
inline_ void Container::DeleteIndex ( udword  index)
inline

Deletes the entry whose index is given.

Definition at line 164 of file IceContainer.h.

References index.

164 { mEntries[index] = mEntries[--mCurNbEntries]; }
bool Container::DeleteKeepingOrder ( udword  entry)

Deletes an entry, preserving the insertion order. If the container contains such an entry, it's removed.

Parameters
entry[in] the value to delete.
Returns
true if the value has been found in the container, else false.
Warning
This method is arbitrary slow (O(n)) and should be used carefully.

Definition at line 270 of file IceContainer.cpp.

271 {
272  // Look for the entry
273  for(udword i=0;i<mCurNbEntries;i++)
274  {
275  if(mEntries[i]==entry)
276  {
277  // Entry has been found at index i.
278  // Shift entries to preserve order. You really should use a linked list instead.
279  mCurNbEntries--;
280  for(udword j=i;j<mCurNbEntries;j++)
281  {
282  mEntries[j] = mEntries[j+1];
283  }
284  return true;
285  }
286  }
287  return false;
288 }
inline_ void Container::DeleteLastEntry ( )
inline

Deletes the very last entry.

Definition at line 162 of file IceContainer.h.

Referenced by Pairs::DeleteLastPair().

162 { if(mCurNbEntries) mCurNbEntries--; }
Container & Container::Empty ( )

Clears the container. All stored values are deleted, and it frees used ram.

See Also
Reset()
Returns
Self-Reference

Definition at line 98 of file IceContainer.cpp.

99 {
100 #ifdef CONTAINER_STATS
101  mUsedRam-=mMaxNbEntries*sizeof(udword);
102 #endif
103  DELETEARRAY(mEntries);
104  mCurNbEntries = mMaxNbEntries = 0;
105  return *this;
106 }
Container & Container::FindNext ( udword entry,
FindMode  find_mode = FIND_CLAMP 
)

Gets the next entry, starting from input one.

Parameters
entry[in/out] On input, the entry to look for. On output, the next entry
find_mode[in] wrap/clamp
Returns
Self-Reference

Definition at line 298 of file IceContainer.cpp.

299 {
300  udword Location;
301  if(Contains(entry, &Location))
302  {
303  Location++;
304  if(Location==mCurNbEntries) Location = find_mode==FIND_WRAP ? 0 : mCurNbEntries-1;
305  entry = mEntries[Location];
306  }
307  return *this;
308 }
Container & Container::FindPrev ( udword entry,
FindMode  find_mode = FIND_CLAMP 
)

Gets the previous entry, starting from input one.

Parameters
entry[in/out] On input, the entry to look for. On output, the previous entry
find_mode[in] wrap/clamp
Returns
Self-Reference

Definition at line 318 of file IceContainer.cpp.

319 {
320  udword Location;
321  if(Contains(entry, &Location))
322  {
323  Location--;
324  if(Location==0xffffffff) Location = find_mode==FIND_WRAP ? mCurNbEntries-1 : 0;
325  entry = mEntries[Location];
326  }
327  return *this;
328 }
inline_ void Container::ForceSize ( udword  size)
inline

Definition at line 133 of file IceContainer.h.

References size.

134  {
135  mCurNbEntries = size;
136  }
inline_ udword* Container::GetEntries ( ) const
inline
inline_ udword Container::GetEntry ( udword  i) const
inline

Returns ith entry.

Definition at line 171 of file IceContainer.h.

inline_ udword Container::GetFirst ( ) const
inline

Definition at line 174 of file IceContainer.h.

174 { return mEntries[0]; }
inline_ float Container::GetGrowthFactor ( ) const
inline

Returns the growth factor.

Definition at line 178 of file IceContainer.h.

inline_ udword Container::GetLast ( ) const
inline

Definition at line 175 of file IceContainer.h.

175 { return mEntries[mCurNbEntries-1]; }
inline_ udword Container::GetNbContainers ( ) const
inline

Definition at line 195 of file IceContainer.h.

195 { return mNbContainers; }
inline_ udword Container::GetTotalBytes ( ) const
inline

Definition at line 196 of file IceContainer.h.

196 { return mUsedRam; }
udword Container::GetUsedRam ( ) const

Gets the ram used by the container.

Returns
the ram used in bytes.

Definition at line 336 of file IceContainer.cpp.

337 {
338  return sizeof(Container) + mMaxNbEntries * sizeof(udword);
339 }
inline_ bool Container::IsFull ( ) const
inline

Checks the container is full.

Definition at line 180 of file IceContainer.h.

inline_ BOOL Container::IsNotEmpty ( ) const
inline

Checks the container is empty.

Definition at line 181 of file IceContainer.h.

Referenced by Pairs::HasPairs().

void Container::operator= ( const Container object)

Operator for "Container A = Container B".

Definition at line 341 of file IceContainer.cpp.

References CopyMemory(), GetEntries(), GetNbEntries(), and SetSize().

342 {
343  SetSize(object.GetNbEntries());
344  CopyMemory(mEntries, object.GetEntries(), mMaxNbEntries*sizeof(udword));
345  mCurNbEntries = mMaxNbEntries;
346 }
inline_ udword Container::operator[] ( udword  i) const
inline

Read-access as an array.

Definition at line 184 of file IceContainer.h.

References OPASSERT.

184 { OPASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
inline_ udword& Container::operator[] ( udword  i)
inline

Write-access as an array.

Definition at line 186 of file IceContainer.h.

References OPASSERT.

186 { OPASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
bool Container::Refit ( )

Refits the container and get rid of unused bytes.

Returns
true if success

Definition at line 182 of file IceContainer.cpp.

183 {
184 #ifdef CONTAINER_STATS
185  // Subtract previous amount of bytes
186  mUsedRam-=mMaxNbEntries*sizeof(udword);
187 #endif
188 
189  // Get just enough entries
190  mMaxNbEntries = mCurNbEntries;
191  if(!mMaxNbEntries) return false;
192 
193  // Get just enough bytes
194  udword* NewEntries = new udword[mMaxNbEntries];
195  CHECKALLOC(NewEntries);
196 
197 #ifdef CONTAINER_STATS
198  // Add current amount of bytes
199  mUsedRam+=mMaxNbEntries*sizeof(udword);
200 #endif
201 
202  // Copy old data
203  CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
204 
205  // Delete old data
206  DELETEARRAY(mEntries);
207 
208  // Assign new pointer
209  mEntries = NewEntries;
210 
211  return true;
212 }
inline_ void Container::Reset ( )
inline

Resets the container. Stored values are discarded but the buffer is kept so that further calls don't need resizing again. That's a kind of temporal coherence.

See Also
Empty()

Definition at line 125 of file IceContainer.h.

Referenced by Opcode::HybridSphereCollider::Collide(), Opcode::HybridAABBCollider::Collide(), Opcode::HybridLSSCollider::Collide(), Opcode::HybridPlanesCollider::Collide(), Opcode::HybridOBBCollider::Collide(), CollisionFaces::Reset(), and Pairs::ResetPairs().

126  {
127  // Avoid the write if possible
128  // ### CMOV
129  if(mCurNbEntries) mCurNbEntries = 0;
130  }
inline_ void Container::SetGrowthFactor ( float  growth)
inline

Sets the growth factor.

Definition at line 179 of file IceContainer.h.

bool Container::SetSize ( udword  nb)

Sets the initial size of the container. If it already contains something, it's discarded.

Parameters
nb[in] Number of entries
Returns
true if success

Definition at line 154 of file IceContainer.cpp.

Referenced by operator=().

155 {
156  // Make sure it's empty
157  Empty();
158 
159  // Checkings
160  if(!nb) return false;
161 
162  // Initialize for nb entries
163  mMaxNbEntries = nb;
164 
165  // Get some bytes for new entries
166  mEntries = new udword[mMaxNbEntries];
167  CHECKALLOC(mEntries);
168 
169 #ifdef CONTAINER_STATS
170  // Add current amount of bytes
171  mUsedRam+=mMaxNbEntries*sizeof(udword);
172 #endif
173  return true;
174 }

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