42 #ifndef TEUCHOS_ARRAY_VIEW_HPP
43 #define TEUCHOS_ARRAY_VIEW_HPP
46 #include "Teuchos_ArrayViewDecl.hpp"
47 #include "Teuchos_ArrayRCP.hpp"
57 template<
class T>
inline
64 template<
class T>
inline
73 template<
class T>
inline
75 :ptr_(p), size_(size_in)
77 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
81 setUpIterators(rcpNodeLookup);
84 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
87 template<
class T>
inline
89 : ptr_(p), size_(size_in)
91 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
95 setUpIterators(rcpNodeLookup);
98 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
102 template<
class T>
inline
104 :ptr_(array.ptr_), size_(array.size_)
105 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
110 template<
class T>
inline
112 :ptr_(array.ptr_), size_(array.size_)
113 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
119 template<
class T>
inline
123 : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
128 template<
class T>
inline
132 : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
138 template<
class T>
inline
142 : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
147 template<
class T>
inline
151 : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
157 template<
class T>
inline
162 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
168 template<
class T>
inline
173 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
180 template<
class T>
inline
184 template<
class T>
inline
207 template<
class T>
inline
210 debug_assert_valid_ptr();
214 template<
class T>
inline
217 debug_assert_valid_ptr();
226 std::ostringstream ss;
228 debug_assert_valid_ptr();
231 for (
size_type i = 0; i < size (); ++i) {
233 ss << operator[] (i);
234 if (i + 1 < size ()) {
246 std::ostringstream ss;
248 debug_assert_valid_ptr();
254 if (i + 1 <
size ()) {
266 TEUCHOSCORE_LIB_DLL_EXPORT std::string
272 TEUCHOSCORE_LIB_DLL_EXPORT std::string
278 TEUCHOSCORE_LIB_DLL_EXPORT std::string
284 TEUCHOSCORE_LIB_DLL_EXPORT std::string
291 template<
class T>
inline
294 debug_assert_valid_ptr();
298 template<
class T>
inline
301 debug_assert_valid_ptr();
306 template<
class T>
inline
309 debug_assert_valid_ptr();
310 debug_assert_in_range(i,1);
314 template<
class T>
inline
317 debug_assert_valid_ptr();
318 debug_assert_in_range(i,1);
323 template<
class T>
inline
326 debug_assert_not_null();
327 debug_assert_valid_ptr();
331 template<
class T>
inline
334 debug_assert_not_null();
335 debug_assert_valid_ptr();
339 template<
class T>
inline
342 debug_assert_not_null();
343 debug_assert_valid_ptr();
344 return *(ptr_+size_-1);
347 template<
class T>
inline
350 debug_assert_not_null();
351 debug_assert_valid_ptr();
352 return *(ptr_+size_-1);
359 template<
class T>
inline
365 debug_assert_valid_ptr();
366 debug_assert_in_range(offset, size_in);
367 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
368 return arcp_(offset, size_in);
375 template<
class T>
inline
381 debug_assert_valid_ptr();
382 debug_assert_in_range(offset, size_in);
383 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
384 return arcp_(offset, size_in);
386 return ArrayView<const T> (ptr_+offset, size_in);
392 template<
class T>
inline
395 return view(offset, size_in);
398 template<
class T>
inline
401 return view(offset, size_in);
405 template<
class T>
inline
408 debug_assert_valid_ptr();
412 template<
class T>
inline
415 debug_assert_valid_ptr();
420 template<
class T>
inline
423 debug_assert_valid_ptr();
424 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
425 return arcp_.getConst()();
430 template<
class T>
inline
436 template<
class T>
inline
449 debug_assert_valid_ptr();
450 debug_assert_not_null();
451 if (this->getRawPtr()==array.getRawPtr() && this->size()==array.size())
453 debug_assert_in_range(0,array.size());
454 std::copy( array.begin(), array.end(), this->begin() );
466 debug_assert_valid_ptr();
467 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
468 return arcp_.create_weak();
477 debug_assert_valid_ptr();
478 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
479 return arcp_.create_weak();
489 debug_assert_valid_ptr();
490 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
491 return arcp_.create_weak() + size_;
500 debug_assert_valid_ptr();
501 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
502 return arcp_.create_weak() + size_;
516 throw_null_ptr_error(
typeName(*
this));
524 throw_null_ptr_error(
typeName(*
this));
535 "Error, size=0 is not allowed!" );
538 ( 0 <= offset && offset+size_in <= this->size() )
543 typeName(*
this)<<
"::assert_in_range():"
544 " Error, [offset,offset+size) = ["<<offset<<
","<<(offset+size_in)<<
")"
545 " does not lie in the range [0,"<<this->size()<<
")!"
556 "Error, size=0 is not allowed!" );
559 ( 0 <= offset && offset+size_in <= this->
size() )
564 typeName(*
this)<<
"::assert_in_range():"
565 " Error, [offset,offset+size) = ["<<offset<<
","<<(offset+size_in)<<
")"
566 " does not lie in the range [0,"<<this->
size()<<
")!"
572 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
576 : ptr_(arcp.getRawPtr()), size_(arcp.size()), arcp_(arcp)
581 : ptr_(arcp.getRawPtr()), size_(arcp.size()), arcp_(arcp)
587 : ptr_(p), size_(size_in), arcp_(arcp)
592 : ptr_(p), size_(size_in), arcp_(arcp)
596 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
603 void ArrayView<T>::setUpIterators(
const ERCPNodeLookup rcpNodeLookup)
605 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
606 if (ptr_ && arcp_.is_null()) {
607 arcp_ = ArrayRCP<T>(ptr_, 0, size_,
false, rcpNodeLookup);
610 (void) rcpNodeLookup;
611 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
615 void ArrayView<const T>::setUpIterators(
const ERCPNodeLookup rcpNodeLookup)
617 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
618 if (ptr_ && arcp_.is_null()) {
619 arcp_ = ArrayRCP<const T>(ptr_, 0, size_,
false, rcpNodeLookup);
622 (void) rcpNodeLookup;
623 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
635 template<
class T>
inline
637 Teuchos::arrayView( T* p,
typename ArrayView<T>::size_type
size )
641 return ArrayView<T>(p,
size);
645 template<
class T>
inline
650 return ArrayView<T>(vec);
654 template<
class T>
inline
659 return ArrayView<const T>(vec);
665 template<
class T>
inline
666 std::vector<T> Teuchos::createVector(
const ArrayView<T> &av )
668 std::vector<T> v(av.begin(), av.end());
675 template<
class T>
inline
676 std::vector<T> Teuchos::createVector(
const ArrayView<const T> &av )
678 std::vector<T> v(av.begin(), av.end());
683 template<
class T>
inline
690 template<
class T>
inline
693 return !av.is_null();
698 std::ostream& Teuchos::operator<<( std::ostream& out, const ArrayView<T>& p )
700 return out << p.toString();
704 template<
class T2,
class T1>
707 Teuchos::av_const_cast(
const ArrayView<T1>& p1)
709 T2 *ptr2 =
const_cast<T2*
>(p1.getRawPtr());
710 return ArrayView<T2>(ptr2, p1.size());
715 template<
class T2,
class T1>
718 Teuchos::av_reinterpret_cast(
const ArrayView<T1>& p1)
720 typedef typename ArrayView<T1>::size_type size_type;
721 const int sizeOfT1 =
sizeof(T1);
722 const int sizeOfT2 =
sizeof(T2);
723 size_type size2 = (p1.size()*sizeOfT1) / sizeOfT2;
724 T2 *ptr2 =
reinterpret_cast<T2*
>(p1.getRawPtr());
725 return ArrayView<T2>(
727 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
735 #endif // TEUCHOS_ARRAY_VIEW_HPP
std::string toString() const
Convert an ArrayView<T> to an std::string
T & front() const
Get the first element.
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
UnConst< T >::Result NonConstType
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!
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).
bool nonnull(const boost::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
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.