00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _VECTOR_H_
00012 #define _VECTOR_H_
00013
00014 #include <cmath>
00015 #include <cstdlib>
00016 #include "CH_assert.H"
00017 #include <vector>
00018 #include <typeinfo>
00019 #include <string>
00020 #include <map>
00021 #include <list>
00022 #include <algorithm>
00023 #include <iostream>
00024
00025 #include "BaseNamespaceHeader.H"
00026
00027 #ifdef CH_USE_MEMORY_TRACKING
00028
00029 typedef std::pair<long int*, long int*> ppair;
00030 typedef std::map<std::string, ppair > VectorList;
00031
00032 typedef std::list<int> lst;
00033 typedef std::map<std::string, lst*> incr;
00034
00035 extern VectorList* vectorList_;
00036 extern incr* vectorIncr_;
00037 #endif
00038
00039 using std::vector;
00040 using std::ostream;
00041
00043
00050 template <class T>
00051
00052 class Vector
00053 {
00054 public:
00056
00060 inline
00061 Vector()
00062 {
00063 }
00064
00066
00069 inline
00070 virtual ~Vector()
00071 {
00072 #ifdef CH_USE_MEMORY_TRACKING
00073 if(size()>0) decrement(size());
00074 #endif
00075 }
00076
00078
00081 inline
00082 Vector(const Vector<T>& invec):v(invec.v)
00083 {
00084 #ifdef CH_USE_MEMORY_TRACKING
00085 increment(invec.size());
00086 #endif
00087 }
00088
00090
00092 inline
00093 Vector(const std::vector<T>& invec):v(invec)
00094 {
00095 #ifdef CH_USE_MEMORY_TRACKING
00096 increment(invec.size());
00097 #endif
00098 }
00099
00101
00103 inline
00104 Vector<T>& operator=(const std::vector<T>& invec)
00105 {
00106 #ifdef CH_USE_MEMORY_TRACKING
00107 decrement(size());
00108 #endif
00109 v=invec;
00110 #ifdef CH_USE_MEMORY_TRACKING
00111 increment(size());
00112 #endif
00113 return *this;
00114 }
00115
00117
00119 inline
00120 Vector<T>& operator=(const Vector<T>& invec)
00121 {
00122 #ifdef CH_USE_MEMORY_TRACKING
00123 decrement(size());
00124 #endif
00125 v=invec.v;
00126 #ifdef CH_USE_MEMORY_TRACKING
00127 increment(size());
00128 #endif
00129 return *this;
00130 }
00131
00133
00136 inline
00137 Vector<T>& assign(const T& inval)
00138 {
00139 v.assign( size(),inval );
00140 return *this;
00141 }
00142
00144
00154 inline
00155 Vector( unsigned int isize)
00156 {
00157 resize(isize);
00158 }
00159
00161
00163 inline
00164 void clear()
00165 {
00166 #ifdef CH_USE_MEMORY_TRACKING
00167 decrement(size());
00168 #endif
00169 v.clear();
00170 }
00171
00173
00175 inline
00176 size_t size() const
00177 {
00178 return v.size();
00179 }
00180
00182
00192 inline
00193 Vector( unsigned int isize, const T& value) : v(isize, value)
00194 {
00195 #ifdef CH_USE_MEMORY_TRACKING
00196 increment(isize);
00197 #endif
00198 }
00199
00201
00214 inline
00215 T& operator[] ( unsigned int n)
00216 {
00217 CH_assert(n < size());
00218
00219
00220
00221 return v[n];
00222 }
00223
00225
00236 inline
00237 const T& operator[] ( unsigned int n) const
00238 {
00239 CH_assert(n < size());
00240
00241
00242
00243 return v[n];
00244 }
00245
00246 inline
00247 const T& back() const
00248 {
00249 return v.back();
00250 }
00251
00252 inline
00253 void swap(Vector<T>& other)
00254 {
00255 v.swap(other.v);
00256 }
00258
00260 inline
00261 void push_back(const T& in)
00262 {
00263 #ifdef CH_USE_MEMORY_TRACKING
00264 increment(1);
00265 #endif
00266
00267 v.push_back(in);
00268 }
00269
00271
00284 inline
00285 void append(const Vector<T>& invec)
00286 {
00287 #ifdef CH_USE_MEMORY_TRACKING
00288 increment(invec.size());
00289 #endif
00290 for (int ivec = 0; ivec < invec.size(); ivec++)
00291 {
00292
00293 v.push_back(invec[ivec]);
00294 }
00295 }
00296
00298
00300 inline
00301 void resize( unsigned int isize)
00302 {
00303
00304 #ifdef CH_USE_MEMORY_TRACKING
00305 if(isize > size()) increment(isize-size());
00306 else decrement(size()-isize);
00307 #endif
00308 unsigned int l= size();
00309
00310 v.resize(isize);
00311 for(; l<isize; ++l)
00312
00313 v[l] = T();
00314 }
00315
00316 inline
00317 void reserve( size_t isize)
00318 {
00319 v.reserve(isize);
00320 }
00321
00322 inline
00323 size_t capacity() const
00324 {
00325 return v.capacity();
00326 }
00328
00330 inline
00331 void resize( unsigned int isize, const T& value)
00332 {
00333
00334 #ifdef CH_USE_MEMORY_TRACKING
00335 if(isize > size()) increment(isize-size());
00336 else decrement(size()-isize);
00337 #endif
00338
00339 v.resize(isize, value);
00340 }
00341
00343
00345 inline void sort()
00346 {
00347 std::sort(v.begin(), v.end());
00348 }
00349
00351 inline std::vector<T>& stdVector()
00352 {
00353 return v;
00354 }
00355
00356 inline const std::vector<T>& constStdVector() const
00357 {
00358 return v;
00359 }
00360
00361 #ifdef CH_USE_MEMORY_TRACKING
00362
00363 static long int bytes;
00364 static long int peak;
00365
00366 #endif
00367
00368 private:
00369
00370 #ifdef CH_USE_MEMORY_TRACKING
00371 inline
00372 void increment(unsigned int i)
00373 {
00374
00375
00376 static unsigned int sizzle = initFunc();
00377 i*=sizzle;
00378 bytes += i;
00379
00380 if(bytes > peak) peak = bytes;
00381 }
00382
00383 inline
00384 void decrement(unsigned int i)
00385 {
00386 if(i==0) return;
00387
00388 i*=sizeof(T);
00389 bytes-=i;
00390
00391
00392 }
00393 #endif
00394
00395 unsigned int initFunc();
00396 std::vector<T> v;
00397 };
00398
00399 #ifdef CH_USE_MEMORY_TRACKING
00400 template <class T>
00401 long int Vector<T>::bytes=0;
00402
00403 template <class T>
00404 long int Vector<T>::peak=0;
00405
00406
00407
00408 #endif
00409
00410
00411
00412 template <class T>
00413 ostream& operator<<(ostream& os, const Vector<T>& vec)
00414 {
00415 for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00416 return os;
00417 }
00418
00419 #ifdef CH_USE_MEMORY_TRACKING
00420
00421 template <class T>
00422 unsigned int Vector<T>::initFunc()
00423 {
00424 if(vectorList_ == NULL)
00425 {
00426 vectorList_ = new VectorList;
00427 vectorIncr_ = new incr;
00428 }
00429
00430 ppair tmp(&bytes, &peak);
00431 vectorList_->operator[](typeid(T).name()) = tmp;
00432
00433
00434
00435 return sizeof(T);
00436 }
00437
00438 #endif
00439
00440 #include "BaseNamespaceFooter.H"
00441 #endif