MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractLinAlgPack_SpVectorView.cpp
Go to the documentation of this file.
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 
43 
44 namespace {
45 
46 // Setup some template classes to check at complile time that
47 // the layout of SpVectorSlice::element_type is proper.
48 template<int N, class T>
49 class assert_compile_time {
50 public:
51  assert_compile_time()
52  {
53  // This should not compile if instantiated with a type T that
54  // is not an integer. However, if the compiler checks this
55  // function without instantiating it, it can not cause an error
56  // because it does not know the type of T to see if the
57  // conversion is legal or not.
58  T d;
59  static_cast<int*>(d);
60  }
61 };
62 
63 // Template specialization for no error
64 template<>
65 class assert_compile_time<0,double> {
66 public:
67  assert_compile_time()
68  {}
69 };
70 
71 // Validate that there is an integer stride between values
72 assert_compile_time<
74  % (int)sizeof(AbstractLinAlgPack::value_type))
75  , double
76  >
77 validate_value_stride;
78 
79 // Validate that there is an integer stride between indexes
80 assert_compile_time<
82  % (int)sizeof(AbstractLinAlgPack::index_type))
83  , double
84  >
85 validate_index_stride;
86 
87 // Compute the stride between values
88 const int values_stride = (int)sizeof(AbstractLinAlgPack::SpVectorSlice::element_type)
89  / (int)sizeof(AbstractLinAlgPack::value_type);
90 
91 // Compute the stride between indexes
92 const int indices_stride = (int)sizeof(AbstractLinAlgPack::SpVectorSlice::element_type)
93  / (int)sizeof(AbstractLinAlgPack::index_type);
94 
95 } // end namespace
96 
99  const SpVectorSlice& sv_in
100  ,const Range1D& rng_in
101  )
102 {
103  using Teuchos::null;
104  const Range1D rng = RangePack::full_range(rng_in,1,sv_in.dim());
105  const SpVectorSlice sv = sv_in(rng);
106 
108 
109  if(!sv.nz()) {
110  sub_vec.initialize(
111  rng.lbound()-1 // global_offset
112  ,rng.size() // sub_dim
113  ,0 // nz
114  ,null // vlaues
115  ,1 // values_stride
116  ,null // indices
117  ,1 // indices_stride
118  ,0 // local_offset
119  ,1 // is_sorted
120  );
121  }
122  else {
123  SpVectorSlice::const_iterator itr = sv.begin();
124  TEUCHOS_TEST_FOR_EXCEPT( !( itr != sv.end() ) );
125  if( sv.dim() && sv.nz() == sv.dim() && sv.is_sorted() ) {
127  Teuchos::arcp(&itr->value(), 0, values_stride*rng.size(), false) ;
128  sub_vec.initialize(
129  rng.lbound()-1 // global_offset
130  ,rng.size() // sub_dim
131  ,values // values
132  ,values_stride // values_stride
133  );
134  }
135  else {
137  Teuchos::arcp(&itr->value(), 0, values_stride*sv.nz(), false) ;
139  Teuchos::arcp(&itr->index(), 0, indices_stride*sv.nz(), false);
140  sub_vec.initialize(
141  rng.lbound()-1 // global_offset
142  ,sv.dim() // sub_dim
143  ,sv.nz() // sub_nz
144  ,values // values
145  ,values_stride // values_stride
146  ,indexes // indices
147  ,indices_stride // indices_stride
148  ,sv.offset() // local_offset
149  ,sv.is_sorted() // is_sorted
150  );
151  }
152  }
153 
154  return sub_vec;
155 }
size_type dim() const
Return the number of elements in the full vector.
Index size() const
Return the size of the range (ubound() - lbound() + 1)
RTOpPack::SparseSubVector sub_vec_view(const SpVectorSlice &sv, const Range1D &rng=Range1D())
Create an RTOpPack::SparseSubVector view object from a SpVectorSlice object.
. One-based subregion index range class.
Index lbound() const
Return lower bound of the range.
SparseSubVectorT< RTOp_value_type > SparseSubVector
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)