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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef _VECTOR_H_
00053 #define _VECTOR_H_
00054
00055 #include <cmath>
00056 #include <cstdlib>
00057 #include <cassert>
00058 #include <vector>
00059 #include <typeinfo>
00060 #include <string>
00061 #include <map>
00062 #include <list>
00063 #include <algorithm>
00064 #include <iostream>
00065
00066 #ifdef ENABLE_MEMORY_TRACKING
00067
00068 typedef std::pair<long int*, long int*> ppair;
00069 typedef std::map<std::string, ppair > VectorList;
00070
00071 typedef std::list<int> lst;
00072 typedef std::map<std::string, lst*> incr;
00073
00074 extern VectorList* vectorList_;
00075 extern incr* vectorIncr_;
00076 #endif
00077
00078 using std::vector;
00079 using std::ostream;
00080
00082
00089 template <class T>
00090
00091 class Vector
00092 {
00093 public:
00095
00099 inline
00100 Vector()
00101 {
00102 }
00103
00105
00108 inline
00109 virtual ~Vector()
00110 {
00111 #ifdef ENABLE_MEMORY_TRACKING
00112 if(size()>0) decrement(size());
00113 #endif
00114 }
00115
00117
00120 inline
00121 Vector(const Vector<T>& invec):v(invec.v)
00122 {
00123 #ifdef ENABLE_MEMORY_TRACKING
00124 increment(invec.size());
00125 #endif
00126 }
00127
00129
00131 inline
00132 Vector(const std::vector<T>& invec):v(invec)
00133 {
00134 #ifdef ENABLE_MEMORY_TRACKING
00135 increment(invec.size());
00136 #endif
00137 }
00138
00140
00142 inline
00143 Vector<T>& operator=(const std::vector<T>& invec)
00144 {
00145 #ifdef ENABLE_MEMORY_TRACKING
00146 decrement(size());
00147 #endif
00148 v=invec;
00149 #ifdef ENABLE_MEMORY_TRACKING
00150 increment(size());
00151 #endif
00152 return *this;
00153 }
00154
00156
00158 inline
00159 Vector<T>& operator=(const Vector<T>& invec)
00160 {
00161 #ifdef ENABLE_MEMORY_TRACKING
00162 decrement(size());
00163 #endif
00164 v=invec.v;
00165 #ifdef ENABLE_MEMORY_TRACKING
00166 increment(size());
00167 #endif
00168 return *this;
00169 }
00170
00172
00175 inline
00176 Vector<T>& assign(const T& inval)
00177 {
00178 v.assign( size(),inval );
00179 return *this;
00180 }
00181
00183
00193 inline
00194 Vector( unsigned int isize)
00195 {
00196 resize(isize);
00197 }
00198
00200
00202 inline
00203 void clear()
00204 {
00205 #ifdef ENABLE_MEMORY_TRACKING
00206 decrement(size());
00207 #endif
00208 v.clear();
00209 }
00210
00212
00214 inline
00215 size_t size() const
00216 {
00217 return v.size();
00218 }
00219
00221
00231 inline
00232 Vector( unsigned int isize, const T& value) : v(isize, value)
00233 {
00234 #ifdef ENABLE_MEMORY_TRACKING
00235 increment(isize);
00236 #endif
00237 }
00238
00240
00253 inline
00254 T& operator[] ( unsigned int n)
00255 {
00256 assert(n < size());
00257
00258
00259
00260 return v[n];
00261 }
00262
00264
00275 inline
00276 const T& operator[] ( unsigned int n) const
00277 {
00278 assert(n < size());
00279
00280
00281
00282 return v[n];
00283 }
00284
00285 inline
00286 const T& back() const
00287 {
00288 return v.back();
00289 }
00290
00291 inline
00292 void swap(Vector<T>& other)
00293 {
00294 v.swap(other.v);
00295 }
00297
00299 inline
00300 void push_back(const T& in)
00301 {
00302 #ifdef ENABLE_MEMORY_TRACKING
00303 increment(1);
00304 #endif
00305
00306 v.push_back(in);
00307 }
00308
00310
00323 inline
00324 void append(const Vector<T>& invec)
00325 {
00326 #ifdef ENABLE_MEMORY_TRACKING
00327 increment(invec.size());
00328 #endif
00329 for (int ivec = 0; ivec < invec.size(); ivec++)
00330 {
00331
00332 v.push_back(invec[ivec]);
00333 }
00334 }
00335
00337
00339 inline
00340 void resize( unsigned int isize)
00341 {
00342
00343 #ifdef ENABLE_MEMORY_TRACKING
00344 if(isize > size()) increment(isize-size());
00345 else decrement(size()-isize);
00346 #endif
00347 unsigned int l= size();
00348
00349 v.resize(isize);
00350 for(; l<isize; ++l)
00351
00352 v[l] = T();
00353 }
00354
00355 inline
00356 void reserve( size_t isize)
00357 {
00358 v.reserve(isize);
00359 }
00360
00361 inline
00362 size_t capacity() const
00363 {
00364 return v.capacity();
00365 }
00367
00369 inline
00370 void resize( unsigned int isize, const T& value)
00371 {
00372
00373 #ifdef ENABLE_MEMORY_TRACKING
00374 if(isize > size()) increment(isize-size());
00375 else decrement(size()-isize);
00376 #endif
00377
00378 v.resize(isize, value);
00379 }
00380
00382
00384 inline void sort()
00385 {
00386 std::sort(v.begin(), v.end());
00387 }
00388
00389 #ifdef ENABLE_MEMORY_TRACKING
00390
00391 static long int bytes;
00392 static long int peak;
00393
00394 #endif
00395
00396 private:
00397
00398 #ifdef ENABLE_MEMORY_TRACKING
00399 inline
00400 void increment(unsigned int i)
00401 {
00402
00403
00404 static unsigned int sizzle = initFunc();
00405 i*=sizzle;
00406 bytes += i;
00407
00408 if(bytes > peak) peak = bytes;
00409 }
00410
00411 inline
00412 void decrement(unsigned int i)
00413 {
00414 if(i==0) return;
00415
00416 i*=sizeof(T);
00417 bytes-=i;
00418
00419
00420 }
00421 #endif
00422
00423 unsigned int initFunc();
00424 std::vector<T> v;
00425 };
00426
00427 #ifdef ENABLE_MEMORY_TRACKING
00428 template <class T>
00429 long int Vector<T>::bytes=0;
00430
00431 template <class T>
00432 long int Vector<T>::peak=0;
00433
00434
00435
00436 #endif
00437
00438
00439
00440 template <class T>
00441 ostream& operator<<(ostream& os, const Vector<T>& vec)
00442 {
00443 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00444 return os;
00445 }
00446
00447 #ifdef ENABLE_MEMORY_TRACKING
00448
00449 template <class T>
00450 unsigned int Vector<T>::initFunc()
00451 {
00452 if(vectorList_ == NULL)
00453 {
00454 vectorList_ = new VectorList;
00455 vectorIncr_ = new incr;
00456 }
00457
00458 ppair tmp(&bytes, &peak);
00459 vectorList_->operator[](typeid(T).name()) = tmp;
00460
00461
00462
00463 return sizeof(T);
00464 }
00465
00466 #endif
00467
00468 #endif