1 /* 2 * tMatrix.h 3 * Avida 4 * 5 * Called "tMatrix.hh" prior to 12/7/05. 6 * Copyright 1999-2011 Michigan State University. All rights reserved. 7 * Copyright 1993-2003 California Institute of Technology. 8 * 9 * 10 * This file is part of Avida. 11 * 12 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 13 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 * 15 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License along with Avida. 19 * If not, see <http://www.gnu.org/licenses/>. 20 * 21 */ 22 23 #ifndef tMatrix_h 24 #define tMatrix_h 25 26 /* 27 Matrix Templates 28 29 Constructor: 30 tMatrix( int rows, int cols ) 31 tMatrix( const tMatrix & rhs ) 32 33 Interface: 34 tMatrix & operator= ( const tMatrix & rhs ) 35 36 unsigned int GetNumRows () const 37 unsigned int GetNumCols () const 38 returns the size of the array 39 40 const T& ElementAt ( const int row, col ) const 41 T& ElementAt ( const int row, col ) 42 const T& operator() ( const int row, col ) const 43 T& operator() ( const int row, col ) 44 return the element at position in the matrix 45 46 const tArray<T>& operator[] ( const int row ) const 47 tArray<T>& operator[] ( const int row ) 48 return the array at the row in the matrix. 49 50 51 */ 52 53 #include "tArray.h" 54 55 #include <cassert> 56 57 /** 58 * This class provides a matrix template. 59 **/ 60 61 template <class T> class tMatrix { 62 protected: 63 // Internal Variables 64 tArray<T> * data; // Data Elements 65 int num_rows; 66 public: GetNumRows()67 int GetNumRows() const { return num_rows; } GetNumCols()68 int GetNumCols() const { return data[0].GetSize(); } 69 ResizeClear(const int _rows,const int _cols)70 void ResizeClear(const int _rows, const int _cols){ 71 if (_rows != GetNumRows()) { 72 num_rows = _rows; 73 assert(_rows > 0); // Invalid size specified for matrix resize 74 if( data != NULL ) delete [] data; // remove old data if exists 75 data = new tArray<T>[_rows]; // Allocate block for data 76 assert(data != NULL); // Memory Allocation Error: Out of Memory? 77 } 78 for (int i = 0; i < GetNumRows(); i++) data[i].ResizeClear(_cols); 79 } 80 Resize(int _rows,int _cols)81 void Resize(int _rows, int _cols) { 82 // Rows and cols must be > 0 83 assert(_rows > 0 && _cols > 0); // Invalid size specified for matrix resize 84 85 if( data != NULL ) { 86 tArray<T> * new_data = new tArray<T>[_rows]; 87 for (int i = 0; i < GetNumRows() && i < _rows; i++) { 88 new_data[i] = data[i]; 89 } 90 delete [] data; // remove old data if exists 91 data = new_data; 92 } else { 93 data = new tArray<T>[_rows]; // Allocate block for data 94 } 95 assert(data != NULL); // Memory Allocation Error: Out of Memory? 96 num_rows = _rows; 97 98 for (int i = 0; i < _rows; i++) data[i].Resize(_cols); 99 } 100 ElementAt(int _row,int _col)101 T & ElementAt(int _row, int _col) { return data[_row][_col]; } ElementAt(int _row,int _col)102 const T & ElementAt(int _row, int _col) const { return data[_row][_col]; } 103 operator()104 T & operator()(int _r, int _c) { return ElementAt(_r, _c); } operator()105 const T & operator()(int _r, int _c) const { return ElementAt(_r, _c); } 106 107 tArray<T> & operator[](int row) { return data[row]; } 108 const tArray<T> & operator[](int row) const { return data[row]; } 109 SetAll(const T & value)110 void SetAll(const T & value) { 111 for (int i = 0; i < num_rows; i++) { 112 data[i].SetAll(value); 113 } 114 } 115 116 117 public: 118 // Default Constructor tMatrix()119 explicit tMatrix() : data(NULL), num_rows(0) { ResizeClear(1,1); } 120 121 // Constructor tMatrix(const int _rows,const int _cols)122 explicit tMatrix(const int _rows, const int _cols) : 123 data(NULL), num_rows(0) { 124 ResizeClear(_rows, _cols); 125 } 126 127 // Assingment Operator 128 tMatrix& operator= (const tMatrix<T>& rhs) { 129 if( GetNumRows() != rhs.GetNumRows() || GetNumCols() != rhs.GetNumCols()) { 130 ResizeClear(rhs.GetNumRows(), rhs.GetNumCols()); 131 } 132 for (int row = 0; row < GetNumRows(); row++) { 133 for (int col = 0; col < GetNumCols(); col++) { 134 data[row][col] = rhs.data[row][col]; 135 } 136 } 137 return *this; 138 } 139 tMatrix(const tMatrix & rhs)140 tMatrix(const tMatrix& rhs) : data(NULL), num_rows(0) { this->operator=(rhs); } 141 142 // Destructor ~tMatrix()143 virtual ~tMatrix() { if (data != NULL) delete [] data; } 144 }; 145 146 #endif 147