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