Thyra Version of the Day
Thyra_DetachedVectorView.hpp
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Thyra_VectorBase.hpp"
00043 
00044 #ifndef THYRA_EXPLICIT_VECTOR_VIEW_HPP
00045 #define THYRA_EXPLICIT_VECTOR_VIEW_HPP
00046 
00047 
00048 namespace Thyra {
00049 
00050 
00071 template<class Scalar>
00072 class ConstDetachedVectorView {
00073 public:
00074 
00110   ConstDetachedVectorView(
00111     const Teuchos::RCP<const VectorBase<Scalar> > &v
00112     ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
00113     )
00114     {
00115       this->initialize(v,rng,forceUnitStride);
00116     }
00117 
00153   ConstDetachedVectorView( const VectorBase<Scalar>& v,
00154     const Range1D &rng = Range1D(), const bool forceUnitStride = false )
00155     {
00156       this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
00157     }
00158 
00162   ~ConstDetachedVectorView()
00163     {
00164       if( sv_s_.stride() != sv_.stride() )
00165         delete [] const_cast<Scalar*>(sv_.values().get());
00166       v_->releaseDetachedView(&sv_s_);
00167     }
00168 
00172   const RTOpPack::ConstSubVectorView<Scalar>& sv() const { return sv_; }
00173 
00175   Teuchos_Index globalOffset() const { return sv_.globalOffset(); }
00176 
00178   Teuchos_Index subDim() const { return sv_.subDim(); }
00179 
00183   const Scalar* values() const { return sv_.values().get(); }
00184 
00188   ptrdiff_t stride() const { return sv_.stride(); }
00189 
00193   const Scalar& operator[](Teuchos_Index i) const { return sv_[i]; }
00194 
00198   const Scalar& operator()(Teuchos_Index i) const { return sv_(i); }
00199 
00200 private:
00201 
00202   Teuchos::RCP<const VectorBase<Scalar> > v_;
00203   RTOpPack::ConstSubVectorView<Scalar>  sv_s_;
00204   RTOpPack::ConstSubVectorView<Scalar>  sv_;
00205 
00206   void initialize(
00207     const Teuchos::RCP<const VectorBase<Scalar> > &v,
00208     const Range1D &rng, const bool forceUnitStride
00209     )
00210     {
00211       v_ = v;
00212       v_->acquireDetachedView(rng,&sv_s_);
00213       if( forceUnitStride && sv_s_.stride() != 1 ) {
00214         TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
00215         //const ArrayRCP<Scalar> values = Teuchos::arcp(sv_s_.subDim());
00216         //Teuchos_Index i; const Scalar *sv_v;
00217         //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00218         //  values[i] = *sv_v;
00219         //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
00220       }
00221       else {
00222         sv_ = sv_s_;
00223       }
00224     }
00225   // Not defined and not to be called
00226   ConstDetachedVectorView();
00227   ConstDetachedVectorView(const ConstDetachedVectorView<Scalar>&);
00228   ConstDetachedVectorView<Scalar>& operator==(const ConstDetachedVectorView<Scalar>&);
00229 };
00230 
00231  
00253 template<class Scalar>
00254 class DetachedVectorView {
00255 public:
00256 
00293   DetachedVectorView(
00294     const Teuchos::RCP<VectorBase<Scalar> > &v
00295     ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
00296     )
00297     {
00298       this->initialize(v,rng,forceUnitStride);
00299     }
00300 
00334   DetachedVectorView( VectorBase<Scalar>& v, const Range1D &rng = Range1D(), const bool forceUnitStride = false )
00335     {
00336       this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
00337     }
00338 
00342   ~DetachedVectorView()
00343     {
00344       if( sv_s_.stride() != sv_.stride() ) {
00345         TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
00346         //Teuchos_Index i; Scalar *sv_v; const Scalar *values;
00347         //for (
00348         //  sv_v = sv_s_.values().get(), values = sv_.values().get(), i=0;
00349         //  i < sv_s_.subDim();
00350         //  ++i, sv_v += sv_s_.stride()
00351         //  )
00352         //{
00353         //  *sv_v = *values++;
00354         //}
00355         //delete [] sv_.values().get();
00356       }
00357       v_->commitDetachedView(&sv_s_);
00358     }
00359 
00363   const RTOpPack::SubVectorView<Scalar>& sv() const { return sv_; }
00364 
00366   Teuchos_Index globalOffset() const { return sv_.globalOffset(); }
00367 
00369   Teuchos_Index subDim() const { return sv_.subDim(); }
00370 
00374   Scalar* values() const { return sv_.values().get(); }
00375 
00379   ptrdiff_t stride() const { return sv_.stride(); }
00380 
00384   Scalar& operator[](Teuchos_Index i) const { return sv_[i]; }
00385 
00388   Scalar& operator()(Teuchos_Index i) const { return sv_(i); }
00389 
00390 private:
00391 
00392   Teuchos::RCP<VectorBase<Scalar> > v_;
00393   RTOpPack::SubVectorView<Scalar>  sv_s_;
00394   RTOpPack::SubVectorView<Scalar>  sv_;
00395 
00396   void initialize(
00397     const Teuchos::RCP<VectorBase<Scalar> > &v
00398     ,const Range1D &rng, const bool forceUnitStride
00399     )
00400     {
00401       v_ = v;
00402       v_->acquireDetachedView(rng,&sv_s_);
00403       if( forceUnitStride && sv_s_.stride() != 1 ) {
00404         TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
00405         //Scalar *values = new Scalar[sv_s_.subDim()];
00406         //Teuchos_Index i; const Scalar *sv_v;
00407         //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
00408         //  values[i] = *sv_v;
00409         //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
00410       }
00411       else {
00412         sv_ = sv_s_;
00413       }
00414     }
00415 
00416   // Not defined and not to be called
00417   DetachedVectorView();
00418   DetachedVectorView(const DetachedVectorView<Scalar>&);
00419   DetachedVectorView<Scalar>& operator==(const DetachedVectorView<Scalar>&);
00420 
00421 };
00422 
00423 
00424 } // namespace Thyra
00425 
00426 
00427 #endif // THYRA_EXPLICIT_VECTOR_VIEW_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines