offvec.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 _OFFVEC_HPP_
00020 #define _OFFVEC_HPP_
00021 
00022 #include "commoninc.hpp"
00023 
00024 template <class F>
00025 class OffVec
00026 {
00027 public:
00028   int  _m;
00029   int  _s;
00030   bool _owndata;
00031   F* _data;
00032 public:
00033   OffVec(int m=0, int s=0): _m(m), _s(s), _owndata(true)  {
00034          if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00035   }
00036   OffVec(int m, int s, bool owndata, F* data): _m(m), _s(s), _owndata(owndata) {
00037          if(_owndata) {
00038                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00039                 if(_m>0) memcpy( _data, data, _m*sizeof(F) );
00040          } else {
00041                 _data = data;
00042          }
00043   }
00044   OffVec(const OffVec& C): _m(C._m), _s(C._s), _owndata(C._owndata)  {
00045          if(_owndata) {
00046                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00047                 if(_m>0) memcpy( _data, C._data, _m*sizeof(F) );
00048          } else {
00049                 _data = C._data;
00050          }
00051   }
00052   ~OffVec() {
00053          if(_owndata) {
00054                 if(_m>0) { delete[] _data; _data = NULL; }
00055          }
00056   }
00057   OffVec& operator=(const OffVec& C)  {
00058          if(_owndata) { 
00059                 if(_m>0) { delete[] _data; _data = NULL; }
00060          }
00061          _m=C._m; _s=C._s; _owndata=C._owndata;
00062          if(_owndata) {
00063                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00064                 if(_m>0) memcpy( _data, C._data, _m*sizeof(F) );
00065          } else {
00066                 _data =C._data;
00067          }
00068          return *this;
00069   }
00070   void resize(int m, int s)  {
00071          assert(_owndata==true);
00072          if(m !=_m) {
00073                 if(_m>0) { delete[] _data; _data = NULL; }
00074                 _m = m;         _s = s;
00075                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL);  memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00076          }
00077   }
00078   const F& operator()(int i) const  {
00079          assert(i>=_s && i<_m+_s);
00080          return _data[i-_s]; 
00081   }
00082   F& operator()(int i)  {
00083          assert(i>=_s && i<_m+_s);
00084          return _data[i-_s]; 
00085   }
00086   
00087   F* data() const { return _data; }
00088   int m() const { return _m; }
00089   int s() const { return _s; }
00090 };
00091 
00092 template <class F> inline ostream& operator<<( ostream& os, const OffVec<F>& vec)
00093 {
00094   os<<vec.m()<<" "<<vec.s()<<endl;
00095   os.setf(ios_base::scientific, ios_base::floatfield);
00096   for(int i=vec.s(); i<vec.m()+vec.s(); i++)     os<<" "<<vec(i);
00097   os<<endl;
00098   return os;
00099 }
00100 template <class F> inline void setvalue(OffVec<F>& vec, F val)
00101 {
00102   for(int i=vec.s(); i<vec.s()+vec.m(); i++)     vec(i) = val;
00103 }
00104 template <class F> inline void clear(OffVec<F>& vec)
00105 {
00106   memset(vec.data(), 0, vec.m()*sizeof(F));
00107 }
00108 
00109 typedef OffVec<bool>   BolOffVec;
00110 typedef OffVec<int>    IntOffVec;
00111 typedef OffVec<double> DblOffVec;
00112 
00113 
00114 #endif
00115 
00116 

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