MeVisLabToolboxReference
|
00001 // **InsertLicense** code 00002 //---------------------------------------------------------------------------------- 00005 00012 #ifndef __WEMContainer_H 00013 #define __WEMContainer_H 00014 00015 #include "WEMBase/WEMPrimitive.h" 00016 #include "WEMVector.h" 00017 00018 00019 ML_START_NAMESPACE 00020 00022 00025 template <class T> 00026 class WEMContainer : public WEMVector<WEMPrimitive> 00027 { 00028 public: 00029 00031 WEMContainer(unsigned int init = 0,unsigned int bs = 8192); 00033 ~WEMContainer(); 00035 T* at(unsigned int pos); 00037 const T* at(unsigned int pos) const; 00039 void swap(unsigned int i,unsigned int j); 00042 unsigned int append(WEMPrimitive *wp); 00045 int remove(WEMPrimitive *wp); 00047 void replace(WEMPrimitive *wp,unsigned int pos); 00049 virtual void destroy(); 00050 }; 00051 00053 00054 template <class T> 00055 WEMContainer<T>::WEMContainer(unsigned int init,unsigned int bs) : 00056 WEMVector<WEMPrimitive>(init,bs) 00057 { 00058 } 00059 00061 00062 template <class T> 00063 WEMContainer<T>::~WEMContainer() 00064 { 00065 } 00066 00068 00069 template <class T> 00070 T* WEMContainer<T>::at(unsigned int pos) 00071 { 00072 WEMPrimitive *wp = WEMVector<WEMPrimitive>::at(pos); 00073 if (wp) 00074 { 00075 return reinterpret_cast<T*>(wp); 00076 } 00077 else 00078 { 00079 return NULL; 00080 } 00081 } 00082 00084 00085 template <class T> 00086 const T* WEMContainer<T>::at(unsigned int pos) const 00087 { 00088 return at(pos); 00089 } 00090 00092 00093 template <class T> 00094 void WEMContainer<T>::destroy() 00095 { 00096 // This is the only safe way to delete entries in this vector!! 00097 // 00098 // Everything else (no overloading, parent call, direct block access) 00099 // goes to WEMVector and causes 'double free or corruption' 00100 00101 const unsigned int n = num(); 00102 00103 for (unsigned int i=0;i<n;i++) 00104 { 00105 if (at(i)) 00106 { 00107 T* toDel = at(i); 00108 ML_DELETE(toDel); 00109 } 00110 WEMVector<WEMPrimitive>::replace(NULL,i); 00111 } 00112 clear(); 00113 } 00114 00116 00117 template <class T> 00118 void WEMContainer<T>::swap(unsigned int i, unsigned int j) 00119 { 00120 WEMVector<WEMPrimitive>::swap(i,j); 00121 at(i)->setHeapPosition(i); 00122 at(j)->setHeapPosition(j); 00123 } 00124 00126 00127 template <class T> 00128 unsigned int WEMContainer<T>::append(WEMPrimitive *wp) 00129 { 00130 if (wp == NULL) { return (num()-1); } 00131 if (wp->getHeapPosition() != -1) { return (num()-1); } 00132 00133 const int vecLength = WEMVector<WEMPrimitive>::appendUnsafe(wp); 00134 last()->setHeapPosition(num()-1); 00135 return vecLength; 00136 } 00137 00139 00140 template <class T> 00141 void WEMContainer<T>::replace(WEMPrimitive *wp, unsigned int pos) 00142 { 00143 WEMVector<WEMPrimitive>::replace(wp,pos); 00144 if (wp) 00145 { 00146 wp->setHeapPosition(pos); 00147 } 00148 } 00149 00151 00152 template <class T> 00153 int WEMContainer<T>::remove(WEMPrimitive *wp) 00154 { 00155 if (wp == NULL) { return -1; } 00156 const int i = wp->getHeapPosition(); 00157 const int n = num(); 00158 if (i >= n) { return -1; } 00159 if (i <= -1) { return -1; } 00160 00161 if (i != n - 1) 00162 { 00163 swap(i,n - 1); 00164 } 00165 at(n - 1)->setHeapPosition(-1); 00166 deleteLast(); 00167 00168 return i; 00169 } 00170 00172 00173 ML_END_NAMESPACE 00174 00175 #endif //__WEMContainer_H