Chombo + EB + MF  3.2
Vector.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _VECTOR_H_
12 #define _VECTOR_H_
13 
14 #include <cmath>
15 #include <cstdlib>
16 #include "CH_assert.H"
17 #include <vector>
18 #include <typeinfo>
19 #include <string>
20 #include <map>
21 #include <list>
22 #include <algorithm>
23 #include <iostream>
24 
25 #include "BaseNamespaceHeader.H"
26 
27 #ifdef CH_USE_MEMORY_TRACKING
28 //these objects are used for memory tracking system
29 typedef std::pair<long int*, long int*> ppair;
30 typedef std::map<std::string, ppair > VectorList;
31 
32 typedef std::list<int> lst; //not currently used, bvs.
33 typedef std::map<std::string, lst*> incr; // not currently used, bvs
34 
35 extern VectorList* vectorList_;
36 extern incr* vectorIncr_;
37 extern unsigned long long int ch_memcount;
38 #endif
39 
40 using std::vector;
41 using std::ostream;
42 
43 /// one dimensional dynamic array
44 /**
45  Vector is a resizable one-dimensional array with constant-time
46  random access and range checking. The template type T must have a
47  default constructor, a copy constructor, and an assignment
48  operator.
49 
50 */
51 template <class T>
52 
53 class Vector
54 {
55  //friend class ParticleVector;
56 public:
57  ///
58  /**
59  Default constructor.
60  Creates a Vector of zero length with null data.
61  */
62  inline
64  {
65  }
66 
67  ///
68  /**
69  Destructor.
70  */
71  inline
72  virtual ~Vector()
73  {
74 #ifdef CH_USE_MEMORY_TRACKING
75  if (size()>0) decrement(size());
76 #endif
77  }
78 
79  ///
80  /**
81  Copy constructor.
82  */
83  inline
84  Vector(const Vector<T>& invec):v(invec.v)
85  {
86 #ifdef CH_USE_MEMORY_TRACKING
87  increment(invec.size());
88 #endif
89  }
90 
91  inline
92  Vector(Vector<T>&& a_invec) noexcept
93  : v(std::move(a_invec.v))
94  {
95 
96  //do not alter counting, this is a move operation
97  }
98  ///
99  /** conversion constructor
100  */
101  inline
102  Vector(const std::vector<T>& invec):v(invec)
103  {
104 #ifdef CH_USE_MEMORY_TRACKING
105  increment(invec.size());
106 #endif
107  }
108 
109 
110  ///
111  /**
112  */
113  inline
114  Vector<T>& operator=(const std::vector<T>& invec)
115  {
116 #ifdef CH_USE_MEMORY_TRACKING
117  decrement(size());
118 #endif
119  v=invec;
120 #ifdef CH_USE_MEMORY_TRACKING
121  increment(size());
122 #endif
123  return *this;
124  }
125 
126  inline
128  {
129  v=std::move(a_invec.v);
130  return *this;
131  }
132  ///
133  /**
134  */
135  inline
137  {
138 #ifdef CH_USE_MEMORY_TRACKING
139  decrement(size());
140 #endif
141  v=invec.v;
142 #ifdef CH_USE_MEMORY_TRACKING
143  increment(size());
144 #endif
145  return *this;
146  }
147 
148  /// assign a scalar to every element of the vector
149  /** [NOTE: cant use operator=() for this because it would be
150  * ambiguous with the (int) constructor for number T types.]
151  */
152  inline
153  Vector<T>& assign(const T& inval)
154  {
155  v.assign( size(),inval );
156  return *this;
157  }
158 
159  ///
160  /**
161  Constructs a Vector with given number of elements.\\
162 
163  {\bf Arguments:}\\
164  size (not modified): number of elements of Vector to construct.\\
165  {\bf This:}\\
166  -------The object is modified----------
167 
168  */
169 
170  inline
171  Vector(/*long*/ unsigned int isize)
172  {
173  resize(isize);
174  }
175 
176  ///
177  /**
178  */
179  inline
180  void clear()
181  {
182 #ifdef CH_USE_MEMORY_TRACKING
183  decrement(size());
184 #endif
185  v.clear();
186  }
187 
188  ///
189  /** size function. returns current size of Vector
190  */
191  inline
192  size_t size() const
193  {
194  return v.size();
195  }
196 
197  ///
198  /**
199  Constructs a Vector with given number of elements and constant value.\\
200 
201  {\bf Arguments:}\\
202  size (not modified): number of elements of Vector to construct.\\
203  value (not modified): value to set every element to.\\
204  {\bf This:}\\
205  -------The object is modified----------
206 
207  */
208  inline
209  Vector(/*long*/ unsigned int isize, const T& value) : v(isize, value)
210  {
211 #ifdef CH_USE_MEMORY_TRACKING
212  increment(isize);
213 #endif
214  }
215 
216  ///
217  /**
218  Returns a modifiable lvalue reference to the value of the given
219  element in this Vector. It is an error if n < 0 or n >= this->size(). \\
220 
221  {\bf Arguments:}\\
222  n (not modified) index of desired element.\\
223  {\bf Returns:}\\
224  modifiable reference to value in Vector at index n.\\
225  {\bf This:}\\
226  -----
227  This object is modified if the returned reference is assigned a new value
228  -----
229  */
230  inline
231  T& operator[] (/*long*/ unsigned int n)
232  {
233  CH_assert(n < size());
234  //vector<T>* svec = static_cast<vector<T>* >(this);
235  //return svec->operator[](n);
236  // return (static_cast<vector<T>* >(this))->operator[](n);
237  return v[n];
238  }
239 
240  ///
241  /**
242  Returns a constant reference to the given element in this
243  Vector.\\
244 
245  {\bf Arguments:}\\
246  n (not modified) index of desired element.\\
247  {\bf Returns:}\\
248  constant reference to value in Vector at index n.\\
249  {\bf This:}\\
250  This object is not modified.
251  */
252  inline
253  const T& operator[] (/*long*/ unsigned int n) const
254  {
255  CH_assert(n < size());
256  //const vector<T>* svec = static_cast<const vector<T>* >(this);
257  //return svec->operator[](n);
258  // return (static_cast<const vector<T>* >(this))->operator[](n);
259  return v[n];
260  }
261 
262  inline
263  T &front()
264  {
265  return v.front();
266  }
267 
268  inline
269  T &back()
270  {
271  return v.back();
272  }
273 
274  inline
275  void pop_back()
276  {
277  v.pop_back();
278  }
279 
280  inline
281  const T& back() const
282  {
283  return v.back();
284  }
285 
286  inline
287  void swap(Vector<T>& other)
288  {
289  v.swap(other.v);
290  }
291  ///
292  /**
293  */
294  inline
295  void push_back(const T& in)
296  {
297 #ifdef CH_USE_MEMORY_TRACKING
298  increment(1);
299 #endif
300  // (static_cast<vector<T>* >(this))->push_back(in);
301  v.push_back(in);
302  }
303 
304  inline
305  void push_back(T&& in)
306  {
307 #ifdef CH_USE_MEMORY_TRACKING
308  increment(1);
309 #endif
310  // (static_cast<vector<T>* >(this))->push_back(in);
311  v.push_back(std::move(in));
312  }
313 
314 
315  ///
316  /**
317  Modifies this Vector by appending the elements of the argument
318  Vector. The new Vector will have a size of this->size() +
319  invec.size() (where this Vector is considered before the append is
320  performed). The first element of invec will have index
321  this->size(), the second element will have index this->size()+1,
322  etc.\\
323 
324  {\bf Arguments:}\\
325  invec (not modified): Vector whose elements to append to this Vector.\\
326  {\bf This:}\\
327  -------The object is modified----------
328  */
329  inline
330  void append(const Vector<T>& invec)
331  {
332 #ifdef CH_USE_MEMORY_TRACKING
333  increment(invec.size());
334 #endif
335  for (int ivec = 0; ivec < invec.size(); ivec++)
336  {
337  // (static_cast<vector<T>* >(this))->push_back(invec[ivec]);
338  v.push_back(invec[ivec]);
339  }
340  }
341 
342  ///
343  /**
344  */
345  inline
346  void resize(/*long*/ unsigned int isize)
347  {
348  // vector<T>* svec = static_cast<vector<T>* >(this);
349 #ifdef CH_USE_MEMORY_TRACKING
350  if (isize > size()) increment(isize-size());
351  else decrement(size()-isize);
352 #endif
353  unsigned int l= size();
354  // svec->resize(isize);
355  v.resize(isize);
356  for (; l<isize; ++l)
357  // svec->operator[](l) = T();
358  v[l] = T();
359  }
360 
361  inline
362  void reserve(/*long*/ size_t isize)
363  {
364  v.reserve(isize);
365  }
366 
367  inline
368  size_t capacity() const
369  {
370  return v.capacity();
371  }
372  ///
373  /**
374  */
375  inline
376  void resize(/*long*/ unsigned int isize, const T& value)
377  {
378  // vector<T>* svec = static_cast<vector<T>* >(this);
379 #ifdef CH_USE_MEMORY_TRACKING
380  if (isize > size()) increment(isize-size());
381  else decrement(size()-isize);
382 #endif
383  // svec->resize(isize, value);
384  v.resize(isize, value);
385  }
386 
387  ///
388  /**
389  */
390  inline void sort()
391  {
392  std::sort(v.begin(), v.end());
393  }
394 
395  /// Returns std::vector under the hood.
396  inline std::vector<T>& stdVector()
397  {
398  return v;
399  }
400 
401  inline const std::vector<T>& constStdVector() const
402  {
403  return v;
404  }
405 
406 #ifdef CH_USE_MEMORY_TRACKING
407  // static template memory tracking data
408  static long int bytes;
409  static long int peak;
410  // static lst* incrementList;
411 #endif
412 
413 private:
414 
415 #ifdef CH_USE_MEMORY_TRACKING
416  inline
417  void increment(unsigned int i)
418  {
419  // warning: pointless comparison of unsigned integer with zero
420  // CH_assert(i>=0); // commented out (ndk)
421  static unsigned int sizzle = initFunc();
422  i*=sizzle;
423  bytes += i;
424  ch_memcount+=i*sizeof(T);
425  // incrementList->insert(incrementList->begin(), i);
426  if (bytes > peak) peak = bytes;
427  }
428 
429  inline
430  void decrement(unsigned int i)
431  {
432  if (i==0) return;
433  // CH_assert(bytes>=0);
434  i*=sizeof(T);
435  bytes-=i;
436  ch_memcount-=i*sizeof(T);
437  // incrementList->insert(incrementList->begin(), -i);
438  // CH_assert(bytes>=0);
439  }
440 #endif
441 
442  unsigned int initFunc();
443  std::vector<T> v;
444 };
445 
446 #ifdef CH_USE_MEMORY_TRACKING
447 template <class T>
448 long int Vector<T>::bytes=0;
449 
450 template <class T>
451 long int Vector<T>::peak=0;
452 
453 //template <class T>
454 //lst* Vector<T>::incrementList=NULL;
455 #endif
456 
457 //class ostream;
458 
459 template <class T>
460 ostream& operator<<(ostream& os, const Vector<T>& vec)
461 {
462  for (unsigned int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
463  return os;
464 }
465 
466 #ifdef CH_USE_MEMORY_TRACKING
467 
468 template <class T>
469 unsigned int Vector<T>::initFunc()
470 {
471  if (vectorList_ == NULL)
472  {
473  vectorList_ = new VectorList;
474  vectorIncr_ = new incr;
475  }
476  // if (incrementList == NULL) incrementList = new lst;
477  ppair tmp(&bytes, &peak);
478  vectorList_->operator[](typeid(T).name()) = tmp;
479  // vectorIncr_->operator[](typeid(T).name()) = incrementList;
480  // bytes = 0;
481  // peak = 0;
482  return sizeof(T);
483 }
484 
485 #endif
486 
487 #include "BaseNamespaceFooter.H"
488 #endif
std::vector< T > & stdVector()
Returns std::vector under the hood.
Definition: Vector.H:396
#define CH_assert(cond)
Definition: CHArray.H:37
Vector< T > & operator=(const std::vector< T > &invec)
Definition: Vector.H:114
Vector(const Vector< T > &invec)
Definition: Vector.H:84
one dimensional dynamic array
Definition: Vector.H:53
void swap(Vector< T > &other)
Definition: Vector.H:287
unsigned int initFunc()
size_t capacity() const
Definition: Vector.H:368
virtual ~Vector()
Definition: Vector.H:72
T & operator[](unsigned int n)
Definition: Vector.H:231
T & front()
Definition: Vector.H:263
void sort()
Definition: Vector.H:390
void append(const Vector< T > &invec)
Definition: Vector.H:330
void pop_back()
Definition: Vector.H:275
void resize(unsigned int isize)
Definition: Vector.H:346
void push_back(T &&in)
Definition: Vector.H:305
void push_back(const T &in)
Definition: Vector.H:295
void clear()
Definition: Vector.H:180
T & back()
Definition: Vector.H:269
const char * name(const FArrayBox &a_dummySpecializationArg)
Definition: CH_HDF5.H:864
const T & back() const
Definition: Vector.H:281
Vector< T > & assign(const T &inval)
assign a scalar to every element of the vector
Definition: Vector.H:153
Vector()
Definition: Vector.H:63
Vector(const std::vector< T > &invec)
Definition: Vector.H:102
Vector(Vector< T > &&a_invec) noexcept
Definition: Vector.H:92
Vector(unsigned int isize, const T &value)
Definition: Vector.H:209
const std::vector< T > & constStdVector() const
Definition: Vector.H:401
size_t size() const
Definition: Vector.H:192
Vector< T > & operator=(Vector< T > &&a_invec)
Definition: Vector.H:127
void reserve(size_t isize)
Definition: Vector.H:362
Vector< T > & operator=(const Vector< T > &invec)
Definition: Vector.H:136
void resize(unsigned int isize, const T &value)
Definition: Vector.H:376
Vector(unsigned int isize)
Definition: Vector.H:171
std::vector< T > v
Definition: Vector.H:443