IceContainer.cpp
Go to the documentation of this file.
00001 
00002 
00008 
00009 
00011 
00021 
00022 
00024 // Precompiled Header
00025 #include "Stdafx.h"
00026 
00027 using namespace IceCore;
00028 
00029 // Static members
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         // Subtract previous amount of bytes
00118         mUsedRam-=mMaxNbEntries*sizeof(udword);
00119 #endif
00120 
00121         // Get more entries
00122         mMaxNbEntries = mMaxNbEntries ? udword(float(mMaxNbEntries)*mGrowthFactor) : 2; // Default nb Entries = 2
00123         if(mMaxNbEntries<mCurNbEntries + needed)        mMaxNbEntries = mCurNbEntries + needed;
00124 
00125         // Get some bytes for new entries
00126         udword* NewEntries = new udword[mMaxNbEntries];
00127         CHECKALLOC(NewEntries);
00128 
00129 #ifdef CONTAINER_STATS
00130         // Add current amount of bytes
00131         mUsedRam+=mMaxNbEntries*sizeof(udword);
00132 #endif
00133 
00134         // Copy old data if needed
00135         if(mCurNbEntries)       CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
00136 
00137         // Delete old data
00138         DELETEARRAY(mEntries);
00139 
00140         // Assign new pointer
00141         mEntries = NewEntries;
00142 
00143         return true;
00144 }
00145 
00147 
00152 
00153 bool Container::SetSize(udword nb)
00154 {
00155         // Make sure it's empty
00156         Empty();
00157 
00158         // Checkings
00159         if(!nb) return false;
00160 
00161         // Initialize for nb entries
00162         mMaxNbEntries = nb;
00163 
00164         // Get some bytes for new entries
00165         mEntries = new udword[mMaxNbEntries];
00166         CHECKALLOC(mEntries);
00167 
00168 #ifdef CONTAINER_STATS
00169         // Add current amount of bytes
00170         mUsedRam+=mMaxNbEntries*sizeof(udword);
00171 #endif
00172         return true;
00173 }
00174 
00176 
00180 
00181 bool Container::Refit()
00182 {
00183 #ifdef CONTAINER_STATS
00184         // Subtract previous amount of bytes
00185         mUsedRam-=mMaxNbEntries*sizeof(udword);
00186 #endif
00187 
00188         // Get just enough entries
00189         mMaxNbEntries = mCurNbEntries;
00190         if(!mMaxNbEntries)      return false;
00191 
00192         // Get just enough bytes
00193         udword* NewEntries = new udword[mMaxNbEntries];
00194         CHECKALLOC(NewEntries);
00195 
00196 #ifdef CONTAINER_STATS
00197         // Add current amount of bytes
00198         mUsedRam+=mMaxNbEntries*sizeof(udword);
00199 #endif
00200 
00201         // Copy old data
00202         CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
00203 
00204         // Delete old data
00205         DELETEARRAY(mEntries);
00206 
00207         // Assign new pointer
00208         mEntries = NewEntries;
00209 
00210         return true;
00211 }
00212 
00214 
00223 
00224 bool Container::Contains(udword entry, udword* location) const
00225 {
00226         // Look for the entry
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         // Look for the entry
00249         for(udword i=0;i<mCurNbEntries;i++)
00250         {
00251                 if(mEntries[i]==entry)
00252                 {
00253                         // 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.
00254                         DeleteIndex(i);
00255                         return true;
00256                 }
00257         }
00258         return false;
00259 }
00260 
00262 
00268 
00269 bool Container::DeleteKeepingOrder(udword entry)
00270 {
00271         // Look for the entry
00272         for(udword i=0;i<mCurNbEntries;i++)
00273         {
00274                 if(mEntries[i]==entry)
00275                 {
00276                         // Entry has been found at index i.
00277                         // Shift entries to preserve order. You really should use a linked list instead.
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 /*void Container::operator=(const Container& object)
00341 {
00342         SetSize(object.GetNbEntries());
00343         CopyMemory(mEntries, object.GetEntries(), mMaxNbEntries*sizeof(udword));
00344         mCurNbEntries = mMaxNbEntries;
00345 }*/


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16