00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #ifndef NDB_VECTOR_HPP 00018 #define NDB_VECTOR_HPP 00019 00020 #include <ndb_global.h> 00021 #include <NdbMutex.h> 00022 00023 template<class T> 00024 struct Vector { 00025 public: 00026 Vector(int sz = 10); 00027 ~Vector(); 00028 00029 T& operator[](unsigned i); 00030 const T& operator[](unsigned i) const; 00031 unsigned size() const { return m_size; }; 00032 00033 void push_back(const T &); 00034 void push(const T&, unsigned pos); 00035 T& set(T&, unsigned pos, T& fill_obj); 00036 T& back(); 00037 00038 void erase(unsigned index); 00039 00040 void clear(); 00041 00042 void fill(unsigned new_size, T & obj); 00043 00044 Vector<T>& operator=(const Vector<T>&); 00045 00046 T* getBase() { return m_items;} 00047 const T* getBase() const { return m_items;} 00048 private: 00049 T * m_items; 00050 unsigned m_size; 00051 unsigned m_incSize; 00052 unsigned m_arraySize; 00053 }; 00054 00055 template<class T> 00056 Vector<T>::Vector(int i){ 00057 m_items = new T[i]; 00058 m_size = 0; 00059 m_arraySize = i; 00060 m_incSize = 50; 00061 } 00062 00063 template<class T> 00064 Vector<T>::~Vector(){ 00065 delete[] m_items; 00066 // safety for placement new usage 00067 m_items = 0; 00068 m_size = 0; 00069 m_arraySize = 0; 00070 } 00071 00072 template<class T> 00073 T & 00074 Vector<T>::operator[](unsigned i){ 00075 if(i >= m_size) 00076 abort(); 00077 return m_items[i]; 00078 } 00079 00080 template<class T> 00081 const T & 00082 Vector<T>::operator[](unsigned i) const { 00083 if(i >= m_size) 00084 abort(); 00085 return m_items[i]; 00086 } 00087 00088 template<class T> 00089 T & 00090 Vector<T>::back(){ 00091 return (* this)[m_size - 1]; 00092 } 00093 00094 template<class T> 00095 void 00096 Vector<T>::push_back(const T & t){ 00097 if(m_size == m_arraySize){ 00098 T * tmp = new T [m_arraySize + m_incSize]; 00099 for (unsigned k = 0; k < m_size; k++) 00100 tmp[k] = m_items[k]; 00101 delete[] m_items; 00102 m_items = tmp; 00103 m_arraySize = m_arraySize + m_incSize; 00104 } 00105 m_items[m_size] = t; 00106 m_size++; 00107 } 00108 00109 template<class T> 00110 void 00111 Vector<T>::push(const T & t, unsigned pos) 00112 { 00113 push_back(t); 00114 if (pos < m_size - 1) 00115 { 00116 for(unsigned i = m_size - 1; i > pos; i--) 00117 { 00118 m_items[i] = m_items[i-1]; 00119 } 00120 m_items[pos] = t; 00121 } 00122 } 00123 00124 template<class T> 00125 T& 00126 Vector<T>::set(T & t, unsigned pos, T& fill_obj) 00127 { 00128 fill(pos, fill_obj); 00129 T& ret = m_items[pos]; 00130 m_items[pos] = t; 00131 return ret; 00132 } 00133 00134 template<class T> 00135 void 00136 Vector<T>::erase(unsigned i){ 00137 if(i >= m_size) 00138 abort(); 00139 00140 for (unsigned k = i; k + 1 < m_size; k++) 00141 m_items[k] = m_items[k + 1]; 00142 m_size--; 00143 } 00144 00145 template<class T> 00146 void 00147 Vector<T>::clear(){ 00148 m_size = 0; 00149 } 00150 00151 template<class T> 00152 void 00153 Vector<T>::fill(unsigned new_size, T & obj){ 00154 while(m_size <= new_size) 00155 push_back(obj); 00156 } 00157 00158 template<class T> 00159 Vector<T>& 00160 Vector<T>::operator=(const Vector<T>& obj){ 00161 if(this != &obj){ 00162 clear(); 00163 for(size_t i = 0; i<obj.size(); i++){ 00164 push_back(obj[i]); 00165 } 00166 } 00167 return * this; 00168 } 00169 00170 template<class T> 00171 struct MutexVector : public NdbLockable { 00172 MutexVector(int sz = 10); 00173 ~MutexVector(); 00174 00175 T& operator[](unsigned i); 00176 const T& operator[](unsigned i) const; 00177 unsigned size() const { return m_size; }; 00178 00179 void push_back(const T &); 00180 void push_back(const T &, bool lockMutex); 00181 T& back(); 00182 00183 void erase(unsigned index); 00184 void erase(unsigned index, bool lockMutex); 00185 00186 void clear(); 00187 void clear(bool lockMutex); 00188 00189 void fill(unsigned new_size, T & obj); 00190 private: 00191 T * m_items; 00192 unsigned m_size; 00193 unsigned m_incSize; 00194 unsigned m_arraySize; 00195 }; 00196 00197 template<class T> 00198 MutexVector<T>::MutexVector(int i){ 00199 m_items = new T[i]; 00200 m_size = 0; 00201 m_arraySize = i; 00202 m_incSize = 50; 00203 } 00204 00205 template<class T> 00206 MutexVector<T>::~MutexVector(){ 00207 delete[] m_items; 00208 // safety for placement new usage 00209 m_items = 0; 00210 m_size = 0; 00211 m_arraySize = 0; 00212 } 00213 00214 template<class T> 00215 T & 00216 MutexVector<T>::operator[](unsigned i){ 00217 if(i >= m_size) 00218 abort(); 00219 return m_items[i]; 00220 } 00221 00222 template<class T> 00223 const T & 00224 MutexVector<T>::operator[](unsigned i) const { 00225 if(i >= m_size) 00226 abort(); 00227 return m_items[i]; 00228 } 00229 00230 template<class T> 00231 T & 00232 MutexVector<T>::back(){ 00233 return (* this)[m_size - 1]; 00234 } 00235 00236 template<class T> 00237 void 00238 MutexVector<T>::push_back(const T & t){ 00239 lock(); 00240 if(m_size == m_arraySize){ 00241 T * tmp = new T [m_arraySize + m_incSize]; 00242 for (unsigned k = 0; k < m_size; k++) 00243 tmp[k] = m_items[k]; 00244 delete[] m_items; 00245 m_items = tmp; 00246 m_arraySize = m_arraySize + m_incSize; 00247 } 00248 m_items[m_size] = t; 00249 m_size++; 00250 unlock(); 00251 } 00252 00253 template<class T> 00254 void 00255 MutexVector<T>::push_back(const T & t, bool lockMutex){ 00256 if(lockMutex) 00257 lock(); 00258 if(m_size == m_arraySize){ 00259 T * tmp = new T [m_arraySize + m_incSize]; 00260 for (unsigned k = 0; k < m_size; k++) 00261 tmp[k] = m_items[k]; 00262 delete[] m_items; 00263 m_items = tmp; 00264 m_arraySize = m_arraySize + m_incSize; 00265 } 00266 m_items[m_size] = t; 00267 m_size++; 00268 if(lockMutex) 00269 unlock(); 00270 } 00271 00272 template<class T> 00273 void 00274 MutexVector<T>::erase(unsigned i){ 00275 if(i >= m_size) 00276 abort(); 00277 00278 lock(); 00279 for (unsigned k = i; k + 1 < m_size; k++) 00280 m_items[k] = m_items[k + 1]; 00281 m_size--; 00282 unlock(); 00283 } 00284 00285 template<class T> 00286 void 00287 MutexVector<T>::erase(unsigned i, bool _lock){ 00288 if(i >= m_size) 00289 abort(); 00290 00291 if(_lock) 00292 lock(); 00293 for (unsigned k = i; k + 1 < m_size; k++) 00294 m_items[k] = m_items[k + 1]; 00295 m_size--; 00296 if(_lock) 00297 unlock(); 00298 } 00299 00300 template<class T> 00301 void 00302 MutexVector<T>::clear(){ 00303 lock(); 00304 m_size = 0; 00305 unlock(); 00306 } 00307 00308 template<class T> 00309 void 00310 MutexVector<T>::clear(bool l){ 00311 if(l) lock(); 00312 m_size = 0; 00313 if(l) unlock(); 00314 } 00315 00316 template<class T> 00317 void 00318 MutexVector<T>::fill(unsigned new_size, T & obj){ 00319 while(m_size <= new_size) 00320 push_back(obj); 00321 } 00322 00323 #endif
1.4.7

