numvec.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 _NUMVEC_HPP_
00020 #define _NUMVEC_HPP_
00021 
00022 #include "commoninc.hpp"
00023 
00024 using std::ostream;
00025 using std::ios_base;
00026 using std::endl;
00027 
00029 template <class F>
00031 class NumVec
00032 {
00033 public:
00035   int  _m;
00037   bool _owndata;
00039   F* _data;
00040 public:
00042   NumVec(int m=0): _m(m), _owndata(true)  {
00043          if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00044   }
00047   NumVec(int m, bool owndata, F* data): _m(m), _owndata(owndata) {
00048          if(_owndata) {
00049                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00050                 if(_m>0) memcpy( _data, data, _m*sizeof(F) );
00051          } else {
00052                 _data = data;
00053          }
00054   }
00056   NumVec(const NumVec& C): _m(C._m), _owndata(C._owndata)  {
00057          if(_owndata) {
00058                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00059                 if(_m>0) memcpy( _data, C._data, _m*sizeof(F) );
00060          } else {
00061                 _data = C._data;
00062          }
00063   }
00065   ~NumVec() {
00066          if(_owndata) {
00067                 if(_m>0) { delete[] _data; _data = NULL; }
00068          }
00069   }
00071   NumVec& operator=(const NumVec& C)  {
00072          if(_owndata) { 
00073                 if(_m>0) { delete[] _data; _data = NULL; }
00074          }
00075          _m = C._m; _owndata=C._owndata;
00076          if(_owndata) {
00077                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL); memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00078                 if(_m>0) memcpy( _data, C._data, _m*sizeof(F) );
00079          } else {
00080                 _data =C._data;
00081          }
00082          return *this;
00083   }
00085   void resize(int m)  {
00086          assert(_owndata==true);
00087          if(m !=_m) {
00088                 if(_m>0) { delete[] _data; _data = NULL; }
00089                 _m = m;
00090                 if(_m>0) { _data = new F[_m]; assert(_data!=NULL);  memset(_data, 0, _m*sizeof(F)); } else _data=NULL;
00091          }
00092   }
00094   const F& operator()(int i) const  {    assert(i>=0 && i<_m);
00095     return _data[i]; 
00096   }
00098   F& operator()(int i)  {        assert(i>=0 && i<_m);
00099     return _data[i]; 
00100   }
00102   F* data() const { return _data; }
00104   int m () const { return _m; }
00105 };
00107 template <class F> inline ostream& operator<<( ostream& os, const NumVec<F>& vec)
00108 {
00109   os<<vec.m()<<endl;
00110   os.setf(ios_base::scientific, ios_base::floatfield);
00111   for(int i=0; i<vec.m(); i++)   os<<" "<<vec(i);
00112   os<<endl;
00113   return os;
00114 }
00116 template <class F> inline void setvalue(NumVec<F>& V, F val)
00117 {
00118   for(int i=0; i<V.m(); i++)
00119          V(i) = val;
00120 }
00122 template <class F> inline void clear(NumVec<F>& V)
00123 {
00124   memset(V.data(), 0, V.m()*sizeof(F));
00125 }
00127 typedef NumVec<bool>   BolNumVec;
00129 typedef NumVec<int>    IntNumVec;
00131 typedef NumVec<double> DblNumVec; //typedef NumVec<double> SclNumVec;
00132 
00133 
00134 #endif

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