Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
52namespace Teuchos {
53
54// Forward declaration; ArrayView uses ArrayRCP in debug mode.
55template<class T> class ArrayRCP;
56
122template<class T>
124public:
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
158 typedef ArrayRCP<T> iterator;
161#else
166#endif
167
169
171
173 ArrayView( ENull null_arg = null );
174
191 const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
192
204
206 ArrayView (std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
207
209 ArrayView (const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
210
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
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
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
382private:
383 T *ptr_; //<! Pointer to the data
384 size_type size_; //<! Number of entries in the view
385#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
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
394#endif
395 }
396
397 void debug_assert_in_range( size_type offset, size_type size_in ) const {
399#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
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
410public: // 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
432template<class T>
434public:
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
447#else
448 typedef pointer 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
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
484
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
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
516private:
517 const T* ptr_;
518 size_type size_;
519#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
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
528#endif
529 }
530
531 void debug_assert_in_range( size_type offset, size_type size_in ) const {
533#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
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
544public: // 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
561template<class T>
563
564
569template<class T>
570ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
571
572
577template<class T>
578ArrayView<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
597template<class T>
598std::vector<T> createVector( const ArrayView<T> &av );
599
600
601#endif // __sun
602
603
611template<class T>
612std::vector<T> createVector( const ArrayView<const T> &av );
613
614
619template<class T>
620bool is_null( const ArrayView<T> &av );
621
622
627template<class T>
628bool nonnull( const ArrayView<T> &av );
629
630
638template<class T>
639std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
640
641
650template<class T2, class T1>
652
653
666template<class T2, class T1>
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
Reference-counted pointer node classes.
ArrayView< const T > getConst() const
Return a const view of *this.
Nonowning array view.
ArrayView< const T > arrayViewFromVector(const std::vector< T > &vec)
Construct a const view of an std::vector.
T * pointer
Type of a pointer to an array element.
bool is_null() const
Returns true if the underlying pointer is null.
iterator end() const
Return an iterator to past the end of the array of data.
ArrayView< T2 > av_const_cast(const ArrayView< T1 > &p1)
Const cast of underlying ArrayView type from const T* to T*.
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.
ArrayView(ENull null_arg=null)
Constructor that initializes to NULL (implicitly or explicitly).
T & front() const
Get the first element.
const T * const_pointer
Type of a const pointer to an array element.
Teuchos_Ordinal Ordinal
Integer index type used throughout ArrayView.
bool nonnull(const ArrayView< T > &av)
Returns true if av.get()!=NULL.
T & reference
Type of a reference to an array element.
Ordinal difference_type
Type representing the difference between two size_type values.
ArrayView< T > arrayViewFromVector(std::vector< T > &vec)
Construct a non-const view of an std::vector.
ArrayView< T > & operator=(const ArrayView< T > &array)
Shallow copy assignment operator.
const ArrayView< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
const_pointer const_iterator
Type of a const iterator.
std::vector< T > createVector(const ArrayView< T > &av)
Get a new std::vector<T> object out of an ArrayView<T> object.
ArrayView< T > view(size_type offset, size_type size) const
Return a view of a contiguous range of elements.
const T & const_reference
Type of a const reference to an array element.
T * data() const
Return a raw pointer to beginning of array.
ArrayView< T2 > av_reinterpret_cast(const ArrayView< T1 > &p1)
Reinterpret cast of underlying ArrayView type from T1* to T2*.
ArrayView< const T > getConst() const
Return a const view of a possibly nonconst view.
std::ostream & operator<<(std::ostream &out, const ArrayView< T > &av)
Output stream inserter.
std::vector< T > createVector(const ArrayView< const T > &av)
Get a new std::vector<T> object out of an ArrayView<const T> object.
const ArrayView< T > & operator()() const
Return *this (just for compatibility with Array and ArrayPtr).
pointer iterator
Type of a nonconst iterator.
size_type size() const
The total number of items in the managed array.
T & back() const
Get the last element.
void assign(const ArrayView< const T > &array) const
Copy the data from one array view object to this array view object.
std::string toString() const
Convert an ArrayView<T> to an std::string
T value_type
Type of each array element.
bool is_null(const ArrayView< T > &av)
Returns true if av.is_null()==true.
T * getRawPtr() const
Return a raw pointer to beginning of array or NULL if unsized.
T & operator[](size_type i) const
Random object access.
Ordinal size_type
Type representing the number of elements in an ArrayRCP or view thereof.
ArrayView< T > arrayView(T *p, typename ArrayView< T >::size_type size)
Construct a const or non-const view to const or non-const data.
Smart reference counting pointer class for automatic garbage collection.
const RCP< T > & assert_valid_ptr() const
If the object pointer is non-null, assert that it is still valid.
ERCPNodeLookup
Used to determine if RCPNode lookup is performed or not.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...