Thyra_DetachedVectorView.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Thyra_VectorBase.hpp"
00030 
00031 #ifndef THYRA_EXPLICIT_VECTOR_VIEW_HPP
00032 #define THYRA_EXPLICIT_VECTOR_VIEW_HPP
00033 
00034 namespace Thyra {
00035 
00133 template<class Scalar>
00134 class ConstDetachedVectorView {
00135 public:
00157   ConstDetachedVectorView(
00158     const Teuchos::RefCountPtr<const VectorBase<Scalar> > &v
00159     ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
00160     )
00161     {
00162       this->initialize(v,rng,forceUnitStride);
00163     }
00185   ConstDetachedVectorView( const VectorBase<Scalar>& v, const Range1D &rng = Range1D(), const bool forceUnitStride = false )
00186     {
00187       this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
00188     }
00190   ~ConstDetachedVectorView()
00191     {
00192       if( sv_s_.stride() != sv_.stride() )
00193         delete [] const_cast<Scalar*>(sv_.values());
00194       v_->releaseDetachedView(&sv_s_);
00195     }
00197   const RTOpPack::ConstSubVectorView<Scalar>& sv() const { return sv_; }
00199   Teuchos_Index   globalOffset() const { return sv_.globalOffset(); }
00201   Teuchos_Index   subDim()       const { return sv_.subDim();  }
00203   const Scalar*     values()       const { return sv_.values();  }
00205   ptrdiff_t         stride()       const { return sv_.stride();  }
00207   const Scalar& operator[](Teuchos_Index i) const { return sv_[i]; }
00209   const Scalar& operator()(Teuchos_Index i) const { return sv_(i); }
00210 private:
00211   //
00212   Teuchos::RefCountPtr<const VectorBase<Scalar> > v_;
00213   RTOpPack::ConstSubVectorView<Scalar>  sv_s_;
00214   RTOpPack::ConstSubVectorView<Scalar>  sv_;
00215   //
00216   void initialize(
00217     const Teuchos::RefCountPtr<const VectorBase<Scalar> > &v
00218     ,const Range1D &rng, const bool forceUnitStride
00219     )
00220     {
00221       v_ = v;
00222       v_->acquireDetachedView(rng,&sv_s_);
00223       if( forceUnitStride && sv_s_.stride() != 1 ) {
00224         Scalar *values = new Scalar[sv_s_.subDim()];
00225         Teuchos_Index i; const Scalar *sv_v;
00226         for( sv_v = sv_s_.values(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00227           values[i] = *sv_v;
00228         sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
00229       }
00230       else {
00231         sv_ = sv_s_;
00232       }
00233     }
00234   // Not defined and not to be called
00235   ConstDetachedVectorView();
00236   ConstDetachedVectorView(const ConstDetachedVectorView<Scalar>&);
00237   ConstDetachedVectorView<Scalar>& operator==(const ConstDetachedVectorView<Scalar>&);
00238 };
00239  
00337 template<class Scalar>
00338 class DetachedVectorView {
00339 public:
00362   DetachedVectorView(
00363     const Teuchos::RefCountPtr<VectorBase<Scalar> > &v
00364     ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
00365     )
00366     {
00367       this->initialize(v,rng,forceUnitStride);
00368     }
00391   DetachedVectorView( VectorBase<Scalar>& v, const Range1D &rng = Range1D(), const bool forceUnitStride = false )
00392     {
00393       this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
00394     }
00396   ~DetachedVectorView()
00397     {
00398       if( sv_s_.stride() != sv_.stride() ) {
00399         Teuchos_Index i; Scalar *sv_v; const Scalar *values;
00400         for( sv_v = sv_s_.values(), values = sv_.values(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00401           *sv_v = *values++;
00402         delete [] sv_.values();
00403       }
00404       v_->commitDetachedView(&sv_s_);
00405     }
00407   const RTOpPack::SubVectorView<Scalar>& sv() const { return sv_; }
00409   Teuchos_Index   globalOffset() const { return sv_.globalOffset(); }
00411   Teuchos_Index   subDim()       const { return sv_.subDim();  }
00413   Scalar*           values()       const { return sv_.values();  }
00415   ptrdiff_t         stride()       const { return sv_.stride();  }
00417   Scalar& operator[](Teuchos_Index i) const { return sv_[i]; }
00419   Scalar& operator()(Teuchos_Index i) const { return sv_(i); }
00420 private:
00421   //
00422   Teuchos::RefCountPtr<VectorBase<Scalar> > v_;
00423   RTOpPack::SubVectorView<Scalar>  sv_s_;
00424   RTOpPack::SubVectorView<Scalar>  sv_;
00425   //
00426   void initialize(
00427     const Teuchos::RefCountPtr<VectorBase<Scalar> > &v
00428     ,const Range1D &rng, const bool forceUnitStride
00429     )
00430     {
00431       v_ = v;
00432       v_->acquireDetachedView(rng,&sv_s_);
00433       if( forceUnitStride && sv_s_.stride() != 1 ) {
00434         Scalar *values = new Scalar[sv_s_.subDim()];
00435         Teuchos_Index i; const Scalar *sv_v;
00436         for( sv_v = sv_s_.values(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00437           values[i] = *sv_v;
00438         sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
00439       }
00440       else {
00441         sv_ = sv_s_;
00442       }
00443     }
00444   // Not defined and not to be called
00445   DetachedVectorView();
00446   DetachedVectorView(const DetachedVectorView<Scalar>&);
00447   DetachedVectorView<Scalar>& operator==(const DetachedVectorView<Scalar>&);
00448 };
00449 
00450 } // namespace Thyra
00451 
00452 #endif // THYRA_EXPLICIT_VECTOR_VIEW_HPP

Generated on Thu Sep 18 12:33:02 2008 for Thyra Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1