OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 
23 #include "FixedAlgebra/FArray1D.h"
24 #include "FixedAlgebra/FSlice.h"
25 #include "Utilities/CLRangeError.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 
39 template<class T, int M, int N>
40 class FArray2D {
41 
42 public:
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.
67  FArray2D();
68 
70  // Set all array elements to [b]t[/b].
71  explicit FArray2D(const T &t);
72 
74  FArray2D(const FArray2D &);
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.
100  iterator begin();
101 
103  // The array is interpreted as a one-dimensional array.
104  // Version for non-constant array.
105  iterator end();
106 
108  // The array is interpreted as a one-dimensional array.
109  // Version for constant array.
110  const_iterator begin() const;
111 
113  // The array is interpreted as a one-dimensional array.
114  // Version for constant array.
115  const_iterator end() const;
116 
118  // Return pointer to beginning of row [b]r[/b].
119  // Throw RangeError, if [b]r[/b] is out of range.
120  row_iterator row_begin(int r);
121 
123  // Return pointer past end of row [b]r[/b].
124  // Throw RangeError, if [b]r[/b] is out of range.
125  row_iterator row_end(int r);
126 
128  // Return pointer to beginning of constant row [b]r[/b].
129  // Throw RangeError, if [b]r[/b] is out of range.
130  const_row_iterator row_begin(int r) const;
131 
133  // Return pointer past end of constant row [b]r[/b].
134  // Throw RangeError, if [b]r[/b] is out of range.
135  const_row_iterator row_end(int r) const;
136 
138  // Return pointer to beginning of row [b]r[/b].
139  // Result is undefined, if [b]r[/b] is out of range.
140  row_iterator operator[](int r);
141 
143  // Return pointer to beginning of row [b]r[/b].
144  // Result is undefined, if [b]r[/b] is out of range.
145  const_row_iterator operator[](int r) const;
146 
148  // Return pointer to beginning of column [b]c[/b].
149  // Throw RangeError if [b]c[/b] is out of range.
150  col_iterator col_begin(int c);
151 
153  // Return pointer past end of column [b]c[/b].
154  // Throw RangeError if [b]c[/b] is out of range.
155  col_iterator col_end(int c);
156 
158  // Return pointer to beginning of column [b]c[/b].
159  // Throw RangeError if [b]c[/b] is out of range.
160  const_col_iterator col_begin(int c) const;
161 
163  // Return pointer past end of column [b]c[/b].
164  // Throw RangeError if [b]c[/b] is out of range.
165  const_col_iterator col_end(int c) const;
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 
196 protected:
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 
209 template <class T, int M, int N>
211  std::fill(begin(), end(), T(0));
212 }
213 
214 
215 template <class T, int M, int N>
216 inline FArray2D<T, M, N>::FArray2D(const T &val) {
217  std::fill(begin(), end(), val);
218 }
219 
220 
221 template<class T, int M, int N>
223  std::copy(rhs.begin(), rhs.end(), begin());
224 }
225 
226 
227 template<class T, int M, int N>
229  std::copy(rhs.begin(), rhs.end(), begin());
230  return *this;
231 }
232 
233 
234 template <class T, int M, int N>
235 inline 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 
243 template <class T, int M, int N>
244 inline 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 
252 template <class T, int M, int N>
253 inline int FArray2D<T, M, N>::nrows() const {
254  return M;
255 }
256 
257 
258 template <class T, int M, int N>
259 inline int FArray2D<T, M, N>::ncols() const {
260  return N;
261 }
262 
263 
264 template <class T, int M, int N>
265 inline int FArray2D<T, M, N>::size() const {
266  return SIZE;
267 }
268 
269 
270 template <class T, int M, int N>
272  return data;
273 }
274 
275 
276 template <class T, int M, int N>
278  return data + SIZE;
279 }
280 
281 
282 template <class T, int M, int N>
284  return data;
285 }
286 
287 
288 template <class T, int M, int N>
290  return data + SIZE;
291 }
292 
293 
294 template <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 
303 template <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 
312 template <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 
321 template <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 
330 template <class T, int M, int N>
332  return data + N * r;
333 }
334 
335 
336 template <class T, int M, int N>
338  return data + N * r;
339 }
340 
341 
342 template <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 
351 template <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 
360 template <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 
369 template <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 
378 template <class T, int M, int N>
379 void FArray2D<T, M, N>::getColumn(FArray1D<T, M> &toArray, int c) const {
380  std::copy(col_begin(c), col_end(c), toArray.begin());
381 }
382 
383 
384 template <class T, int M, int N>
385 void 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 
390 template <class T, int M, int N>
391 void 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 
396 template <class T, int M, int N>
397 void 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 
402 template <class T, int M, int N>
403 void 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 
408 template <class T, int M, int N>
409 void 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 
414 template <class T, int M, int N>
415 std::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
T data[M *N]
Definition: FArray2D.h:199
row_iterator operator[](int r)
Get row iterator.
Definition: FArray2D.h:331
const T * const_iterator
Iterator for constant array.
Definition: FArray2D.h:51
Definition: rbendmap.h:8
col_iterator col_end(int c)
Get column iterator.
Definition: FArray2D.h:352
FConstSlice< T, N > const_col_iterator
Iterator for access by columns.
Definition: FArray2D.h:63
FArray2D()
Default constructor.
Definition: FArray2D.h:210
void swapColumns(int c1, int c2)
Exchange columns.
Definition: FArray2D.h:403
void putRow(const FArray1D< T, N > &fromArray, int r)
Store row.
Definition: FArray2D.h:397
void putColumn(const FArray1D< T, M > &fromArray, int c)
Store column.
Definition: FArray2D.h:391
int nrows() const
Get number of rows.
Definition: FArray2D.h:253
void getColumn(FArray1D< T, M > &toArray, int c) const
Fetch column.
Definition: FArray2D.h:379
iterator begin()
Get iterator pointing to beginning of array.
Definition: FArray1D.h:192
iterator begin()
Get beginning of data.
Definition: FArray2D.h:271
T * row_iterator
Iterator for access by rows.
Definition: FArray2D.h:54
FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field)
Definition: Inform.h:104
iterator end()
Get pointer past end of data.
Definition: FArray2D.h:277
Constant version of FSlice.
Definition: FSlice.h:101
int size() const
Get total size.
Definition: FArray2D.h:265
T value_type
The value type of the array.
Definition: FArray2D.h:45
row_iterator row_end(int r)
Get row iterator.
Definition: FArray2D.h:304
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
int ncols() const
Get number of columns.
Definition: FArray2D.h:259
col_iterator col_begin(int c)
Get column iterator.
Definition: FArray2D.h:343
const FArray2D< T, M, N > & operator=(const FArray2D< T, M, N > &)
Assignment.
Definition: FArray2D.h:228
iterator end()
Get iterator pointing past end of array.
Definition: FArray1D.h:198
FSlice< T, N > col_iterator
Iterator for access by columns.
Definition: FArray2D.h:60
T * iterator
Iterator for the array.
Definition: FArray2D.h:48
T & operator()(int r, int c)
Get element.
Definition: FArray2D.h:235
static const int SIZE
Definition: FArray2D.h:202
const T * const_row_iterator
Iterator for access by rows.
Definition: FArray2D.h:57
void swapRows(int r1, int r2)
Exchange rows.
Definition: FArray2D.h:409
Range error.
Definition: CLRangeError.h:33
A templated representation for one-dimensional arrays.
Definition: FArray1D.h:39
An iterator permitting to iterate with a stride different from 1.
Definition: FSlice.h:30
void getRow(FArray1D< T, N > &toArray, int r) const
Fetch row.
Definition: FArray2D.h:385
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
A templated representation for 2-dimensional arrays.
Definition: FArray2D.h:40
row_iterator row_begin(int r)
Get row iterator.
Definition: FArray2D.h:295