OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Array1D.h
Go to the documentation of this file.
1 #ifndef CLASSIC_Array1D_HH
2 #define CLASSIC_Array1D_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: Array1D.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1.2.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: Array1D
13 
14 // ------------------------------------------------------------------------
15 // Class category: Algebra
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2004/11/18 22:18:05 $
19 // $Author: jsberg $
20 //
21 // ------------------------------------------------------------------------
22 
23 #include "Utilities/CLRangeError.h"
24 #include <algorithm>
25 #include <ostream>
26 
27 
28 // Template class Array1D<T>
29 // ------------------------------------------------------------------------
31 // A templated representation for one-dimensional arrays.
32 // This class implements storage management and component access,
33 // but contains no arithmetic operations.
34 
35 template<class T>
36 class Array1D {
37 
38 public:
39 
41  typedef T value_type;
42 
44  typedef T *iterator;
45 
47  typedef const T *const_iterator;
48 
50  // Constructs an array of zero length.
51  Array1D();
52 
54  // Reserves space for [b]n[/b] elements and leaves them undefined.
55  explicit Array1D(int n);
56 
58  // Reserves space for [b]n[/b] elements and store [b]n[/b] copies of
59  // [b]t[/b].
60  Array1D(int n, const T &t);
61 
62  Array1D(const Array1D<T> &);
63  ~Array1D();
65 
67  // Return a reference to the element in position [b]n[/b].
68  // Throw RangeError, if [b]n[/b] is out of range.
69  T &operator()(int n);
70 
72  // Return the value of the element in position [b]n[/b].
73  // Throw RangeError, if [b]n[/b] is out of range.
74  const T &operator()(int n) const;
75 
77  // Return a reference to the element in position [b]n[/b].
78  // Result is undefined, if [b]n[/b] is out of range.
79  T &operator[](int);
80 
82  // Return the value of the element in position [b]n[/b].
83  // Result is undefined, if [b]n[/b] is out of range.
84  const T &operator[](int) const;
85 
87  // Return pointer to beginning of array.
88  // Version for non-constant objects.
89  iterator begin();
90 
92  // Return pointer past end of array.
93  // Version for non-constant objects.
94  iterator end();
95 
97  // Return pointer to beginning of array.
98  // Version for constant objects.
99  const_iterator begin() const;
100 
102  // Return pointer past end of array.
103  // Version for constant objects.
104  const_iterator end() const;
105 
107  int size() const;
108 
110  // Elements added are left undefined.
111  void resize(int size);
112 
113 protected:
114 
115  // The array size.
116  int len;
117 
118  // The array data.
119  T *data;
120 };
121 
122 
123 // Template function implementation.
124 // ------------------------------------------------------------------------
125 
126 template<class T>
128  len(0), data(0)
129 {}
130 
131 
132 template<class T>
134  len(array.len), data(new T[len]) {
135  std::copy(array.begin(), array.end(), begin());
136 }
137 
138 
139 template<class T>
141  len(n), data(new T[len])
142 {}
143 
144 
145 template<class T>
146 Array1D<T>::Array1D(int n, const T &val):
147  len(n), data(new T[len]) {
148  std::fill(begin(), end(), val);
149 }
150 
151 
152 template<class T>
154  delete [] data;
155 }
156 
157 
158 template<class T>
160  if(this != &rhs) {
161  if(rhs.len > len) {
162  delete [] data;
163  data = new T[rhs.len];
164  }
165 
166  len = rhs.len;
167  std::copy(rhs.begin(), rhs.end(), begin());
168  }
169  return *this;
170 }
171 
172 
173 template<class T> inline
175  return data[i];
176 }
177 
178 
179 template<class T> inline
180 const T &Array1D<T>::operator[](int i) const {
181  return data[i];
182 }
183 
184 
185 template<class T>
187  if(i < 0 || i >= size()) {
188  throw CLRangeError("Array1D::operator()", "Index out of range.");
189  }
190  return data[i];
191 }
192 
193 
194 template<class T>
195 const T &Array1D<T>::operator()(int i) const {
196  if(i < 0 || i >= size()) {
197  throw CLRangeError("Array1D::operator()", "Index out of range.");
198  }
199  return data[i];
200 }
201 
202 
203 template<class T>
205  return data;
206 }
207 
208 
209 template<class T>
211  return data + len;
212 }
213 
214 
215 template<class T>
217  return data;
218 }
219 
220 
221 template<class T>
223  return data + len;
224 }
225 
226 
227 template<class T>
228 int Array1D<T>::size() const {
229  return len;
230 }
231 
232 
233 template<class T>
235  if(len < n) {
236  T *old = data;
237  data = new T[n];
238  std::copy(old, old + len, begin());
239  delete [] old;
240  }
241  len = n;
242 }
243 
244 
245 template <class T>
246 std::ostream &operator<<(std::ostream &os, const Array1D<T> &v) {
247  for(int i = 0; i < v.size(); ++i) {
248  os << v[i] << " ";
249  }
250 
251  os << std::endl;
252  return os;
253 }
254 
255 #endif // CLASSIC_Array1D_HH
Definition: rbendmap.h:8
Array1D< T > & operator=(const Array1D< T > &)
Definition: Array1D.h:159
iterator end()
Get end of data.
Definition: Array1D.h:210
int len
Definition: Array1D.h:116
T * data
Definition: Array1D.h:119
const T * const_iterator
The iterator type for constant array.
Definition: Array1D.h:47
int size() const
Get array size.
Definition: Array1D.h:228
Array1D()
Default constructor.
Definition: Array1D.h:127
Truncated power series.
Definition: Tps.h:27
iterator begin()
Get beginning of data.
Definition: Array1D.h:204
T value_type
The value type of this array.
Definition: Array1D.h:41
T & operator[](int)
Get reference to element.
Definition: Array1D.h:174
~Array1D()
Definition: Array1D.h:153
void resize(int size)
Change array size.
Definition: Array1D.h:234
Range error.
Definition: CLRangeError.h:33
T * iterator
The iterator type for the array.
Definition: Array1D.h:44
One-dimensional array.
Definition: Array1D.h:36
T & operator()(int n)
Get reference to element.
Definition: Array1D.h:186
Inform & endl(Inform &inf)
Definition: Inform.cpp:42