AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AbstractLinAlgPack_VectorMutableSubView.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "AbstractLinAlgPack_VectorMutableSubView.hpp"
43 #include "Teuchos_Assert.hpp"
44 #include "Teuchos_Workspace.hpp"
45 #include "Teuchos_dyn_cast.hpp"
46 
47 namespace AbstractLinAlgPack {
48 
49 VectorMutableSubView::VectorMutableSubView( const vec_mut_ptr_t& vec, const Range1D& rng )
50 {
51  this->initialize(vec,rng);
52 }
53 
54 void VectorMutableSubView::initialize( const vec_mut_ptr_t& vec, const Range1D& rng )
55 {
56  namespace rcp = MemMngPack;
58  full_vec_ = vec;
59  this->has_changed();
60 }
61 
63 {
65  full_vec_ = Teuchos::null;
66  this->has_changed();
67 }
68 
69 // Overriddend form Vector
70 
72 {
73  return VectorSubView::sub_view(rng); // Had to override to resolve conflicit!
74 }
75 
76 // Overridden from VectorMutable
77 
78 void VectorMutableSubView::set_ele( index_type i, value_type val )
79 {
81  full_vec_->set_ele( space_impl().rng().lbound() + i - 1, val );
82  this->has_changed();
83 }
84 
87 {
88  namespace rcp = MemMngPack;
89  const size_type this_dim = this->dim();
90  const Range1D rng = RangePack::full_range( rng_in, 1, this_dim );
92  if( rng.lbound() == 1 && rng.ubound() == this_dim )
93  return Teuchos::rcp(this,false); // Do not own memory!
94  // Below we will return an object that is not this entire object so
95  // we must wipe out cache.
96  this->has_changed();
97  const index_type this_offset = space_impl().rng().lbound() - 1;
98  return Teuchos::rcp(
100  full_vec_
101  ,Range1D(
102  this_offset + rng.lbound()
103  ,this_offset + rng.ubound() )
104  ) );
105 }
106 
107 void VectorMutableSubView::get_sub_vector( const Range1D& rng_in, RTOpPack::MutableSubVector* sub_vec )
108 {
109 #ifdef TEUCHOS_DEBUG
110  TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error ,"VectorMutableSubView::get_sub_vector(...): Error!" );
111 #endif
112  const index_type this_dim = this->dim();
113  const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
114  space_impl().validate_range(rng);
115  const index_type this_offset = space_impl().rng().lbound() - 1;
116  full_vec_->get_sub_vector( rng + this_offset, sub_vec );
117  sub_vec->setGlobalOffset( sub_vec->globalOffset() - this_offset );
118 }
119 
120 void VectorMutableSubView::commit_sub_vector( RTOpPack::MutableSubVector* sub_vec )
121 {
122 #ifdef TEUCHOS_DEBUG
123  TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error, "VectorMutableSubView::commit_sub_vector(...): Error!" );
124 #endif
125  const index_type this_offset = space_impl().rng().lbound() - 1;
126  sub_vec->setGlobalOffset( sub_vec->globalOffset() + this_offset );
127  full_vec_->commit_sub_vector( sub_vec );
128  this->has_changed();
129 }
130 
131 void VectorMutableSubView::set_sub_vector( const RTOpPack::SparseSubVector& sub_vec_in )
132 {
133  const index_type this_offset = space_impl().rng().lbound() - 1;
134  RTOpPack::SparseSubVector sub_vec = sub_vec_in;
135  sub_vec.setGlobalOffset( sub_vec.globalOffset() + this_offset );
136  full_vec_->set_sub_vector( sub_vec );
137  this->has_changed();
138 }
139 
140 } // end namespace AbstractLinAlgPack
void set_sub_vector(const RTOpPack::SparseSubVector &sub_vec)
const VectorSpaceSubSpace & space_impl() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Index ubound() const
void initialize(const vec_mut_ptr_t &vec, const Range1D &rng)
Initialize.
void commit_sub_vector(RTOpPack::MutableSubVector *sub_vec)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void has_changed() const
Must be called by any vector subclass that modifies this vector object!
void get_sub_vector(const Range1D &rng, RTOpPack::MutableSubVector *sub_vec)
void validate_range(const Range1D &rng) const
Validate rng.
vec_ptr_t sub_view(const Range1D &rng) const
Index lbound() const
void initialize(const vec_ptr_t &full_vec, const Range1D &rng)
Initialize a sub-view based on a full vector.
vec_ptr_t sub_view(const Range1D &rng) const
Overridden to pick VectorSubView::sub_view().