Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ArrayViewDecl.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_DECL_HPP
43 #define TEUCHOS_ARRAY_VIEW_DECL_HPP
44 
45 
46 #include "Teuchos_RCPNode.hpp"
47 #include "Teuchos_ENull.hpp"
48 #include "Teuchos_NullIteratorTraits.hpp"
49 #include "Teuchos_ConstTypeTraits.hpp"
50 #include <vector>
51 
52 namespace Teuchos {
53 
54 // Forward declaration; ArrayView uses ArrayRCP in debug mode.
55 template<class T> class ArrayRCP;
56 
122 template<class T>
123 class ArrayView {
124 public:
126 
127 
129  typedef Teuchos_Ordinal Ordinal;
130 
133 
136 
138  typedef T value_type;
139 
143  typedef T* pointer;
144 
146  typedef const T* const_pointer;
147 
151  typedef T& reference;
152 
154  typedef const T& const_reference;
155 
156 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
157  typedef ArrayRCP<T> iterator;
161 #else
162  typedef pointer iterator;
166 #endif
167 
169 
171 
173  ArrayView( ENull null_arg = null );
174 
190  ArrayView (T* p, size_type size,
191  const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
192 
203  ArrayView (const ArrayView<T>& array);
204 
206  ArrayView (std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
207 
209  ArrayView (const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
210 
212  ArrayView<T>& operator= (const ArrayView<T>& array);
213 
215  ~ArrayView();
216 
218 
220 
222  bool is_null() const;
223 
225  size_type size() const;
226 
228  std::string toString() const;
229 
231 
233 
235  inline T* getRawPtr() const;
236 
240  inline T* data() const;
241 
249  T& operator[](size_type i) const;
250 
252  T& front() const;
253 
255  T& back() const;
256 
258 
260 
275  ArrayView<T> view( size_type offset, size_type size ) const;
276 
281 
283  const ArrayView<T>& operator() () const;
284 
287 
295  operator ArrayView<const T>() const;
296 
298 
300 
319  void assign (const ArrayView<const T>& array) const;
320 
322 
324 
337  iterator begin() const;
338 
351  iterator end() const;
352 
354 
356 
360  const ArrayView<T>& assert_not_null() const;
361 
366  const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
367 
369 
370 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
371 
372  // I should make these private but templated friends are not very portable.
373  // Besides, if a client directly calls this it will not compile in an
374  // optimized build.
375 
376  explicit ArrayView( const ArrayRCP<T> &arcp );
377 
378  explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
379 
380 #endif
381 
382 private:
383  T *ptr_; //<! Pointer to the data
384  size_type size_; //<! Number of entries in the view
385 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
386  ArrayRCP<T> arcp_;
387 #endif
388 
389  void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
390 
391  void debug_assert_not_null() const {
392 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
393  assert_not_null();
394 #endif
395  }
396 
397  void debug_assert_in_range( size_type offset, size_type size_in ) const {
398  (void)offset; (void)size_in;
399 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
400  assert_in_range(offset, size_in);
401 #endif
402  }
403 
404  void debug_assert_valid_ptr() const {
405 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
406  arcp_.access_private_node().assert_valid_ptr(*this);
407 #endif
408  }
409 
410 public: // Bad bad bad
411 
412  // This is a very bad breach of encapsulation but it exists to avoid
413  // problems with portability of tempalted friends
414  T* access_private_ptr() const
415  { return ptr_; }
416 
417 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
418  ArrayRCP<T> access_private_arcp() const
419  { return arcp_; }
420 #endif
421 
422 };
423 
424 
432 template<class T>
433 class ArrayView<const T> {
434 public:
435  typedef Teuchos_Ordinal Ordinal;
436  typedef Ordinal size_type;
437  typedef Ordinal difference_type;
438  typedef const T value_type;
439  typedef const T* pointer;
440  typedef const T* const_pointer;
441  typedef const T& reference;
442  typedef const T& const_reference;
443 
444 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
445  typedef ArrayRCP<const T> iterator;
447 #else
448  typedef pointer iterator;
449  typedef const_pointer const_iterator;
450 #endif
451 
452  ArrayView( ENull null_arg = null );
453 
454  ArrayView (const T* p, size_type size,
455  const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
456 
457  ArrayView (const ArrayView<const T>& array);
458 
459  ArrayView (std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
460 
461  ArrayView (const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
462 
464 
465  ~ArrayView();
466 
467  bool is_null() const;
468 
469  size_type size() const;
470 
471  std::string toString() const;
472 
473  inline const T* getRawPtr() const;
474 
475  inline const T* data() const;
476 
477  const T& operator[] (size_type i) const;
478 
479  const T& front() const;
480 
481  const T& back() const;
482 
483  ArrayView<const T> view( size_type offset, size_type size ) const;
484 
485  ArrayView<const T> operator()( size_type offset, size_type size ) const;
486 
487  const ArrayView<const T>& operator()() const;
488 
495 
496  iterator begin() const;
497 
498  iterator end() const;
499 
500  const ArrayView<const T>& assert_not_null() const;
501 
502  const ArrayView<const T>& assert_in_range( size_type offset, size_type size ) const;
503 
504 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
505 
506  // I should make these private but templated friends are not very
507  // portable. Besides, if a client directly calls this it will not
508  // compile in an optimized build.
509 
510  explicit ArrayView (const ArrayRCP<const T> &arcp );
511 
512  explicit ArrayView (const T* p, size_type size, const ArrayRCP<const T> &arcp );
513 
514 #endif
515 
516 private:
517  const T* ptr_;
518  int size_;
519 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
520  ArrayRCP<const T> arcp_;
521 #endif
522 
523  void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
524 
525  void debug_assert_not_null() const {
526 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
527  assert_not_null();
528 #endif
529  }
530 
531  void debug_assert_in_range( size_type offset, size_type size_in ) const {
532  (void)offset; (void)size_in;
533 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
534  assert_in_range(offset, size_in);
535 #endif
536  }
537 
538  void debug_assert_valid_ptr() const {
539 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
540  arcp_.access_private_node().assert_valid_ptr(*this);
541 #endif
542  }
543 
544 public: // Bad bad bad
545 
546  // This is a very bad breach of encapsulation but it exists to avoid
547  // problems with portability of templated friends
548  const T* access_private_ptr() const { return ptr_; }
549 
550 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
551  ArrayRCP<const T> access_private_arcp() const { return arcp_; }
552 #endif
553 };
554 
555 
556 
561 template<class T>
562 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
563 
564 
569 template<class T>
570 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
571 
572 
577 template<class T>
578 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
579 
580 
581 #ifndef __sun
582 
583 
584 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
585 // a call to this function below is ambiguous. However, if you just comment
586 // the function out, then the code on g++ (3.4.6 at least) will not compile.
587 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
588 
589 
597 template<class T>
598 std::vector<T> createVector( const ArrayView<T> &av );
599 
600 
601 #endif // __sun
602 
603 
611 template<class T>
612 std::vector<T> createVector( const ArrayView<const T> &av );
613 
614 
619 template<class T>
620 bool is_null( const ArrayView<T> &av );
621 
622 
627 template<class T>
628 bool nonnull( const ArrayView<T> &av );
629 
630 
638 template<class T>
639 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
640 
641 
650 template<class T2, class T1>
651 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
652 
653 
666 template<class T2, class T1>
667 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
668 
669 
670 } // end namespace Teuchos
671 
672 
673 //
674 // Inline members
675 //
676 
677 
678 // ToDo: Fill in!
679 
680 
681 #endif // TEUCHOS_ARRAY_VIEW_DECL_HPP
T & reference
Type of a reference to an array element.
std::string toString() const
Convert an ArrayView&lt;T&gt; to an std::string
T & front() const
Get the first element.
const T * const_pointer
Type of a const pointer to an array 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-&gt;get()==NULL orthis-&gt;get()!=NULL, throws RangeError if (offset &lt; 0 ...
Partial specialization of ArrayRCP for const T.
const T & const_reference
Type of a const reference to an array element.
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.
const_pointer const_iterator
Type of a const iterator.
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.
void assign(const ArrayView< const T > &array) const
Copy the data from one array view object to this array view object.
Teuchos_Ordinal Ordinal
Integer index type used throughout ArrayView.
T * pointer
Type of a pointer to an array element.
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.
T value_type
Type of each array element.
const ArrayView< T > & operator()() const
Return *this (just for compatibility with Array and ArrayPtr).
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.
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.
T & operator[](size_type i) const
Random object access.
Reference-counted pointer node classes.
Ordinal difference_type
Type representing the difference between two size_type values.
ArrayView< T > & operator=(const ArrayView< T > &array)
Shallow copy assignment operator.
ArrayView< const T > getConst() const
Return a const view of a possibly nonconst view.
T & back() const
Get the last element.
Reference-counted smart pointer for managing arrays.