00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MATRIX_H_
00021 #define MATRIX_H_
00022
00023 #include "../Config.h"
00024 #include <fstream>
00025
00026 namespace Util {
00027
00028
00031 template <class element> class Matrix {
00032 public:
00035 Matrix(int rows, int cols);
00036
00038 Matrix(int rows, int cols, const element& defElement);
00039
00040 ~Matrix();
00041
00049 element& operator()(int row, int col) { return elemList[numCols()*row + col]; }
00050 const element& operator()(int row, int col) const { return elemList[numCols()*row + col]; }
00051
00054 int numRows() const { return rows; }
00055
00058 int numCols() const { return cols; }
00059 protected:
00060 int rows;
00061 int cols;
00062 element* elemList;
00063 private:
00064 Matrix(const Matrix& COPY_CTOR_NOT_IMPLEMTED);
00065 void operator = (const Matrix& ASSIGNMENT_NOT_YET_IMPLEMTED);
00066 };
00067
00071 template <class T> void operator >> (std::istream& instream, Matrix<T>& mat);
00072
00073 template <class element>
00074 Matrix<element>::Matrix(int numRows, int numCols, const element& defElement){
00075 if (numRows < 0) numRows = 0;
00076 if (numCols < 0) numCols = 0;
00077
00078 rows = numRows;
00079 cols = numCols;
00080
00081 elemList = new element[rows*cols];
00082
00083 for (int i = 0; i < rows; i++) {
00084 for (int j = 0; j < cols; j++) {
00085 this->operator()(i,j) = defElement;
00086 }
00087 }
00088 }
00089
00090 template <class element> Matrix<element>::Matrix(int rows, int cols) :
00091 Matrix<element>(rows, cols, element())
00092 { }
00093
00094 template <class element> Matrix<element>::~Matrix() {
00095 delete[] elemList;
00096 }
00097
00098 template <class T> void operator >> (std::istream& instream, Matrix<T>& mat) {
00099 for (int j = 0; j < mat.numRows(); j++) {
00100 for (int i = 0; i < mat.numCols(); i++) {
00101 instream >> mat(i,j);
00102 }
00103 }
00104 }
00105
00106 };
00107
00108 #endif // ifdef MATRIX_H_