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
00042
00043
00044
00045
00046
00047
00048
00049
00050 template <class T>
00051
00052 class Vector
00053 {
00054
00055 public:
00056
00057
00058
00059
00060
00061 inline
00062 Vector()
00063 {
00064 }
00065
00066
00067
00068
00069
00070 inline
00071 virtual ~Vector()
00072 {
00073 #ifdef CH_USE_MEMORY_TRACKING
00074 if (size()>0) decrement(size());
00075 #endif
00076 }
00077
00078
00079
00080
00081
00082 inline
00083 Vector(const Vector<T>& invec):v(invec.v)
00084 {
00085 #ifdef CH_USE_MEMORY_TRACKING
00086 increment(invec.size());
00087 #endif
00088 }
00089
00090
00091
00092
00093 inline
00094 Vector(const std::vector<T>& invec):v(invec)
00095 {
00096 #ifdef CH_USE_MEMORY_TRACKING
00097 increment(invec.size());
00098 #endif
00099 }
00100
00101
00102
00103
00104 inline
00105 Vector<T>& operator=(const std::vector<T>& invec)
00106 {
00107 #ifdef CH_USE_MEMORY_TRACKING
00108 decrement(size());
00109 #endif
00110 v=invec;
00111 #ifdef CH_USE_MEMORY_TRACKING
00112 increment(size());
00113 #endif
00114 return *this;
00115 }
00116
00117
00118
00119
00120 inline
00121 Vector<T>& operator=(const Vector<T>& invec)
00122 {
00123 #ifdef CH_USE_MEMORY_TRACKING
00124 decrement(size());
00125 #endif
00126 v=invec.v;
00127 #ifdef CH_USE_MEMORY_TRACKING
00128 increment(size());
00129 #endif
00130 return *this;
00131 }
00132
00133
00134
00135
00136
00137 inline
00138 Vector<T>& assign(const T& inval)
00139 {
00140 v.assign( size(),inval );
00141 return *this;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 inline
00156 Vector( unsigned int isize)
00157 {
00158 resize(isize);
00159 }
00160
00161
00162
00163
00164 inline
00165 void clear()
00166 {
00167 #ifdef CH_USE_MEMORY_TRACKING
00168 decrement(size());
00169 #endif
00170 v.clear();
00171 }
00172
00173
00174
00175
00176 inline
00177 size_t size() const
00178 {
00179 return v.size();
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 inline
00194 Vector( unsigned int isize, const T& value) : v(isize, value)
00195 {
00196 #ifdef CH_USE_MEMORY_TRACKING
00197 increment(isize);
00198 #endif
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 inline
00216 T& operator[] ( unsigned int n)
00217 {
00218 CH_assert(n < size());
00219
00220
00221
00222 return v[n];
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 inline
00238 const T& operator[] ( unsigned int n) const
00239 {
00240 CH_assert(n < size());
00241
00242
00243
00244 return v[n];
00245 }
00246
00247
00248 inline
00249 T &front()
00250 {
00251 return v.front();
00252 }
00253
00254
00255 inline
00256 T &back()
00257 {
00258 return v.back();
00259 }
00260
00261
00262 inline
00263 void pop_back()
00264 {
00265 v.pop_back();
00266 }
00267
00268 inline
00269 const T& back() const
00270 {
00271 return v.back();
00272 }
00273
00274 inline
00275 void swap(Vector<T>& other)
00276 {
00277 v.swap(other.v);
00278 }
00279
00280
00281
00282 inline
00283 void push_back(const T& in)
00284 {
00285 #ifdef CH_USE_MEMORY_TRACKING
00286 increment(1);
00287 #endif
00288
00289 v.push_back(in);
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 inline
00307 void append(const Vector<T>& invec)
00308 {
00309 #ifdef CH_USE_MEMORY_TRACKING
00310 increment(invec.size());
00311 #endif
00312 for (int ivec = 0; ivec < invec.size(); ivec++)
00313 {
00314
00315 v.push_back(invec[ivec]);
00316 }
00317 }
00318
00319
00320
00321
00322 inline
00323 void resize( unsigned int isize)
00324 {
00325
00326 #ifdef CH_USE_MEMORY_TRACKING
00327 if (isize > size()) increment(isize-size());
00328 else decrement(size()-isize);
00329 #endif
00330 unsigned int l= size();
00331
00332 v.resize(isize);
00333 for (; l<isize; ++l)
00334
00335 v[l] = T();
00336 }
00337
00338 inline
00339 void reserve( size_t isize)
00340 {
00341 v.reserve(isize);
00342 }
00343
00344 inline
00345 size_t capacity() const
00346 {
00347 return v.capacity();
00348 }
00349
00350
00351
00352 inline
00353 void resize( unsigned int isize, const T& value)
00354 {
00355
00356 #ifdef CH_USE_MEMORY_TRACKING
00357 if (isize > size()) increment(isize-size());
00358 else decrement(size()-isize);
00359 #endif
00360
00361 v.resize(isize, value);
00362 }
00363
00364
00365
00366
00367 inline void sort()
00368 {
00369 std::sort(v.begin(), v.end());
00370 }
00371
00372
00373 inline std::vector<T>& stdVector()
00374 {
00375 return v;
00376 }
00377
00378 inline const std::vector<T>& constStdVector() const
00379 {
00380 return v;
00381 }
00382
00383 #ifdef CH_USE_MEMORY_TRACKING
00384
00385 static long int bytes;
00386 static long int peak;
00387
00388 #endif
00389
00390 private:
00391
00392 #ifdef CH_USE_MEMORY_TRACKING
00393 inline
00394 void increment(unsigned int i)
00395 {
00396
00397
00398 static unsigned int sizzle = initFunc();
00399 i*=sizzle;
00400 bytes += i;
00401
00402 if (bytes > peak) peak = bytes;
00403 }
00404
00405 inline
00406 void decrement(unsigned int i)
00407 {
00408 if (i==0) return;
00409
00410 i*=sizeof(T);
00411 bytes-=i;
00412
00413
00414 }
00415 #endif
00416
00417 unsigned int initFunc();
00418 std::vector<T> v;
00419 };
00420
00421 #ifdef CH_USE_MEMORY_TRACKING
00422 template <class T>
00423 long int Vector<T>::bytes=0;
00424
00425 template <class T>
00426 long int Vector<T>::peak=0;
00427
00428
00429
00430 #endif
00431
00432
00433
00434 template <class T>
00435 ostream& operator<<(ostream& os, const Vector<T>& vec)
00436 {
00437 for (int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00438 return os;
00439 }
00440
00441 #ifdef CH_USE_MEMORY_TRACKING
00442
00443 template <class T>
00444 unsigned int Vector<T>::initFunc()
00445 {
00446 if (vectorList_ == NULL)
00447 {
00448 vectorList_ = new VectorList;
00449 vectorIncr_ = new incr;
00450 }
00451
00452 ppair tmp(&bytes, &peak);
00453 vectorList_->operator[](typeid(T).name()) = tmp;
00454
00455
00456
00457 return sizeof(T);
00458 }
00459
00460 #endif
00461
00462 #include "BaseNamespaceFooter.H"
00463 #endif