Hosting courtesy of Sourceforge

SourceForge Logo
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Matrix.h

Go to the documentation of this file.
00001 //
00002 //      Copyright (C) 2002 Robert Renaud
00003 //
00004 //      This program is free software; you can redistribute it and/or
00005 //      modify it under the terms of the GNU General Public License
00006 //      as published by the Free Software Foundation; either version 2
00007 //      of the License, or (at your option) any later version.
00008 //
00009 //      This program is distributed in the hope that it will be useful,
00010 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 //
00013 //      See the GNU General Public License for more details.
00014 //
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 
00020 #ifndef MATRIX_H_
00021 #define MATRIX_H_
00022 
00023 #include "../Config.h"
00024 #include <fstream> // for std::istream
00025 
00026 namespace Util {
00027 
00028         // TODO: implement a Matrix iterator so Util::freeDynamicContainer works on Matrix<stuff*>
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_

Generated on Tue May 21 07:26:51 2002 for BomberLAN by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001