IceContainer.cpp
Go to the documentation of this file.
1 
8 
11 
21 
24 // Precompiled Header
25 #include "Stdafx.h"
26 
27 using namespace IceCore;
28 
29 // Static members
30 #ifdef CONTAINER_STATS
33 #endif
34 
36 
39 Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
41 {
42 #ifdef CONTAINER_STATS
43  mNbContainers++;
44  mUsedRam+=sizeof(Container);
45 #endif
46 }
47 
49 
52 Container::Container(udword size, float growth_factor) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(growth_factor)
54 {
55 #ifdef CONTAINER_STATS
56  mNbContainers++;
57  mUsedRam+=sizeof(Container);
58 #endif
59  SetSize(size);
60 }
61 
63 
66 Container::Container(const Container& object) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
68 {
69 #ifdef CONTAINER_STATS
70  mNbContainers++;
71  mUsedRam+=sizeof(Container);
72 #endif
73  *this = object;
74 }
75 
77 
82 {
83  Empty();
84 #ifdef CONTAINER_STATS
85  mNbContainers--;
86  mUsedRam-=GetUsedRam();
87 #endif
88 }
89 
91 
98 {
99 #ifdef CONTAINER_STATS
100  mUsedRam-=mMaxNbEntries*sizeof(udword);
101 #endif
102  DELETEARRAY(mEntries);
103  mCurNbEntries = mMaxNbEntries = 0;
104  return *this;
105 }
106 
108 
113 bool Container::Resize(udword needed)
115 {
116 #ifdef CONTAINER_STATS
117  // Subtract previous amount of bytes
118  mUsedRam-=mMaxNbEntries*sizeof(udword);
119 #endif
120 
121  // Get more entries
122  mMaxNbEntries = mMaxNbEntries ? udword(float(mMaxNbEntries)*mGrowthFactor) : 2; // Default nb Entries = 2
123  if(mMaxNbEntries<mCurNbEntries + needed) mMaxNbEntries = mCurNbEntries + needed;
124 
125  // Get some bytes for new entries
126  udword* NewEntries = new udword[mMaxNbEntries];
127  CHECKALLOC(NewEntries);
128 
129 #ifdef CONTAINER_STATS
130  // Add current amount of bytes
131  mUsedRam+=mMaxNbEntries*sizeof(udword);
132 #endif
133 
134  // Copy old data if needed
135  if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
136 
137  // Delete old data
138  DELETEARRAY(mEntries);
139 
140  // Assign new pointer
141  mEntries = NewEntries;
142 
143  return true;
144 }
145 
147 
152 bool Container::SetSize(udword nb)
154 {
155  // Make sure it's empty
156  Empty();
157 
158  // Checkings
159  if(!nb) return false;
160 
161  // Initialize for nb entries
162  mMaxNbEntries = nb;
163 
164  // Get some bytes for new entries
165  mEntries = new udword[mMaxNbEntries];
166  CHECKALLOC(mEntries);
167 
168 #ifdef CONTAINER_STATS
169  // Add current amount of bytes
170  mUsedRam+=mMaxNbEntries*sizeof(udword);
171 #endif
172  return true;
173 }
174 
176 
180 bool Container::Refit()
182 {
183 #ifdef CONTAINER_STATS
184  // Subtract previous amount of bytes
185  mUsedRam-=mMaxNbEntries*sizeof(udword);
186 #endif
187 
188  // Get just enough entries
189  mMaxNbEntries = mCurNbEntries;
190  if(!mMaxNbEntries) return false;
191 
192  // Get just enough bytes
193  udword* NewEntries = new udword[mMaxNbEntries];
194  CHECKALLOC(NewEntries);
195 
196 #ifdef CONTAINER_STATS
197  // Add current amount of bytes
198  mUsedRam+=mMaxNbEntries*sizeof(udword);
199 #endif
200 
201  // Copy old data
202  CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
203 
204  // Delete old data
205  DELETEARRAY(mEntries);
206 
207  // Assign new pointer
208  mEntries = NewEntries;
209 
210  return true;
211 }
212 
214 
223 bool Container::Contains(udword entry, udword* location) const
225 {
226  // Look for the entry
227  for(udword i=0;i<mCurNbEntries;i++)
228  {
229  if(mEntries[i]==entry)
230  {
231  if(location) *location = i;
232  return true;
233  }
234  }
235  return false;
236 }
237 
239 
245 bool Container::Delete(udword entry)
247 {
248  // Look for the entry
249  for(udword i=0;i<mCurNbEntries;i++)
250  {
251  if(mEntries[i]==entry)
252  {
253  // 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.
254  DeleteIndex(i);
255  return true;
256  }
257  }
258  return false;
259 }
260 
262 
268 bool Container::DeleteKeepingOrder(udword entry)
270 {
271  // Look for the entry
272  for(udword i=0;i<mCurNbEntries;i++)
273  {
274  if(mEntries[i]==entry)
275  {
276  // Entry has been found at index i.
277  // Shift entries to preserve order. You really should use a linked list instead.
278  mCurNbEntries--;
279  for(udword j=i;j<mCurNbEntries;j++)
280  {
281  mEntries[j] = mEntries[j+1];
282  }
283  return true;
284  }
285  }
286  return false;
287 }
288 
290 
296 Container& Container::FindNext(udword& entry, FindMode find_mode)
298 {
299  udword Location;
300  if(Contains(entry, &Location))
301  {
302  Location++;
303  if(Location==mCurNbEntries) Location = find_mode==FIND_WRAP ? 0 : mCurNbEntries-1;
304  entry = mEntries[Location];
305  }
306  return *this;
307 }
308 
310 
316 Container& Container::FindPrev(udword& entry, FindMode find_mode)
318 {
319  udword Location;
320  if(Contains(entry, &Location))
321  {
322  Location--;
323  if(Location==0xffffffff) Location = find_mode==FIND_WRAP ? mCurNbEntries-1 : 0;
324  entry = mEntries[Location];
325  }
326  return *this;
327 }
328 
330 
334 udword Container::GetUsedRam() const
336 {
337  return sizeof(Container) + mMaxNbEntries * sizeof(udword);
338 }
339 
340 /*void Container::operator=(const Container& object)
341 {
342  SetSize(object.GetNbEntries());
343  CopyMemory(mEntries, object.GetEntries(), mMaxNbEntries*sizeof(udword));
344  mCurNbEntries = mMaxNbEntries;
345 }*/
inline_ bool Contains(const Point &p) const
bool Refit()
bool SetSize(udword nb)
#define null
our own NULL pointer
Definition: IceTypes.h:57
bool Contains(udword entry, udword *location=null) const
bool Resize(udword needed=1)
png_uint_32 size
Definition: png.h:1521
Container & Empty()
#define DELETEARRAY(x)
Deletes an array.
png_uint_32 i
Definition: png.h:2735
udword GetUsedRam() const
unsigned int udword
sizeof(udword) must be 4
Definition: IceTypes.h:65
static udword mUsedRam
Amount of bytes used by containers in the system.
Definition: OPC_IceHook.h:201
def j(str, encoding="cp932")
#define CHECKALLOC(x)
bool DeleteKeepingOrder(udword entry)
png_infop int int location
Definition: png.h:2487
FindMode
Definition: IceContainer.h:17
inline_ void CopyMemory(void *dest, const void *src, udword size)
Container & FindNext(udword &entry, FindMode find_mode=FIND_CLAMP)
static udword mNbContainers
Number of containers around.
Definition: OPC_IceHook.h:200
Container & FindPrev(udword &entry, FindMode find_mode=FIND_CLAMP)
bool Delete(udword entry)
void * object
Definition: jmemsys.h:48


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:22