Loading...
Searching...
No Matches
Dune::Std::span< Element, Extent > Class Template Reference
Common » Utilities » C++ utilities and backports

A contiguous sequence of elements with static or dynamic extent. More...

`#include <dune/common/std/span.hh>`

Inheritance diagram for Dune::Std::span< Element, Extent >:

## Public Types

using | element_type = Element
using | value_type = std::remove_cv_t<element_type>
using | size_type = std::size_t
using | difference_type = std::ptrdiff_t
using | pointer = element_type*
using | reference = element_type&
using | const_reference = const element_type&
using | iterator = pointer
using | reverse_iterator = std::reverse_iterator<iterator>
using | const_iterator = const iterator
using | const_reverse_iterator = std::reverse_iterator<const_iterator>

## Static Public Attributes

static constexpr size_type | extent = Extent

## Span constructors

template<std::size_t e = extent, std::enable_if_t<(e==dynamic_extent||e==0), int > = 0>
constexpr | span () noexcept
| Default construct an empty span.
template<class Iter, class U = std::remove_reference_t<decltype(*std::declval<Iter>())>, std::enable_if_t< std::is_convertible_v< U(*)[], element_type(*)[]>, int > = 0>
constexpr | span (Iter first, size_type size)
| Constructs a span that is a view over the range [first, first+size).
template<class Iter, class U = std::remove_reference_t<decltype(*std::declval<Iter>())>, std::enable_if_t< std::is_convertible_v< U(*)[], element_type(*)[]>, int > = 0>
constexpr | span (Iter first, Iter last)
| Constructs a span that is a view over the range [first, last).
template<class Range, decltype(std::begin(std::declval< Range >()), std::end(std::declval< Range >()), bool{}) = true, std::enable_if_t< not std::is_array_v< Range >, int > = 0>
constexpr | span (Range &range)
| Constructs a span that is a view over the range [range.begin(), range.end()).
template<std::size_t N, std::size_t e = extent, std::enable_if_t<(e==Std::dynamic_extent||e==N), int > = 0>
constexpr | span (Impl::TypeIdentity_t< element_type >(&data)[N]) noexcept
| Constructs a span that is a view over the C-array.
template<class T, size_t N, std::size_t e = extent, std::enable_if_t<(e==Std::dynamic_extent||e==N), int > = 0, std::enable_if_t< std::is_convertible_v< T(*)[], element_type(*)[]>, int > = 0>
constexpr | span (std::array< T, N > &arr) noexcept
| Constructs a span that is a view over the array.
template<class T, size_t N, std::size_t e = extent, std::enable_if_t<(e==Std::dynamic_extent||e==N), int > = 0, std::enable_if_t< std::is_convertible_v< const T(*)[], element_type(*)[]>, int > = 0>
constexpr | span (const std::array< T, N > &arr) noexcept
| Constructs a span that is a view over the const array.
template<class E = element_type, std::enable_if_t< std::is_const_v< E >, int > = 0>
constexpr | span (std::initializer_list< value_type > il)
| Constructs a span that is a view over the initializer-list.
constexpr | span (const span &other) noexcept=default
| Copy constructor.
template<class OtherElementType, std::size_t OtherExtent, std::enable_if_t<(extent==Std::dynamic_extent||OtherExtent==Std::dynamic_extent||extent==OtherExtent), int > = 0, std::enable_if_t< std::is_convertible_v< OtherElementType(*)[], element_type(*)[]>, int > = 0>
constexpr | span (const span< OtherElementType, OtherExtent > &s) noexcept
| Converting copy constructor.
constexpr span & | operator= (const span &other) noexcept=default
| Copy assignment operator.

## Iterators

constexpr iterator | begin () const noexcept
| Returns an iterator to the beginning.
constexpr iterator | end () const noexcept
| Returns an iterator to the end.
constexpr const_iterator | cbegin () const noexcept
| Returns an iterator to the beginning.
constexpr const_iterator | cend () const noexcept
| Returns an iterator to the end.
constexpr reverse_iterator | rbegin () const noexcept
| Returns a reverse iterator starting at the end.
constexpr reverse_iterator | rend () const noexcept
| Returns a reverse iterator ending at the beginning.
constexpr const_reverse_iterator | crbegin () const noexcept
| Returns a reverse iterator starting at the end.
constexpr const_reverse_iterator | crend () const noexcept
| Returns a reverse iterator ending at the beginning.

## Element and data access

constexpr reference | front () const
| Access the first element.
constexpr reference | back () const
| Access the last element.
constexpr reference | at (size_type i) const
| Access specified element with bounds checking.
constexpr reference | operator[] (size_type i) const
| Access specified element.
constexpr pointer | data () const noexcept
| Direct access to the underlying contiguous storage.

## Subspans

template<std::size_t Count>
constexpr span< element_type, Count > | first () const
| Obtains a subspan consisting of the first Count elements of the sequence.
template<std::size_t Count>
constexpr span< element_type, Count > | last () const
| Obtains a subspan consisting of the last Count elements of the sequence.
template<std::size_t Offset, std::size_t Count = Std::dynamic_extent>
constexpr span< element_type, subspan_extent(Offset, Count)> | subspan () const
| Obtains a subspan consisting of Count elements of the sequence starting at Offset.
constexpr span< element_type, Std::dynamic_extent > | first (size_type count) const
| Obtains a subspan consisting of the first count elements of the sequence.
constexpr span< element_type, Std::dynamic_extent > | last (size_type count) const
| Obtains a subspan consisting of the last count elements of the sequence.
constexpr span< element_type, Std::dynamic_extent > | subspan (size_type offset, size_type count=Std::dynamic_extent) const
| Obtains a subspan consisting of count elements of the sequence starting at offset.

## Size information

constexpr size_type | size_bytes () const noexcept
| Returns the size of the sequence in bytes.
constexpr bool | empty () const noexcept
| Checks if the sequence is empty.

## Detailed Description

template<class Element, std::size_t Extent = Std::dynamic_extent>
class Dune::Std::span< Element, Extent >

A contiguous sequence of elements with static or dynamic extent.

The class template span describes an object that can refer to a contiguous sequence of objects with the first element of the sequence at position zero. A span can either have a static extent, in which case the number of elements in the sequence is known at compile-time and encoded in the type, or a dynamic extent.

If a span has dynamic extent, a typical implementation holds two members: a pointer to Element and a size. A span with static extent may have only one member: a pointer to Element.

The implementation is based on the C++ standard working draft N4971 and the documentation provided in cppreference.

Example:

std::vector v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// view data as contiguous memory representing 10 elements
auto s1 = Dune::Std::span(v.data(), 10);
// view data as contiguous memory with static size
auto s2 = Dune::Std::span<int,10>(v.data());
// write data using 2D view
for (std::size_t i = 0; i != s1.size(); i++)
s1[i] = 2*i;
Dune::Std::span
span(T(&)[N]) -> span< T, N >

Template Parameters

  Element | The element type; a complete object type that is not an abstract class type.
  Extent | Specifies number of elements in the sequence, or Std::dynamic_extent if dynamic.

## Member Typedef Documentation

## const_iterator

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::const_iterator = const iterator

## const_reference

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::const_reference = const element_type&

## const_reverse_iterator

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::const_reverse_iterator = std::reverse_iterator<const_iterator>

## difference_type

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::difference_type = std::ptrdiff_t

## element_type

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::element_type = Element

## iterator

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::iterator = pointer

## pointer

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::pointer = element_type*

## reference

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::reference = element_type&

## reverse_iterator

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::reverse_iterator = std::reverse_iterator<iterator>

## size_type

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::size_type = std::size_t

## value_type

template<class Element, std::size_t Extent = Std::dynamic_extent>

using Dune::Std::span< Element, Extent >::value_type = std::remove_cv_t<element_type>

## Constructor & Destructor Documentation

## span() [1/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<std::size_t e = extent, std::enable_if_t<(e==dynamic_extent||e==0), int > = 0>

Dune::Std::span< Element, Extent >::span | ( |  | ) |

inline constexpr noexcept

Default construct an empty span.

## span() [2/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class Iter, class U = std::remove_reference_t<decltype(*std::declval<Iter>())>, std::enable_if_t< std::is_convertible_v< U(*)[], element_type(*)[]>, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | Iter | first,
|  | size_type | size )

inline constexpr

Constructs a span that is a view over the range [first, first+size).

## span() [3/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class Iter, class U = std::remove_reference_t<decltype(*std::declval<Iter>())>, std::enable_if_t< std::is_convertible_v< U(*)[], element_type(*)[]>, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | Iter | first,
|  | Iter | last )

inline constexpr

Constructs a span that is a view over the range [first, last).

## span() [4/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class Range, decltype(std::begin(std::declval< Range >()), std::end(std::declval< Range >()), bool{}) = true, std::enable_if_t< not std::is_array_v< Range >, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | Range & | range | ) |

inline constexpr

Constructs a span that is a view over the range [range.begin(), range.end()).

## span() [5/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<std::size_t N, std::size_t e = extent, std::enable_if_t<(e==Std::dynamic_extent||e==N), int > = 0>

Dune::Std::span< Element, Extent >::span | ( | Impl::TypeIdentity_t< element_type >(&) | data[N] | ) |

inline constexpr noexcept

Constructs a span that is a view over the C-array.

## span() [6/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class T, size_t N, std::size_t e = extent, std::enable_if_t<(e==Std::dynamic_extent||e==N), int > = 0, std::enable_if_t< std::is_convertible_v< T(*)[], element_type(*)[]>, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | std::array< T, N > & | arr | ) |

inline constexpr noexcept

Constructs a span that is a view over the array.

## span() [7/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class T, size_t N, std::size_t e = extent, std::enable_if_t<(e==Std::dynamic_extent||e==N), int > = 0, std::enable_if_t< std::is_convertible_v< const T(*)[], element_type(*)[]>, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | const std::array< T, N > & | arr | ) |

inline constexpr noexcept

Constructs a span that is a view over the const array.

## span() [8/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class E = element_type, std::enable_if_t< std::is_const_v< E >, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | std::initializer_list< value_type > | il | ) |

inline constexpr

Constructs a span that is a view over the initializer-list.

## span() [9/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>

Dune::Std::span< Element, Extent >::span | ( | const span< Element, Extent > & | other | ) |

constexpr default noexcept

Copy constructor.

## span() [10/10]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<class OtherElementType, std::size_t OtherExtent, std::enable_if_t<(extent==Std::dynamic_extent||OtherExtent==Std::dynamic_extent||extent==OtherExtent), int > = 0, std::enable_if_t< std::is_convertible_v< OtherElementType(*)[], element_type(*)[]>, int > = 0>

Dune::Std::span< Element, Extent >::span | ( | const span< OtherElementType, OtherExtent > & | s | ) |

inline constexpr noexcept

Converting copy constructor.

## Member Function Documentation

## at()

template<class Element, std::size_t Extent = Std::dynamic_extent>

reference Dune::Std::span< Element, Extent >::at | ( | size_type | i | ) | const

inline constexpr

Access specified element with bounds checking.

## back()

template<class Element, std::size_t Extent = Std::dynamic_extent>

reference Dune::Std::span< Element, Extent >::back | ( |  | ) | const

inline constexpr

Access the last element.

## begin()

template<class Element, std::size_t Extent = Std::dynamic_extent>

iterator Dune::Std::span< Element, Extent >::begin | ( |  | ) | const

inline constexpr noexcept

Returns an iterator to the beginning.

## cbegin()

template<class Element, std::size_t Extent = Std::dynamic_extent>

const_iterator Dune::Std::span< Element, Extent >::cbegin | ( |  | ) | const

inline constexpr noexcept

Returns an iterator to the beginning.

## cend()

template<class Element, std::size_t Extent = Std::dynamic_extent>

const_iterator Dune::Std::span< Element, Extent >::cend | ( |  | ) | const

inline constexpr noexcept

Returns an iterator to the end.

## crbegin()

template<class Element, std::size_t Extent = Std::dynamic_extent>

const_reverse_iterator Dune::Std::span< Element, Extent >::crbegin | ( |  | ) | const

inline constexpr noexcept

Returns a reverse iterator starting at the end.

## crend()

template<class Element, std::size_t Extent = Std::dynamic_extent>

const_reverse_iterator Dune::Std::span< Element, Extent >::crend | ( |  | ) | const

inline constexpr noexcept

Returns a reverse iterator ending at the beginning.

## data()

template<class Element, std::size_t Extent = Std::dynamic_extent>

pointer Dune::Std::span< Element, Extent >::data | ( |  | ) | const

inline constexpr noexcept

Direct access to the underlying contiguous storage.

## empty()

template<class Element, std::size_t Extent = Std::dynamic_extent>

bool Dune::Std::span< Element, Extent >::empty | ( |  | ) | const

inline nodiscard constexpr noexcept

Checks if the sequence is empty.

## end()

template<class Element, std::size_t Extent = Std::dynamic_extent>

iterator Dune::Std::span< Element, Extent >::end | ( |  | ) | const

inline constexpr noexcept

Returns an iterator to the end.

## first() [1/2]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<std::size_t Count>

span< element_type, Count > Dune::Std::span< Element, Extent >::first | ( |  | ) | const

inline constexpr

Obtains a subspan consisting of the first Count elements of the sequence.

## first() [2/2]

template<class Element, std::size_t Extent = Std::dynamic_extent>

span< element_type, Std::dynamic_extent > Dune::Std::span< Element, Extent >::first | ( | size_type | count | ) | const

inline constexpr

Obtains a subspan consisting of the first count elements of the sequence.

## front()

template<class Element, std::size_t Extent = Std::dynamic_extent>

reference Dune::Std::span< Element, Extent >::front | ( |  | ) | const

inline constexpr

Access the first element.

## last() [1/2]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<std::size_t Count>

span< element_type, Count > Dune::Std::span< Element, Extent >::last | ( |  | ) | const

inline constexpr

Obtains a subspan consisting of the last Count elements of the sequence.

## last() [2/2]

template<class Element, std::size_t Extent = Std::dynamic_extent>

span< element_type, Std::dynamic_extent > Dune::Std::span< Element, Extent >::last | ( | size_type | count | ) | const

inline constexpr

Obtains a subspan consisting of the last count elements of the sequence.

## operator=()

template<class Element, std::size_t Extent = Std::dynamic_extent>

span & Dune::Std::span< Element, Extent >::operator= | ( | const span< Element, Extent > & | other | ) |

constexpr default noexcept

Copy assignment operator.

## operator[]()

template<class Element, std::size_t Extent = Std::dynamic_extent>

reference Dune::Std::span< Element, Extent >::operator[] | ( | size_type | i | ) | const

inline constexpr

Access specified element.

## rbegin()

template<class Element, std::size_t Extent = Std::dynamic_extent>

reverse_iterator Dune::Std::span< Element, Extent >::rbegin | ( |  | ) | const

inline constexpr noexcept

Returns a reverse iterator starting at the end.

## rend()

template<class Element, std::size_t Extent = Std::dynamic_extent>

reverse_iterator Dune::Std::span< Element, Extent >::rend | ( |  | ) | const

inline constexpr noexcept

Returns a reverse iterator ending at the beginning.

## size_bytes()

template<class Element, std::size_t Extent = Std::dynamic_extent>

size_type Dune::Std::span< Element, Extent >::size_bytes | ( |  | ) | const

inline constexpr noexcept

Returns the size of the sequence in bytes.

## subspan() [1/2]

template<class Element, std::size_t Extent = Std::dynamic_extent>
template<std::size_t Offset, std::size_t Count = Std::dynamic_extent>

span< element_type, subspan_extent(Offset, Count)> Dune::Std::span< Element, Extent >::subspan | ( |  | ) | const

inline constexpr

Obtains a subspan consisting of Count elements of the sequence starting at Offset.

Note
  If Count == Std::dynamic_extent, the subspan starting at Offset goes until the end of the current span.

## subspan() [2/2]

template<class Element, std::size_t Extent = Std::dynamic_extent>

span< element_type, Std::dynamic_extent > Dune::Std::span< Element, Extent >::subspan | ( | size_type | offset,
|  | size_type | count = Std::dynamic_extent ) const

inline constexpr

Obtains a subspan consisting of count elements of the sequence starting at offset.

Note
  If count == Std::dynamic_extent, the subspan starting at offset goes until the end of the current span.

## Member Data Documentation

## extent

template<class Element, std::size_t Extent = Std::dynamic_extent>

size_type Dune::Std::span< Element, Extent >::extent = Extent

static constexpr

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

- span.hh