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 #include <iostream>
00047
00048 #ifdef ENABLE_MEMORY_TRACKING
00049
00050 typedef std::pair<long int*, long int*> ppair;
00051 typedef std::map<std::string, ppair > VectorList;
00052
00053 typedef std::list<int> lst;
00054 typedef std::map<std::string, lst*> incr;
00055
00056 extern VectorList* vectorList_;
00057 extern incr* vectorIncr_;
00058 #endif
00059
00060 using std::vector;
00061 using std::ostream;
00062
00064
00073 template <class T>
00074
00075 class Vector
00076 {
00077 public:
00079
00083 inline
00084 Vector()
00085 {
00086 }
00087
00089
00092 inline
00093 virtual ~Vector()
00094 {
00095 #ifdef ENABLE_MEMORY_TRACKING
00096 if(size()>0) decrement(size());
00097 #endif
00098 }
00099
00101
00104 inline
00105 Vector(const Vector<T>& invec):v(invec.v)
00106 {
00107 #ifdef ENABLE_MEMORY_TRACKING
00108 increment(invec.size());
00109 #endif
00110 }
00111
00113
00115 inline
00116 Vector(const std::vector<T>& invec):v(invec)
00117 {
00118 #ifdef ENABLE_MEMORY_TRACKING
00119 increment(invec.size());
00120 #endif
00121 }
00122
00124
00126 inline
00127 Vector<T>& operator=(const std::vector<T>& invec)
00128 {
00129 #ifdef ENABLE_MEMORY_TRACKING
00130 decrement(size());
00131 #endif
00132 v=invec;
00133 #ifdef ENABLE_MEMORY_TRACKING
00134 increment(size());
00135 #endif
00136 return *this;
00137 }
00138
00140
00142 inline
00143 Vector<T>& operator=(const Vector<T>& invec)
00144 {
00145 #ifdef ENABLE_MEMORY_TRACKING
00146 decrement(size());
00147 #endif
00148 v=invec.v;
00149 #ifdef ENABLE_MEMORY_TRACKING
00150 increment(size());
00151 #endif
00152 return *this;
00153 }
00154
00156
00159 inline
00160 Vector<T>& assign(const T& inval)
00161 {
00162 v.assign( size(),inval );
00163 return *this;
00164 }
00165
00167
00177 inline
00178 Vector( unsigned int isize)
00179 {
00180 resize(isize);
00181 }
00182
00183
00184
00186
00188 inline
00189 void clear()
00190 {
00191 #ifdef ENABLE_MEMORY_TRACKING
00192 decrement(size());
00193 #endif
00194 v.clear();
00195 }
00196
00198
00200 inline
00201 size_t size() const
00202 {
00203 return v.size();
00204 }
00205
00207
00217 inline
00218 Vector( unsigned int isize, const T& value) : v(isize, value)
00219 {
00220 #ifdef ENABLE_MEMORY_TRACKING
00221 increment(isize);
00222 #endif
00223 }
00224
00226
00239 inline
00240 T& operator[] ( unsigned int n)
00241 {
00242 assert(n < size());
00243
00244
00245
00246 return v[n];
00247 }
00248
00250
00261 inline
00262 const T& operator[] ( unsigned int n) const
00263 {
00264 assert(n < size());
00265
00266
00267
00268 return v[n];
00269 }
00270
00271 inline
00272 void swap(Vector<T>& other)
00273 {
00274 v.swap(other.v);
00275 }
00277
00279 inline
00280 void push_back(const T& in)
00281 {
00282 #ifdef ENABLE_MEMORY_TRACKING
00283 increment(1);
00284 #endif
00285
00286 v.push_back(in);
00287 }
00288
00290
00303 inline
00304 void append(const Vector<T>& invec)
00305 {
00306 #ifdef ENABLE_MEMORY_TRACKING
00307 increment(invec.size());
00308 #endif
00309 for (int ivec = 0; ivec < invec.size(); ivec++)
00310 {
00311
00312 v.push_back(invec[ivec]);
00313 }
00314 }
00315
00317
00319 inline
00320 void resize( unsigned int isize)
00321 {
00322
00323 #ifdef ENABLE_MEMORY_TRACKING
00324 if(isize > size()) increment(isize-size());
00325 else decrement(size()-isize);
00326 #endif
00327 unsigned int l= size();
00328
00329 v.resize(isize);
00330 for(; l<isize; ++l)
00331
00332 v[l] = T();
00333 }
00334
00335 inline
00336 void reserve( size_t isize)
00337 {
00338 v.reserve(isize);
00339 }
00340
00341 inline
00342 size_t capacity() const
00343 {
00344 return v.capacity();
00345 }
00347
00349 inline
00350 void resize( unsigned int isize, const T& value)
00351 {
00352
00353 #ifdef ENABLE_MEMORY_TRACKING
00354 if(isize > size()) increment(isize-size());
00355 else decrement(size()-isize);
00356 #endif
00357
00358 v.resize(isize, value);
00359 }
00360
00362
00364 inline void sort()
00365 {
00366 std::sort(v.begin(), v.end());
00367 }
00368
00369 #ifdef ENABLE_MEMORY_TRACKING
00370
00371 static long int bytes;
00372 static long int peak;
00373
00374 #endif
00375
00376 private:
00377
00378 #ifdef ENABLE_MEMORY_TRACKING
00379 inline
00380 void increment(unsigned int i)
00381 {
00382
00383
00384 static unsigned int sizzle = initFunc();
00385 i*=sizzle;
00386 bytes += i;
00387
00388 if(bytes > peak) peak = bytes;
00389 }
00390
00391 inline
00392 void decrement(unsigned int i)
00393 {
00394 if(i==0) return;
00395
00396 i*=sizeof(T);
00397 bytes-=i;
00398
00399
00400 }
00401 #endif
00402
00403 unsigned int initFunc();
00404 std::vector<T> v;
00405 };
00406
00407 #ifdef ENABLE_MEMORY_TRACKING
00408 template <class T>
00409 long int Vector<T>::bytes=0;
00410
00411 template <class T>
00412 long int Vector<T>::peak=0;
00413
00414
00415
00416 #endif
00417
00418
00419
00420 template <class T>
00421 ostream& operator<<(ostream& os, const Vector<T>& vec)
00422 {
00423 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00424 return os;
00425 }
00426
00427 #ifdef ENABLE_MEMORY_TRACKING
00428
00429 template <class T>
00430 unsigned int Vector<T>::initFunc()
00431 {
00432 if(vectorList_ == NULL)
00433 {
00434 vectorList_ = new VectorList;
00435 vectorIncr_ = new incr;
00436 }
00437
00438 ppair tmp(&bytes, &peak);
00439 vectorList_->operator[](typeid(T).name()) = tmp;
00440
00441
00442
00443 return sizeof(T);
00444 }
00445
00446 #endif
00447 #endif
00448