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