/* * File: matrix.h * Summary: Two-dimensional array class. */ #ifndef MATRIX_H #define MATRIX_H template class Matrix { public: Matrix(int width, int height, const Z &initial); Matrix(int width, int height); ~Matrix(); void init(const Z &initial); Z &operator () (int x, int y) { return data[x + y * mwidth]; } const Z &operator () (int x, int y) const { return data[x + y * mwidth]; } int width() const { return mwidth; } int height() const { return mheight; } private: Z *data; int mwidth, mheight, size; }; template Matrix::Matrix(int _width, int _height, const Z &initial) : data(NULL), mwidth(_width), mheight(_height), size(_width * _height) { data = new Z [ size ]; init(initial); } template Matrix::Matrix(int _width, int _height) : data(NULL), mwidth(_width), mheight(_height), size(_width * _height) { data = new Z [ size ]; } template Matrix::~Matrix() { delete [] data; } template void Matrix::init(const Z &initial) { for (int i = 0; i < size; ++i) data[i] = initial; } #endif // MATRIX_H