Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_ArrayViewDecl.hpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_ARRAY_VIEW_DECL_HPP
11#define TEUCHOS_ARRAY_VIEW_DECL_HPP
12
13
14#include "Teuchos_RCPNode.hpp"
15#include "Teuchos_ENull.hpp"
16#include "Teuchos_NullIteratorTraits.hpp"
17#include <vector>
18
19namespace Teuchos {
20
21// Forward declaration; ArrayView uses ArrayRCP in debug mode.
22template<class T> class ArrayRCP;
23
89template<class T>
90class ArrayView {
91public:
93
94
96 typedef Teuchos_Ordinal Ordinal;
97
100
103
105 typedef T value_type;
106
110 typedef T* pointer;
111
113 typedef const T* const_pointer;
114
118 typedef T& reference;
119
121 typedef const T& const_reference;
122
123#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
125 typedef ArrayRCP<T> iterator;
128#else
133#endif
134
136
138
140 ArrayView( ENull null_arg = null );
141
157 ArrayView (T* p, size_type size,
158 const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
159
170 ArrayView (const ArrayView<T>& array);
171
173 ArrayView (std::vector<typename std::remove_const_t<T>>& vec);
174
176 ArrayView (const std::vector<typename std::remove_const_t<T>>& vec);
177
179 ArrayView<T>& operator= (const ArrayView<T>& array);
180
182 ~ArrayView();
183
185
187
189 bool is_null() const;
190
192 size_type size() const;
193
195 std::string toString() const;
196
198
200
202 inline T* getRawPtr() const;
203
207 inline T* data() const;
208
216 T& operator[](size_type i) const;
217
219 T& front() const;
220
222 T& back() const;
223
225
227
242 ArrayView<T> view( size_type offset, size_type size ) const;
243
248
250 const ArrayView<T>& operator() () const;
251
254
262 operator ArrayView<const T>() const;
263
265
267
286 void assign (const ArrayView<const T>& array) const;
287
289
291
304 iterator begin() const;
305
318 iterator end() const;
319
321
323
327 const ArrayView<T>& assert_not_null() const;
328
333 const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
334
336
337#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
338
339 // I should make these private but templated friends are not very portable.
340 // Besides, if a client directly calls this it will not compile in an
341 // optimized build.
342
343 explicit ArrayView( const ArrayRCP<T> &arcp );
344
345 explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
346
347#endif
348
349private:
350 T *ptr_; //<! Pointer to the data
351 size_type size_; //<! Number of entries in the view
352#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
353 ArrayRCP<T> arcp_;
354#endif
355
356 void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
357
358 void debug_assert_not_null() const {
359#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
361#endif
362 }
363
364 void debug_assert_in_range( size_type offset, size_type size_in ) const {
365 (void)offset; (void)size_in;
366#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
367 assert_in_range(offset, size_in);
368#endif
369 }
370
371 void debug_assert_valid_ptr() const {
372#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
373 arcp_.access_private_node().assert_valid_ptr(*this);
374#endif
375 }
376
377public: // Bad bad bad
378
379 // This is a very bad breach of encapsulation but it exists to avoid
380 // problems with portability of tempalted friends
381 T* access_private_ptr() const
382 { return ptr_; }
383
384#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
385 ArrayRCP<T> access_private_arcp() const
386 { return arcp_; }
387#endif
388
389};
390
391
399template<class T>
400class ArrayView<const T> {
401public:
402 typedef Teuchos_Ordinal Ordinal;
403 typedef Ordinal size_type;
404 typedef Ordinal difference_type;
405 typedef const T value_type;
406 typedef const T* pointer;
407 typedef const T* const_pointer;
408 typedef const T& reference;
409 typedef const T& const_reference;
410
411#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
412 typedef ArrayRCP<const T> iterator;
413 typedef ArrayRCP<const T> const_iterator;
414#else
415 typedef pointer iterator;
416 typedef const_pointer const_iterator;
417#endif
418
419 ArrayView( ENull null_arg = null );
420
421 ArrayView (const T* p, size_type size,
422 const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
423
424 ArrayView (const ArrayView<const T>& array);
425
426 ArrayView (std::vector<typename std::remove_const_t<T>>& vec);
427
428 ArrayView (const std::vector<typename std::remove_const_t<T>>& vec);
429
430 ArrayView<const T>& operator= (const ArrayView<const T>& array);
431
432 ~ArrayView();
433
434 bool is_null() const;
435
436 size_type size() const;
437
438 std::string toString() const;
439
440 inline const T* getRawPtr() const;
441
442 inline const T* data() const;
443
444 const T& operator[] (size_type i) const;
445
446 const T& front() const;
447
448 const T& back() const;
449
450 ArrayView<const T> view( size_type offset, size_type size ) const;
451
452 ArrayView<const T> operator()( size_type offset, size_type size ) const;
453
454 const ArrayView<const T>& operator()() const;
455
461 ArrayView<const T> getConst() const;
462
463 iterator begin() const;
464
465 iterator end() const;
466
467 const ArrayView<const T>& assert_not_null() const;
468
469 const ArrayView<const T>& assert_in_range( size_type offset, size_type size ) const;
470
471#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
472
473 // I should make these private but templated friends are not very
474 // portable. Besides, if a client directly calls this it will not
475 // compile in an optimized build.
476
477 explicit ArrayView (const ArrayRCP<const T> &arcp );
478
479 explicit ArrayView (const T* p, size_type size, const ArrayRCP<const T> &arcp );
480
481#endif
482
483private:
484 const T* ptr_;
485 size_type size_;
486#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
487 ArrayRCP<const T> arcp_;
488#endif
489
490 void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
491
492 void debug_assert_not_null() const {
493#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
495#endif
496 }
497
498 void debug_assert_in_range( size_type offset, size_type size_in ) const {
499 (void)offset; (void)size_in;
500#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
501 assert_in_range(offset, size_in);
502#endif
503 }
504
505 void debug_assert_valid_ptr() const {
506#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
507 arcp_.access_private_node().assert_valid_ptr(*this);
508#endif
509 }
510
511public: // Bad bad bad
512
513 // This is a very bad breach of encapsulation but it exists to avoid
514 // problems with portability of templated friends
515 const T* access_private_ptr() const { return ptr_; }
516
517#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
518 ArrayRCP<const T> access_private_arcp() const { return arcp_; }
519#endif
520};
521
522
523
528template<class T>
529ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
530
531
536template<class T>
537ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
538
539
544template<class T>
545ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
546
547
548#ifndef __sun
549
550
551// 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
552// a call to this function below is ambiguous. However, if you just comment
553// the function out, then the code on g++ (3.4.6 at least) will not compile.
554// Therefore, I have no choice but to put in a hacked ifdef for the sun.
555
556
564template<class T>
565std::vector<T> createVector( const ArrayView<T> &av );
566
567
568#endif // __sun
569
570
578template<class T>
579std::vector<T> createVector( const ArrayView<const T> &av );
580
581
586template<class T>
587bool is_null( const ArrayView<T> &av );
588
589
594template<class T>
595bool nonnull( const ArrayView<T> &av );
596
597
605template<class T>
606std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
607
608
617template<class T2, class T1>
618ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
619
620
633template<class T2, class T1>
634ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
635
636
637} // end namespace Teuchos
638
639
640//
641// Inline members
642//
643
644
645// ToDo: Fill in!
646
647
648#endif // TEUCHOS_ARRAY_VIEW_DECL_HPP
Reference-counted pointer node classes.
Reference-counted smart pointer for managing arrays.
ArrayRCP< T > arcp(const RCP< Array< T > > &v)
Wrap an RCP<Array<T> > object as an ArrayRCP<T> object.
const ArrayRCP< T > & assert_valid_ptr() const
If the object pointer is non-null, assert that it is still valid.
ArrayView< const T > getConst() const
Return a const view of *this.
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.
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,...