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
00048 typedef std::pair<long int*, long int*> ppair;
00049 typedef std::map<std::string, ppair > VectorList;
00050
00051 typedef std::list<int> lst;
00052 typedef std::map<std::string, lst*> incr;
00053
00054 extern VectorList* vectorList_;
00055 extern incr* vectorIncr_;
00056
00057 using std::vector;
00058 using std::ostream;
00059
00061
00070 template <class T>
00071
00072 class Vector
00073 {
00074 public:
00076
00080 inline
00081 Vector()
00082 {
00083 }
00084
00086
00089 inline
00090 virtual ~Vector()
00091 {
00092 #ifdef ENABLE_MEMORY_TRACKING
00093 if(size()>0) decrement(size());
00094 #endif
00095 }
00096
00098
00101 inline
00102 Vector(const Vector<T>& invec):v(invec.v)
00103 {
00104 #ifdef ENABLE_MEMORY_TRACKING
00105 increment(invec.size());
00106 #endif
00107 }
00108
00110
00112 inline
00113 Vector(const std::vector<T>& invec):v(invec)
00114 {
00115 #ifdef ENABLE_MEMORY_TRACKING
00116 increment(invec.size());
00117 #endif
00118 }
00119
00121
00123 inline
00124 Vector<T>& operator=(const std::vector<T>& invec)
00125 {
00126 #ifdef ENABLE_MEMORY_TRACKING
00127 decrement(size());
00128 #endif
00129 v=invec;
00130 #ifdef ENABLE_MEMORY_TRACKING
00131 increment(size());
00132 #endif
00133 return *this;
00134 }
00135
00137
00139 inline
00140 Vector<T>& operator=(const Vector<T>& invec)
00141 {
00142 #ifdef ENABLE_MEMORY_TRACKING
00143 decrement(size());
00144 #endif
00145 v=invec.v;
00146 #ifdef ENABLE_MEMORY_TRACKING
00147 increment(size());
00148 #endif
00149 return *this;
00150 }
00151
00153
00163 inline
00164 Vector( unsigned int isize)
00165 {
00166 resize(isize);
00167 }
00168
00170
00172 inline
00173 void clear()
00174 {
00175 #ifdef ENABLE_MEMORY_TRACKING
00176 decrement(size());
00177 #endif
00178 v.clear();
00179 }
00180
00182
00184 inline
00185 size_t size() const
00186 {
00187 return v.size();
00188 }
00189
00191
00201 inline
00202 Vector( unsigned int isize, const T& value) : v(isize, value)
00203 {
00204 #ifdef ENABLE_MEMORY_TRACKING
00205 increment(isize);
00206 #endif
00207 }
00208
00210
00223 inline
00224 T& operator[] ( unsigned int n)
00225 {
00226 assert(n < size());
00227
00228
00229
00230 return v[n];
00231 }
00232
00234
00245 inline
00246 const T& operator[] ( unsigned int n) const
00247 {
00248 assert(n < size());
00249
00250
00251
00252 return v[n];
00253 }
00254
00256
00258 inline
00259 void push_back(const T& in)
00260 {
00261 #ifdef ENABLE_MEMORY_TRACKING
00262 increment(1);
00263 #endif
00264
00265 v.push_back(in);
00266 }
00267
00269
00282 inline
00283 void append(const Vector<T>& invec)
00284 {
00285 #ifdef ENABLE_MEMORY_TRACKING
00286 increment(invec.size());
00287 #endif
00288 for (int ivec = 0; ivec < invec.size(); ivec++)
00289 {
00290
00291 v.push_back(invec[ivec]);
00292 }
00293 }
00294
00296
00298 inline
00299 void resize( unsigned int isize)
00300 {
00301
00302 #ifdef ENABLE_MEMORY_TRACKING
00303 if(isize > size()) increment(isize-size());
00304 else decrement(size()-isize);
00305 #endif
00306 unsigned int l= size();
00307
00308 v.resize(isize);
00309 for(; l<isize; ++l)
00310
00311 v[l] = T();
00312 }
00313
00314 inline
00315 void reserve( size_t isize)
00316 {
00317 v.reserve(isize);
00318 }
00319
00320 inline
00321 size_t capacity() const
00322 {
00323 return v.capacity();
00324 }
00326
00328 inline
00329 void resize( unsigned int isize, const T& value)
00330 {
00331
00332 #ifdef ENABLE_MEMORY_TRACKING
00333 if(isize > size()) increment(isize-size());
00334 else decrement(size()-isize);
00335 #endif
00336
00337 v.resize(isize, value);
00338 }
00339
00341
00343 inline void sort()
00344 {
00345 std::sort(v.begin(), v.end());
00346 }
00347
00348 #ifdef ENABLE_MEMORY_TRACKING
00349
00350 static long int bytes;
00351 static long int peak;
00352
00353 #endif
00354
00355 private:
00356
00357 #ifdef ENABLE_MEMORY_TRACKING
00358 inline
00359 void increment(unsigned int i)
00360 {
00361
00362
00363 static unsigned int sizzle = initFunc();
00364 i*=sizzle;
00365 bytes += i;
00366
00367 if(bytes > peak) peak = bytes;
00368 }
00369
00370 inline
00371 void decrement(unsigned int i)
00372 {
00373 if(i==0) return;
00374
00375 i*=sizeof(T);
00376 bytes-=i;
00377
00378
00379 }
00380 #endif
00381
00382 unsigned int initFunc();
00383 std::vector<T> v;
00384 };
00385
00386 #ifdef ENABLE_MEMORY_TRACKING
00387 template <class T>
00388 long int Vector<T>::bytes=0;
00389
00390 template <class T>
00391 long int Vector<T>::peak=0;
00392
00393
00394
00395 #endif
00396
00397
00398
00399 template <class T>
00400 ostream& operator<<(ostream& os, const Vector<T>& vec)
00401 {
00402 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00403 return os;
00404 }
00405
00406 template <class T>
00407 unsigned int Vector<T>::initFunc()
00408 {
00409 if(vectorList_ == NULL)
00410 {
00411 vectorList_ = new VectorList;
00412 vectorIncr_ = new incr;
00413 }
00414
00415 ppair tmp(&bytes, &peak);
00416 vectorList_->operator[](typeid(T).name()) = tmp;
00417
00418
00419
00420 return sizeof(T);
00421 }
00422
00423 #endif
00424