| 1 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 |
/** |
| 3 |
* Contains a simple container class. |
| 4 |
* \file IceContainer.cpp |
| 5 |
* \author Pierre Terdiman |
| 6 |
* \date February, 5, 2000 |
| 7 |
*/ |
| 8 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 9 |
|
| 10 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 11 |
/** |
| 12 |
* Contains a list of 32-bits values. |
| 13 |
* Use this class when you need to store an unknown number of values. The list is automatically |
| 14 |
* resized and can contains 32-bits entities (dwords or floats) |
| 15 |
* |
| 16 |
* \class Container |
| 17 |
* \author Pierre Terdiman |
| 18 |
* \version 1.0 |
| 19 |
* \date 08.15.98 |
| 20 |
*/ |
| 21 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 22 |
|
| 23 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 24 |
// Precompiled Header |
| 25 |
#include "Stdafx.h" |
| 26 |
|
| 27 |
using namespace IceCore; |
| 28 |
|
| 29 |
#define MAX_RESERVE_GROWTH_SIZE 65536U |
| 30 |
|
| 31 |
// Static members |
| 32 |
#ifdef CONTAINER_STATS |
| 33 |
udword Container::mNbContainers = 0; |
| 34 |
udword Container::mUsedRam = 0; |
| 35 |
#endif |
| 36 |
|
| 37 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 38 |
/** |
| 39 |
* Constructor. No entries allocated there. |
| 40 |
*/ |
| 41 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 42 |
Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2) |
| 43 |
{ |
| 44 |
#ifdef CONTAINER_STATS |
| 45 |
mNbContainers++; |
| 46 |
mUsedRam+=sizeof(Container); |
| 47 |
#endif |
| 48 |
} |
| 49 |
|
| 50 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 51 |
/** |
| 52 |
* Constructor. Also allocates a given number of entries. |
| 53 |
*/ |
| 54 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 |
Container::Container(udword size, float growth_factor) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(growth_factor) |
| 56 |
{ |
| 57 |
#ifdef CONTAINER_STATS |
| 58 |
mNbContainers++; |
| 59 |
mUsedRam+=sizeof(Container); |
| 60 |
#endif |
| 61 |
SetSize(size); |
| 62 |
} |
| 63 |
|
| 64 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 |
/** |
| 66 |
* Copy constructor. |
| 67 |
*/ |
| 68 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 69 |
Container::Container(const Container& object) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2) |
| 70 |
{ |
| 71 |
#ifdef CONTAINER_STATS |
| 72 |
mNbContainers++; |
| 73 |
mUsedRam+=sizeof(Container); |
| 74 |
#endif |
| 75 |
*this = object; |
| 76 |
} |
| 77 |
|
| 78 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 79 |
/** |
| 80 |
* Destructor. Frees everything and leaves. |
| 81 |
*/ |
| 82 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 |
Container::~Container() |
| 84 |
{ |
| 85 |
Empty(); |
| 86 |
#ifdef CONTAINER_STATS |
| 87 |
mNbContainers--; |
| 88 |
mUsedRam-=GetUsedRam(); |
| 89 |
#endif |
| 90 |
} |
| 91 |
|
| 92 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 93 |
/** |
| 94 |
* Clears the container. All stored values are deleted, and it frees used ram. |
| 95 |
* \see Reset() |
| 96 |
* \return Self-Reference |
| 97 |
*/ |
| 98 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 99 |
Container& Container::Empty() |
| 100 |
{ |
| 101 |
#ifdef CONTAINER_STATS |
| 102 |
mUsedRam-=mMaxNbEntries*sizeof(udword); |
| 103 |
#endif |
| 104 |
DELETEARRAY(mEntries); |
| 105 |
mCurNbEntries = mMaxNbEntries = 0; |
| 106 |
return *this; |
| 107 |
} |
| 108 |
|
| 109 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 110 |
/** |
| 111 |
* Resizes the container. |
| 112 |
* \param needed [in] assume the container can be added at least "needed" values |
| 113 |
* \return true if success. |
| 114 |
*/ |
| 115 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 116 |
bool Container::Resize(udword needed) |
| 117 |
{ |
| 118 |
#ifdef CONTAINER_STATS |
| 119 |
// Subtract previous amount of bytes |
| 120 |
mUsedRam-=mMaxNbEntries*sizeof(udword); |
| 121 |
#endif |
| 122 |
|
| 123 |
if (MAX_UDWORD - mCurNbEntries < needed) |
| 124 |
{ |
| 125 |
CHECKALLOC(null); |
| 126 |
} |
| 127 |
|
| 128 |
// Get more entries |
| 129 |
udword NewMaxNbEntries = mMaxNbEntries ? udword(mMaxNbEntries * mGrowthFactor) : 2; // Default nb Entries = 2 |
| 130 |
|
| 131 |
if (NewMaxNbEntries <= mMaxNbEntries) NewMaxNbEntries = MAX_UDWORD - mMaxNbEntries < MAX_RESERVE_GROWTH_SIZE ? MAX_UDWORD : mMaxNbEntries + MAX_RESERVE_GROWTH_SIZE; |
| 132 |
else if (NewMaxNbEntries - mMaxNbEntries > MAX_RESERVE_GROWTH_SIZE) NewMaxNbEntries = mMaxNbEntries + MAX_RESERVE_GROWTH_SIZE; |
| 133 |
|
| 134 |
if (NewMaxNbEntries < mCurNbEntries + needed) NewMaxNbEntries = mCurNbEntries + needed; |
| 135 |
|
| 136 |
// Get some bytes for new entries |
| 137 |
udword* NewEntries = new udword[NewMaxNbEntries]; |
| 138 |
CHECKALLOC(NewEntries); |
| 139 |
|
| 140 |
// Copy old data if needed |
| 141 |
if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword)); |
| 142 |
|
| 143 |
// Delete old data |
| 144 |
DELETEARRAY(mEntries); |
| 145 |
|
| 146 |
// Assign new pointer |
| 147 |
mEntries = NewEntries; |
| 148 |
mMaxNbEntries = NewMaxNbEntries; |
| 149 |
|
| 150 |
#ifdef CONTAINER_STATS |
| 151 |
// Add current amount of bytes |
| 152 |
mUsedRam+=mMaxNbEntries*sizeof(udword); |
| 153 |
#endif |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 159 |
/** |
| 160 |
* Sets the initial size of the container. If it already contains something, it's discarded. |
| 161 |
* \param nb [in] Number of entries |
| 162 |
* \return true if success |
| 163 |
*/ |
| 164 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 165 |
bool Container::SetSize(udword nb) |
| 166 |
{ |
| 167 |
// Make sure it's empty |
| 168 |
Empty(); |
| 169 |
|
| 170 |
// Checkings |
| 171 |
if(!nb) return false; |
| 172 |
|
| 173 |
// Initialize for nb entries |
| 174 |
mMaxNbEntries = nb; |
| 175 |
|
| 176 |
// Get some bytes for new entries |
| 177 |
mEntries = new udword[mMaxNbEntries]; |
| 178 |
CHECKALLOC(mEntries); |
| 179 |
|
| 180 |
#ifdef CONTAINER_STATS |
| 181 |
// Add current amount of bytes |
| 182 |
mUsedRam+=mMaxNbEntries*sizeof(udword); |
| 183 |
#endif |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 188 |
/** |
| 189 |
* Refits the container and get rid of unused bytes. |
| 190 |
* \return true if success |
| 191 |
*/ |
| 192 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 193 |
bool Container::Refit() |
| 194 |
{ |
| 195 |
#ifdef CONTAINER_STATS |
| 196 |
// Subtract previous amount of bytes |
| 197 |
mUsedRam-=mMaxNbEntries*sizeof(udword); |
| 198 |
#endif |
| 199 |
|
| 200 |
// Get just enough entries |
| 201 |
mMaxNbEntries = mCurNbEntries; |
| 202 |
if(!mMaxNbEntries) return false; |
| 203 |
|
| 204 |
// Get just enough bytes |
| 205 |
udword* NewEntries = new udword[mMaxNbEntries]; |
| 206 |
CHECKALLOC(NewEntries); |
| 207 |
|
| 208 |
#ifdef CONTAINER_STATS |
| 209 |
// Add current amount of bytes |
| 210 |
mUsedRam+=mMaxNbEntries*sizeof(udword); |
| 211 |
#endif |
| 212 |
|
| 213 |
// Copy old data |
| 214 |
CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword)); |
| 215 |
|
| 216 |
// Delete old data |
| 217 |
DELETEARRAY(mEntries); |
| 218 |
|
| 219 |
// Assign new pointer |
| 220 |
mEntries = NewEntries; |
| 221 |
|
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 226 |
/** |
| 227 |
* Checks whether the container already contains a given value. |
| 228 |
* \param entry [in] the value to look for in the container |
| 229 |
* \param location [out] a possible pointer to store the entry location |
| 230 |
* \see Add(udword entry) |
| 231 |
* \see Add(float entry) |
| 232 |
* \see Empty() |
| 233 |
* \return true if the value has been found in the container, else false. |
| 234 |
*/ |
| 235 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 236 |
bool Container::Contains(udword entry, udword* location) const |
| 237 |
{ |
| 238 |
// Look for the entry |
| 239 |
for(udword i=0;i<mCurNbEntries;i++) |
| 240 |
{ |
| 241 |
if(mEntries[i]==entry) |
| 242 |
{ |
| 243 |
if(location) *location = i; |
| 244 |
return true; |
| 245 |
} |
| 246 |
} |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 251 |
/** |
| 252 |
* Deletes an entry. If the container contains such an entry, it's removed. |
| 253 |
* \param entry [in] the value to delete. |
| 254 |
* \return true if the value has been found in the container, else false. |
| 255 |
* \warning This method is arbitrary slow (O(n)) and should be used carefully. Insertion order is not preserved. |
| 256 |
*/ |
| 257 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 258 |
bool Container::Delete(udword entry) |
| 259 |
{ |
| 260 |
// Look for the entry |
| 261 |
for(udword i=0;i<mCurNbEntries;i++) |
| 262 |
{ |
| 263 |
if(mEntries[i]==entry) |
| 264 |
{ |
| 265 |
// 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. |
| 266 |
DeleteIndex(i); |
| 267 |
return true; |
| 268 |
} |
| 269 |
} |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 274 |
/** |
| 275 |
* Deletes an entry, preserving the insertion order. If the container contains such an entry, it's removed. |
| 276 |
* \param entry [in] the value to delete. |
| 277 |
* \return true if the value has been found in the container, else false. |
| 278 |
* \warning This method is arbitrary slow (O(n)) and should be used carefully. |
| 279 |
*/ |
| 280 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 281 |
bool Container::DeleteKeepingOrder(udword entry) |
| 282 |
{ |
| 283 |
// Look for the entry |
| 284 |
for(udword i=0;i<mCurNbEntries;i++) |
| 285 |
{ |
| 286 |
if(mEntries[i]==entry) |
| 287 |
{ |
| 288 |
// Entry has been found at index i. |
| 289 |
// Shift entries to preserve order. You really should use a linked list instead. |
| 290 |
mCurNbEntries--; |
| 291 |
for(udword j=i;j<mCurNbEntries;j++) |
| 292 |
{ |
| 293 |
mEntries[j] = mEntries[j+1]; |
| 294 |
} |
| 295 |
return true; |
| 296 |
} |
| 297 |
} |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 302 |
/** |
| 303 |
* Gets the next entry, starting from input one. |
| 304 |
* \param entry [in/out] On input, the entry to look for. On output, the next entry |
| 305 |
* \param find_mode [in] wrap/clamp |
| 306 |
* \return Self-Reference |
| 307 |
*/ |
| 308 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 309 |
Container& Container::FindNext(udword& entry, FindMode find_mode) |
| 310 |
{ |
| 311 |
udword Location; |
| 312 |
if(Contains(entry, &Location)) |
| 313 |
{ |
| 314 |
Location++; |
| 315 |
if(Location==mCurNbEntries) Location = find_mode==FIND_WRAP ? 0 : mCurNbEntries-1; |
| 316 |
entry = mEntries[Location]; |
| 317 |
} |
| 318 |
return *this; |
| 319 |
} |
| 320 |
|
| 321 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 322 |
/** |
| 323 |
* Gets the previous entry, starting from input one. |
| 324 |
* \param entry [in/out] On input, the entry to look for. On output, the previous entry |
| 325 |
* \param find_mode [in] wrap/clamp |
| 326 |
* \return Self-Reference |
| 327 |
*/ |
| 328 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 329 |
Container& Container::FindPrev(udword& entry, FindMode find_mode) |
| 330 |
{ |
| 331 |
udword Location; |
| 332 |
if(Contains(entry, &Location)) |
| 333 |
{ |
| 334 |
Location--; |
| 335 |
if(Location==0xffffffff) Location = find_mode==FIND_WRAP ? mCurNbEntries-1 : 0; |
| 336 |
entry = mEntries[Location]; |
| 337 |
} |
| 338 |
return *this; |
| 339 |
} |
| 340 |
|
| 341 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 342 |
/** |
| 343 |
* Gets the ram used by the container. |
| 344 |
* \return the ram used in bytes. |
| 345 |
*/ |
| 346 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 347 |
udword Container::GetUsedRam() const |
| 348 |
{ |
| 349 |
return sizeof(Container) + mMaxNbEntries * sizeof(udword); |
| 350 |
} |
| 351 |
|
| 352 |
/*void Container::operator=(const Container& object) |
| 353 |
{ |
| 354 |
SetSize(object.GetNbEntries()); |
| 355 |
CopyMemory(mEntries, object.GetEntries(), mMaxNbEntries*sizeof(udword)); |
| 356 |
mCurNbEntries = mMaxNbEntries; |
| 357 |
}*/ |