matrix.cpp
Go to the documentation of this file.
00001 /*
00002  *   Copyright (c) 2007 John Weaver
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation; either version 2 of the License, or
00007  *   (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.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program; if not, write to the Free Software
00016  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017  */
00018 
00019 #include "matrix.h"
00020 
00021 #include <cassert>
00022 #include <cstdlib>
00023 #include <algorithm>
00024 
00025 /*export*/ template <class T>
00026 Matrix<T>::Matrix() {
00027   m_rows = 0;
00028   m_columns = 0;
00029   m_matrix = NULL;
00030 }
00031 
00032 //
00034 //Matrix<T>::Matrix(std::initializer_list<std::initializer_list<T>> init) {
00035 //  m_matrix = NULL;
00036 //  m_rows = init.size();
00037 //  if ( m_rows == 0 ) {
00038 //    m_columns = 0;
00039 //  } else {
00040 //    m_columns = init.begin()->size();
00041 //    if ( m_columns > 0 ) {
00042 //      resize(m_rows, m_columns);
00043 //    }
00044 //  }
00045 //
00046 //  int i = 0, j;
00047 //  for ( auto row = init.begin() ; row != init.end() ; ++row, ++i ) {
00048 //    assert ( row->size() == m_columns && "All rows must have the same number of columns." );
00049 //    j = 0;
00050 //    for ( auto value = row->begin() ; value != row->end() ; ++value, ++j ) {
00051 //      m_matrix[i][j] = *value;
00052 //    }
00053 //  }
00054 //}
00055 
00056 /*export*/ template <class T>
00057 Matrix<T>::Matrix(const Matrix<T> &other) {
00058   if ( other.m_matrix != NULL ) {
00059     // copy arrays
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 /*export*/ template <class T>
00075 Matrix<T>::Matrix(unsigned int rows, unsigned int columns) {
00076   m_matrix = NULL;
00077   resize(rows, columns);
00078 }
00079 
00080 /*export*/ template <class T>
00081 Matrix<T> &
00082 Matrix<T>::operator= (const Matrix<T> &other) {
00083   if ( other.m_matrix != NULL ) {
00084     // copy arrays
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     // free arrays
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 /*export*/ template <class T>
00108 Matrix<T>::~Matrix() {
00109   if ( m_matrix != NULL ) {
00110     // free arrays
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 /*export*/ 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     // alloc arrays
00127     m_matrix = new T*[rows]; // rows
00128     for ( unsigned int i = 0 ; i < rows ; i++ ) {
00129       m_matrix[i] = new T[columns]; // columns
00130     }
00131 
00132     m_rows = rows;
00133     m_columns = columns;
00134     clear();
00135   } else {
00136     // save array pointer
00137     T **new_matrix;
00138     // alloc new arrays
00139     new_matrix = new T*[rows]; // rows
00140     for ( unsigned int i = 0 ; i < rows ; i++ ) {
00141       new_matrix[i] = new T[columns]; // columns
00142       for ( unsigned int j = 0 ; j < columns ; j++ ) {
00143         new_matrix[i][j] = default_value;
00144       }
00145     }
00146 
00147     // copy data from saved pointer to new arrays
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     // delete old arrays
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 /*export*/ 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 /*export*/ 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 /*export*/ 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 /*export*/ 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 /*export*/ 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 }


explorer
Author(s): Daniel Neuhold , Torsten Andre
autogenerated on Thu Jun 6 2019 20:59:53