Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _APMATRIX_H
- #define _APMATRIX_H
- #include "apvector.h"
- // *******************************************************************
- // Last Revised: 8/14/98
- // changed abort() to exit(1), dhj
- //
- // APCS matrix class
- //
- // extends apvector.h to two dimensional "safe" (range-checked) matrices
- // examples are given at the end of this file
- // *******************************************************************
- template <class itemType>
- class apmatrix
- {
- public:
- // constructors/destructor
- apmatrix( ); // default size 0 x 0
- apmatrix( int rows, int cols ); // size rows x cols
- apmatrix( int rows, int cols,
- const itemType & fillValue ); // all entries == fillValue
- apmatrix( const apmatrix & mat ); // copy constructor
- ~apmatrix( ); // destructor
- // assignment
- const apmatrix & operator = ( const apmatrix & rhs );
- // accessors
- int numrows( ) const; // number of rows
- int numcols( ) const; // number of columns
- // indexing
- const apvector<itemType> & operator [ ] ( int k ) const; // range-checked indexing
- apvector<itemType> & operator [ ] ( int k ); // range-checked indexing
- // modifiers
- void resize( int newRows, int newCols ); // resizes matrix to newRows x newCols
- // (can result in losing values)
- private:
- int myRows; // # of rows (capacity)
- int myCols; // # of cols (capacity)
- apvector<apvector<itemType> > myMatrix; // the matrix of items
- };
- // *******************************************************************
- // Specifications for matrix functions
- //
- // To use this class, itemType must satisfy the same constraints
- // as forvector class.
- //
- // Any violation of a function's precondition will result in an error message
- // followed by a call to exit.
- //
- // constructors/destructor
- //
- // apmatrix( );
- // postcondition: matrix of size 0x0 is constructed, and therefore
- // will need to be resized later
- //
- // apmatrix( int rows, int cols );
- // precondition: 0 <= rows and 0 <= cols
- // postcondition: matrix of size rows x cols is constructed
- //
- // apmatrix( int rows, int cols, const itemType & fillValue );
- // precondition: 0 <= rows and 0 <= cols
- // postcondition: matrix of size rows x cols is constructed
- // all entries are set by assignment to fillValue after
- // default construction
- //
- // apmatrix( const apmatrix<itemType> & mat );
- // postcondition: matrix is a copy of mat
- //
- // ~apmatrix( );
- // postcondition: matrix is destroyed
- //
- // assignment
- //
- // const apmatrix & operator = ( const apmatrix & rhs );
- // postcondition: normal assignment via copying has been performed
- // (if matrix and rhs were different sizes, matrix has
- // been resized to match the size of rhs)
- //
- // accessors
- //
- // int numrows( ) const;
- // postcondition: returns number of rows
- //
- // int numcols( ) const;
- // postcondition: returns number of columns
- //
- // indexing
- //
- // const apvector<itemType> & operator [ ] ( int k ) const;
- // precondition: 0 <= k < number of rows
- // postcondition: returns k-th row
- //
- // apvector<itemType> & operator [ ] ( int k );
- // precondition: 0 <= k < number of rows
- // postcondition: returns k-th row
- //
- // modifiers
- //
- // void resize( int newRows, int newCols );
- // precondition: matrix size is rows X cols,
- // 0 <= newRows and 0 <= newCols
- // postcondition: matrix size is newRows X newCols;
- // for each 0 <= j <= min(rows,newRows) and
- // for each 0 <= k <= min(cols,newCols), matrix[j][k] is
- // a copy of the original; other elements of matrix are
- // initialized using the default constructor for itemType
- // Note: if newRows < rows or newCols < cols,
- // elements may be lost
- //
- // Examples of use:
- //
- // apmatrix<double> dmat( 100, 80 ); // 100 x 80 matrix of doubles
- // apmatrix<double> dzmat( 100, 80, 0.0 ); // initialized to 0.0
- // apmatrix<apstring> smat( 300, 1 ); // 300 strings
- // apmatrix<int> imat; // has room for 0 ints
- #include "apmatrix.cpp"
- #endif
- ----------------------------------------------------------------------------------
- // *******************************************************************
- // Last Revised: 8/14/98
- // abort changed to exit, dhj
- //
- // September 1, 1997 -- APCS matrix class IMPLEMENTATION
- //
- // see matrix.h for complete documentation of functions
- //
- // extends vector class to two-dimensional matrices
- // *******************************************************************
- #include "apmatrix.h"
- #include <stdlib.h>
- #include <iostream>
- template <class itemType>
- apmatrix<itemType>::apmatrix()
- : myRows(0),
- myCols(0),
- myMatrix(0)
- // postcondition: matrix of size 0x0 is constructed, and therefore
- // will need to be resized later
- {
- }
- template <class itemType>
- apmatrix<itemType>::apmatrix(int rows,int cols)
- : myRows(rows),
- myCols(cols),
- myMatrix(rows)
- // precondition: 0 <= rows and 0 <= cols
- // postcondition: matrix of size rows x cols is constructed
- {
- int k;
- for(k=0; k < rows; k++)
- {
- myMatrix[k].resize(cols);
- }
- }
- template <class itemType>
- apmatrix<itemType>::apmatrix(int rows, int cols, const itemType & fillValue)
- : myRows(rows),
- myCols(cols),
- myMatrix(rows)
- // precondition: 0 <= rows and 0 <= cols
- // postcondition: matrix of size rows x cols is constructed
- // all entries are set by assignment to fillValue after
- // default construction
- //
- {
- int j,k;
- for(j=0; j < rows; j++)
- {
- myMatrix[j].resize(cols);
- for(k=0; k < cols; k++)
- {
- myMatrix[j][k] = fillValue;
- }
- }
- }
- template <class itemType>
- apmatrix<itemType>::apmatrix(const apmatrix<itemType> & mat)
- : myRows(mat.myRows),
- myCols(mat.myCols),
- myMatrix(mat.myRows)
- // postcondition: matrix is a copy of mat
- {
- int k;
- // copy elements
- for(k = 0; k < myRows; k++)
- {
- // cast to avoid const problems (const -> non-const)
- myMatrix[k] = (apvector<itemType> &) mat.myMatrix[k];
- }
- }
- template <class itemType>
- apmatrix<itemType>::~apmatrix ()
- // postcondition: matrix is destroyed
- {
- // vector destructor frees everything
- }
- template <class itemType>
- const apmatrix<itemType> &
- apmatrix<itemType>::operator = (const apmatrix<itemType> & rhs)
- // postcondition: normal assignment via copying has been performed
- // (if matrix and rhs were different sizes, matrix has
- // been resized to match the size of rhs)
- {
- if (this != &rhs) // don't assign to self!
- {
- myMatrix.resize(rhs.myRows); // resize to proper # of rows
- myRows = rhs.myRows; // set dimensions
- myCols = rhs.myCols;
- // copy rhs
- int k;
- for(k=0; k < myRows; k++)
- {
- myMatrix[k] = rhs.myMatrix[k];
- }
- }
- return *this;
- }
- template <class itemType>
- int apmatrix<itemType>::numrows() const
- // postcondition: returns number of rows
- {
- return myRows;
- }
- template <class itemType>
- int apmatrix<itemType>::numcols() const
- // postcondition: returns number of columns
- {
- return myCols;
- }
- template <class itemType>
- void apmatrix<itemType>::resize(int newRows, int newCols)
- // precondition: matrix size is rows X cols,
- // 0 <= newRows and 0 <= newCols
- // postcondition: matrix size is newRows X newCols;
- // for each 0 <= j <= min(rows,newRows) and
- // for each 0 <= k <= min(cols,newCols), matrix[j][k] is
- // a copy of the original; other elements of matrix are
- // initialized using the default constructor for itemType
- // Note: if newRows < rows or newCols < cols,
- // elements may be lost
- //
- {
- int k;
- myMatrix.resize(newRows);
- for(k=0; k < newRows; k++)
- {
- myMatrix[k].resize(newCols);
- }
- myRows = newRows;
- myCols = newCols;
- }
- template <class itemType>
- const apvector<itemType> &
- apmatrix<itemType>::operator [] (int k) const
- // precondition: 0 <= k < number of rows
- // postcondition: returns k-th row
- {
- if (k < 0 || myRows <= k)
- {
- cerr << "Illegal matrix index: " << k << " max index = ";
- cerr << myRows-1 << endl;
- exit(1);
- }
- return myMatrix[k];
- }
- template <class itemType>
- apvector<itemType> &
- apmatrix<itemType>::operator [] (int k)
- // precondition: 0 <= k < number of rows
- // postcondition: returns k-th row
- {
- if (k < 0 || myRows <= k)
- {
- cerr << "Illegal matrix index: " << k << " max index = ";
- cerr << myRows-1 << endl;
- exit(1);
- }
- return myMatrix[k];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement