| 1 |
#include "Array2.h" |
| 2 |
#include <sstream> |
| 3 |
|
| 4 |
// Default constructor |
| 5 |
Array2::Array2() : |
| 6 |
_ownData(false), _nrows(0), _ncols(), _buffer(0), _rows(0) |
| 7 |
{ } |
| 8 |
|
| 9 |
// Size/array constructor |
| 10 |
Array2::Array2(int nrows, int ncols, long* data) : |
| 11 |
_ownData(false), _nrows(0), _ncols(), _buffer(0), _rows(0) |
| 12 |
{ |
| 13 |
resize(nrows, ncols, data); |
| 14 |
} |
| 15 |
|
| 16 |
// Copy constructor |
| 17 |
Array2::Array2(const Array2 & source) : |
| 18 |
_nrows(source._nrows), _ncols(source._ncols) |
| 19 |
{ |
| 20 |
_ownData = true; |
| 21 |
allocateMemory(); |
| 22 |
*this = source; |
| 23 |
} |
| 24 |
|
| 25 |
// Destructor |
| 26 |
Array2::~Array2() |
| 27 |
{ |
| 28 |
deallocateMemory(); |
| 29 |
} |
| 30 |
|
| 31 |
// Assignment operator |
| 32 |
Array2 & Array2::operator=(const Array2 & source) |
| 33 |
{ |
| 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) |
| 37 |
{ |
| 38 |
for (int j=0; j < ncols; ++j) |
| 39 |
{ |
| 40 |
(*this)[i][j] = source[i][j]; |
| 41 |
} |
| 42 |
} |
| 43 |
return *this; |
| 44 |
} |
| 45 |
|
| 46 |
// Equals operator |
| 47 |
bool Array2::operator==(const Array2 & other) const |
| 48 |
{ |
| 49 |
if (_nrows != other._nrows) return false; |
| 50 |
if (_ncols != other._ncols) return false; |
| 51 |
for (int i=0; i < _nrows; ++i) |
| 52 |
{ |
| 53 |
for (int j=0; j < _ncols; ++j) |
| 54 |
{ |
| 55 |
if ((*this)[i][j] != other[i][j]) return false; |
| 56 |
} |
| 57 |
} |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
// Length accessors |
| 62 |
int Array2::nrows() const |
| 63 |
{ |
| 64 |
return _nrows; |
| 65 |
} |
| 66 |
|
| 67 |
int Array2::ncols() const |
| 68 |
{ |
| 69 |
return _ncols; |
| 70 |
} |
| 71 |
|
| 72 |
// Resize array |
| 73 |
void Array2::resize(int nrows, int ncols, long* data) |
| 74 |
{ |
| 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; |
| 78 |
deallocateMemory(); |
| 79 |
_nrows = nrows; |
| 80 |
_ncols = ncols; |
| 81 |
if (!data) |
| 82 |
{ |
| 83 |
allocateMemory(); |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
_ownData = false; |
| 88 |
_buffer = data; |
| 89 |
allocateRows(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void Array2::resize(int nrows, int ncols) |
| 94 |
{ |
| 95 |
resize(nrows, ncols, nullptr); |
| 96 |
} |
| 97 |
|
| 98 |
// Set item accessor |
| 99 |
Array1 & Array2::operator[](int i) |
| 100 |
{ |
| 101 |
if (i < 0 || i > _nrows) throw std::out_of_range("Array2 row index out of range"); |
| 102 |
return _rows[i]; |
| 103 |
} |
| 104 |
|
| 105 |
// Get item accessor |
| 106 |
const Array1 & Array2::operator[](int i) const |
| 107 |
{ |
| 108 |
if (i < 0 || i > _nrows) throw std::out_of_range("Array2 row index out of range"); |
| 109 |
return _rows[i]; |
| 110 |
} |
| 111 |
|
| 112 |
// String output |
| 113 |
std::string Array2::asString() const |
| 114 |
{ |
| 115 |
std::stringstream result; |
| 116 |
result << "[ "; |
| 117 |
for (int i=0; i < _nrows; ++i) |
| 118 |
{ |
| 119 |
if (i > 0) result << " "; |
| 120 |
result << (*this)[i].asString(); |
| 121 |
if (i < _nrows-1) result << "," << std::endl; |
| 122 |
} |
| 123 |
result << " ]" << std::endl; |
| 124 |
return result.str(); |
| 125 |
} |
| 126 |
|
| 127 |
// Get view |
| 128 |
void Array2::view(int* nrows, int* ncols, long** data) const |
| 129 |
{ |
| 130 |
*nrows = _nrows; |
| 131 |
*ncols = _ncols; |
| 132 |
*data = _buffer; |
| 133 |
} |
| 134 |
|
| 135 |
// Private methods |
| 136 |
void Array2::allocateMemory() |
| 137 |
{ |
| 138 |
if (_nrows * _ncols == 0) |
| 139 |
{ |
| 140 |
_ownData = false; |
| 141 |
_buffer = 0; |
| 142 |
_rows = 0; |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
_ownData = true; |
| 147 |
_buffer = new long[_nrows*_ncols]; |
| 148 |
allocateRows(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void Array2::allocateRows() |
| 153 |
{ |
| 154 |
_rows = new Array1[_nrows]; |
| 155 |
for (int i=0; i < _nrows; ++i) |
| 156 |
{ |
| 157 |
_rows[i].resize(_ncols, &_buffer[i*_ncols]); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void Array2::deallocateMemory() |
| 162 |
{ |
| 163 |
if (_ownData && _nrows && _ncols && _buffer) |
| 164 |
{ |
| 165 |
delete [] _rows; |
| 166 |
delete [] _buffer; |
| 167 |
} |
| 168 |
_ownData = false; |
| 169 |
_nrows = 0; |
| 170 |
_ncols = 0; |
| 171 |
_buffer = 0; |
| 172 |
_rows = 0; |
| 173 |
} |