Thyra Package Browser (Single Doxygen Collection) Version of the Day
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 
00035 namespace Thyra {
00036 
00037 
00058 template<class Scalar>
00059 class ConstDetachedVectorView {
00060 public:
00061 
00097   ConstDetachedVectorView(
00098     const Teuchos::RCP<const VectorBase<Scalar> > &v
00099     ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
00100     )
00101     {
00102       this->initialize(v,rng,forceUnitStride);
00103     }
00104 
00140   ConstDetachedVectorView( const VectorBase<Scalar>& v,
00141     const Range1D &rng = Range1D(), const bool forceUnitStride = false )
00142     {
00143       this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
00144     }
00145 
00149   ~ConstDetachedVectorView()
00150     {
00151       if( sv_s_.stride() != sv_.stride() )
00152         delete [] const_cast<Scalar*>(sv_.values().get());
00153       v_->releaseDetachedView(&sv_s_);
00154     }
00155 
00159   const RTOpPack::ConstSubVectorView<Scalar>& sv() const { return sv_; }
00160 
00162   Teuchos_Index globalOffset() const { return sv_.globalOffset(); }
00163 
00165   Teuchos_Index subDim() const { return sv_.subDim(); }
00166 
00170   const Scalar* values() const { return sv_.values().get(); }
00171 
00175   ptrdiff_t stride() const { return sv_.stride(); }
00176 
00180   const Scalar& operator[](Teuchos_Index i) const { return sv_[i]; }
00181 
00185   const Scalar& operator()(Teuchos_Index i) const { return sv_(i); }
00186 
00187 private:
00188 
00189   Teuchos::RCP<const VectorBase<Scalar> > v_;
00190   RTOpPack::ConstSubVectorView<Scalar>  sv_s_;
00191   RTOpPack::ConstSubVectorView<Scalar>  sv_;
00192 
00193   void initialize(
00194     const Teuchos::RCP<const VectorBase<Scalar> > &v,
00195     const Range1D &rng, const bool forceUnitStride
00196     )
00197     {
00198       v_ = v;
00199       v_->acquireDetachedView(rng,&sv_s_);
00200       if( forceUnitStride && sv_s_.stride() != 1 ) {
00201         TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
00202         //const ArrayRCP<Scalar> values = Teuchos::arcp(sv_s_.subDim());
00203         //Teuchos_Index i; const Scalar *sv_v;
00204         //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00205         //  values[i] = *sv_v;
00206         //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
00207       }
00208       else {
00209         sv_ = sv_s_;
00210       }
00211     }
00212   // Not defined and not to be called
00213   ConstDetachedVectorView();
00214   ConstDetachedVectorView(const ConstDetachedVectorView<Scalar>&);
00215   ConstDetachedVectorView<Scalar>& operator==(const ConstDetachedVectorView<Scalar>&);
00216 };
00217 
00218  
00240 template<class Scalar>
00241 class DetachedVectorView {
00242 public:
00243 
00280   DetachedVectorView(
00281     const Teuchos::RCP<VectorBase<Scalar> > &v
00282     ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
00283     )
00284     {
00285       this->initialize(v,rng,forceUnitStride);
00286     }
00287 
00321   DetachedVectorView( VectorBase<Scalar>& v, const Range1D &rng = Range1D(), const bool forceUnitStride = false )
00322     {
00323       this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
00324     }
00325 
00329   ~DetachedVectorView()
00330     {
00331       if( sv_s_.stride() != sv_.stride() ) {
00332         TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
00333         //Teuchos_Index i; Scalar *sv_v; const Scalar *values;
00334         //for (
00335         //  sv_v = sv_s_.values().get(), values = sv_.values().get(), i=0;
00336         //  i < sv_s_.subDim();
00337         //  ++i, sv_v += sv_s_.stride()
00338         //  )
00339         //{
00340         //  *sv_v = *values++;
00341         //}
00342         //delete [] sv_.values().get();
00343       }
00344       v_->commitDetachedView(&sv_s_);
00345     }
00346 
00350   const RTOpPack::SubVectorView<Scalar>& sv() const { return sv_; }
00351 
00353   Teuchos_Index globalOffset() const { return sv_.globalOffset(); }
00354 
00356   Teuchos_Index subDim() const { return sv_.subDim(); }
00357 
00361   Scalar* values() const { return sv_.values().get(); }
00362 
00366   ptrdiff_t stride() const { return sv_.stride(); }
00367 
00371   Scalar& operator[](Teuchos_Index i) const { return sv_[i]; }
00372 
00375   Scalar& operator()(Teuchos_Index i) const { return sv_(i); }
00376 
00377 private:
00378 
00379   Teuchos::RCP<VectorBase<Scalar> > v_;
00380   RTOpPack::SubVectorView<Scalar>  sv_s_;
00381   RTOpPack::SubVectorView<Scalar>  sv_;
00382 
00383   void initialize(
00384     const Teuchos::RCP<VectorBase<Scalar> > &v
00385     ,const Range1D &rng, const bool forceUnitStride
00386     )
00387     {
00388       v_ = v;
00389       v_->acquireDetachedView(rng,&sv_s_);
00390       if( forceUnitStride && sv_s_.stride() != 1 ) {
00391         TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
00392         //Scalar *values = new Scalar[sv_s_.subDim()];
00393         //Teuchos_Index i; const Scalar *sv_v;
00394         //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00395         //  values[i] = *sv_v;
00396         //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
00397       }
00398       else {
00399         sv_ = sv_s_;
00400       }
00401     }
00402 
00403   // Not defined and not to be called
00404   DetachedVectorView();
00405   DetachedVectorView(const DetachedVectorView<Scalar>&);
00406   DetachedVectorView<Scalar>& operator==(const DetachedVectorView<Scalar>&);
00407 
00408 };
00409 
00410 
00411 } // namespace Thyra
00412 
00413 
00414 #endif // THYRA_EXPLICIT_VECTOR_VIEW_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines