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 "SPACE.H"
00058 #include <vector>
00059 #include <typeinfo>
00060 #include <string>
00061 #include <map>
00062 #include <list>
00063 #include <algorithm>
00064 #include <iostream>
00065
00066 #ifdef CH_USE_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 CH_USE_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 CH_USE_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 CH_USE_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 CH_USE_MEMORY_TRACKING
00146 decrement(size());
00147 #endif
00148 v=invec;
00149 #ifdef CH_USE_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 CH_USE_MEMORY_TRACKING
00162 decrement(size());
00163 #endif
00164 v=invec.v;
00165 #ifdef CH_USE_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 CH_USE_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 CH_USE_MEMORY_TRACKING
00235 increment(isize);
00236 #endif
00237 }
00238
00240
00253 inline
00254 T& operator[] ( unsigned int n)
00255 {
00256 CH_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 CH_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 CH_USE_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 CH_USE_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 CH_USE_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 CH_USE_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
00390 inline std::vector<T>& stdVector()
00391 {
00392 return v;
00393 }
00394
00395 #ifdef CH_USE_MEMORY_TRACKING
00396
00397 static long int bytes;
00398 static long int peak;
00399
00400 #endif
00401
00402 private:
00403
00404 #ifdef CH_USE_MEMORY_TRACKING
00405 inline
00406 void increment(unsigned int i)
00407 {
00408
00409
00410 static unsigned int sizzle = initFunc();
00411 i*=sizzle;
00412 bytes += i;
00413
00414 if(bytes > peak) peak = bytes;
00415 }
00416
00417 inline
00418 void decrement(unsigned int i)
00419 {
00420 if(i==0) return;
00421
00422 i*=sizeof(T);
00423 bytes-=i;
00424
00425
00426 }
00427 #endif
00428
00429 unsigned int initFunc();
00430 std::vector<T> v;
00431 };
00432
00433 #ifdef CH_USE_MEMORY_TRACKING
00434 template <class T>
00435 long int Vector<T>::bytes=0;
00436
00437 template <class T>
00438 long int Vector<T>::peak=0;
00439
00440
00441
00442 #endif
00443
00444
00445
00446 template <class T>
00447 ostream& operator<<(ostream& os, const Vector<T>& vec)
00448 {
00449 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00450 return os;
00451 }
00452
00453 #ifdef CH_USE_MEMORY_TRACKING
00454
00455 template <class T>
00456 unsigned int Vector<T>::initFunc()
00457 {
00458 if(vectorList_ == NULL)
00459 {
00460 vectorList_ = new VectorList;
00461 vectorIncr_ = new incr;
00462 }
00463
00464 ppair tmp(&bytes, &peak);
00465 vectorList_->operator[](typeid(T).name()) = tmp;
00466
00467
00468
00469 return sizeof(T);
00470 }
00471
00472 #endif
00473
00474 #endif