FE_matrix.h
上传用户:italyroyal
上传日期:2013-05-06
资源大小:473k
文件大小:3k
- ///////////////////////////////////////////////////////////////////////////////
- // This is a part of the Feature program.
- // Version: 1.0
- // Date: February 22, 2003
- // Programmer: Oh-Wook Kwon
- // Copyright(c) 2003 Oh-Wook Kwon. All rights reserved. owkwon@ucsd.edu
- ///////////////////////////////////////////////////////////////////////////////
- // ------------------------------------------------------------------------
- // zero-based matrix using vector container
- // ------------------------------------------------------------------------
- #ifndef _FE_MATRIX_H_
- #define _FE_MATRIX_H_
- #ifdef min
- #undef min
- #endif
- #ifdef max
- #undef max
- #endif
- #include <vector>
- using namespace std;
- #pragma warning(disable:4786)
- template <typename T>
- class FeMatrix : public vector<vector<T> > {
- public:
- int m,n;
- FeMatrix(int m_=0,int n_=0,const T& v=T());
- virtual ~FeMatrix();
- FeMatrix<T>& operator=(const FeMatrix<T>& A);
- FeMatrix<T>& operator=(const T& a);
- int Resize(int m, int n);
- };
- // ------------------------------------------------------------------------
- // Matrix implementation
- // ------------------------------------------------------------------------
- #ifndef local_max
- #define local_max(a, b) (((a) > (b)) ? (a) : (b))
- #endif
- #ifndef local_min
- #define local_min(a, b) (((a) < (b)) ? (a) : (b))
- #endif
- /////////////////////////////
- // constructor and destructor
- /////////////////////////////
- template <typename T> FeMatrix<T>::FeMatrix(int m_, int n_, const T& v_) : m(m_), n(n_) {
- int i;
- (*this).resize(m);
- for(i=0;i<m;i++) (*this)[i].resize(n);
- for(i=0;i<m;i++) for(int j=0;j<n;j++) (*this)[i][j]=v_;
- }
- template <typename T> inline FeMatrix<T>::~FeMatrix() {
- }
- /////////////////////////////
- // operator overloading
- /////////////////////////////
- template <typename T> inline FeMatrix<T>& FeMatrix<T>::operator=(const FeMatrix<T>& A) {
- if(this == &A) return *this;
- if ( A.n != n || A.m != m) Resize(A.m, A.n);
- //for(int i=0;i<m;i++) for(int j=0;j<n;j++) (*this)[i][j]=A[i][j];
- for(int i=0;i<m;i++) (*this)[i]=A[i];
- return *this;
- }
- template <typename T> FeMatrix<T>& FeMatrix<T>::operator=(const T& a) {
- for(int i=0; i<m; ++i) for(int j=0;j<n; ++j) (*this)[i][j] = a;
- return *this;
- }
- /////////////////////////////
- // member functions
- /////////////////////////////
- template <typename T> int FeMatrix<T>::Resize(int new_m, int new_n) {
- if(new_m==m && new_n==n) return 1;
- // must preserve data
- int i;
- for(i=0;i<local_min(m,new_m);i++) { (*this)[i].resize(new_n); }
- (*this).resize(new_m);
- for(i=m;i<new_m;i++) { (*this)[i].resize(new_n); }
- m=new_m; n=new_n;
- return 1;
- }
- #endif