AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects Version of the Day
AbstractLinAlgPack_VectorMutableSubView.cpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) 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 (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "AbstractLinAlgPack_VectorMutableSubView.hpp"
00043 #include "Teuchos_Assert.hpp"
00044 #include "Teuchos_Workspace.hpp"
00045 #include "Teuchos_dyn_cast.hpp"
00046 
00047 namespace AbstractLinAlgPack {
00048 
00049 VectorMutableSubView::VectorMutableSubView( const vec_mut_ptr_t& vec, const Range1D& rng )
00050 {
00051   this->initialize(vec,rng);
00052 }
00053 
00054 void VectorMutableSubView::initialize( const vec_mut_ptr_t& vec, const Range1D& rng )
00055 {
00056   namespace rcp = MemMngPack;
00057   VectorSubView::initialize(vec,rng);
00058   full_vec_ = vec;
00059   this->has_changed();
00060 }
00061 
00062 void VectorMutableSubView::set_uninitialized()
00063 {
00064   VectorSubView::set_uninitialized();
00065   full_vec_ = Teuchos::null;
00066   this->has_changed();
00067 }
00068 
00069 // Overriddend form Vector
00070 
00071 Vector::vec_ptr_t VectorMutableSubView::sub_view( const Range1D& rng ) const
00072 {
00073   return VectorSubView::sub_view(rng); // Had to override to resolve conflicit!
00074 }
00075 
00076 // Overridden from VectorMutable
00077 
00078 void VectorMutableSubView::set_ele( index_type i, value_type val )
00079 {
00080   space_impl().validate_range(Range1D(i,i));
00081   full_vec_->set_ele( space_impl().rng().lbound() + i - 1, val );
00082   this->has_changed();
00083 }
00084 
00085 VectorMutable::vec_mut_ptr_t
00086 VectorMutableSubView::sub_view( const Range1D& rng_in )
00087 {
00088   namespace rcp = MemMngPack;
00089   const size_type this_dim = this->dim();
00090   const Range1D rng = RangePack::full_range( rng_in, 1, this_dim );
00091   space_impl().validate_range(rng);
00092   if( rng.lbound() == 1 && rng.ubound() == this_dim )
00093     return Teuchos::rcp(this,false); // Do not own memory!
00094   // Below we will return an object that is not this entire object so
00095   // we must wipe out cache.
00096   this->has_changed();
00097   const index_type this_offset = space_impl().rng().lbound() - 1;
00098   return Teuchos::rcp(
00099     new VectorMutableSubView(
00100       full_vec_
00101       ,Range1D( 
00102         this_offset  + rng.lbound()
00103         ,this_offset + rng.ubound() )
00104       ) );
00105 }
00106 
00107 void VectorMutableSubView::get_sub_vector( const Range1D& rng_in, RTOpPack::MutableSubVector* sub_vec )
00108 {
00109 #ifdef TEUCHOS_DEBUG
00110   TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error ,"VectorMutableSubView::get_sub_vector(...): Error!" );
00111 #endif
00112   const index_type this_dim = this->dim();
00113   const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
00114   space_impl().validate_range(rng);
00115   const index_type this_offset = space_impl().rng().lbound() - 1;
00116   full_vec_->get_sub_vector( rng + this_offset, sub_vec );
00117   sub_vec->setGlobalOffset( sub_vec->globalOffset() - this_offset );
00118 }
00119 
00120 void VectorMutableSubView::commit_sub_vector( RTOpPack::MutableSubVector* sub_vec )
00121 {
00122 #ifdef TEUCHOS_DEBUG
00123   TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error, "VectorMutableSubView::commit_sub_vector(...): Error!" );
00124 #endif
00125   const index_type this_offset = space_impl().rng().lbound() - 1;
00126   sub_vec->setGlobalOffset( sub_vec->globalOffset() + this_offset );
00127   full_vec_->commit_sub_vector( sub_vec );
00128   this->has_changed();
00129 }
00130 
00131 void VectorMutableSubView::set_sub_vector( const RTOpPack::SparseSubVector& sub_vec_in )
00132 {
00133   const index_type            this_offset = space_impl().rng().lbound() - 1;
00134   RTOpPack::SparseSubVector   sub_vec = sub_vec_in;
00135   sub_vec.setGlobalOffset( sub_vec.globalOffset() + this_offset );
00136   full_vec_->set_sub_vector( sub_vec );
00137   this->has_changed();
00138 }
00139 
00140 } // end namespace AbstractLinAlgPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends