OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FArray2D.h
Go to the documentation of this file.
1#ifndef CLASSIC_FArray2D_HH
2#define CLASSIC_FArray2D_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: FArray2D.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1.2.3 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: FArray2D<T,N>
13//
14// ------------------------------------------------------------------------
15// Class category: FixedAlgebra
16// ------------------------------------------------------------------------
17//
18// $Date: 2004/11/18 22:18:06 $
19// $Author: jsberg $
20//
21// ------------------------------------------------------------------------
22
24#include "FixedAlgebra/FSlice.h"
26#include <algorithm>
27#include <iosfwd>
28#include <iomanip>
29
30
31// Template class FArray2D<T,M,N>
32// ------------------------------------------------------------------------
34// This version has fixed dimensions. It implements storage management
35// and access, but contains no arithmetic operations.
36// The destructor generated by the compiler performs the correct operation.
37// For speed reasons it is not implemented.
38
39template<class T, int M, int N>
40class FArray2D {
41
42public:
43
45 typedef T value_type;
46
48 typedef T *iterator;
49
51 typedef const T *const_iterator;
52
54 typedef T *row_iterator;
55
57 typedef const T *const_row_iterator;
58
61
64
66 // Constructs zero array.
68
70 // Set all array elements to [b]t[/b].
71 explicit FArray2D(const T &t);
72
75
78
80 // Return a reference to the element in row [b]r[/b] and column [b]c[/b].
81 T &operator()(int r, int c);
82
84 // Return a constant reference to the element in row [b]r[/b] and
85 // column [b]c[/b].
86 const T &operator()(int r, int c) const;
87
89 int nrows() const;
90
92 int ncols() const;
93
95 int size() const;
96
98 // The array is interpreted as a one-dimensional array.
99 // Version for non-constant array.
101
103 // The array is interpreted as a one-dimensional array.
104 // Version for non-constant array.
106
108 // The array is interpreted as a one-dimensional array.
109 // Version for constant array.
111
113 // The array is interpreted as a one-dimensional array.
114 // Version for constant array.
116
118 // Return pointer to beginning of row [b]r[/b].
119 // Throw RangeError, if [b]r[/b] is out of range.
121
123 // Return pointer past end of row [b]r[/b].
124 // Throw RangeError, if [b]r[/b] is out of range.
126
128 // Return pointer to beginning of constant row [b]r[/b].
129 // Throw RangeError, if [b]r[/b] is out of range.
131
133 // Return pointer past end of constant row [b]r[/b].
134 // Throw RangeError, if [b]r[/b] is out of range.
136
138 // Return pointer to beginning of row [b]r[/b].
139 // Result is undefined, if [b]r[/b] is out of range.
141
143 // Return pointer to beginning of row [b]r[/b].
144 // Result is undefined, if [b]r[/b] is out of range.
146
148 // Return pointer to beginning of column [b]c[/b].
149 // Throw RangeError if [b]c[/b] is out of range.
151
153 // Return pointer past end of column [b]c[/b].
154 // Throw RangeError if [b]c[/b] is out of range.
156
158 // Return pointer to beginning of column [b]c[/b].
159 // Throw RangeError if [b]c[/b] is out of range.
161
163 // Return pointer past end of column [b]c[/b].
164 // Throw RangeError if [b]c[/b] is out of range.
166
168 // Copy column [b]c[/b] into [b]toArray[/b].
169 // Throw RangeError if [b]c[/b] is out of range.
170 void getColumn(FArray1D<T, M> &toArray, int c) const;
171
173 // Copy row [b]r[/b] into [b]toArray[/b].
174 void getRow(FArray1D<T, N> &toArray, int r) const;
175
177 // Copy [b]fromArray[/b] to column [b]c[/b].
178 // Throw RangeError if [b]c[/b] is out of range.
179 void putColumn(const FArray1D<T, M> &fromArray, int c);
180
182 // Copy [b]fromArray[/b] to row [b]r[/b].
183 // Throw RangeError if [b]r[/b] is out of range.
184 void putRow(const FArray1D<T, N> &fromArray, int r);
185
187 // Exchange columns [b]c1[/b] and [b]c2[/b].
188 // Throw RangeError, if either index is out of range.
189 void swapColumns(int c1, int c2);
190
192 // Exchange rows [b]r1[/b] and [b]r2[/b].
193 // Throw RangeError, if either index is out of range.
194 void swapRows(int r1, int r2);
195
196protected:
197
198 // Array data.
199 T data[M*N];
200
201 // Array size.
202 static const int SIZE = M *N;
203};
204
205
206// Template implementation.
207// ------------------------------------------------------------------------
208
209template <class T, int M, int N>
211 std::fill(begin(), end(), T(0));
212}
213
214
215template <class T, int M, int N>
216inline FArray2D<T, M, N>::FArray2D(const T &val) {
217 std::fill(begin(), end(), val);
218}
219
220
221template<class T, int M, int N>
223 std::copy(rhs.begin(), rhs.end(), begin());
224}
225
226
227template<class T, int M, int N>
229 std::copy(rhs.begin(), rhs.end(), begin());
230 return *this;
231}
232
233
234template <class T, int M, int N>
235inline T &FArray2D<T, M, N>::operator()(int r, int c) {
236 if(r >= M || c >= N) {
237 throw CLRangeError("FArray2D::operator()", "Index out of range.");
238 }
239 return data[N*r+c];
240}
241
242
243template <class T, int M, int N>
244inline const T &FArray2D<T, M, N>::operator()(int r, int c) const {
245 if(r >= M || c >= N) {
246 throw CLRangeError("FArray2D::operator()", "Index out of range.");
247 }
248 return data[N*r+c];
249}
250
251
252template <class T, int M, int N>
253inline int FArray2D<T, M, N>::nrows() const {
254 return M;
255}
256
257
258template <class T, int M, int N>
259inline int FArray2D<T, M, N>::ncols() const {
260 return N;
261}
262
263
264template <class T, int M, int N>
265inline int FArray2D<T, M, N>::size() const {
266 return SIZE;
267}
268
269
270template <class T, int M, int N>
272 return data;
273}
274
275
276template <class T, int M, int N>
278 return data + SIZE;
279}
280
281
282template <class T, int M, int N>
284 return data;
285}
286
287
288template <class T, int M, int N>
290 return data + SIZE;
291}
292
293
294template <class T, int M, int N>
296 if(r >= M) {
297 throw CLRangeError("FArray2D::row_begin()", "Row index out of range.");
298 }
299 return data + N * r;
300}
301
302
303template <class T, int M, int N>
305 if(r >= M) {
306 throw CLRangeError("FArray2D::row_end()", "Row index out of range.");
307 }
308 return data + N * (r + 1);
309}
310
311
312template <class T, int M, int N>
314 if(r >= M) {
315 throw CLRangeError("FArray2D::row_begin()", "Row index out of range.");
316 }
317 return data + N * r;
318}
319
320
321template <class T, int M, int N>
323 if(r >= M) {
324 throw CLRangeError("FArray2D::row_end()", "Row index out of range.");
325 }
326 return data + N * (r + 1);
327}
328
329
330template <class T, int M, int N>
332 return data + N * r;
333}
334
335
336template <class T, int M, int N>
338 return data + N * r;
339}
340
341
342template <class T, int M, int N>
344 if(c >= N) {
345 throw CLRangeError("FArray2D::col_begin()", "Column index out of range.");
346 }
347 return col_iterator(begin() + c);
348}
349
350
351template <class T, int M, int N>
353 if(c >= N) {
354 throw CLRangeError("FArray2D::col_end()", "Column index out of range.");
355 }
356 return col_iterator(end() + c);
357}
358
359
360template <class T, int M, int N>
362 if(c >= N) {
363 throw CLRangeError("FArray2D::col_begin()", "Column index out of range.");
364 }
365 return const_col_iterator(begin() + c);
366}
367
368
369template <class T, int M, int N>
371 if(c >= N) {
372 throw CLRangeError("FArray2D::col_end()", "Column index out of range.");
373 }
374 return const_col_iterator(end() + c);
375}
376
377
378template <class T, int M, int N>
380 std::copy(col_begin(c), col_end(c), toArray.begin());
381}
382
383
384template <class T, int M, int N>
385void FArray2D<T, M, N>::getRow(FArray1D<T, N> &toArray, int r) const {
386 std::copy(row_begin(r), row_end(r), toArray.begin());
387}
388
389
390template <class T, int M, int N>
391void FArray2D<T, M, N>::putColumn(const FArray1D<T, M> &fromArray, int c) {
392 std::copy(fromArray.begin(), fromArray.end(), col_begin(c));
393}
394
395
396template <class T, int M, int N>
397void FArray2D<T, M, N>::putRow(const FArray1D<T, N> &fromArray, int r) {
398 std::copy(fromArray.begin(), fromArray.end(), row_begin(r));
399}
400
401
402template <class T, int M, int N>
403void FArray2D<T, M, N>::swapColumns(int col1, int col2) {
404 std::swap_ranges(col_begin(col1), col_end(col1), col_begin(col2));
405}
406
407
408template <class T, int M, int N>
409void FArray2D<T, M, N>::swapRows(int row1, int row2) {
410 std::swap_ranges(row_begin(row1), row_end(row1), row_begin(row2));
411}
412
413
414template <class T, int M, int N>
415std::ostream &operator<<(std::ostream &os, const FArray2D<T, M, N> &v) {
416 // next two lines require changes when g++ library becomes standard.
417 std::streamsize old_prec = os.precision(12);
418 os.setf(std::ios::scientific, std::ios::floatfield);
419
420 for(int i = 0; i < M; ++i) {
421 for(int j = 0; j < N; ++j) {
422 os << std::setw(20) << v[i][j] << " ";
423 }
424 os << std::endl;
425 }
426
427 // next two lines require changes when g++ library becomes standard.
428 os.setf(std::ios::fixed, std::ios::floatfield);
429 os.precision(old_prec);
430 return os;
431}
432
433
434#endif // CLASSIC_FArray2D_HH
std::ostream & operator<<(std::ostream &os, const FArray2D< T, M, N > &v)
Definition: FArray2D.h:415
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
@ SIZE
Definition: IndexMap.cpp:174
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
A templated representation for one-dimensional arrays.
Definition: FArray1D.h:39
iterator end()
Get iterator pointing past end of array.
Definition: FArray1D.h:198
iterator begin()
Get iterator pointing to beginning of array.
Definition: FArray1D.h:192
A templated representation for 2-dimensional arrays.
Definition: FArray2D.h:40
T value_type
The value type of the array.
Definition: FArray2D.h:45
const T * const_iterator
Iterator for constant array.
Definition: FArray2D.h:51
FArray2D(const FArray2D &)
Copy constructor.
Definition: FArray2D.h:222
int ncols() const
Get number of columns.
Definition: FArray2D.h:259
FConstSlice< T, N > const_col_iterator
Iterator for access by columns.
Definition: FArray2D.h:63
T data[M *N]
Definition: FArray2D.h:199
int size() const
Get total size.
Definition: FArray2D.h:265
iterator end()
Get pointer past end of data.
Definition: FArray2D.h:277
row_iterator row_end(int r)
Get row iterator.
Definition: FArray2D.h:304
row_iterator operator[](int r)
Get row iterator.
Definition: FArray2D.h:331
void getRow(FArray1D< T, N > &toArray, int r) const
Fetch row.
Definition: FArray2D.h:385
T * row_iterator
Iterator for access by rows.
Definition: FArray2D.h:54
const_row_iterator row_begin(int r) const
Get row iterator.
Definition: FArray2D.h:313
const_row_iterator operator[](int r) const
Get row iterator.
Definition: FArray2D.h:337
const_col_iterator col_end(int c) const
Get column iterator.
Definition: FArray2D.h:370
const FArray2D< T, M, N > & operator=(const FArray2D< T, M, N > &)
Assignment.
Definition: FArray2D.h:228
void getColumn(FArray1D< T, M > &toArray, int c) const
Fetch column.
Definition: FArray2D.h:379
void putColumn(const FArray1D< T, M > &fromArray, int c)
Store column.
Definition: FArray2D.h:391
col_iterator col_begin(int c)
Get column iterator.
Definition: FArray2D.h:343
const T * const_row_iterator
Iterator for access by rows.
Definition: FArray2D.h:57
void swapColumns(int c1, int c2)
Exchange columns.
Definition: FArray2D.h:403
const_col_iterator col_begin(int c) const
Get column iterator.
Definition: FArray2D.h:361
const T & operator()(int r, int c) const
Get element.
Definition: FArray2D.h:244
const_iterator begin() const
Get beginning of data.
Definition: FArray2D.h:283
int nrows() const
Get number of rows.
Definition: FArray2D.h:253
row_iterator row_begin(int r)
Get row iterator.
Definition: FArray2D.h:295
T & operator()(int r, int c)
Get element.
Definition: FArray2D.h:235
void putRow(const FArray1D< T, N > &fromArray, int r)
Store row.
Definition: FArray2D.h:397
T * iterator
Iterator for the array.
Definition: FArray2D.h:48
static const int SIZE
Definition: FArray2D.h:202
void swapRows(int r1, int r2)
Exchange rows.
Definition: FArray2D.h:409
FArray2D(const T &t)
Constructor.
Definition: FArray2D.h:216
const_iterator end() const
Get pointer past end of data.
Definition: FArray2D.h:289
const_row_iterator row_end(int r) const
Get row iterator.
Definition: FArray2D.h:322
FArray2D()
Default constructor.
Definition: FArray2D.h:210
FSlice< T, N > col_iterator
Iterator for access by columns.
Definition: FArray2D.h:60
iterator begin()
Get beginning of data.
Definition: FArray2D.h:271
col_iterator col_end(int c)
Get column iterator.
Definition: FArray2D.h:352
An iterator permitting to iterate with a stride different from 1.
Definition: FSlice.h:30
Constant version of FSlice.
Definition: FSlice.h:101
Range error.
Definition: CLRangeError.h:33
FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field)
Definition: Inform.h:101