matrix.hpp

    1: #ifndef MATRIX_HPP
    2: #define MATRIX_HPP
    3: /*----------------------------------------------------------------------------
    4: 
    5:   Author:    Andy Rushton
    6:   Copyright: (c) Andy Rushton, 2004
    7:   License:   BSD License, see ../docs/license.html
    8: 
    9:   General-purpose 2D matrix data structure 
   10: 
   11: ------------------------------------------------------------------------------*/
   12: #include "os_fixes.hpp"
   13: #include "textio.hpp"
   14: #include "persistent.hpp"
   15: 
   16: ////////////////////////////////////////////////////////////////////////////////
   17: 
   18: template<typename T> class matrix
   19: {
   20: public:
   21:   matrix(unsigned rows = 0, unsigned cols = 0, const T& fill = T());
   22:   ~matrix(void);
   23: 
   24:   matrix(const matrix&);
   25:   matrix& operator =(const matrix&);
   26: 
   27:   void resize(unsigned rows, unsigned cols, const T& fill = T());
   28: 
   29:   unsigned rows(void) const;
   30:   unsigned columns(void) const;
   31: 
   32:   void erase(const T& fill = T());
   33:   void erase(unsigned row, unsigned col, const T& fill = T());
   34:   void insert(unsigned row, unsigned col, const T&);
   35:   const T& item(unsigned row, unsigned col) const;
   36:   T& item(unsigned row, unsigned col);
   37:   const T& operator()(unsigned row, unsigned col) const;
   38:   T& operator()(unsigned row, unsigned col);
   39: 
   40:   void fill(const T& item = T());
   41:   void fill_column(unsigned col, const T& item = T());
   42:   void fill_row(unsigned row, const T& item = T());
   43:   void fill_leading_diagonal(const T& item = T());
   44:   void fill_trailing_diagonal(const T& item = T());
   45:   void make_identity(const T& one, const T& zero = T());
   46: 
   47:   void transpose(void);
   48: 
   49:   // persistence routines
   50: 
   51:   void dump(dump_context&) const throw(persistent_dump_failed);
   52:   void restore(restore_context&) throw(persistent_restore_failed);
   53: 
   54: private:
   55:   unsigned m_rows;
   56:   unsigned m_cols;
   57:   T** m_data;
   58: };
   59: 
   60: ////////////////////////////////////////////////////////////////////////////////
   61: 
   62: template<typename T> otext& print(otext& str, const matrix<T>& mat, unsigned indent = 0);
   63: template<typename T> otext& operator << (otext& str, const matrix<T>& mat);
   64: 
   65: ////////////////////////////////////////////////////////////////////////////////
   66: // non-member versions of the persistence functions
   67: 
   68: template<typename T>
   69: void dump_matrix(dump_context& str, const matrix<T>& data) throw(persistent_dump_failed);
   70: 
   71: template<typename T>
   72: void restore_matrix(restore_context& str, matrix<T>& data) throw(persistent_restore_failed);
   73: 
   74: ////////////////////////////////////////////////////////////////////////////////
   75: 
   76: #include "matrix.tpp"
   77: #endif