Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DetachedVectorView.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Thyra_VectorBase.hpp"
11 
12 #ifndef THYRA_EXPLICIT_VECTOR_VIEW_HPP
13 #define THYRA_EXPLICIT_VECTOR_VIEW_HPP
14 
15 
16 namespace Thyra {
17 
18 
39 template<class Scalar>
41 public:
42 
79  const Teuchos::RCP<const VectorBase<Scalar> > &v
80  ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
81  )
82  {
83  this->initialize(v,rng,forceUnitStride);
84  }
85 
122  const Range1D &rng = Range1D(), const bool forceUnitStride = false )
123  {
124  this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
125  }
126 
131  {
132  if( sv_s_.stride() != sv_.stride() )
133  delete [] const_cast<Scalar*>(sv_.values().get());
134  v_->releaseDetachedView(&sv_s_);
135  }
136 
140  const RTOpPack::ConstSubVectorView<Scalar>& sv() const { return sv_; }
141 
143  Teuchos_Ordinal globalOffset() const { return sv_.globalOffset(); }
144 
146  Teuchos_Ordinal subDim() const { return sv_.subDim(); }
147 
151  const Scalar* values() const { return sv_.values().get(); }
152 
156  ptrdiff_t stride() const { return sv_.stride(); }
157 
161  const Scalar& operator[](Teuchos_Ordinal i) const { return sv_[i]; }
162 
166  const Scalar& operator()(Teuchos_Ordinal i) const { return sv_(i); }
167 
168 private:
169 
173 
174  void initialize(
175  const Teuchos::RCP<const VectorBase<Scalar> > &v,
176  const Range1D &rng, const bool forceUnitStride
177  )
178  {
179  v_ = v;
180  v_->acquireDetachedView(rng,&sv_s_);
181  if( forceUnitStride && sv_s_.stride() != 1 ) {
182  TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
183  //const ArrayRCP<Scalar> values = Teuchos::arcp(sv_s_.subDim());
184  //Teuchos_Ordinal i; const Scalar *sv_v;
185  //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
186  // values[i] = *sv_v;
187  //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
188  }
189  else {
190  sv_ = sv_s_;
191  }
192  }
193  // Not defined and not to be called
195  ConstDetachedVectorView(const ConstDetachedVectorView<Scalar>&);
196  ConstDetachedVectorView<Scalar>& operator==(const ConstDetachedVectorView<Scalar>&);
197 };
198 
199 
221 template<class Scalar>
223 public:
224 
263  ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
264  )
265  {
266  this->initialize(v,rng,forceUnitStride);
267  }
268 
302  DetachedVectorView( VectorBase<Scalar>& v, const Range1D &rng = Range1D(), const bool forceUnitStride = false )
303  {
304  this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
305  }
306 
311  {
312  if( sv_s_.stride() != sv_.stride() ) {
313  Teuchos::TestForTermination_terminate("I don't think non-unit stride has ever been tested!");
314  //Teuchos_Ordinal i; Scalar *sv_v; const Scalar *values;
315  //for (
316  // sv_v = sv_s_.values().get(), values = sv_.values().get(), i=0;
317  // i < sv_s_.subDim();
318  // ++i, sv_v += sv_s_.stride()
319  // )
320  //{
321  // *sv_v = *values++;
322  //}
323  //delete [] sv_.values().get();
324  }
325  v_->commitDetachedView(&sv_s_);
326  }
327 
331  const RTOpPack::SubVectorView<Scalar>& sv() const { return sv_; }
332 
334  Teuchos_Ordinal globalOffset() const { return sv_.globalOffset(); }
335 
337  Teuchos_Ordinal subDim() const { return sv_.subDim(); }
338 
342  Scalar* values() const { return sv_.values().get(); }
343 
347  ptrdiff_t stride() const { return sv_.stride(); }
348 
352  Scalar& operator[](Teuchos_Ordinal i) const { return sv_[i]; }
353 
356  Scalar& operator()(Teuchos_Ordinal i) const { return sv_(i); }
357 
358 private:
359 
363 
364  void initialize(
366  ,const Range1D &rng, const bool forceUnitStride
367  )
368  {
369  v_ = v;
370  v_->acquireDetachedView(rng,&sv_s_);
371  if( forceUnitStride && sv_s_.stride() != 1 ) {
372  TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
373  //Scalar *values = new Scalar[sv_s_.subDim()];
374  //Teuchos_Ordinal i; const Scalar *sv_v;
375  //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
376  // values[i] = *sv_v;
377  //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
378  }
379  else {
380  sv_ = sv_s_;
381  }
382  }
383 
384  // Not defined and not to be called
386  DetachedVectorView(const DetachedVectorView<Scalar>&);
387  DetachedVectorView<Scalar>& operator==(const DetachedVectorView<Scalar>&);
388 
389 };
390 
391 
392 } // namespace Thyra
393 
394 
395 #endif // THYRA_EXPLICIT_VECTOR_VIEW_HPP
ptrdiff_t stride() const
Return the stride between elements in the array returned from this-&gt;values().
~ConstDetachedVectorView()
Free the explicit view on the VectorBase object v passed to ConstDetachedVectorView().
ConstDetachedVectorView(const Teuchos::RCP< const VectorBase< Scalar > > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit non-mutable (const) view of a subset of elements.
Scalar * values() const
Return a pointer to a Scalar array containing the explicit view.
Create an explicit non-mutable (const) view of a VectorBase object.
const RTOpPack::SubVectorView< Scalar > & sv() const
Returns the explicit view as an RTOpPack::ConstSubVectorView&lt;Scalar&gt; object.
const RTOpPack::ConstSubVectorView< Scalar > & sv() const
Returns the explicit view as an RTOpPack::ConstSubVectorView&lt;Scalar&gt; object.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForTermination_terminate(const std::string &msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
Teuchos_Ordinal globalOffset() const
Returns the global offset for the explicit view.
Teuchos_Ordinal subDim() const
Returns the dimension of the explicit view.
Teuchos_Ordinal subDim() const
Returns the dimension of the explicit view.
Create an explicit mutable (non-const) view of a VectorBase object.
~DetachedVectorView()
Construct an explicit mutable (non-const) view of a subset of elements.
Abstract interface for finite-dimensional dense vectors.
ptrdiff_t stride() const
Return the stride between elements in the array returned from this-&gt;values().
Scalar & operator[](Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL &amp;&amp; (0 &lt;= i &lt; subDim()-1).
const Scalar & operator()(Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL &amp;&amp; (0 &lt;= i &lt; subDim()-1).
Scalar & operator()(Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL &amp;&amp; (0 &lt;= i &lt; subDim()-1).
Teuchos_Ordinal globalOffset() const
Returns the global offset for the explicit view.
ConstDetachedVectorView(const VectorBase< Scalar > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit non-mutable (const) view of a subset of elements.
const Scalar & operator[](Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL &amp;&amp; (0 &lt;= i &lt; subDim()-1).
DetachedVectorView(const Teuchos::RCP< VectorBase< Scalar > > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit mutable (non-const) view of a subset of elements.
const Scalar * values() const
Return a pointer to a Scalar array containing the explicit view.
Teuchos::Range1D Range1D