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
00315
00317 inline
00318 void resize( unsigned int isize, const T& value)
00319 {
00320
00321 #ifdef ENABLE_MEMORY_TRACKING
00322 if(isize > size()) increment(isize-size());
00323 else decrement(size()-isize);
00324 #endif
00325
00326 v.resize(isize, value);
00327 }
00328
00330
00332 inline void sort()
00333 {
00334 std::sort(v.begin(), v.end());
00335 }
00336
00337 #ifdef ENABLE_MEMORY_TRACKING
00338
00339 static long int bytes;
00340 static long int peak;
00341
00342 #endif
00343
00344 private:
00345
00346 #ifdef ENABLE_MEMORY_TRACKING
00347 inline
00348 void increment(unsigned int i)
00349 {
00350
00351
00352 static unsigned int sizzle = initFunc();
00353 i*=sizzle;
00354 bytes += i;
00355
00356 if(bytes > peak) peak = bytes;
00357 }
00358
00359 inline
00360 void decrement(unsigned int i)
00361 {
00362 if(i==0) return;
00363
00364 i*=sizeof(T);
00365 bytes-=i;
00366
00367
00368 }
00369 #endif
00370
00371 unsigned int initFunc();
00372 std::vector<T> v;
00373 };
00374
00375 #ifdef ENABLE_MEMORY_TRACKING
00376 template <class T>
00377 long int Vector<T>::bytes=0;
00378
00379 template <class T>
00380 long int Vector<T>::peak=0;
00381
00382
00383
00384 #endif
00385
00386
00387
00388 template <class T>
00389 ostream& operator<<(ostream& os, const Vector<T>& vec)
00390 {
00391 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00392 return os;
00393 }
00394
00395 template <class T>
00396 unsigned int Vector<T>::initFunc()
00397 {
00398 if(vectorList_ == NULL)
00399 {
00400 vectorList_ = new VectorList;
00401 vectorIncr_ = new incr;
00402 }
00403
00404 ppair tmp(&bytes, &peak);
00405 vectorList_->operator[](typeid(T).name()) = tmp;
00406
00407
00408
00409 return sizeof(T);
00410 }
00411
00412 #endif
00413