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