Go to the documentation of this file.00001
00002
00008
00009
00011
00021
00022
00024
00025 #include "Stdafx.h"
00026
00027 using namespace IceCore;
00028
00029
00030 #ifdef CONTAINER_STATS
00031 udword Container::mNbContainers = 0;
00032 udword Container::mUsedRam = 0;
00033 #endif
00034
00036
00039
00040 Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
00041 {
00042 #ifdef CONTAINER_STATS
00043 mNbContainers++;
00044 mUsedRam+=sizeof(Container);
00045 #endif
00046 }
00047
00049
00052
00053 Container::Container(udword size, float growth_factor) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(growth_factor)
00054 {
00055 #ifdef CONTAINER_STATS
00056 mNbContainers++;
00057 mUsedRam+=sizeof(Container);
00058 #endif
00059 SetSize(size);
00060 }
00061
00063
00066
00067 Container::Container(const Container& object) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
00068 {
00069 #ifdef CONTAINER_STATS
00070 mNbContainers++;
00071 mUsedRam+=sizeof(Container);
00072 #endif
00073 *this = object;
00074 }
00075
00077
00080
00081 Container::~Container()
00082 {
00083 Empty();
00084 #ifdef CONTAINER_STATS
00085 mNbContainers--;
00086 mUsedRam-=GetUsedRam();
00087 #endif
00088 }
00089
00091
00096
00097 Container& Container::Empty()
00098 {
00099 #ifdef CONTAINER_STATS
00100 mUsedRam-=mMaxNbEntries*sizeof(udword);
00101 #endif
00102 DELETEARRAY(mEntries);
00103 mCurNbEntries = mMaxNbEntries = 0;
00104 return *this;
00105 }
00106
00108
00113
00114 bool Container::Resize(udword needed)
00115 {
00116 #ifdef CONTAINER_STATS
00117
00118 mUsedRam-=mMaxNbEntries*sizeof(udword);
00119 #endif
00120
00121
00122 mMaxNbEntries = mMaxNbEntries ? udword(float(mMaxNbEntries)*mGrowthFactor) : 2;
00123 if(mMaxNbEntries<mCurNbEntries + needed) mMaxNbEntries = mCurNbEntries + needed;
00124
00125
00126 udword* NewEntries = new udword[mMaxNbEntries];
00127 CHECKALLOC(NewEntries);
00128
00129 #ifdef CONTAINER_STATS
00130
00131 mUsedRam+=mMaxNbEntries*sizeof(udword);
00132 #endif
00133
00134
00135 if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
00136
00137
00138 DELETEARRAY(mEntries);
00139
00140
00141 mEntries = NewEntries;
00142
00143 return true;
00144 }
00145
00147
00152
00153 bool Container::SetSize(udword nb)
00154 {
00155
00156 Empty();
00157
00158
00159 if(!nb) return false;
00160
00161
00162 mMaxNbEntries = nb;
00163
00164
00165 mEntries = new udword[mMaxNbEntries];
00166 CHECKALLOC(mEntries);
00167
00168 #ifdef CONTAINER_STATS
00169
00170 mUsedRam+=mMaxNbEntries*sizeof(udword);
00171 #endif
00172 return true;
00173 }
00174
00176
00180
00181 bool Container::Refit()
00182 {
00183 #ifdef CONTAINER_STATS
00184
00185 mUsedRam-=mMaxNbEntries*sizeof(udword);
00186 #endif
00187
00188
00189 mMaxNbEntries = mCurNbEntries;
00190 if(!mMaxNbEntries) return false;
00191
00192
00193 udword* NewEntries = new udword[mMaxNbEntries];
00194 CHECKALLOC(NewEntries);
00195
00196 #ifdef CONTAINER_STATS
00197
00198 mUsedRam+=mMaxNbEntries*sizeof(udword);
00199 #endif
00200
00201
00202 CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
00203
00204
00205 DELETEARRAY(mEntries);
00206
00207
00208 mEntries = NewEntries;
00209
00210 return true;
00211 }
00212
00214
00223
00224 bool Container::Contains(udword entry, udword* location) const
00225 {
00226
00227 for(udword i=0;i<mCurNbEntries;i++)
00228 {
00229 if(mEntries[i]==entry)
00230 {
00231 if(location) *location = i;
00232 return true;
00233 }
00234 }
00235 return false;
00236 }
00237
00239
00245
00246 bool Container::Delete(udword entry)
00247 {
00248
00249 for(udword i=0;i<mCurNbEntries;i++)
00250 {
00251 if(mEntries[i]==entry)
00252 {
00253
00254 DeleteIndex(i);
00255 return true;
00256 }
00257 }
00258 return false;
00259 }
00260
00262
00268
00269 bool Container::DeleteKeepingOrder(udword entry)
00270 {
00271
00272 for(udword i=0;i<mCurNbEntries;i++)
00273 {
00274 if(mEntries[i]==entry)
00275 {
00276
00277
00278 mCurNbEntries--;
00279 for(udword j=i;j<mCurNbEntries;j++)
00280 {
00281 mEntries[j] = mEntries[j+1];
00282 }
00283 return true;
00284 }
00285 }
00286 return false;
00287 }
00288
00290
00296
00297 Container& Container::FindNext(udword& entry, FindMode find_mode)
00298 {
00299 udword Location;
00300 if(Contains(entry, &Location))
00301 {
00302 Location++;
00303 if(Location==mCurNbEntries) Location = find_mode==FIND_WRAP ? 0 : mCurNbEntries-1;
00304 entry = mEntries[Location];
00305 }
00306 return *this;
00307 }
00308
00310
00316
00317 Container& Container::FindPrev(udword& entry, FindMode find_mode)
00318 {
00319 udword Location;
00320 if(Contains(entry, &Location))
00321 {
00322 Location--;
00323 if(Location==0xffffffff) Location = find_mode==FIND_WRAP ? mCurNbEntries-1 : 0;
00324 entry = mEntries[Location];
00325 }
00326 return *this;
00327 }
00328
00330
00334
00335 udword Container::GetUsedRam() const
00336 {
00337 return sizeof(Container) + mMaxNbEntries * sizeof(udword);
00338 }
00339
00340
00341
00342
00343
00344
00345