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