offmat.hpp

Go to the documentation of this file.
00001 /* Kernel Independent Fast Multipole Method
00002    Copyright (C) 2004 Lexing Ying, New York University
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, or (at your option)
00007 any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00012 for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; see the file COPYING.  If not, write to the Free
00016 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017 02111-1307, USA.  */
00019 #ifndef _OFFMAT_HPP_
00020 #define _OFFMAT_HPP_
00021 
00022 #include "offvec.hpp"
00023 
00024 template <class F>
00025 class OffMat
00026 {
00027 public:
00028   int _m, _n;
00029   int _s, _t;
00030   bool _owndata;
00031   F* _data;
00032 public:
00033   OffMat(int m=0, int n=0, int s=0, int t=0): _m(m), _n(n), _s(s), _t(t), _owndata(true) {
00034          if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00035   }
00036   OffMat(int m, int n, int s, int t, bool owndata, F* data): _m(m), _n(n), _s(s), _t(t), _owndata(owndata) {
00037          if(_owndata) {
00038                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00039                 if(_m>0 && _n>0) memcpy( _data, data, _m*_n*sizeof(F) );
00040          } else {
00041                 _data = data;
00042          }
00043   }
00044   OffMat(const OffMat& C): _m(C._m), _n(C._n), _s(C._s), _t(C._t), _owndata(C._owndata) {
00045          if(_owndata) {
00046                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00047                 if(_m>0 && _n>0) memcpy( _data, C._data, _m*_n*sizeof(F) );
00048          } else {
00049                 _data = C._data;
00050          }
00051   }
00052   ~OffMat() { 
00053          if(_owndata) { 
00054                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00055          }
00056   }
00057   OffMat& operator=(const OffMat& C) {
00058          if(_owndata) { 
00059                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00060          }
00061          _m = C._m; _n=C._n; _s = C._s; _t=C._t; _owndata=C._owndata;
00062          if(_owndata) {
00063                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00064                 if(_m>0 && _n>0) memcpy( _data, C._data, _m*_n*sizeof(F) );
00065          } else {
00066                 _data = C._data;
00067          }
00068          return *this;
00069   }
00070   void resize(int m, int n, int s, int t)  {
00071          assert( _owndata==true );
00072          if(_m!=m || _n!=n) {
00073                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00074                 _m = m; _n = n; _s = s; _t = t;
00075                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00076          }
00077   }
00078   const F& operator()(int i, int j) const  {
00079          assert( i>=_s && i<_m+_s && j>=_t && j<_n+_t );
00080          return _data[(i-_s) + (j-_t)*_m];
00081   }
00082   F& operator()(int i, int j)  {
00083          assert( i>=_s && i<_m+_s && j>=_t && j<_n+_t );
00084          return _data[(i-_s) + (j-_t)*_m];
00085   }
00086   
00087   F* data() const { return _data; }
00088   F* clmdata(int j) { return &(_data[j*_m]); }
00089   int m() const { return _m; }
00090   int n() const { return _n; }
00091   int s() const { return _s; }
00092   int t() const { return _t; }
00093 };
00094 
00095 template <class F> inline ostream& operator<<( ostream& os, const OffMat<F>& mat)
00096 {
00097   os<<mat.m()<<" "<<mat.n()<<" "<<mat.s()<<" "<<mat.t()<<endl;
00098   os.setf(ios_base::scientific, ios_base::floatfield);
00099   for(int i=mat.s(); i<mat.s()+mat.m(); i++) {
00100          for(int j=mat.t(); j<mat.t()+mat.n(); j++)
00101                 os<<" "<<mat(i,j);
00102          os<<endl;
00103   }
00104   return os;
00105 }
00106 template <class F> inline void setvalue(OffMat<F>& mat, F val)
00107 {
00108   for(int i=mat.s(); i<mat.s()+mat.m(); i++)
00109          for(int j=mat.t(); j<mat.t()+mat.n(); j++)
00110                 mat(i,j) = val;
00111 }
00112 template <class F> inline void clear(OffMat<F>& M)
00113 {
00114   memset(M.data(), 0, M.m()*M.n()*sizeof(F));
00115 }
00116 
00117 typedef OffMat<bool>   BolOffMat;
00118 typedef OffMat<int>    IntOffMat;
00119 typedef OffMat<double> DblOffMat;
00120 
00121 /*
00122   void getColumn(int j, Vector<F>& vec)  {
00123   assert( j>=0 && j<n() );
00124          vec.resize(m());
00125          for(int i=0; i<m(); i++)
00126                 vec(i) = (*this)(i,j);
00127   }
00128   void getRow(int i, Vector<F>& vec)  {
00129          assert( i>=0 && i<m() );
00130          vec.resize(n());
00131          for(int j=0; j<n(); j++)
00132                 vec(j) = (*this)(i,j);
00133   }
00134   void setColumn(int j, Vector<F>& vec)  {
00135          assert( j>=0 && j<n() );
00136          assert( vec.length() == m() );
00137          for(int i=0; i<m(); i++)
00138                 (*this)(i,j) = vec(i);
00139   }
00140   void setRow(int i, Vector<F>& vec)  {
00141          assert( i>=0 && i<m());
00142          assert( vec.length() == n());
00143          for(int j=0; j<n(); j++)
00144          (*this)(i,j) = vec(j);
00145          }
00146 */
00147 
00148 #endif
00149 
00150 
00151 
00152 

Generated on Sun Dec 4 18:13:12 2005 for common by  doxygen 1.4.5