MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLWEM/WEMDataStructure/WEMVector.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00007 
00014 #ifndef __WEMVector_H
00015 #define __WEMVector_H
00016 
00017 #include "MLWEMIncludes.h"
00018 
00019 
00020 ML_START_NAMESPACE
00021 
00023 
00028 template <class T>
00029 class WEMVector 
00030 {
00031 public:
00032 
00034   WEMVector(unsigned int init = 0,unsigned int bs = 8192);
00036   virtual ~WEMVector();
00038   inline unsigned int num() const {return _length;}
00040   inline T* at(unsigned int pos) const { return (pos<_length) ? _block[pos] : NULL; }
00042   inline T* first() {return at(0);}
00044   inline const T* first() const {return first();}
00046   inline T* last() {return at(_length-1);}
00048   inline const T* last() const {return last();}
00050   virtual unsigned int append(T* elem);
00052   virtual void swap(unsigned int p1,unsigned int p2);
00055   virtual void clear();
00057   virtual void deleteAt(unsigned int pos);
00059   virtual void deleteLast();
00061   virtual int remove(T* elem);
00063   virtual int lookup(T* elem) const;
00065   virtual int removeUnSwapped(T* elem);
00068   virtual void destroy();
00070   virtual void replace(T* elem,unsigned int pos);
00072   void reserve(unsigned int init);
00073 
00074 private:
00075 
00077   unsigned int _length;
00079   unsigned int _capacity;
00081   unsigned int _BLOCKSIZE;
00083   T* *_block;
00084 
00085 protected:
00086 
00088   virtual void expand();
00091   virtual unsigned int appendUnsafe(T* elem);
00092 };
00093 
00095 
00096 template <class T>
00097 WEMVector<T>::WEMVector(unsigned int init,unsigned int bs)
00098 {
00099   _BLOCKSIZE = bs;
00100   _length    = 0;
00101   _capacity  = init;
00102   _block     = NULL;
00103   if (_capacity > 0)
00104   {
00105     ML_CHECK_NEW(_block, T*[_capacity])      
00106   }
00107   for (unsigned int i=0; i < _capacity; i++) 
00108   {
00109     _block[i] = NULL;
00110   }
00111 }
00112 
00114 
00115 template <class T>
00116 WEMVector<T>::~WEMVector()
00117 {
00118   clear();
00119 }
00120 
00122 
00123 template <class T>
00124 void WEMVector<T>::clear()
00125 {
00126   if (_capacity > 0) 
00127   {
00128     ML_DELETE_ARRAY(_block);
00129   }
00130   _length = _capacity = 0;
00131 }
00132 
00134 
00135 template <class T>
00136 void WEMVector<T>::destroy()
00137 {
00138   for (unsigned int i = 0; i < _length; i++) 
00139   {
00140     ML_DELETE(_block[i]);            
00141   }
00142   _length = 0;
00143 }
00144 
00146 
00147 template <class T>
00148 void WEMVector<T>::expand()
00149 {
00150   T* *newblock = NULL;
00151   ML_CHECK_NEW(newblock, T*[_capacity+_BLOCKSIZE]);
00152 
00153   for (unsigned int i = 0; i < _length; i++) 
00154   { 
00155     newblock[i] = _block[i]; 
00156   }
00157   ML_DELETE_ARRAY(_block); 
00158 
00159   _block = newblock;
00160   _capacity += _BLOCKSIZE;
00161 }
00162 
00164 
00165 template <class T>
00166 unsigned int WEMVector<T>::append(T* elem)
00167 {
00168   if (elem) 
00169   {
00170     if (_length == _capacity) 
00171     { 
00172       expand(); 
00173     }
00174     _block[_length] = elem;
00175     _length++;
00176   }
00177   return _length-1;
00178 }
00179 
00181 
00182 template <class T>
00183 unsigned int WEMVector<T>::appendUnsafe(T* elem)
00184 {
00185   if (_length == _capacity) 
00186   { 
00187     expand(); 
00188   }
00189   _block[_length] = elem;
00190   _length++;
00191   return _length-1;
00192 }
00193 
00195 
00196 template <class T>
00197 void WEMVector<T>::replace(T* elem,unsigned int pos)
00198 {
00199   _block[pos] = elem;
00200 }
00201 
00203 
00204 template <class T>
00205 void WEMVector<T>::swap(unsigned int p1,unsigned int p2)
00206 {
00207   T* temp = NULL;
00208   if (p1 != p2) 
00209   {
00210     T*& pb1 = _block[p1];
00211     T*& pb2 = _block[p2];
00212     temp = pb1;
00213     pb1  = pb2;
00214     pb2  = temp;
00215   }
00216 }
00217 
00219 
00220 template <class T>
00221 void WEMVector<T>::deleteAt(unsigned int pos)
00222 {
00223   if (pos != _length-1)
00224   {  
00225     swap(pos,_length-1); 
00226   }
00227   deleteLast();
00228 }
00229 
00231 
00232 template <class T>
00233 void WEMVector<T>::deleteLast()
00234 {
00235   if (_length > 0)
00236   {
00237     _length--;
00238   }
00239 }
00240 
00242 
00243 template <class T>
00244 int WEMVector<T>::lookup(T* elem) const
00245 {
00246   int pos = -1;
00247   for (unsigned int i = 0; i < _length; i++) 
00248   {
00249     if (at(i) == elem) 
00250     {
00251       pos = i;
00252       break;
00253     }
00254   }
00255   return pos;
00256 }
00257 
00259 
00260 template <class T>
00261 int WEMVector<T>::remove(T* elem)
00262 {
00263   const int pos = lookup(elem);
00264   if (pos != -1)
00265   { 
00266     deleteAt(pos); 
00267   }
00268   return pos;
00269 }
00270 
00272 
00273 template <class T>
00274 int WEMVector<T>::removeUnSwapped(T* elem)
00275 {
00276   const int pos = lookup(elem);
00277   if (pos != -1) 
00278   {
00279     for (unsigned int i = pos; i < _length-1; i++) 
00280     {
00281       _block[i] = _block[i+1];
00282     }
00283     deleteLast();
00284   }
00285   return pos;
00286 }
00287 
00289 
00290 template <class T>
00291 void WEMVector<T>::reserve(unsigned int init)
00292 {
00293   if (init <= _capacity) { return; }
00294 
00295   T* *newblock = NULL;
00296   ML_CHECK_NEW(newblock, T*[init]);  
00297   for (unsigned int i = 0; i < _length; i++) 
00298   { 
00299     newblock[i] = _block[i]; 
00300   }
00301   if(_capacity > 0) 
00302   { 
00303     ML_DELETE_ARRAY(_block); 
00304   }
00305   _block = newblock;
00306   _capacity = init;
00307 }
00308 
00311 
00312 ML_END_NAMESPACE
00313 
00314 #endif // __WEMVector_H