10 #ifndef TEUCHOS_ARRAY_VIEW_HPP
11 #define TEUCHOS_ARRAY_VIEW_HPP
14 #include "Teuchos_ArrayViewDecl.hpp"
15 #include "Teuchos_ArrayRCP.hpp"
25 template<
class T>
inline
32 template<
class T>
inline
41 template<
class T>
inline
43 :ptr_(size_in == 0 ? nullptr : p), size_(size_in)
45 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
59 setUpIterators(rcpNodeLookup);
62 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
65 template<
class T>
inline
67 : ptr_(size_in == 0 ? nullptr : p), size_(size_in)
69 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
83 setUpIterators(rcpNodeLookup);
86 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
90 template<
class T>
inline
92 :ptr_(array.ptr_), size_(array.size_)
93 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
98 template<
class T>
inline
100 :ptr_(array.ptr_), size_(array.size_)
101 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
107 template<
class T>
inline
109 std::vector<
typename std::remove_const_t<T>>& vec
111 : ptr_( vec.empty() ? 0 : vec.data() ), size_(vec.size())
116 template<
class T>
inline
118 std::vector<
typename std::remove_const_t<T>>& vec
120 : ptr_( vec.empty() ? 0 : vec.data() ), size_(vec.size())
126 template<
class T>
inline
128 const std::vector<
typename std::remove_const_t<T>>& vec
130 : ptr_( vec.empty() ? 0 : vec.data() ), size_(vec.size())
135 template<
class T>
inline
137 const std::vector<
typename std::remove_const_t<T>>& vec
139 : ptr_( vec.empty() ? 0 : vec.data() ), size_(vec.size())
145 template<
class T>
inline
150 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
156 template<
class T>
inline
161 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
168 template<
class T>
inline
172 template<
class T>
inline
195 template<
class T>
inline
198 debug_assert_valid_ptr();
202 template<
class T>
inline
205 debug_assert_valid_ptr();
214 std::ostringstream ss;
216 debug_assert_valid_ptr();
219 for (
size_type i = 0; i < size (); ++i) {
221 ss << operator[] (i);
222 if (i + 1 < size ()) {
234 std::ostringstream ss;
236 debug_assert_valid_ptr();
242 if (i + 1 <
size ()) {
254 TEUCHOSCORE_LIB_DLL_EXPORT std::string
260 TEUCHOSCORE_LIB_DLL_EXPORT std::string
266 TEUCHOSCORE_LIB_DLL_EXPORT std::string
272 TEUCHOSCORE_LIB_DLL_EXPORT std::string
279 template<
class T>
inline
282 debug_assert_valid_ptr();
286 template<
class T>
inline
289 debug_assert_valid_ptr();
293 template<
class T>
inline
296 debug_assert_valid_ptr();
300 template<
class T>
inline
303 debug_assert_valid_ptr();
307 template<
class T>
inline
310 debug_assert_valid_ptr();
311 debug_assert_in_range(i,1);
315 template<
class T>
inline
318 debug_assert_valid_ptr();
319 debug_assert_in_range(i,1);
324 template<
class T>
inline
327 debug_assert_not_null();
328 debug_assert_valid_ptr();
332 template<
class T>
inline
335 debug_assert_not_null();
336 debug_assert_valid_ptr();
340 template<
class T>
inline
343 debug_assert_not_null();
344 debug_assert_valid_ptr();
345 return *(ptr_+size_-1);
348 template<
class T>
inline
351 debug_assert_not_null();
352 debug_assert_valid_ptr();
353 return *(ptr_+size_-1);
360 template<
class T>
inline
363 if (size_in == 0) {
return null; }
364 debug_assert_valid_ptr();
365 debug_assert_in_range(offset, size_in);
368 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
369 ,arcp_.persistingView(offset, size_in)
376 template<
class T>
inline
379 if (size_in == 0) {
return null; }
380 debug_assert_valid_ptr();
381 debug_assert_in_range(offset, size_in);
382 return ArrayView<const T>(
384 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
385 ,arcp_.persistingView(offset, size_in)
393 template<
class T>
inline
396 return view(offset, size_in);
399 template<
class T>
inline
402 return view(offset, size_in);
406 template<
class T>
inline
409 debug_assert_valid_ptr();
413 template<
class T>
inline
416 debug_assert_valid_ptr();
421 template<
class T>
inline
424 debug_assert_valid_ptr();
425 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
432 template<
class T>
inline
438 template<
class T>
inline
451 debug_assert_valid_ptr();
452 debug_assert_not_null();
453 if (this->getRawPtr()==array.getRawPtr() && this->size()==array.size())
455 debug_assert_in_range(0,array.size());
456 std::copy( array.begin(), array.end(), this->begin() );
468 debug_assert_valid_ptr();
469 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
470 return arcp_.create_weak();
479 debug_assert_valid_ptr();
480 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
481 return arcp_.create_weak();
491 debug_assert_valid_ptr();
492 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
493 return arcp_.create_weak() + size_;
502 debug_assert_valid_ptr();
503 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
504 return arcp_.create_weak() + size_;
518 throw_null_ptr_error(
typeName(*
this));
526 throw_null_ptr_error(
typeName(*
this));
537 "Error, size=0 is not allowed!" );
540 ( 0 <= offset && offset+size_in <= this->size() )
545 typeName(*
this)<<
"::assert_in_range():"
546 " Error, [offset,offset+size) = ["<<offset<<
","<<(offset+size_in)<<
")"
547 " does not lie in the range [0,"<<this->size()<<
")!"
558 "Error, size=0 is not allowed!" );
561 ( 0 <= offset && offset+size_in <= this->
size() )
566 typeName(*
this)<<
"::assert_in_range():"
567 " Error, [offset,offset+size) = ["<<offset<<
","<<(offset+size_in)<<
")"
568 " does not lie in the range [0,"<<this->
size()<<
")!"
574 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
578 : ptr_(arcp.getRawPtr()), size_(arcp.size()), arcp_(arcp)
583 : ptr_(arcp.getRawPtr()), size_(arcp.size()), arcp_(arcp)
589 : ptr_(p), size_(size_in), arcp_(arcp)
594 : ptr_(p), size_(size_in), arcp_(arcp)
598 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
605 void ArrayView<T>::setUpIterators(
const ERCPNodeLookup rcpNodeLookup)
607 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
608 if (ptr_ && arcp_.is_null()) {
609 arcp_ = ArrayRCP<T>(ptr_, 0, size_,
false, rcpNodeLookup);
612 (void) rcpNodeLookup;
613 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
617 void ArrayView<const T>::setUpIterators(
const ERCPNodeLookup rcpNodeLookup)
619 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
620 if (ptr_ && arcp_.is_null()) {
621 arcp_ = ArrayRCP<const T>(ptr_, 0, size_,
false, rcpNodeLookup);
624 (void) rcpNodeLookup;
625 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
637 template<
class T>
inline
639 Teuchos::arrayView( T* p,
typename ArrayView<T>::size_type
size )
643 return ArrayView<T>(p,
size);
647 template<
class T>
inline
652 return ArrayView<T>(vec);
656 template<
class T>
inline
661 return ArrayView<const T>(vec);
667 template<
class T>
inline
668 std::vector<T> Teuchos::createVector(
const ArrayView<T> &av )
670 std::vector<T> v(av.begin(), av.end());
677 template<
class T>
inline
678 std::vector<T> Teuchos::createVector(
const ArrayView<const T> &av )
680 std::vector<T> v(av.begin(), av.end());
685 template<
class T>
inline
692 template<
class T>
inline
695 return !av.is_null();
700 std::ostream& Teuchos::operator<<( std::ostream& out, const ArrayView<T>& p )
702 return out << p.toString();
706 template<
class T2,
class T1>
709 Teuchos::av_const_cast(
const ArrayView<T1>& p1)
711 T2 *ptr2 =
const_cast<T2*
>(p1.getRawPtr());
712 return ArrayView<T2>(ptr2, p1.size());
717 template<
class T2,
class T1>
720 Teuchos::av_reinterpret_cast(
const ArrayView<T1>& p1)
722 typedef typename ArrayView<T1>::size_type size_type;
723 const int sizeOfT1 =
sizeof(T1);
724 const int sizeOfT2 =
sizeof(T2);
725 size_type size2 = (p1.size()*sizeOfT1) / sizeOfT2;
726 T2 *ptr2 =
reinterpret_cast<T2*
>(p1.getRawPtr());
727 return ArrayView<T2>(
729 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
737 #endif // TEUCHOS_ARRAY_VIEW_HPP
std::string toString() const
Convert an ArrayView<T> to an std::string
T & front() const
Get the first element.
pointer iterator
Type of a nonconst iterator.
bool is_null() const
Returns true if the underlying pointer is null.
const ArrayView< T > & assert_in_range(size_type offset, size_type size) const
Throws NullReferenceError if this->get()==NULL orthis->get()!=NULL, throws RangeError if (offset < 0 ...
iterator begin() const
Return an iterator to beginning of the array of data.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Ordinal size_type
Type representing the number of elements in an ArrayRCP or view thereof.
size_type size() const
The total number of items in the managed array.
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
T * data() const
Return a raw pointer to beginning of array.
ArrayRCP< T2 > arcp_reinterpret_cast(const ArrayRCP< T1 > &p1)
Reinterpret cast of underlying ArrayRCP type from T1* to T2*.
void assign(const ArrayView< const T > &array) const
Copy the data from one array view object to this array view object.
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
T * getRawPtr() const
Return a raw pointer to beginning of array or NULL if unsized.
const ArrayView< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
const ArrayView< T > & operator()() const
Return *this (just for compatibility with Array and ArrayPtr).
ERCPNodeLookup
Used to determine if RCPNode lookup is performed or not.
ArrayView(ENull null_arg=null)
Constructor that initializes to NULL (implicitly or explicitly).
iterator end() const
Return an iterator to past the end of the array of data.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
ArrayView< T > view(size_type offset, size_type size) const
Return a view of a contiguous range of elements.
Partial specialization of ArrayView for const T.
Range error exception class.
T & operator[](size_type i) const
Random object access.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
ArrayView< T > & operator=(const ArrayView< T > &array)
Shallow copy assignment operator.
Definition of Teuchos::as, for conversions between types.
ArrayView< const T > getConst() const
Return a const view of a possibly nonconst view.
T & back() const
Get the last element.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.