NGSolve  4.9
linalg/vvector.hpp
00001 namespace ngla
00002 {
00003 
00004 
00005   template <class T> class VFlatVector;
00006   template <class T> class VVector;
00007 
00008 
00009 
00010 
00011   template <typename TSCAL = double> 
00012   class S_BaseVectorPtr : virtual public S_BaseVector<TSCAL>
00013   {
00014   protected:
00015     TSCAL * pdata;
00016     int es;
00017     bool ownmem;
00018     
00019   public:
00020     S_BaseVectorPtr (int as, int aes, void * adata) throw()
00021     {
00022       this->size = as;
00023       es = aes;
00024       pdata = static_cast<TSCAL*> (adata);
00025       ownmem = false;
00026       this->entrysize = es * sizeof(TSCAL) / sizeof(double);
00027     }
00028 
00029     S_BaseVectorPtr (int as, int aes) 
00030     {
00031       this->size = as;
00032       es = aes;
00033       pdata = new TSCAL[as*aes];
00034       ownmem = true;
00035       this->entrysize = es * sizeof(TSCAL) / sizeof(double);
00036     }
00037 
00038     void SetSize (int as) 
00039     {
00040       if (ownmem) delete [] pdata;
00041       this->size = as;
00042       pdata = new TSCAL[as*es];
00043       ownmem = true;
00044     }
00045 
00046     void AssignMemory (int as, void * adata)
00047     {
00048       this->size = as; 
00049       this->pdata = static_cast<TSCAL*> (adata); 
00050     }
00051     
00052     virtual ~S_BaseVectorPtr ()
00053     {
00054       if (ownmem) delete [] pdata;
00055     }
00056 
00057     virtual void * Memory () const throw()
00058     {
00059       return pdata; 
00060     }
00061     
00062     virtual BaseVector * Range (int begin, int end) const
00063     {
00064       return new S_BaseVectorPtr<TSCAL> (end-begin, es, pdata+begin*es);
00065     }
00066 
00067     virtual BaseVector * Range (IntRange range) const
00068     {
00069       return new S_BaseVectorPtr<TSCAL> (range.Size(), es, pdata+range.First()*es);
00070     }
00071 
00072 
00073     FlatVector<TSCAL> operator() (int i) const
00074     {
00075       return FlatVector<TSCAL> (es, pdata+i*es);
00076     }
00077 
00078     virtual BaseVector * CreateVector ( const Array<int> * procs = 0) const
00079     {
00080       switch (es)
00081         {
00082         case 1: return new VVector<TSCAL> (this->size);
00083         case 2: return new VVector<Vec<2,TSCAL> > (this->size);
00084         case 3: return new VVector<Vec<3,TSCAL> > (this->size);
00085         }
00086       return new S_BaseVectorPtr<TSCAL> (this->size, es);
00087     }
00088   
00089     virtual ostream & Print (ostream & ost) const
00090     {
00091       // return (ost << FV() << endl);
00092       if (es == 1)
00093         ost << FlatVector<TSCAL> (this->size, pdata) << endl;
00094       else
00095         ost << FlatSysVector<TSCAL> (this->size, es, pdata);
00096       return ost;
00097     }
00098   };
00099 
00100 
00101 
00102 
00103 
00104 
00108   template <typename T = double>
00109   class VFlatVector :  virtual public S_BaseVectorPtr<typename mat_traits<T>::TSCAL>
00110   {
00111   public:
00112     typedef typename mat_traits<T>::TSCAL TSCAL;
00113     enum { ES = sizeof(T) / sizeof(TSCAL) };
00114 
00115     explicit VFlatVector () throw()
00116       : S_BaseVectorPtr<TSCAL> (0, ES, NULL)
00117     { ; }
00118 
00119     explicit VFlatVector (int as, T * adata) throw()
00120       : S_BaseVectorPtr<TSCAL> (as, ES, (void*)adata)
00121     { ; }
00122 
00123     VFlatVector & operator= (TSCAL s1)
00124     {
00125       BaseVector::operator= (s1);
00126       return *this;
00127     }
00128 
00129     template <typename T2>
00130     VFlatVector & operator= (const VVecExpr<T2> & v)
00131     {
00132       BaseVector::operator= (v);
00133       return *this;
00134     }
00135   };
00136 
00137 
00138 
00139 
00140 
00141 
00145   template <typename T = double> 
00146   class  VVector : virtual public S_BaseVectorPtr<typename mat_traits<T>::TSCAL>
00147   {
00148   public:
00149     typedef typename mat_traits<T>::TSCAL TSCAL;
00150     enum { ES = sizeof(T) / sizeof(TSCAL) };
00151 
00152     explicit VVector (int as)
00153       : S_BaseVectorPtr<TSCAL> (as, ES) 
00154     { ; }
00155 
00156     virtual ~VVector() { ; }
00157 
00158     VVector & operator= (TSCAL s1)
00159     {
00160       BaseVector::operator= (s1);
00161       return *this;
00162     }
00163 
00164     VVector & operator= (const VVector & v2)
00165     {
00166       BaseVector::operator= (v2);
00167       return *this;
00168     }
00169 
00170     template <typename T2>
00171     VVector & operator= (const VVecExpr<T2> & v)
00172     {
00173       BaseVector::operator= (v);
00174       return *this;
00175     }
00176 
00177     FlatVector<T> FV () const throw()
00178     {
00179       return FlatVector<T> (this->size, this->pdata);
00180     }
00181 
00182     T & operator() (int i) const
00183     {
00184       return static_cast<T*> (static_cast<void*> (this->pdata))[i];
00185     }
00186   };
00187 
00188 
00189 }