6
_ownData(false), _nrows(0), _ncols(), _buffer(0), _rows(0)
9
// Size/array constructor
10
Array2::Array2(int nrows, int ncols, long* data) :
11
_ownData(false), _nrows(0), _ncols(), _buffer(0), _rows(0)
13
resize(nrows, ncols, data);
17
Array2::Array2(const Array2 & source) :
18
_nrows(source._nrows), _ncols(source._ncols)
31
// Assignment operator
32
Array2 & Array2::operator=(const Array2 & source)
34
int nrows = _nrows < source._nrows ? _nrows : source._nrows;
35
int ncols = _ncols < source._ncols ? _ncols : source._ncols;
36
for (int i=0; i < nrows; ++i)
38
for (int j=0; j < ncols; ++j)
40
(*this)[i][j] = source[i][j];
47
bool Array2::operator==(const Array2 & other) const
49
if (_nrows != other._nrows) return false;
50
if (_ncols != other._ncols) return false;
51
for (int i=0; i < _nrows; ++i)
53
for (int j=0; j < _ncols; ++j)
55
if ((*this)[i][j] != other[i][j]) return false;
62
int Array2::nrows() const
67
int Array2::ncols() const
73
void Array2::resize(int nrows, int ncols, long* data)
75
if (nrows < 0) throw std::invalid_argument("Array2 nrows less than 0");
76
if (ncols < 0) throw std::invalid_argument("Array2 ncols less than 0");
77
if (nrows == _nrows && ncols == _ncols) return;
94
Array1 & Array2::operator[](int i)
96
if (i < 0 || i > _nrows) throw std::out_of_range("Array2 row index out of range");
101
const Array1 & Array2::operator[](int i) const
103
if (i < 0 || i > _nrows) throw std::out_of_range("Array2 row index out of range");
108
std::string Array2::asString() const
110
std::stringstream result;
112
for (int i=0; i < _nrows; ++i)
114
if (i > 0) result << " ";
115
result << (*this)[i].asString();
116
if (i < _nrows-1) result << "," << std::endl;
118
result << " ]" << std::endl;
123
void Array2::view(int* nrows, int* ncols, long** data) const
131
void Array2::allocateMemory()
133
if (_nrows * _ncols == 0)
142
_buffer = new long[_nrows*_ncols];
147
void Array2::allocateRows()
149
_rows = new Array1[_nrows];
150
for (int i=0; i < _nrows; ++i)
152
_rows[i].resize(_ncols, &_buffer[i*_ncols]);
156
void Array2::deallocateMemory()
158
if (_ownData && _nrows*_ncols && _buffer)