MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLWEM/WEMDataStructure/WEMObjectVector.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00007 
00014 #ifndef __WEMObjectVector_H
00015 #define __WEMObjectVector_H
00016 
00017 #include "MLWEMIncludes.h"
00018 
00019 ML_START_NAMESPACE
00020 
00022 
00027 template <class T>
00028 class WEMObjectVector 
00029 {
00030 public:
00031 
00033   WEMObjectVector(unsigned int init = 0,unsigned int bs = 8192);
00035   virtual ~WEMObjectVector();
00037   inline unsigned int num() const { return length; }
00039   inline T* at(unsigned int pos) const { return (pos<length) ? &(block[pos]) : NULL; }
00041   inline T* first() {return at(0);}
00043   inline const T* first() const {return first();}
00045   virtual T* append(unsigned int *pos);
00047   virtual void deleteAt(unsigned int pos);
00049   virtual void destroy();
00050 
00051 private:
00052 
00054   unsigned int length;
00056   unsigned int capacity;
00058   unsigned int BLOCKSIZE;
00060   T **block;
00062   std::vector<unsigned int> deleted;
00063 
00064 protected:
00065 
00067   virtual void expand();
00068 };
00069 
00072 
00073 template <class T>
00074 WEMObjectVector<T>::WEMObjectVector(unsigned int init,unsigned int bs)
00075 {
00076   BLOCKSIZE = bs;
00077   length    = 0;
00078   capacity  = 0;
00079   block     = NULL;
00080   deleted.clear();
00081   
00082   if (init > 0)
00083   {
00084     const unsigned int iBS = init/BLOCKSIZE+1;
00085     ML_CHECK_NEW(block, T*[iBS])
00086     for (unsigned int i = 0; i < iBS; i++) 
00087     {
00088       ML_CHECK_NEW(block[i], T[BLOCKSIZE]);
00089     }
00090     capacity = iBS * BLOCKSIZE;
00091   }
00092 }
00093 
00095 
00096 template <class T>
00097 WEMObjectVector<T>::~WEMObjectVector()
00098 {
00099   destroy();
00100 }
00101 
00103 
00104 template <class T>
00105 void WEMObjectVector<T>::destroy()
00106 {
00107   if (capacity > 0) 
00108   {
00109     const unsigned int cBRatio = capacity/BLOCKSIZE;
00110     for (unsigned int i=0;i<cBRatio;i++) 
00111     {
00112       ML_DELETE_ARRAY(block[i]);
00113     }
00114     ML_DELETE_ARRAY(block);
00115   }
00116   deleted.clear();
00117   length = capacity = 0;
00118 }
00119 
00121 
00122 template <class T>
00123 void WEMObjectVector<T>::expand()
00124 {
00125   unsigned int i = 0;
00126   const unsigned int cBS = capacity/BLOCKSIZE;
00127   T* *newblock = NULL;
00128 
00129   ML_CHECK_NEW(newblock, T*[cBS+1]);
00130   for (i = 0; i < cBS; i++)
00131   { 
00132     newblock[i] = block[i]; 
00133   }
00134 
00135   ML_CHECK_NEW(newblock[cBS], T[BLOCKSIZE]);
00136 
00137   if (capacity > 0) 
00138   {
00139     ML_DELETE_ARRAY(block);
00140   }
00141   block = newblock;
00142   capacity += BLOCKSIZE;
00143 }
00144 
00146 
00147 template <class T>
00148 T* WEMObjectVector<T>::append(unsigned int *pos)
00149 {
00150   T *elem = NULL;
00151 
00152   if (deleted.size() > 0) 
00153   {
00154     *pos = deleted.back();
00155     deleted.pop_back();
00156     elem = &(block[*pos/BLOCKSIZE][*pos%BLOCKSIZE]);
00157     return elem;
00158   }
00159 
00160   if (length == capacity) 
00161   { 
00162     expand(); 
00163   }
00164 
00165   elem = &(block[length/BLOCKSIZE][length%BLOCKSIZE]);
00166 
00167   *pos = length;
00168   length ++;
00169   return elem;
00170 }
00171 
00173 
00174 template <class T>
00175 void WEMObjectVector<T>::deleteAt(unsigned int pos)
00176 {
00177   deleted.push_back(pos);
00178 }
00179 
00182 
00183 ML_END_NAMESPACE
00184 
00185 #endif // __WEMObjectVector_H