template<class T>
Corrade::Containers::StridedArrayView class

Array view with size and stride information.

Immutable wrapper around continuous sparse range of data, useful for easy iteration over interleaved arrays. Usage example:

struct Position {
    float x, y;
};

Position positions[]{{-0.5f, -0.5f}, { 0.5f, -0.5f}, { 0.0f,  0.5f}};

Containers::StridedArrayView<float> horizontalPositions{
    &positions[0].x, Containers::arraySize(positions), sizeof(Position)};

/* Move to the right */
for(float& x: horizontalPositions) x += 3.0f;

For convenience, similarly to ArrayView, this class is implicitly convertible from plain C arrays, ArrayView and StaticArrayView, with stride equal to array type size. The following two statements are equivalent:

int data[] { 1, 42, 1337, -69 };

Containers::StridedArrayView<int> view1{data, 4, sizeof(int)};
Containers::StridedArrayView<int> view2 = data;

Unlike ArrayView, this wrapper doesn't provide direct pointer access because pointer arithmetic doesn't work as usual here.

STL compatibility

On compilers that support C++2a and std::span, implicit conversion of it to a StridedArrayView is provided in Corrade/Containers/ArrayViewStlSpan.h. The conversion is provided in a separate header to avoid unconditional #include <span>, which significantly affects compile times. The following table lists allowed conversions:

Corrade typeSTL type
StridedArrayView<T>std::span<T>
StridedArrayView<T>std::span<size, const T>
StridedArrayView<const T>std::span<T>

See ArrayView STL compatibility for more information.

Public types

using Type = T
Element type.
using ErasedType = std::conditional<std::is_const<T>::value, const void, void>::type
Erased type.

Constructors, destructors, conversion operators

StridedArrayView(std::nullptr_t) constexpr noexcept
Conversion from nullptr
StridedArrayView() constexpr noexcept
Default constructor.
StridedArrayView(T* data, std::size_t size, std::size_t stride) constexpr noexcept
Construct a view on an array with explicit length.
template<class U, std::size_t size>
StridedArrayView(U(&data)[size]) constexpr noexcept
Construct a view on a fixed-size array.
template<class U>
StridedArrayView(StridedArrayView<U> view) constexpr noexcept
Construct a view on StridedArrayView.
template<class U>
StridedArrayView(ArrayView<U> view) constexpr noexcept
Construct a view on ArrayView.
template<std::size_t size, class U>
StridedArrayView(StaticArrayView<size, U> view) constexpr noexcept
Construct a view on StaticArrayView.
template<class U, class = decltype(Implementation::ArrayViewConverter<T, typename std::decay<U && >::type>::from(std::declval<U && >()))>
StridedArrayView(U&& other) constexpr noexcept
Construct a view from an external view representation.
operator bool() const explicit constexpr
Whether the array is non-empty.

Public functions

auto data() const -> ErasedType* constexpr
Array data.
auto size() const -> std::size_t constexpr
Array size.
auto stride() const -> std::size_t constexpr
Array stride.
auto empty() const -> bool constexpr
Whether the array is empty.
auto operator[](std::size_t i) const -> T&
Element access.
auto begin() const -> StridedIterator<T>
Iterator to first element.
auto cbegin() const -> StridedIterator<T>
auto end() const -> StridedIterator<T>
Iterator to (one item after) last element.
auto cend() const -> StridedIterator<T>
auto front() const -> T&
First element.
auto back() const -> T&
Last element.
auto slice(std::size_t begin, std::size_t end) const -> StridedArrayView<T>
Array slice.
auto prefix(std::size_t end) const -> StridedArrayView<T>
Array prefix.
auto suffix(std::size_t begin) const -> StridedArrayView<T>
Array suffix.

Function documentation

template<class T>
Corrade::Containers::StridedArrayView<T>::StridedArrayView() constexpr noexcept

Default constructor.

Creates empty view. Copy non-empty Array or ArrayView onto the instance to make it useful.

template<class T>
Corrade::Containers::StridedArrayView<T>::StridedArrayView(T* data, std::size_t size, std::size_t stride) constexpr noexcept

Construct a view on an array with explicit length.

Parameters
data Data pointer
size Data size
stride Data stride

template<class T> template<class U, std::size_t size>
Corrade::Containers::StridedArrayView<T>::StridedArrayView(U(&data)[size]) constexpr noexcept

Construct a view on a fixed-size array.

Parameters
data Fixed-size array

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size; stride is implicitly set to sizeof(T).

template<class T> template<class U>
Corrade::Containers::StridedArrayView<T>::StridedArrayView(StridedArrayView<U> view) constexpr noexcept

Construct a view on StridedArrayView.

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size.

template<class T> template<class U>
Corrade::Containers::StridedArrayView<T>::StridedArrayView(ArrayView<U> view) constexpr noexcept

Construct a view on ArrayView.

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size; stride is implicitly set to sizeof(T).

template<class T> template<std::size_t size, class U>
Corrade::Containers::StridedArrayView<T>::StridedArrayView(StaticArrayView<size, U> view) constexpr noexcept

Construct a view on StaticArrayView.

Enabled only if T* is implicitly convertible to U*. Expects that both types have the same size; stride is implicitly set to sizeof(T).

template<class T> template<class U, class = decltype(Implementation::ArrayViewConverter<T, typename std::decay<U && >::type>::from(std::declval<U && >()))>
Corrade::Containers::StridedArrayView<T>::StridedArrayView(U&& other) constexpr noexcept

Construct a view from an external view representation.

template<class T>
StridedIterator<T> Corrade::Containers::StridedArrayView<T>::begin() const

Iterator to first element.

template<class T>
StridedIterator<T> Corrade::Containers::StridedArrayView<T>::cbegin() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T>
StridedIterator<T> Corrade::Containers::StridedArrayView<T>::end() const

Iterator to (one item after) last element.

template<class T>
StridedIterator<T> Corrade::Containers::StridedArrayView<T>::cend() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class T>
T& Corrade::Containers::StridedArrayView<T>::front() const

First element.

Expects there is at least one element.

template<class T>
T& Corrade::Containers::StridedArrayView<T>::back() const

Last element.

Expects there is at least one element.

template<class T>
StridedArrayView<T> Corrade::Containers::StridedArrayView<T>::slice(std::size_t begin, std::size_t end) const

Array slice.

Both arguments are expected to be in range.

template<class T>
StridedArrayView<T> Corrade::Containers::StridedArrayView<T>::prefix(std::size_t end) const

Array prefix.

Equivalent to data.slice(0, end). If end is 0, returns zero-sized nullptr array.

template<class T>
StridedArrayView<T> Corrade::Containers::StridedArrayView<T>::suffix(std::size_t begin) const

Array suffix.

Equivalent to data.slice(begin, data.size()).

template<class T> template<class U, class T>
StridedArrayView<U> arrayCast(StridedArrayView<T> view)

Reinterpret-cast a strided array view.

Size of the new array is the same as original. Expects that both types are standard layout and sizeof(U) is not larger than stride() of the original array.

struct Pixel {
    std::uint8_t r, g, b, a;
};

Pixel pixels[]{{0x33, 0xff, 0x99, 0x66}, {0x11, 0xab, 0x33, 0xff}};

auto red = Containers::StridedArrayView<std::uint8_t>{&pixels[0].r, 2, 4};
auto rgba = Containers::arrayCast<Pixel>(red);