Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ArrayView.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) 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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_ARRAY_VIEW_HPP
43 #define TEUCHOS_ARRAY_VIEW_HPP
44 
45 
46 #include "Teuchos_ArrayViewDecl.hpp"
47 #include "Teuchos_ArrayRCP.hpp"
48 #include "Teuchos_as.hpp"
49 
50 
51 namespace Teuchos {
52 
53 
54 // Constructors/Destructors
55 
56 
57 template<class T> inline
59  : ptr_(0), size_(0)
60 {
61  setUpIterators();
62 }
63 
64 template<class T> inline
66  : ptr_(0), size_(0)
67 {
68  setUpIterators();
69 }
70 
71 
72 
73 template<class T> inline
74 ArrayView<T>::ArrayView( T* p, size_type size_in, const ERCPNodeLookup rcpNodeLookup )
75  :ptr_(p), size_(size_in)
76 {
77 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
78  TEUCHOS_TEST_FOR_EXCEPT( p != 0 && size_in <= 0 );
79  TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 );
80  // This only does something if HAVE_TEUCHOS_ARRAY_BOUNDSCHECK is defined.
81  setUpIterators(rcpNodeLookup);
82 #else
83  (void) rcpNodeLookup; // Silence "unused variable" compiler warning.
84 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
85 }
86 
87 template<class T> inline
88 ArrayView<const T>::ArrayView(const T* p, size_type size_in, const ERCPNodeLookup rcpNodeLookup )
89  : ptr_(p), size_(size_in)
90 {
91 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
92  TEUCHOS_TEST_FOR_EXCEPT( p != 0 && size_in <= 0 );
93  TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 );
94  // This only does something if HAVE_TEUCHOS_ARRAY_BOUNDSCHECK is defined.
95  setUpIterators(rcpNodeLookup);
96 #else
97  (void) rcpNodeLookup; // Silence "unused variable" compiler warning.
98 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
99 }
100 
101 
102 template<class T> inline
104  :ptr_(array.ptr_), size_(array.size_)
105 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
106  ,arcp_(array.arcp_)
107 #endif
108 {}
109 
110 template<class T> inline
112  :ptr_(array.ptr_), size_(array.size_)
113 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
114  ,arcp_(array.arcp_)
115 #endif
116 {}
117 
118 
119 template<class T> inline
121  std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
122  )
123  : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
124 {
125  setUpIterators();
126 }
127 
128 template<class T> inline
130  std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
131  )
132  : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
133 {
134  setUpIterators();
135 }
136 
137 
138 template<class T> inline
140  const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
141  )
142  : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
143 {
144  setUpIterators();
145 }
146 
147 template<class T> inline
149  const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
150  )
151  : ptr_( vec.empty() ? 0 : &vec[0] ), size_(vec.size())
152 {
153  setUpIterators();
154 }
155 
156 
157 template<class T> inline
159 {
160  ptr_ = array.ptr_;
161  size_ = array.size_;
162 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
163  arcp_ = array.arcp_;
164 #endif
165  return *this;
166 }
167 
168 template<class T> inline
170 {
171  ptr_ = array.ptr_;
172  size_ = array.size_;
173 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
174  arcp_ = array.arcp_;
175 #endif
176  return *this;
177 }
178 
179 
180 template<class T> inline
182 {}
183 
184 template<class T> inline
186 {}
187 
188 
189 // General query functions
190 
191 
192 template<class T>
193 inline
195 {
196  return ptr_ == 0;
197 }
198 
199 template<class T>
200 inline
201 bool ArrayView<const T>::is_null() const
202 {
203  return ptr_ == 0;
204 }
205 
206 
207 template<class T> inline
209 {
210  debug_assert_valid_ptr();
211  return size_;
212 }
213 
214 template<class T> inline
216 {
217  debug_assert_valid_ptr();
218  return size_;
219 }
220 
221 
222 template<typename T>
223 std::string ArrayView<T>::toString() const
224 {
225  using Teuchos::as;
226  std::ostringstream ss;
227 
228  debug_assert_valid_ptr();
229 
230  ss << "{";
231  for (size_type i = 0; i < size (); ++i) {
232  // NOTE: This depends on std::ostream::operator<<(const T&).
233  ss << operator[] (i);
234  if (i + 1 < size ()) {
235  ss << ", ";
236  }
237  }
238  ss << "}";
239  return ss.str ();
240 }
241 
242 template<typename T>
243 std::string ArrayView<const T>::toString() const
244 {
245  using Teuchos::as;
246  std::ostringstream ss;
247 
248  debug_assert_valid_ptr();
249 
250  ss << "{";
251  for (size_type i = 0; i < size (); ++i) {
252  // NOTE: This depends on std::ostream::operator<<(const T&).
253  ss << operator[] (i);
254  if (i + 1 < size ()) {
255  ss << ", ";
256  }
257  }
258  ss << "}";
259  return ss.str ();
260 }
261 
262 
263 // Specialization for float. We use sufficient precision that no
264 // digits are lost after writing to string and reading back in again.
265 template<>
266 TEUCHOSCORE_LIB_DLL_EXPORT std::string
268 
269 // Specialization for (const) float. We use sufficient precision that no
270 // digits are lost after writing to string and reading back in again.
271 template<>
272 TEUCHOSCORE_LIB_DLL_EXPORT std::string
274 
275 // Specialization for double. We use sufficient precision that no
276 // digits are lost after writing to string and reading back in again.
277 template<>
278 TEUCHOSCORE_LIB_DLL_EXPORT std::string
280 
281 // Specialization for (const) double. We use sufficient precision that no
282 // digits are lost after writing to string and reading back in again.
283 template<>
284 TEUCHOSCORE_LIB_DLL_EXPORT std::string
286 
287 
288 // Element Access Functions
289 
290 
291 template<class T> inline
293 {
294  debug_assert_valid_ptr();
295  return ptr_;
296 }
297 
298 template<class T> inline
299 const T* ArrayView<const T>::getRawPtr() const
300 {
301  debug_assert_valid_ptr();
302  return ptr_;
303 }
304 
305 
306 template<class T> inline
308 {
309  debug_assert_valid_ptr();
310  debug_assert_in_range(i,1);
311  return ptr_[i];
312 }
313 
314 template<class T> inline
315 const T& ArrayView<const T>::operator[](size_type i) const
316 {
317  debug_assert_valid_ptr();
318  debug_assert_in_range(i,1);
319  return ptr_[i];
320 }
321 
322 
323 template<class T> inline
325 {
326  debug_assert_not_null();
327  debug_assert_valid_ptr();
328  return *ptr_;
329 }
330 
331 template<class T> inline
332 const T& ArrayView<const T>::front() const
333 {
334  debug_assert_not_null();
335  debug_assert_valid_ptr();
336  return *ptr_;
337 }
338 
339 template<class T> inline
341 {
342  debug_assert_not_null();
343  debug_assert_valid_ptr();
344  return *(ptr_+size_-1);
345 }
346 
347 template<class T> inline
348 const T& ArrayView<const T>::back() const
349 {
350  debug_assert_not_null();
351  debug_assert_valid_ptr();
352  return *(ptr_+size_-1);
353 }
354 
355 
356 // Views
357 
358 
359 template<class T> inline
361 {
362  if (size_in == 0) {
363  return null;
364  }
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);
369 #endif
370  return ArrayView<T>(ptr_+offset, size_in);
371  // WARNING: The above code had better be correct since we are using raw
372  // pointer arithmetic!
373 }
374 
375 template<class T> inline
376 ArrayView<const T> ArrayView<const T>::view(size_type offset, size_type size_in) const
377 {
378  if (size_in == 0) {
379  return null;
380  }
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);
385 #endif
386  return ArrayView<const T> (ptr_+offset, size_in);
387  // WARNING: The above code had better be correct since we are using raw
388  // pointer arithmetic!
389 }
390 
391 
392 template<class T> inline
394 {
395  return view(offset, size_in);
396 }
397 
398 template<class T> inline
399 ArrayView<const T> ArrayView<const T>::operator()(size_type offset, size_type size_in) const
400 {
401  return view(offset, size_in);
402 }
403 
404 
405 template<class T> inline
407 {
408  debug_assert_valid_ptr();
409  return *this;
410 }
411 
412 template<class T> inline
414 {
415  debug_assert_valid_ptr();
416  return *this;
417 }
418 
419 
420 template<class T> inline
422 {
423  debug_assert_valid_ptr();
424 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
425  return arcp_.getConst()();
426 #endif
427  return ArrayView<const T>(ptr_, size_);
428 }
429 
430 template<class T> inline
432  return *this;
433 }
434 
435 
436 template<class T> inline
438 {
439  return getConst();
440 }
441 
442 
443 // Assignment
444 
445 
446 template<class T>
447 void ArrayView<T>::assign(const ArrayView<const T>& array) const
448 {
449  debug_assert_valid_ptr();
450  debug_assert_not_null();
451  if (this->getRawPtr()==array.getRawPtr() && this->size()==array.size())
452  return; // Assignment to self
453  debug_assert_in_range(0,array.size());
454  std::copy( array.begin(), array.end(), this->begin() );
455  // Note: Above, in debug mode, the iterators are range checked! In
456  // optimized mode, these are raw pointers which should run very fast!
457 }
458 
459 
460 // Standard Container-Like Functions
461 
462 
463 template<class T>
465 {
466  debug_assert_valid_ptr();
467 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
468  return arcp_.create_weak();
469 #else
470  return ptr_;
471 #endif
472 }
473 
474 template<class T>
476 {
477  debug_assert_valid_ptr();
478 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
479  return arcp_.create_weak();
480 #else
481  return ptr_;
482 #endif
483 }
484 
485 
486 template<class T>
488 {
489  debug_assert_valid_ptr();
490 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
491  return arcp_.create_weak() + size_;
492 #else
493  return ptr_ + size_;
494 #endif
495 }
496 
497 template<class T>
499 {
500  debug_assert_valid_ptr();
501 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
502  return arcp_.create_weak() + size_;
503 #else
504  return ptr_ + size_;
505 #endif
506 }
507 
508 
509 // Assertion Functions.
510 
511 
512 template<class T>
514 {
515  if(!ptr_)
516  throw_null_ptr_error(typeName(*this));
517  return *this;
518 }
519 
520 template<class T>
522 {
523  if(!ptr_)
524  throw_null_ptr_error(typeName(*this));
525  return *this;
526 }
527 
528 
529 template<class T>
530 const ArrayView<T>&
532 {
533  assert_not_null();
534  TEUCHOS_TEST_FOR_EXCEPTION( size_in == as<size_type>(0), RangeError,
535  "Error, size=0 is not allowed!" );
537  !(
538  ( 0 <= offset && offset+size_in <= this->size() )
539  &&
540  size_in >= 0
541  ),
542  RangeError,
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()<<")!"
546  );
547  return*this;
548 }
549 
550 template<class T>
551 const ArrayView<const T>&
552 ArrayView<const T>::assert_in_range(size_type offset, size_type size_in) const
553 {
554  assert_not_null();
555  TEUCHOS_TEST_FOR_EXCEPTION( size_in == as<size_type>(0), RangeError,
556  "Error, size=0 is not allowed!" );
558  !(
559  ( 0 <= offset && offset+size_in <= this->size() )
560  &&
561  size_in >= 0
562  ),
563  RangeError,
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()<<")!"
567  );
568  return*this;
569 }
570 
571 
572 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
573 
574 template<class T>
575 ArrayView<T>::ArrayView( const ArrayRCP<T> &arcp )
576  : ptr_(arcp.getRawPtr()), size_(arcp.size()), arcp_(arcp)
577 {}
578 
579 template<class T>
580 ArrayView<const T>::ArrayView( const ArrayRCP<const T> &arcp )
581  : ptr_(arcp.getRawPtr()), size_(arcp.size()), arcp_(arcp)
582 {}
583 
584 
585 template<class T>
586 ArrayView<T>::ArrayView(T* p, size_type size_in, const ArrayRCP<T> &arcp)
587  : ptr_(p), size_(size_in), arcp_(arcp)
588 {}
589 
590 template<class T>
591 ArrayView<const T>::ArrayView(const T* p, size_type size_in, const ArrayRCP<const T> &arcp)
592  : ptr_(p), size_(size_in), arcp_(arcp)
593 {}
594 
595 
596 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
597 
598 
599 // private
600 
601 
602 template<class T>
603 void ArrayView<T>::setUpIterators(const ERCPNodeLookup rcpNodeLookup)
604 {
605 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
606  if (ptr_ && arcp_.is_null()) {
607  arcp_ = ArrayRCP<T>(ptr_, 0, size_, false, rcpNodeLookup);
608  }
609 #else
610  (void) rcpNodeLookup; // Silence "unused variable" compiler warning.
611 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
612 }
613 
614 template<class T>
615 void ArrayView<const T>::setUpIterators(const ERCPNodeLookup rcpNodeLookup)
616 {
617 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
618  if (ptr_ && arcp_.is_null()) {
619  arcp_ = ArrayRCP<const T>(ptr_, 0, size_, false, rcpNodeLookup);
620  }
621 #else
622  (void) rcpNodeLookup; // Silence "unused variable" compiler warning.
623 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
624 }
625 
626 
627 } // namespace Teuchos
628 
629 
630 //
631 // Nonmember helper functions
632 //
633 
634 
635 template<class T> inline
637 Teuchos::arrayView( T* p, typename ArrayView<T>::size_type size )
638 {
639  if (size == 0)
640  return null;
641  return ArrayView<T>(p, size);
642 }
643 
644 
645 template<class T> inline
646 Teuchos::ArrayView<T> Teuchos::arrayViewFromVector( std::vector<T>& vec )
647 {
648  if (vec.size() == 0)
649  return null;
650  return ArrayView<T>(vec);
651 }
652 
653 
654 template<class T> inline
655 Teuchos::ArrayView<const T> Teuchos::arrayViewFromVector( const std::vector<T>& vec )
656 {
657  if (vec.size() == 0)
658  return null;
659  return ArrayView<const T>(vec);
660 }
661 
662 
663 #ifndef __sun
664 
665 template<class T> inline
666 std::vector<T> Teuchos::createVector( const ArrayView<T> &av )
667 {
668  std::vector<T> v(av.begin(), av.end());
669  return v;
670 }
671 
672 #endif // __sun
673 
674 
675 template<class T> inline
676 std::vector<T> Teuchos::createVector( const ArrayView<const T> &av )
677 {
678  std::vector<T> v(av.begin(), av.end());
679  return v;
680 }
681 
682 
683 template<class T> inline
684 bool Teuchos::is_null( const ArrayView<T> &av )
685 {
686  return av.is_null();
687 }
688 
689 
690 template<class T> inline
691 bool Teuchos::nonnull( const ArrayView<T> &av )
692 {
693  return !av.is_null();
694 }
695 
696 
697 template<class T>
698 std::ostream& Teuchos::operator<<( std::ostream& out, const ArrayView<T>& p )
699 {
700  return out << p.toString();
701 }
702 
703 
704 template<class T2, class T1>
705 REFCOUNTPTR_INLINE
707 Teuchos::av_const_cast(const ArrayView<T1>& p1)
708 {
709  T2 *ptr2 = const_cast<T2*>(p1.getRawPtr());
710  return ArrayView<T2>(ptr2, p1.size());
711  // Note: Above is just fine even if p1.get()==NULL!
712 }
713 
714 
715 template<class T2, class T1>
716 REFCOUNTPTR_INLINE
718 Teuchos::av_reinterpret_cast(const ArrayView<T1>& p1)
719 {
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>(
726  ptr2, size2
727 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
728  ,arcp_reinterpret_cast<T2>(p1.access_private_arcp())
729 #endif
730  );
731  // Note: Above is just fine even if p1.get()==NULL!
732 }
733 
734 
735 #endif // TEUCHOS_ARRAY_VIEW_HPP
std::string toString() const
Convert an ArrayView&lt;T&gt; 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-&gt;get()==NULL orthis-&gt;get()!=NULL, throws RangeError if (offset &lt; 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.
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-&gt;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.
Nonowning array view.
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.
Definition: Teuchos_as.hpp:287
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.