MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLCSO/CSOTools/CSOObjectVector.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 
00011 
00012 #ifndef __CSOObjectVector_H
00013 #define __CSOObjectVector_H
00014 
00015 #include "CSOHeapObject.h"
00016 
00017 
00018 ML_START_NAMESPACE
00019 
00020 
00022 
00027 template <class T>
00028 class CSOObjectVector 
00029 {
00030 
00031 public:
00032 
00034   CSOObjectVector(unsigned int init = 0,unsigned int bs = 65535);
00036   virtual ~CSOObjectVector();
00037 
00039   inline unsigned int num() const {return length;}
00041   inline T* atBoundsCheck(unsigned int pos) const { return (pos<length) ? block[pos] : NULL; }
00043   inline T* at(unsigned int pos) const { return block[pos]; }
00045   inline T* firstBoundsCheck() const {return atBoundsCheck(0);}
00047   inline T* first() const {return at(0);}
00049   inline T* lastBoundsCheck() const {return atBoundsCheck(length-1);}
00051   inline T* last() const {return at(length-1);}
00053   virtual unsigned int append(T* elem);
00055   virtual void swap(unsigned int p1,unsigned int p2);
00058   virtual void clear();
00060   virtual void deleteAt(unsigned int pos);
00062   virtual void deleteLast();
00064   virtual int remove(T* elem);
00066   virtual int lookup(T* elem) const;
00068   virtual int removeUnSwapped(T* elem);
00071   virtual void destroy();
00073   virtual void replace(T* elem,unsigned int pos);
00075   void reserve(unsigned int init);
00076 
00077 private:
00078 
00080   unsigned int length;
00082   unsigned int capacity;
00083   unsigned int BLOCKSIZE;
00085   T* *block;
00086 
00087 protected:
00088 
00090   virtual void expand();
00093   virtual unsigned int appendUnsafe(T* elem);
00094 };
00095 
00098 
00099 template <class T>
00100 void deleteVector(CSOObjectVector<T> *vector,bool deleteEntries=true)
00101 {
00102   if (!vector) 
00103   { 
00104     return; 
00105   }
00106   if (deleteEntries) 
00107   { 
00108     vector->destroy(); 
00109   }
00110   ML_DELETE(vector);
00111 }
00112 
00115 
00116 template <class T>
00117 CSOObjectVector<T>::CSOObjectVector(unsigned int init,unsigned int bs)
00118 {
00119   BLOCKSIZE = bs;
00120   length    = 0;
00121   capacity  = init;
00122   block     = NULL;
00123   if (capacity) 
00124   {
00125     ML_CHECK_NEW(block, T*[capacity])      
00126   }
00127 }
00128 
00130 
00131 template <class T>
00132 CSOObjectVector<T>::~CSOObjectVector()
00133 {
00134   clear();
00135 }
00136 
00138 
00139 template <class T>
00140 void CSOObjectVector<T>::clear()
00141 {
00142   if (capacity > 0) 
00143   {
00144     ML_DELETE_ARRAY(block);
00145   }
00146   length = capacity = 0;
00147 }
00148 
00150 
00151 template <class T>
00152 void CSOObjectVector<T>::destroy()
00153 {
00154   for (unsigned int i = 0; i < length; ++i) 
00155   {
00156     ML_DELETE(block[i]);            
00157   }
00158   length = 0;
00159 }
00160 
00162 
00163 template <class T>
00164 void CSOObjectVector<T>::expand()
00165 {
00166   T* *newblock = NULL;
00167   ML_CHECK_NEW(newblock, T*[capacity+BLOCKSIZE]);
00168 
00169   for (unsigned int i = 0; i < length; ++i) 
00170   {
00171     newblock[i] = block[i]; 
00172   }
00173   ML_DELETE_ARRAY(block); 
00174 
00175   block = newblock;
00176   capacity += BLOCKSIZE;
00177 }
00178 
00180 
00181 template <class T>
00182 unsigned int CSOObjectVector<T>::append(T* elem)
00183 {
00184   if (elem)
00185   {
00186     if (length == capacity) 
00187     { 
00188       expand(); 
00189     }
00190     block[length] = elem;
00191     length++;
00192   }
00193   return length-1;
00194 }
00195 
00197 
00198 template <class T>
00199 unsigned int CSOObjectVector<T>::appendUnsafe(T* elem)
00200 {
00201   if (length == capacity) 
00202   { 
00203     expand(); 
00204   }
00205   block[length] = elem;
00206   length++;
00207   return length-1;
00208 }
00209 
00211 
00212 template <class T>
00213 void CSOObjectVector<T>::replace(T* elem,unsigned int pos)
00214 {
00215   block[pos] = elem;
00216 }
00217 
00219 
00220 template <class T>
00221 void CSOObjectVector<T>::swap(unsigned int p1,unsigned int p2)
00222 {
00223   T* temp;
00224   if (p1 != p2) 
00225   {
00226     T*& pb1 = block[p1]; //use a temp reference to avoid redundant dereferencing
00227     T*& pb2 = block[p2];
00228     temp = pb1;
00229     pb1 = pb2;
00230     pb2 = temp;
00231   }
00232 }
00233 
00235 
00236 template <class T>
00237 void CSOObjectVector<T>::deleteAt(unsigned int pos)
00238 {
00239   if (pos != length-1) 
00240   {  
00241     swap(pos,length-1); 
00242   }
00243   deleteLast();
00244 }
00245 
00247 
00248 template <class T>
00249 void CSOObjectVector<T>::deleteLast()
00250 {
00251   if (length > 0)
00252   { 
00253     length--; 
00254     block[length] = NULL;
00255   }
00256 }
00257 
00259 
00260 template <class T>
00261 int CSOObjectVector<T>::lookup(T* elem) const
00262 {
00263   int pos = -1;
00264   for (unsigned int i = 0; i < length; ++i) 
00265   {
00266     if (at(i) == elem) 
00267     {
00268       pos = i;
00269       break;
00270     }
00271   }
00272   return pos;
00273 }
00274 
00276 
00277 template <class T>
00278 int CSOObjectVector<T>::remove(T* elem)
00279 {
00280   int pos = lookup(elem);
00281   if (pos != -1)
00282   { 
00283     deleteAt(pos); 
00284   }
00285   return pos;
00286 }
00287 
00289 
00290 template <class T>
00291 int CSOObjectVector<T>::removeUnSwapped(T* elem)
00292 {
00293   const int pos = lookup(elem);
00294   if (pos != -1) 
00295   {
00296     for (unsigned int i = pos; i < length-1; ++i) 
00297     {
00298       block[i] = block[i+1];
00299     }
00300     deleteLast();
00301   }
00302   return pos;
00303 }
00304 
00306 
00307 template <class T>
00308 void CSOObjectVector<T>::reserve(unsigned int init)
00309 {
00310   if (init <= capacity) 
00311   { 
00312     return; 
00313   }
00314   T* *newblock = NULL;
00315   ML_CHECK_NEW(newblock, T*[init]);  
00316   for (unsigned int i = 0; i < length; ++i) 
00317   { 
00318     newblock[i] = block[i]; 
00319   }
00320   if(capacity > 0)
00321   { 
00322     ML_DELETE_ARRAY(block); 
00323   }
00324   block = newblock;
00325   capacity = init;
00326 }
00327 
00329 
00330 
00331 ML_END_NAMESPACE
00332 
00333 #endif // __CSOObjectVector_H