00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _SIMP_VECTOR_H_
00035 #define _SIMP_VECTOR_H_
00036
00037 #include <cmath>
00038 #include <cstdlib>
00039 #include <cassert>
00040 #include <vector>
00041 #include <typeinfo>
00042 #include <string>
00043 #include <map>
00044 #include <list>
00045 #include <algorithm>
00046
00047 #ifdef ENABLE_MEMORY_TRACKING
00048
00049 typedef std::pair<long int*, long int*> ppair;
00050 typedef std::map<std::string, ppair > VectorList;
00051
00052 typedef std::list<int> lst;
00053 typedef std::map<std::string, lst*> incr;
00054
00055 extern VectorList* vectorList_;
00056 extern incr* vectorIncr_;
00057 #endif
00058
00059 using std::vector;
00060 using std::ostream;
00061
00063
00072 template <class T>
00073
00074 class Vector
00075 {
00076 public:
00078
00082 inline
00083 Vector()
00084 {
00085 }
00086
00088
00091 inline
00092 virtual ~Vector()
00093 {
00094 #ifdef ENABLE_MEMORY_TRACKING
00095 if(size()>0) decrement(size());
00096 #endif
00097 }
00098
00100
00103 inline
00104 Vector(const Vector<T>& invec):v(invec.v)
00105 {
00106 #ifdef ENABLE_MEMORY_TRACKING
00107 increment(invec.size());
00108 #endif
00109 }
00110
00112
00114 inline
00115 Vector(const std::vector<T>& invec):v(invec)
00116 {
00117 #ifdef ENABLE_MEMORY_TRACKING
00118 increment(invec.size());
00119 #endif
00120 }
00121
00123
00125 inline
00126 Vector<T>& operator=(const std::vector<T>& invec)
00127 {
00128 #ifdef ENABLE_MEMORY_TRACKING
00129 decrement(size());
00130 #endif
00131 v=invec;
00132 #ifdef ENABLE_MEMORY_TRACKING
00133 increment(size());
00134 #endif
00135 return *this;
00136 }
00137
00139
00141 inline
00142 Vector<T>& operator=(const Vector<T>& invec)
00143 {
00144 #ifdef ENABLE_MEMORY_TRACKING
00145 decrement(size());
00146 #endif
00147 v=invec.v;
00148 #ifdef ENABLE_MEMORY_TRACKING
00149 increment(size());
00150 #endif
00151 return *this;
00152 }
00153
00155
00158 inline
00159 Vector<T>& assign(const T& inval)
00160 {
00161 v.assign( size(),inval );
00162 return *this;
00163 }
00164
00166
00176 inline
00177 Vector( unsigned int isize)
00178 {
00179 resize(isize);
00180 }
00181
00182
00183
00185
00187 inline
00188 void clear()
00189 {
00190 #ifdef ENABLE_MEMORY_TRACKING
00191 decrement(size());
00192 #endif
00193 v.clear();
00194 }
00195
00197
00199 inline
00200 size_t size() const
00201 {
00202 return v.size();
00203 }
00204
00206
00216 inline
00217 Vector( unsigned int isize, const T& value) : v(isize, value)
00218 {
00219 #ifdef ENABLE_MEMORY_TRACKING
00220 increment(isize);
00221 #endif
00222 }
00223
00225
00238 inline
00239 T& operator[] ( unsigned int n)
00240 {
00241 assert(n < size());
00242
00243
00244
00245 return v[n];
00246 }
00247
00249
00260 inline
00261 const T& operator[] ( unsigned int n) const
00262 {
00263 assert(n < size());
00264
00265
00266
00267 return v[n];
00268 }
00269
00271
00273 inline
00274 void push_back(const T& in)
00275 {
00276 #ifdef ENABLE_MEMORY_TRACKING
00277 increment(1);
00278 #endif
00279
00280 v.push_back(in);
00281 }
00282
00284
00297 inline
00298 void append(const Vector<T>& invec)
00299 {
00300 #ifdef ENABLE_MEMORY_TRACKING
00301 increment(invec.size());
00302 #endif
00303 for (int ivec = 0; ivec < invec.size(); ivec++)
00304 {
00305
00306 v.push_back(invec[ivec]);
00307 }
00308 }
00309
00311
00313 inline
00314 void resize( unsigned int isize)
00315 {
00316
00317 #ifdef ENABLE_MEMORY_TRACKING
00318 if(isize > size()) increment(isize-size());
00319 else decrement(size()-isize);
00320 #endif
00321 unsigned int l= size();
00322
00323 v.resize(isize);
00324 for(; l<isize; ++l)
00325
00326 v[l] = T();
00327 }
00328
00329 inline
00330 void reserve( size_t isize)
00331 {
00332 v.reserve(isize);
00333 }
00334
00335 inline
00336 size_t capacity() const
00337 {
00338 return v.capacity();
00339 }
00341
00343 inline
00344 void resize( unsigned int isize, const T& value)
00345 {
00346
00347 #ifdef ENABLE_MEMORY_TRACKING
00348 if(isize > size()) increment(isize-size());
00349 else decrement(size()-isize);
00350 #endif
00351
00352 v.resize(isize, value);
00353 }
00354
00356
00358 inline void sort()
00359 {
00360 std::sort(v.begin(), v.end());
00361 }
00362
00363 #ifdef ENABLE_MEMORY_TRACKING
00364
00365 static long int bytes;
00366 static long int peak;
00367
00368 #endif
00369
00370 private:
00371
00372 #ifdef ENABLE_MEMORY_TRACKING
00373 inline
00374 void increment(unsigned int i)
00375 {
00376
00377
00378 static unsigned int sizzle = initFunc();
00379 i*=sizzle;
00380 bytes += i;
00381
00382 if(bytes > peak) peak = bytes;
00383 }
00384
00385 inline
00386 void decrement(unsigned int i)
00387 {
00388 if(i==0) return;
00389
00390 i*=sizeof(T);
00391 bytes-=i;
00392
00393
00394 }
00395 #endif
00396
00397 unsigned int initFunc();
00398 std::vector<T> v;
00399 };
00400
00401 #ifdef ENABLE_MEMORY_TRACKING
00402 template <class T>
00403 long int Vector<T>::bytes=0;
00404
00405 template <class T>
00406 long int Vector<T>::peak=0;
00407
00408
00409
00410 #endif
00411
00412
00413
00414 template <class T>
00415 ostream& operator<<(ostream& os, const Vector<T>& vec)
00416 {
00417 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00418 return os;
00419 }
00420
00421 #ifdef ENABLE_MEMORY_TRACKING
00422
00423 template <class T>
00424 unsigned int Vector<T>::initFunc()
00425 {
00426 if(vectorList_ == NULL)
00427 {
00428 vectorList_ = new VectorList;
00429 vectorIncr_ = new incr;
00430 }
00431
00432 ppair tmp(&bytes, &peak);
00433 vectorList_->operator[](typeid(T).name()) = tmp;
00434
00435
00436
00437 return sizeof(T);
00438 }
00439
00440 #endif
00441 #endif
00442