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   ML_TRACE_IN("template <class T>void deleteVector(CSOObjectVector<T> *vector,bool deleteEntries=true)");
00103 
00104   if (vector==NULL)  { return; }
00105   if (deleteEntries) { vector->destroy(); }
00106   ML_DELETE(vector);
00107 }
00108 
00111 
00112 template <class T>
00113 CSOObjectVector<T>::CSOObjectVector(unsigned int init,unsigned int bs)
00114 {
00115   ML_TRACE_IN("template <class T>CSOObjectVector<T>::CSOObjectVector(unsigned int init,unsigned int bs)");
00116 
00117   BLOCKSIZE = bs;
00118   length    = 0;
00119   capacity  = init;
00120   block     = NULL;
00121   if (capacity) {
00122     ML_CHECK_NEW(block, T*[capacity])      
00123   }
00124 }
00125 
00127 
00128 template <class T>
00129 CSOObjectVector<T>::~CSOObjectVector()
00130 {
00131   ML_TRACE_IN_TIME_CRITICAL("template <class T>CSOObjectVector<T>::~CSOObjectVector()");
00132 
00133   clear();
00134 }
00135 
00137 
00138 template <class T>
00139 void CSOObjectVector<T>::clear()
00140 {
00141   ML_TRACE_IN("template <class T>void CSOObjectVector<T>::clear()");
00142 
00143   if (capacity > 0) {
00144     ML_DELETE_ARRAY(block);
00145   }
00146   length = capacity = 0;
00147 }
00148 
00150 
00151 template <class T>
00152 void CSOObjectVector<T>::destroy()
00153 {
00154   ML_TRACE_IN("template <class T>void CSOObjectVector<T>::destroy()");
00155 
00156   for (unsigned int i = 0; i < length; ++i) {
00157     ML_DELETE(block[i]);            
00158   }
00159   length = 0;
00160 }
00161 
00163 
00164 template <class T>
00165 void CSOObjectVector<T>::expand()
00166 {
00167   ML_TRACE_IN("template <class T>void CSOObjectVector<T>::expand()");
00168 
00169   T* *newblock = NULL;
00170   ML_CHECK_NEW(newblock, T*[capacity+BLOCKSIZE]);
00171 
00172   for (unsigned int i = 0; i < length; ++i) { newblock[i] = block[i]; }
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   ML_TRACE_IN_TIME_CRITICAL("template <class T>unsigned int CSOObjectVector<T>::append(T* elem)");
00185 
00186   if (elem != NULL) {
00187     if (length == capacity) { expand(); }
00188     block[length] = elem;
00189     length++;
00190   }
00191   return length-1;
00192 }
00193 
00195 
00196 template <class T>
00197 unsigned int CSOObjectVector<T>::appendUnsafe(T* elem)
00198 {
00199   ML_TRACE_IN_TIME_CRITICAL("template <class T>unsigned int CSOObjectVector<T>::appendUnsafe(T* elem)");
00200 
00201   if (length == capacity) { expand(); }
00202   block[length] = elem;
00203   length++;
00204   return length-1;
00205 }
00206 
00208 
00209 template <class T>
00210 void CSOObjectVector<T>::replace(T* elem,unsigned int pos)
00211 {
00212   ML_TRACE_IN_TIME_CRITICAL("template <class T>void CSOObjectVector<T>::replace(T* elem,unsigned int pos)");
00213 
00214   block[pos] = elem;
00215 }
00216 
00218 
00219 template <class T>
00220 void CSOObjectVector<T>::swap(unsigned int p1,unsigned int p2)
00221 {
00222   ML_TRACE_IN_TIME_CRITICAL("template <class T>void CSOObjectVector<T>::swap(unsigned int p1,unsigned int p2)");
00223 
00224   T* temp;
00225   if (p1 != p2) {
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   ML_TRACE_IN_TIME_CRITICAL("template <class T>void CSOObjectVector<T>::deleteAt(unsigned int pos)");
00240 
00241   if (pos != length-1) {  swap(pos,length-1); }
00242   deleteLast();
00243 }
00244 
00246 
00247 template <class T>
00248 void CSOObjectVector<T>::deleteLast()
00249 {
00250   ML_TRACE_IN_TIME_CRITICAL("template <class T>void CSOObjectVector<T>::deleteLast()");
00251 
00252   if (length>0) { 
00253     length--; 
00254     block[length] = NULL;
00255   }
00256 }
00257 
00259 
00260 template <class T>
00261 int CSOObjectVector<T>::lookup(T* elem) const
00262 {
00263   ML_TRACE_IN_TIME_CRITICAL("template <class T>int CSOObjectVector<T>::lookup(T* elem) const");
00264 
00265   int pos = -1;
00266   for (unsigned int i = 0; i < length; ++i) {
00267     if (at(i) == elem) {
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   ML_TRACE_IN_TIME_CRITICAL("template <class T>int CSOObjectVector<T>::remove(T* elem)");
00281 
00282   int pos = lookup(elem);
00283   if (pos != -1) { deleteAt(pos); }
00284   return pos;
00285 }
00286 
00288 
00289 template <class T>
00290 int CSOObjectVector<T>::removeUnSwapped(T* elem)
00291 {
00292   ML_TRACE_IN_TIME_CRITICAL("template <class T>int CSOObjectVector<T>::removeUnSwapped(T* elem)");
00293 
00294   const int pos = lookup(elem);
00295   if (pos != -1) {
00296     for (unsigned int i = pos; i < length-1; ++i) {
00297       block[i] = block[i+1];
00298     }
00299     deleteLast();
00300   }
00301   return pos;
00302 }
00303 
00305 
00306 template <class T>
00307 void CSOObjectVector<T>::reserve(unsigned int init)
00308 {
00309   ML_TRACE_IN("template <class T>void CSOObjectVector<T>::reserve(unsigned int init)");
00310 
00311   if (init <= capacity) { return; }
00312   T* *newblock = NULL;
00313   ML_CHECK_NEW(newblock, T*[init]);  
00314   for (unsigned int i = 0; i < length; ++i) { newblock[i] = block[i]; }
00315   if(capacity > 0) { ML_DELETE_ARRAY(block); }
00316   block = newblock;
00317   capacity = init;
00318 }
00319 
00321 
00322 
00323 ML_END_NAMESPACE
00324 
00325 #endif // __CSOObjectVector_H