Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "matrix.h"
00020
00021 #include <cassert>
00022 #include <cstdlib>
00023 #include <algorithm>
00024
00025 template <class T>
00026 Matrix<T>::Matrix() {
00027 m_rows = 0;
00028 m_columns = 0;
00029 m_matrix = NULL;
00030 }
00031
00032
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 template <class T>
00057 Matrix<T>::Matrix(const Matrix<T> &other) {
00058 if ( other.m_matrix != NULL ) {
00059
00060 m_matrix = NULL;
00061 resize(other.m_rows, other.m_columns);
00062 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00063 for ( unsigned int j = 0 ; j < m_columns ; j++ ) {
00064 m_matrix[i][j] = other.m_matrix[i][j];
00065 }
00066 }
00067 } else {
00068 m_matrix = NULL;
00069 m_rows = 0;
00070 m_columns = 0;
00071 }
00072 }
00073
00074 template <class T>
00075 Matrix<T>::Matrix(unsigned int rows, unsigned int columns) {
00076 m_matrix = NULL;
00077 resize(rows, columns);
00078 }
00079
00080 template <class T>
00081 Matrix<T> &
00082 Matrix<T>::operator= (const Matrix<T> &other) {
00083 if ( other.m_matrix != NULL ) {
00084
00085 resize(other.m_rows, other.m_columns);
00086 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00087 for ( unsigned int j = 0 ; j < m_columns ; j++ ) {
00088 m_matrix[i][j] = other.m_matrix[i][j];
00089 }
00090 }
00091 } else {
00092
00093 for ( unsigned int i = 0 ; i < m_columns ; i++ ) {
00094 delete [] m_matrix[i];
00095 }
00096
00097 delete [] m_matrix;
00098
00099 m_matrix = NULL;
00100 m_rows = 0;
00101 m_columns = 0;
00102 }
00103
00104 return *this;
00105 }
00106
00107 template <class T>
00108 Matrix<T>::~Matrix() {
00109 if ( m_matrix != NULL ) {
00110
00111 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00112 delete [] m_matrix[i];
00113 }
00114
00115 delete [] m_matrix;
00116 }
00117 m_matrix = NULL;
00118 }
00119
00120 template <class T>
00121 void
00122 Matrix<T>::resize(unsigned int rows, unsigned int columns, T default_value) {
00123 assert ( rows > 0 && columns > 0 && "Columns and rows must exist." );
00124
00125 if ( m_matrix == NULL ) {
00126
00127 m_matrix = new T*[rows];
00128 for ( unsigned int i = 0 ; i < rows ; i++ ) {
00129 m_matrix[i] = new T[columns];
00130 }
00131
00132 m_rows = rows;
00133 m_columns = columns;
00134 clear();
00135 } else {
00136
00137 T **new_matrix;
00138
00139 new_matrix = new T*[rows];
00140 for ( unsigned int i = 0 ; i < rows ; i++ ) {
00141 new_matrix[i] = new T[columns];
00142 for ( unsigned int j = 0 ; j < columns ; j++ ) {
00143 new_matrix[i][j] = default_value;
00144 }
00145 }
00146
00147
00148 unsigned int minrows = std::min<unsigned int>(rows, m_rows);
00149 unsigned int mincols = std::min<unsigned int>(columns, m_columns);
00150 for ( unsigned int x = 0 ; x < minrows ; x++ ) {
00151 for ( unsigned int y = 0 ; y < mincols ; y++ ) {
00152 new_matrix[x][y] = m_matrix[x][y];
00153 }
00154 }
00155
00156
00157 if ( m_matrix != NULL ) {
00158 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00159 delete [] m_matrix[i];
00160 }
00161
00162 delete [] m_matrix;
00163 }
00164
00165 m_matrix = new_matrix;
00166 }
00167
00168 m_rows = rows;
00169 m_columns = columns;
00170 }
00171
00172 template <class T>
00173 void
00174 Matrix<T>::clear() {
00175 assert( m_matrix != NULL );
00176
00177 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00178 for ( unsigned int j = 0 ; j < m_columns ; j++ ) {
00179 m_matrix[i][j] = 0;
00180 }
00181 }
00182 }
00183
00184 template <class T>
00185 inline T&
00186 Matrix<T>::operator ()(unsigned int x, unsigned int y) {
00187 assert ( x < m_rows );
00188 assert ( y < m_columns );
00189 assert ( m_matrix != NULL );
00190 return m_matrix[x][y];
00191 }
00192
00193
00194 template <class T>
00195 inline const T&
00196 Matrix<T>::operator ()(unsigned int x, unsigned int y) const {
00197 assert ( x < m_rows );
00198 assert ( y < m_columns );
00199 assert ( m_matrix != NULL );
00200 return m_matrix[x][y];
00201 }
00202
00203
00204 template <class T>
00205 const T
00206 Matrix<T>::min() const {
00207 assert( m_matrix != NULL );
00208 assert ( m_rows > 0 );
00209 assert ( m_columns > 0 );
00210 T min = m_matrix[0][0];
00211
00212 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00213 for ( unsigned int j = 0 ; j < m_columns ; j++ ) {
00214 min = std::min<T>(min, m_matrix[i][j]);
00215 }
00216 }
00217
00218 return min;
00219 }
00220
00221
00222 template <class T>
00223 const T
00224 Matrix<T>::max() const {
00225 assert( m_matrix != NULL );
00226 assert ( m_rows > 0 );
00227 assert ( m_columns > 0 );
00228 T max = m_matrix[0][0];
00229
00230 for ( unsigned int i = 0 ; i < m_rows ; i++ ) {
00231 for ( unsigned int j = 0 ; j < m_columns ; j++ ) {
00232 max = std::max<T>(max, m_matrix[i][j]);
00233 }
00234 }
00235
00236 return max;
00237 }