MeVisLabToolboxReference
|
00001 // **InsertLicense** code 00002 //---------------------------------------------------------------------------------- 00007 00015 #ifndef __WEMFastVector_H 00016 #define __WEMFastVector_H 00017 00018 #include "MLWEMIncludes.h" 00019 00020 ML_START_NAMESPACE 00021 00023 00028 template <class T> 00029 class WEMFastVector 00030 { 00031 public: 00032 00034 WEMFastVector(unsigned int init); 00036 virtual ~WEMFastVector(); 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 at(0);} 00046 inline T* last() {return at(_length - 1);} 00048 inline const T* last() const {return at(_length - 1);} 00050 virtual unsigned int append(T* elem); 00052 virtual void swap(unsigned int p1, unsigned int p2); 00055 virtual void clear(); 00057 virtual void destroy(); 00059 virtual void deleteAt(unsigned int pos); 00061 virtual void deleteLast(); 00063 virtual int remove(T* elem); 00065 virtual int lookup(T* elem) const; 00068 virtual int removeUnSwapped(T* elem); 00070 virtual void replace(T* elem, unsigned int pos); 00071 00072 private: 00073 00075 unsigned int _length; 00077 unsigned int _capacity; 00079 T* *_block; 00080 }; 00081 00084 00085 template <class T> 00086 WEMFastVector<T>::WEMFastVector(unsigned int init) 00087 { 00088 _length = 0; 00089 _capacity = init; 00090 ML_CHECK_NEW(_block, T*[init]); 00091 00092 for (unsigned int i = 0; i < init; i ++) 00093 { 00094 _block[i] = NULL; 00095 } 00096 } 00097 00099 00100 template <class T> 00101 WEMFastVector<T>::~WEMFastVector() 00102 { 00103 destroy(); 00104 ML_DELETE_ARRAY(_block); 00105 } 00106 00108 00109 template <class T> 00110 void WEMFastVector<T>::clear() 00111 { 00112 _length = 0; 00113 } 00114 00116 00117 template <class T> 00118 void WEMFastVector<T>::destroy() 00119 { 00120 // _capacity is used here to delete all blocks, also the hidden (deleted) ones 00121 for (unsigned int i = 0; i < _capacity; i ++) 00122 { 00123 ML_DELETE(_block[i]); 00124 } 00125 _length = 0; 00126 } 00127 00129 00130 template <class T> 00131 unsigned int WEMFastVector<T>::append(T* elem) 00132 { 00133 if (elem) 00134 { 00135 if (_length < _capacity) 00136 { 00137 _block[_length] = elem; 00138 _length ++; 00139 } 00140 } 00141 return _length - 1; 00142 } 00143 00145 00146 template <class T> 00147 void WEMFastVector<T>::replace(T* elem, unsigned int pos) 00148 { 00149 _block[pos] = elem; 00150 } 00151 00153 00154 template <class T> 00155 void WEMFastVector<T>::swap(unsigned int p1, unsigned int p2) 00156 { 00157 T* temp; 00158 if (p1 != p2) 00159 { 00160 T*& pb1 = _block[p1]; 00161 T*& pb2 = _block[p2]; 00162 temp = pb1; 00163 pb1 = pb2; 00164 pb2 = temp; 00165 } 00166 } 00167 00169 00170 template <class T> 00171 void WEMFastVector<T>::deleteAt(unsigned int pos) 00172 { 00173 if (pos >= _length) { return; } 00174 00175 if (pos != _length - 1) 00176 { 00177 swap(pos, _length - 1); 00178 } 00179 deleteLast(); 00180 } 00181 00183 00184 template <class T> 00185 void WEMFastVector<T>::deleteLast() 00186 { 00187 if (_length > 0) { _length --; } 00188 } 00189 00191 00192 template <class T> 00193 int WEMFastVector<T>::lookup(T* elem) const 00194 { 00195 int pos = -1; 00196 for (unsigned int i = 0; i < _length; i ++) 00197 { 00198 if (at(i) == elem) 00199 { 00200 pos = i; 00201 break; 00202 } 00203 } 00204 return pos; 00205 } 00206 00208 00209 template <class T> 00210 int WEMFastVector<T>::remove(T* elem) 00211 { 00212 int pos = lookup(elem); 00213 if (pos != -1) 00214 { 00215 deleteAt(pos); 00216 } 00217 return pos; 00218 } 00219 00221 00222 template <class T> 00223 int WEMFastVector<T>::removeUnSwapped(T* elem) 00224 { 00225 int pos = lookup(elem); 00226 if (pos != -1) 00227 { 00228 for (unsigned int i = pos; i < _length - 1; i ++) 00229 { 00230 _block[i] = _block[i + 1]; 00231 } 00232 deleteLast(); 00233 } 00234 return pos; 00235 } 00236 00238 00239 ML_END_NAMESPACE 00240 00241 #endif // __WEMFastVector_H