MeVisLabToolboxReference
|
00001 // **InsertLicense** code 00002 //---------------------------------------------------------------------------------- 00004 00011 #ifndef __WEMIndexVector_H 00012 #define __WEMIndexVector_H 00013 00014 #include "WEMObjectVector.h" 00015 00016 00017 ML_START_NAMESPACE 00018 00020 00022 template <class T> 00023 class WEMIndexVector 00024 { 00025 public: 00026 00028 WEMIndexVector(unsigned int init = 0,unsigned int bs = 8192); 00030 virtual ~WEMIndexVector(); 00032 inline unsigned int num() const {return length;} 00034 inline T* at(unsigned int pos) const { return (pos<length) ? pointers[pos] : NULL; } 00036 inline T* first() { return at(0); } 00038 inline const T* first() const { return first(); } 00040 T* append(); 00043 int remove(T *t); 00045 virtual void destroy(); 00046 00047 private: 00048 00050 void swap(unsigned int i,unsigned int j); 00052 virtual void expand(); 00054 unsigned int length; 00056 unsigned int capacity; 00058 unsigned int BLOCKSIZE; 00060 T* *pointers; 00062 int *positions; 00064 WEMObjectVector<T> *objects; 00065 }; 00066 00068 00069 template <class T> 00070 WEMIndexVector<T>::WEMIndexVector(unsigned int init,unsigned int bs) 00071 { 00072 BLOCKSIZE = bs; 00073 length = 0; 00074 capacity = init; 00075 pointers = NULL; 00076 positions = NULL; 00077 objects = NULL; 00078 00079 objects = new WEMObjectVector<T>(capacity,BLOCKSIZE); 00080 if (capacity > 0) 00081 { 00082 ML_CHECK_NEW(pointers, T*[capacity]); 00083 ML_CHECK_NEW(positions, int[capacity]); 00084 } 00085 for (unsigned int i = 0; i < capacity; i++) 00086 { 00087 pointers[i] = NULL; 00088 positions[i] = -1; 00089 } 00090 } 00091 00093 00094 template <class T> 00095 WEMIndexVector<T>::~WEMIndexVector() 00096 { 00097 destroy(); 00098 ML_DELETE(objects); 00099 } 00100 00102 00103 template <class T> 00104 void WEMIndexVector<T>::destroy() 00105 { 00106 ML_DELETE_ARRAY(pointers); 00107 ML_DELETE_ARRAY(positions); 00108 00109 length = capacity = 0; 00110 objects->destroy(); 00111 } 00112 00114 00115 template <class T> 00116 void WEMIndexVector<T>::swap(unsigned int i,unsigned int j) 00117 { 00118 T* tTemp = NULL; 00119 int pTemp; 00120 if (i != j) 00121 { 00122 T*& pi = pointers[i]; 00123 T*& pj = pointers[j]; 00124 tTemp = pi; 00125 pi = pj; 00126 pj = tTemp; 00127 00128 pTemp = positions[i]; 00129 positions[i] = positions[j]; 00130 positions[j] = pTemp; 00131 00132 pointers[i]->setEntryNumber(i); 00133 pointers[j]->setEntryNumber(j); 00134 } 00135 } 00136 00138 00139 template <class T> 00140 void WEMIndexVector<T>::expand() 00141 { 00142 T* *newpointers = NULL; 00143 int *newpositions = NULL; 00144 ML_CHECK_NEW(newpointers, T*[capacity+BLOCKSIZE]); 00145 ML_CHECK_NEW(newpositions, int[capacity+BLOCKSIZE]); 00146 00147 for (unsigned int i=0;i<length;i++) 00148 { 00149 newpointers[i] = pointers[i]; 00150 newpositions[i] = positions[i]; 00151 } 00152 00153 ML_DELETE_ARRAY(pointers); 00154 ML_DELETE_ARRAY(positions); 00155 00156 pointers = newpointers; 00157 positions = newpositions; 00158 00159 capacity += BLOCKSIZE; 00160 } 00161 00163 00164 template <class T> 00165 T* WEMIndexVector<T>::append() 00166 { 00167 unsigned int pos; 00168 T *t = objects->append(&pos); 00169 if (t) 00170 { 00171 if (length == capacity) 00172 { 00173 expand(); 00174 } 00175 pointers[length] = t; 00176 positions[length] = pos; 00177 t->setEntryNumber(length); 00178 length++; 00179 } 00180 return t; 00181 } 00182 00184 00185 template <class T> 00186 int WEMIndexVector<T>::remove(T *t) 00187 { 00188 if (t == NULL) { return -1; } 00189 const int i = t->getEntryNumber(); 00190 const int n = num(); 00191 if (i >= n) { return -1; } 00192 if (i <= -1) { return -1; } 00193 00194 objects->deleteAt(positions[i]); 00195 00196 if (i != n - 1) 00197 { 00198 swap(i,n - 1); 00199 } 00200 at(n - 1)->setEntryNumber(-1); 00201 00202 if (length) length --; 00203 00204 return i; 00205 } 00206 00208 00209 ML_END_NAMESPACE 00210 00211 #endif