OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
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
35template<class T>
36class Array1D {
37
38public:
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.
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
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.
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.
90
92 // Return pointer past end of array.
93 // Version for non-constant objects.
95
97 // Return pointer to beginning of array.
98 // Version for constant objects.
100
102 // Return pointer past end of array.
103 // Version for constant objects.
105
107 int size() const;
108
110 // Elements added are left undefined.
111 void resize(int size);
112
113protected:
114
115 // The array size.
116 int len;
117
118 // The array data.
120};
121
122
123// Template function implementation.
124// ------------------------------------------------------------------------
125
126template<class T>
128 len(0), data(0)
129{}
130
131
132template<class T>
134 len(array.len), data(new T[len]) {
135 std::copy(array.begin(), array.end(), begin());
136}
137
138
139template<class T>
141 len(n), data(new T[len])
142{}
143
144
145template<class T>
146Array1D<T>::Array1D(int n, const T &val):
147 len(n), data(new T[len]) {
148 std::fill(begin(), end(), val);
149}
150
151
152template<class T>
154 delete [] data;
155}
156
157
158template<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
173template<class T> inline
175 return data[i];
176}
177
178
179template<class T> inline
180const T &Array1D<T>::operator[](int i) const {
181 return data[i];
182}
183
184
185template<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
194template<class T>
195const 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
203template<class T>
205 return data;
206}
207
208
209template<class T>
211 return data + len;
212}
213
214
215template<class T>
217 return data;
218}
219
220
221template<class T>
223 return data + len;
224}
225
226
227template<class T>
228int Array1D<T>::size() const {
229 return len;
230}
231
232
233template<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
245template <class T>
246std::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
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
std::ostream & operator<<(std::ostream &os, const Array1D< T > &v)
Definition: Array1D.h:246
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
One-dimensional array.
Definition: Array1D.h:36
Array1D(int n, const T &t)
Constructor.
Definition: Array1D.h:146
const_iterator end() const
Get end of data.
Definition: Array1D.h:222
Array1D(int n)
Constructor.
Definition: Array1D.h:140
int len
Definition: Array1D.h:116
~Array1D()
Definition: Array1D.h:153
iterator begin()
Get beginning of data.
Definition: Array1D.h:204
T * data
Definition: Array1D.h:119
const T & operator[](int) const
Get value of element.
Definition: Array1D.h:180
Array1D(const Array1D< T > &)
Definition: Array1D.h:133
int size() const
Get array size.
Definition: Array1D.h:228
iterator end()
Get end of data.
Definition: Array1D.h:210
T * iterator
The iterator type for the array.
Definition: Array1D.h:44
const T * const_iterator
The iterator type for constant array.
Definition: Array1D.h:47
T & operator()(int n)
Get reference to element.
Definition: Array1D.h:186
T & operator[](int)
Get reference to element.
Definition: Array1D.h:174
const T & operator()(int n) const
Get value of element.
Definition: Array1D.h:195
Array1D< T > & operator=(const Array1D< T > &)
Definition: Array1D.h:159
void resize(int size)
Change array size.
Definition: Array1D.h:234
const_iterator begin() const
Get beginning of data.
Definition: Array1D.h:216
Array1D()
Default constructor.
Definition: Array1D.h:127
T value_type
The value type of this array.
Definition: Array1D.h:41
Range error.
Definition: CLRangeError.h:33