SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
canArray.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Scientific Computation Research Center
3  *
4  * This work is open source software, licensed under the terms of the
5  * BSD license as described in the LICENSE file in the top-level directory.
6  */
7 
8 #ifndef CAN_ARRAY_H
9 #define CAN_ARRAY_H
10 
14 #include <cstddef>
15 
19 namespace can {
20 
22 template <class T, unsigned N=0>
23 class Array
24 {
25  public:
27  Array() {}
29  Array(Array<T,N> const& other) {copy(other);}
31  ~Array() {}
34  {
35  copy(other);
36  return *this;
37  }
39  T& operator[](unsigned i) {return elems[i];}
41  T const& operator[](unsigned i) const {return elems[i];}
43  unsigned size() const {return N;}
44  void resize(unsigned n) { (void) n; }
45  protected:
46  T elems[N];
47  void copy(Array<T,N> const& other)
48  {
49  for (unsigned i=0; i < N; ++i)
50  elems[i] = other.elems[i];
51  }
52 };
53 
55 template <class T>
56 class Array<T,0>
57 {
58  public:
60  Array() : sz(0), elems(0) {}
62  Array(unsigned n) : sz(0), elems(0) {resize(n);}
64  Array(Array<T,0> const& other) : sz(0), elems(0) {copy(other);}
66  ~Array() {delete [] elems;}
69  {
70  copy(other);
71  return *this;
72  }
74  T& operator[](unsigned i) {return elems[i];}
76  T const& operator[](unsigned i) const {return elems[i];}
78  unsigned size() const {return sz;}
80  void resize(unsigned n)
81  {
82  if (sz == n)
83  return;
84  delete [] elems;
85  sz = n;
86  elems = new T[n];
87  }
88  void resize_copy(unsigned n)
89  {
90  if (sz == n)
91  return;
92  T* tmp = new T[n];
93  unsigned min = sz > n ? n : sz;
94  for(unsigned int x = 0; x < min; x++)
95  tmp[x] = elems[x];
96  delete [] elems;
97  sz = n;
98  elems = tmp;
99  }
100  typedef T* iterator;
101  typedef T const* const_iterator;
102  T const* begin() const {return elems;}
103  T const* end() const {return elems + sz;}
104  T* begin() {return elems;}
105  T* end() {return elems + sz;}
106  protected:
107  unsigned sz;
108  T* elems;
109  void copy(Array<T,0> const& other)
110  {
111  if (sz != other.sz)
112  resize(other.sz);
113  for (unsigned i = 0; i < sz; ++i)
114  elems[i] = other.elems[i];
115  }
116 };
117 
118 }
119 
120 #endif
Array< T, 0 > & operator=(Array< T, 0 > const &other)
assignment operator
Definition: canArray.h:68
T & operator[](unsigned i)
mutable index operator
Definition: canArray.h:74
T const & operator[](unsigned i) const
immutable index operator
Definition: canArray.h:41
T const & operator[](unsigned i) const
immutable index operator
Definition: canArray.h:76
Array()
default constructor - necessary
Definition: canArray.h:27
run-time (dynamic) array
Definition: canArray.h:56
~Array()
destructor - need to delete elems
Definition: canArray.h:66
Array(Array< T, N > const &other)
copy constructor
Definition: canArray.h:29
Array(unsigned n)
construct with n elems
Definition: canArray.h:62
~Array()
elems is destroyed automatically
Definition: canArray.h:31
compile-time (static) array of size N
Definition: canArray.h:23
Array()
default constructor - no allocation
Definition: canArray.h:60
T & operator[](unsigned i)
mutable index operator
Definition: canArray.h:39
unsigned size() const
get the size of this array
Definition: canArray.h:78
Array(Array< T, 0 > const &other)
copy constructor
Definition: canArray.h:64
unsigned size() const
get the size of this array
Definition: canArray.h:43
void resize(unsigned n)
resize the array
Definition: canArray.h:80
Array< T, N > & operator=(Array< T, N > const &other)
assignment operator
Definition: canArray.h:33