Reference documentation for deal.II version Git 3e8fd02 2017-09-22 11:37:14 +0200
Public Types | Public Member Functions | Private Attributes | Friends | Related Functions | List of all members
ArrayView< ElementType > Class Template Reference

#include <deal.II/base/array_view.h>

Public Types

typedef ElementType value_type
 
typedef value_typeiterator
 
typedef const ElementType * const_iterator
 

Public Member Functions

 ArrayView ()
 
 ArrayView (value_type *starting_element, const std::size_t n_elements)
 
 ArrayView (const ArrayView< typename std::remove_cv< value_type >::type > &view)
 
 ArrayView (const std::vector< typename std::remove_cv< value_type >::type > &vector)
 
 ArrayView (std::vector< typename std::remove_cv< value_type >::type > &vector)
 
std::size_t size () const
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
value_typeoperator[] (const std::size_t i) const
 

Private Attributes

value_type *const starting_element
 
const std::size_t n_elements
 

Friends

class ArrayView< const ElementType >
 

Related Functions

(Note that these are not member functions.)

template<typename ElementType >
ArrayView< ElementType > make_array_view (std::vector< ElementType > &vector)
 
template<typename ElementType >
ArrayView< const ElementType > make_array_view (const std::vector< ElementType > &vector)
 
template<typename ElementType >
ArrayView< ElementType > make_array_view (std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
 
template<typename Iterator >
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type > make_array_view (const Iterator begin, const Iterator end)
 
template<typename ElementType >
ArrayView< ElementType > make_array_view (ElementType *const begin, ElementType *const end)
 
template<typename ElementType >
ArrayView< const ElementType > make_array_view (const std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
 
template<typename ElementType >
ArrayView< ElementType > make_array_view (Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
 
template<typename ElementType >
ArrayView< const ElementType > make_array_view (const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
 
template<typename ElementType >
ArrayView< ElementType > make_array_view (Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
 
template<typename ElementType >
ArrayView< const ElementType > make_array_view (const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
 

Detailed Description

template<typename ElementType>
class ArrayView< ElementType >

A class that represents a window of memory locations of type ElementType and presents it as if it was an array that can be accessed via an operator[]. In essence, this class is nothing more than just a pointer to the first location and an integer that represents the length of the array in elements. The memory remains owned by whoever allocated it, as this class does not take over ownership.

The advantage of using this class is that you don't have to pass around pairs of pointers and that operator[] checks for the validity of the index with which you subscript this array view.

This class can handle views to both non-constant and constant memory locations. If you want to represent a view of a constant array, then the template argument type of this class needs to be const as well. The following code snippet gives an example:

std::vector<int> array = get_data(); // a writable array
ArrayView<int> view (&array[5], 5); // a view of elements 5..9 (inclusive)
view[2] = 42; // array[7] is set to 42
ArrayView<const int> const_view (&array[5], 5); // same view, but read-only
int element_7 = const_view[2]; // returns 42
const_view[2] = 42; // error, can't write into this view

In either case, accessing an element of a view does not change the ArrayView object itself, and consequently ArrayView::operator[] is a const function. This corresponds to the notion that a view simply represents a, well, "view" of memory that is owned by someone else. Thus, accessing elements of the view changes the memory managed by some other object, but not the view itself, allowing us to make ArrayView::operator[] a const member function. This is in contrast to, say, std::vector, which manages the memory it points to and changing an element of the std::vector therefore changes the std::vector object itself – consequently, the std::vector::operator[] is non-const.

Author
Wolfgang Bangerth, 2015, David Wells, 2017

Definition at line 70 of file array_view.h.

Member Typedef Documentation

template<typename ElementType>
typedef ElementType ArrayView< ElementType >::value_type

A typedef that denotes the "value_type" of this container-like class, i.e., the type of the element it "stores" or points to.

Definition at line 77 of file array_view.h.

template<typename ElementType>
typedef value_type* ArrayView< ElementType >::iterator

A typedef for iterators pointing into the array.

Definition at line 82 of file array_view.h.

template<typename ElementType>
typedef const ElementType* ArrayView< ElementType >::const_iterator

A typedef for const iterators pointing into the array.

Definition at line 87 of file array_view.h.

Constructor & Destructor Documentation

template<typename ElementType >
ArrayView< ElementType >::ArrayView ( )
inline

Default constructor. Creates an invalid view that does not point to anything at all.

Definition at line 220 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::ArrayView ( value_type starting_element,
const std::size_t  n_elements 
)
inline

Constructor.

Parameters
[in]starting_elementA pointer to the first element of the array this object should represent.
[in]n_elementsThe length (in elements) of the chunk of memory this object should represent.
Note
The object that is constructed from these arguments has no knowledge how large the object into which it points really is. As a consequence, whenever you call ArrayView::operator[], the array view can check that the given index is within the range of the view, but it can't check that the view is indeed a subset of the valid range of elements of the underlying object that allocated that range. In other words, you need to ensure that the range of the view specified by the two arguments to this constructor is in fact a subset of the elements of the array into which it points. The appropriate way to do this is to use the make_array_view() functions.

Definition at line 231 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::ArrayView ( const ArrayView< typename std::remove_cv< value_type >::type > &  view)
inline

Copy constructor from array views that point to non-const elements. If the current object will point to non-const elements, then this is a straight forward copy constructor. On the other hand, if the current type's ElementType template argument is a const qualified type, then the current constructor is a conversion constructor that converts a non-const view to a const view, akin to converting a non-const pointer to a const pointer.

Definition at line 243 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::ArrayView ( const std::vector< typename std::remove_cv< value_type >::type > &  vector)
inline

A constructor that automatically creates a view from a std::vector object. The view encompasses all elements of the given vector.

This implicit conversion constructor is particularly useful when calling a function that takes an ArrayView object as argument, and passing in a std::vector.

Note
This constructor takes a reference to a const vector as argument. It can only be used to initialize ArrayView objects that point to const memory locations, such as ArrayView<const double>. You cannot initialize ArrayView objects to non-const memory with such arguments, such as ArrayView<double>.

Definition at line 254 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::ArrayView ( std::vector< typename std::remove_cv< value_type >::type > &  vector)
inline

A constructor that automatically creates a view from a std::vector object. The view encompasses all elements of the given vector.

This implicit conversion constructor is particularly useful when calling a function that takes an ArrayView object as argument, and passing in a std::vector.

Note
This constructor takes a reference to a non-const vector as argument. It can be used to initialize ArrayView objects that point to either const memory locations, such as ArrayView<const double>, or to non-const memory, such as ArrayView<double>.

Definition at line 280 of file array_view.h.

Member Function Documentation

template<typename ElementType >
std::size_t ArrayView< ElementType >::size ( ) const
inline

Return the size (in elements) of the view of memory this object represents.

Definition at line 292 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::iterator ArrayView< ElementType >::begin ( )
inline

Return an iterator pointing to the beginning of the array view.

Definition at line 300 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::iterator ArrayView< ElementType >::end ( )
inline

Return an iterator pointing to one past the end of the array view.

Definition at line 309 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::const_iterator ArrayView< ElementType >::begin ( ) const
inline

Return a constant iterator pointing to the beginning of the array view.

Definition at line 317 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::const_iterator ArrayView< ElementType >::end ( ) const
inline

Return a constant iterator pointing to one past the end of the array view.

Definition at line 326 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType >::value_type & ArrayView< ElementType >::operator[] ( const std::size_t  i) const
inline

Return a reference to the \(i\)th element of the range represented by the current object.

This function is marked as const because it does not change the view object. It may however return a reference to a non-const memory location depending on whether the template type of the class is const or not.

Definition at line 335 of file array_view.h.

Friends And Related Function Documentation

template<typename ElementType >
ArrayView< ElementType > make_array_view ( std::vector< ElementType > &  vector)
related

Create a view to an entire std::vector object. This is equivalent to initializing an ArrayView object with a pointer to the first element and the size of the given argument.

This function is used for non-const references to objects of vector type. Such objects contain elements that can be written to. Consequently, the return type of this function is a view to a set of writable objects.

Parameters
[in]vectorThe vector for which we want to have an array view object. The array view corresponds to the entire vector.

Definition at line 360 of file array_view.h.

template<typename ElementType >
ArrayView< const ElementType > make_array_view ( const std::vector< ElementType > &  vector)
related

Create a view to an entire std::vector object. This is equivalent to initializing an ArrayView object with a pointer to the first element and the size of the given argument.

This function is used for const references to objects of vector type because they contain immutable elements. Consequently, the return type of this function is a view to a set of const objects.

Parameters
[in]vectorThe vector for which we want to have an array view object. The array view corresponds to the entire vector.

Definition at line 384 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType > make_array_view ( std::vector< ElementType > &  vector,
const std::size_t  starting_index,
const std::size_t  size_of_view 
)
related

Create a view to a part of a std::vector object. This is equivalent to initializing the ArrayView object with a pointer to the starting_index- th element and the size_of_view as the length of the view.

This function is used for non-const references to objects of vector type. Such objects contain elements that can be written to. Consequently, the return type of this function is a view to a set of writable objects.

Parameters
[in]vectorThe vector for which we want to have an array view object.
[in]starting_indexThe index of the first element of the vector that will be part of this view.
[in]size_of_view
Precondition
starting_index + size_of_view <= vector.size()

Definition at line 413 of file array_view.h.

template<typename Iterator >
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type > make_array_view ( const Iterator  begin,
const Iterator  end 
)
related

Create an ArrayView that takes a pair of iterators as arguments. The type of the ArrayView is inferred from the value type of the iterator (e.g., the view created from two const iterators will have a const type).

Warning
The iterators begin and end must bound (in the usual half-open way) a contiguous in memory range of values. This function is intended for use with iterators into containers like boost::container::small_vector or std::vector and will not work correctly with, e.g., boost::container::stable_vector or std::deque.

Definition at line 441 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType > make_array_view ( ElementType *const  begin,
ElementType *const  end 
)
related

Create a view from a pair of pointers. ElementType may be const-qualified.

Warning
The pointers begin and end must bound (in the usual half-open way) a contiguous in memory range of values.

Definition at line 465 of file array_view.h.

template<typename ElementType >
ArrayView< const ElementType > make_array_view ( const std::vector< ElementType > &  vector,
const std::size_t  starting_index,
const std::size_t  size_of_view 
)
related

Create a view to a part of a std::vector object. This is equivalent to initializing the ArrayView object with a pointer to the starting_index- th element and the size_of_view as the length of the view.

This function is used for const references to objects of vector type because they contain immutable elements. Consequently, the return type of this function is a view to a set of const objects.

Parameters
[in]vectorThe vector for which we want to have an array view object.
[in]starting_indexThe index of the first element of the vector that will be part of this view.
[in]size_of_view
Precondition
starting_index + size_of_view <= vector.size()

Definition at line 496 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType > make_array_view ( Table< 2, ElementType > &  table,
const typename Table< 2, ElementType >::size_type  row 
)
related

Create a view to an entire row of a Table<2> object. This is equivalent to initializing an ArrayView object with a pointer to the first element of the given row, and the length of the row as the length of the view.

This function is used for non-const references to objects of Table type. Such objects contain elements that can be written to. Consequently, the return type of this function is a view to a set of writable objects.

Parameters
[in]tableThe Table for which we want to have an array view object. The array view corresponds to an entire row.
[in]rowThe index of the row into the table to which this view should correspond.

Definition at line 528 of file array_view.h.

template<typename ElementType >
ArrayView< const ElementType > make_array_view ( const Table< 2, ElementType > &  table,
const typename Table< 2, ElementType >::size_type  row 
)
related

Create a view to an entire row of a Table<2> object. This is equivalent to initializing an ArrayView object with a pointer to the first element of the given row, and the length of the row as the length of the view.

This function is used for const references to objects of Table type because they contain immutable elements. Consequently, the return type of this function is a view to a set of const objects.

Parameters
[in]tableThe Table for which we want to have an array view object. The array view corresponds to an entire row.
[in]rowThe index of the row into the table to which this view should correspond.

Definition at line 556 of file array_view.h.

template<typename ElementType >
ArrayView< ElementType > make_array_view ( Table< 2, ElementType > &  table,
const typename Table< 2, ElementType >::size_type  row,
const typename Table< 2, ElementType >::size_type  starting_column,
const std::size_t  size_of_view 
)
related

Create a view to (a part of) a row of a Table<2> object.

This function is used for non-const references to objects of Table type. Such objects contain elements that can be written to. Consequently, the return type of this function is a view to a set of writable objects.

Parameters
[in]tableThe Table for which we want to have an array view object. The array view corresponds to an entire row.
[in]rowThe index of the row into the table to which this view should correspond.
[in]starting_columnThe index of the column into the given row of the table that corresponds to the first element of this view.
[in]size_of_viewThe number of elements this view should have. This corresponds to the number of columns in the current row to which the view should correspond.

Definition at line 587 of file array_view.h.

template<typename ElementType >
ArrayView< const ElementType > make_array_view ( const Table< 2, ElementType > &  table,
const typename Table< 2, ElementType >::size_type  row,
const typename Table< 2, ElementType >::size_type  starting_column,
const std::size_t  size_of_view 
)
related

Create a view to (a part of) a row of a Table<2> object.

This function is used for const references to objects of Table type because they contain immutable elements. Consequently, the return type of this function is a view to a set of const objects.

Parameters
[in]tableThe Table for which we want to have an array view object. The array view corresponds to an entire row.
[in]rowThe index of the row into the table to which this view should correspond.
[in]starting_columnThe index of the column into the given row of the table that corresponds to the first element of this view.
[in]size_of_viewThe number of elements this view should have. This corresponds to the number of columns in the current row to which the view should correspond.

Definition at line 625 of file array_view.h.

Member Data Documentation

template<typename ElementType>
value_type* const ArrayView< ElementType >::starting_element
private

A pointer to the first element of the range of locations in memory that this object represents.

Definition at line 203 of file array_view.h.

template<typename ElementType>
const std::size_t ArrayView< ElementType >::n_elements
private

The length of the array this object represents.

Definition at line 208 of file array_view.h.


The documentation for this class was generated from the following file: