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
IceContainer.cpp
Go to the documentation of this file.
1 
8 
11 
21 
24 // Precompiled Header
25 #include "Stdafx.h"
26 
27 
28 using namespace Opcode;
29 
30 // Static members
31 #ifdef CONTAINER_STATS
32 udword Container::mNbContainers = 0;
33 udword Container::mUsedRam = 0;
34 #endif
35 
37 
40 Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
42 {
43 #ifdef CONTAINER_STATS
44  mNbContainers++;
45  mUsedRam+=sizeof(Container);
46 #endif
47 }
48 
50 
53 Container::Container(udword size, float growth_factor) : 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 }
62 
64 
67 Container::Container(const Container& object) : 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 }
76 
78 
83 {
84  Empty();
85 #ifdef CONTAINER_STATS
86  mNbContainers--;
87  mUsedRam-=GetUsedRam();
88 #endif
89 }
90 
92 
99 {
100 #ifdef CONTAINER_STATS
101  mUsedRam-=mMaxNbEntries*sizeof(udword);
102 #endif
103  DELETEARRAY(mEntries);
104  mCurNbEntries = mMaxNbEntries = 0;
105  return *this;
106 }
107 
109 
114 bool Container::Resize(udword needed)
116 {
117 #ifdef CONTAINER_STATS
118  // Subtract previous amount of bytes
119  mUsedRam-=mMaxNbEntries*sizeof(udword);
120 #endif
121 
122  // Get more entries
123  mMaxNbEntries = mMaxNbEntries ? udword(float(mMaxNbEntries)*mGrowthFactor) : 2; // Default nb Entries = 2
124  if(mMaxNbEntries<mCurNbEntries + needed) mMaxNbEntries = mCurNbEntries + needed;
125 
126  // Get some bytes for new entries
127  udword* NewEntries = new udword[mMaxNbEntries];
128  CHECKALLOC(NewEntries);
129 
130 #ifdef CONTAINER_STATS
131  // Add current amount of bytes
132  mUsedRam+=mMaxNbEntries*sizeof(udword);
133 #endif
134 
135  // Copy old data if needed
136  if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
137 
138  // Delete old data
139  DELETEARRAY(mEntries);
140 
141  // Assign new pointer
142  mEntries = NewEntries;
143 
144  return true;
145 }
146 
148 
153 bool Container::SetSize(udword nb)
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 }
175 
177 
181 bool Container::Refit()
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 }
213 
215 
224 bool Container::Contains(udword entry, udword* location) const
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 }
238 
240 
246 bool Container::Delete(udword entry)
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 }
261 
263 
269 bool Container::DeleteKeepingOrder(udword entry)
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 }
289 
291 
297 Container& Container::FindNext(udword& entry, FindMode find_mode)
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 }
309 
311 
317 Container& Container::FindPrev(udword& entry, FindMode find_mode)
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 }
329 
331 
335 udword Container::GetUsedRam() const
337 {
338  return sizeof(Container) + mMaxNbEntries * sizeof(udword);
339 }
340 
341 void Container::operator=(const Container& object)
342 {
343  SetSize(object.GetNbEntries());
344  CopyMemory(mEntries, object.GetEntries(), mMaxNbEntries*sizeof(udword));
345  mCurNbEntries = mMaxNbEntries;
346 }
347