offtns.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 _OFFTNS_HPP_
00020 #define _OFFTNS_HPP_
00021 
00022 #include "offmat.hpp"
00023 
00024 template <class F>
00025 class OffTns
00026 {
00027 public:
00028   int _m, _n, _p;
00029   int _s, _t, _u;
00030   bool _owndata;
00031   F* _data;
00032 public:
00033   OffTns(int m=0, int n=0, int p=0, int s=0, int t=0, int u=0): _m(m), _n(n), _p(p), _s(s), _t(t), _u(u), _owndata(true) {
00034          if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00035   }
00036   OffTns(int m, int n, int p, int s, int t, int u, bool owndata, F* data): _m(m), _n(n), _p(p), _s(s), _t(t), _u(u), _owndata(owndata) {
00037          if(_owndata) {
00038                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00039                 if(_m>0 && _n>0 && _p>0) { for(int i=0; i<_m*_n*_p; i++) _data[i] = data[i]; }
00040          } else {
00041                 _data = data;
00042          }
00043   }
00044   OffTns(const OffTns& C): _m(C._m), _n(C._n), _p(C._p), _s(C._s), _t(C._t), _u(C._u), _owndata(C._owndata) {
00045          if(_owndata) {
00046                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00047                 if(_m>0 && _n>0 && _p>0) { for(int i=0; i<_m*_n*_p; i++) _data[i] = C._data[i]; }
00048          } else {
00049                 _data = C._data;
00050          }
00051   }
00052   ~OffTns() { 
00053          if(_owndata) { 
00054                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00055          }
00056   }
00057   OffTns& operator=(const OffTns& C) {
00058          if(_owndata) { 
00059                 if(_m>0 && _n>0 && _p>0) { delete[] _data; _data = NULL; } 
00060          }
00061          _m = C._m; _n=C._n; _p=C._p;   _s = C._s; _t=C._t; _u=C._u; _owndata=C._owndata;
00062          if(_owndata) {
00063                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00064                 if(_m>0 && _n>0 && _p>0) { for(int i=0; i<_m*_n*_p; i++) _data[i] = C._data[i]; }
00065          } else {
00066                 _data = C._data;
00067          }
00068          return *this;
00069   }
00070   void resize(int m, int n, int p, int s, int t, int u)  {
00071          assert( _owndata==true );
00072          if(_m!=m || _n!=n || _p!=p) {
00073                 if(_m>0 && _n>0 && _p>0) { delete[] _data; _data = NULL; } 
00074                 _m = m; _n = n; _p=p; _s=s; _t=t; _u=u;
00075                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00076          }
00077   }
00078   const F& operator()(int i, int j, int k) const  {
00079          assert( i>=_s && i<_m+_s && j>=_t && j<_n+_t && k>=_u && k<_p+_u);
00080          return _data[(i-_s) + (j-_t)*_m + (k-_u)*_m*_n];
00081   }
00082   F& operator()(int i, int j, int k)  {
00083          assert( i>=_s && i<_m+_s && j>=_t && j<_n+_t && k>=_u && k<_p+_u);
00084          return _data[(i-_s) + (j-_t)*_m + (k-_u)*_m*_n];
00085   }
00086   
00087   F* data() const { return _data; }
00088   int m() const { return _m; }
00089   int n() const { return _n; }
00090   int p() const { return _p; }
00091   int s() const { return _s; }
00092   int t() const { return _t; }
00093   int u() const { return _u; }
00094 };
00095 
00096 template <class F> inline ostream& operator<<( ostream& os, const OffTns<F>& tns)
00097 {
00098   os<<tns.m()<<" "<<tns.n()<<" "<<tns.p()<<" "<<tns.s()<<" "<<tns.t()<<" "<<tns.u()<<endl;
00099   os.setf(ios_base::scientific, ios_base::floatfield);
00100   for(int i=tns.s(); i<tns.s()+tns.m(); i++) {
00101          for(int j=tns.t(); j<tns.t()+tns.n(); j++) {
00102                 for(int k=tns.u(); k<tns.u()+tns.p(); k++) {
00103                   os<<" "<<tns(i,j,k);
00104                 }
00105                 os<<endl;
00106          }
00107          os<<endl;
00108   }
00109   return os;
00110 }
00111 template <class F> inline void setvalue(OffTns<F>& M, F val)
00112 {
00113   for(int i=0; i<M.m(); i++)
00114          for(int j=0; j<M.n(); j++)
00115                 for(int k=0; k<M.p(); k++)
00116                   M(i,j,k) = val;
00117   return;
00118 }
00119 
00120 typedef OffTns<bool>   BolOffTns;
00121 typedef OffTns<int>    IntOffTns;
00122 typedef OffTns<double> DblOffTns;
00123 typedef OffTns<cpx>    CpxOffTns;
00124 
00125 #endif
00126 
00127 
00128 
00129 

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