1 #ifndef CLASSIC_Array2D_HH
2 #define CLASSIC_Array2D_HH
234 rows(0), cols(0), len(0), data(0)
240 rows(array.rows), cols(array.cols), len(array.len), data(new
T[len]) {
247 rows(r), cols(c), len(r *c), data(new
T[len])
253 rows(r), cols(c), len(r *c), data(new
T[len]) {
271 data =
new T[rhs.
len];
276 std::copy(rhs.
begin(), rhs.
end(), begin());
284 if(r < 0 || r >= rows || c < 0 || c >= ncols()) {
285 throw CLRangeError(
"Array2d::operator()",
"Index out of range.");
287 return data[cols*r+
c];
293 if(r < 0 || r >= rows || c < 0 || c >= ncols()) {
294 throw CLRangeError(
"Array2d::operator()",
"Index out of range.");
296 return data[cols*r+
c];
345 throw CLRangeError(
"Array2D::row_begin()",
"Row index out of range.");
347 return &data[cols*r];
354 throw CLRangeError(
"Array2D::row_end()",
"Row index out of range.");
356 return &data[cols*(r+1)];
363 throw CLRangeError(
"Array2D::row_begin()",
"Row index out of range.");
365 return &data[cols*r];
372 throw CLRangeError(
"Array2D::row_end()",
"Row index out of range.");
374 return &data[cols*(r+1)];
380 return &data[cols*r];
386 return &data[cols*r];
393 throw CLRangeError(
"Array2D::col_begin()",
"Column index out of range.");
402 throw CLRangeError(
"Array2D::col_end()",
"Column index out of range.");
411 throw CLRangeError(
"Array2D::col_begin()",
"Column index out of range.");
420 throw CLRangeError(
"Array2D::col_end()",
"Column index out of range.");
429 std::copy(col_begin(c), col_end(c), toArray.
begin());
436 std::copy(row_begin(r), row_end(r), toArray.
begin());
442 if(fromArray.
size() != rows) {
443 throw SizeError(
"Array2D::putColumn()",
"Dimensions inconsistent.");
445 std::copy(fromArray.
begin(), fromArray.
end(), col_begin(c));
451 if(fromArray.
size() != cols) {
452 throw SizeError(
"Array2D::putRow()",
"Dimensions inconsistent.");
454 std::copy(fromArray.
begin(), fromArray.
end(), row_begin(r));
460 std::swap_ranges(col_begin(col1), col_end(col1), col_begin(col2));
466 std::swap_ranges(row_begin(row1), row_end(row1), row_begin(row2));
471 std::ostream &operator<<(std::ostream &os, const Array2D<T> &v) {
472 for(
int i = 0; i < v.nrows(); ++i) {
473 for(
int j = 0; j < v.ncols(); ++j) {
474 os << v(i, j) <<
" ";
482 #endif // CLASSIC_Array2D_HH
T * row_iterator
The iterator type for access by rows.
void swapColumns(int c1, int c2)
Exchange columns.
Array2D< T > & operator=(const Array2D< T > &)
iterator begin()
Get pointer to beginning of data.
Iterator for array slice.
col_iterator col_begin(int c)
Get column iterator.
iterator end()
Get end of data.
int ncols() const
Get number of columns.
col_iterator col_end(int c)
Get column iterator.
int size() const
Get array size.
const T * const_row_iterator
The iterator type for access by rows for a constant array.
void putRow(const Array1D< T > &fromArray, int r)
Store row.
T * iterator
The iterator type for sequential access to all elements.
const T * const_iterator
The iterator type for sequential access to all elements.
SliceIterator< T > col_iterator
The iterator type for access by columns.
iterator begin()
Get beginning of data.
T value_type
The value type of the array.
Iterator for array slice.
row_iterator row_begin(int r)
Get row iterator.
constexpr double c
The velocity of light in m/s.
void getColumn(Array1D< T > &toArray, int c) const
Fetch column.
iterator end()
Get pointer past end of data.
T & operator()(int r, int c)
Get element reference.
int size() const
Get total size (rows times columns).
ConstSliceIterator< T > const_col_iterator
The iterator type for access by columns for a constant array.
void getRow(Array1D< T > &toArray, int r) const
Fetch row.
Array2D()
Default constructor.
row_iterator row_end(int r)
Get row iterator.
int nrows() const
Get number of rows.
void swapRows(int row1, int row2)
Exchange rows.
Inform & endl(Inform &inf)
row_iterator operator[](int r)
Get row iterator.
void putColumn(const Array1D< T > &fromArray, int c)
Store column.