Skip to content

strata::TensorView

Part of Views and expressions.

template <typename T, shape_like S, layout_for< S > Layout = Dense<>>
class strata::TensorView;

A non-owning view of memory as a tensor: a pointer and the layout's index to offset mapping.

TensorView<const float, ...> reads only. A slice of a view is another view of the same memory; nothing is copied.

Assigning to a view writes the memory it views, as assigning to an element does, and never makes the view point elsewhere: slice<0>(x, 1) = row;, block(x, seq<2>{0}, seq<2>{0}) += 1. What is written is a tensor of the view's shape, or a scalar for every element, and may read the view's own memory (see assign). Every operator that T has is there, = and += to >>=, each as the built-in one is: a += b is a = T(a + b) element by element. They are const: a view is a handle, and the elements are what is written, so a temporary view takes them too. A view of const T takes none.

Template parameters

Name Description
T The element type; const for a read-only view.
S The shape.
Layout The layout policy, column-major Dense<> by default.

Members

TensorView

constexpr TensorView(T *data, const mapping_type &mapping) noexcept

A view of the memory at data, indexed through mapping.

constexpr TensorView(T *data, const S &shape) noexcept

A view of the memory at data, indexed through the layout's mapping of shape.

template <typename U, shape_like OtherShape, typename OtherLayout>
constexpr TensorView(const TensorView<U, OtherShape, OtherLayout> &other) noexcept

Converts to a view that knows less about the same memory.

const may be added (a U* must convert to a T* without slicing), static extents and strides turned dynamic, and Dense taken as Strided. TensorView<float, Shape<3, 4>> converts to TensorView<const float, Shape<Dyn, Dyn>, Strided<Dyn, Dyn>>, never the other way.

operator=

constexpr TensorView &requires(!std::is_const_v<T>) operator=(const TensorView &other)

Writes the elements of other into the elements of this view, not the pointer: a view is no value to rebind.

Precondition

The shapes are equal.

template <typename E>
constexpr const TensorView & operator=(const E &source) const

Writes source into the elements of the view: view = source, view += source and the other compound operators.

source is a tensor of the view's shape, or a scalar for every element. Its elements are combined with the view's by the operator and converted to T. source is computed first wherever it reads the view's memory at other positions than it writes it (a = permute<1, 0>(a)), and is not wherever it does not (a = a + b); noalias(source) promises that it does not.

Precondition

The extents of source are the view's.

data

constexpr T * data() const noexcept

The pointer to the first element.

mapping

constexpr const mapping_type & mapping() const noexcept

The index to offset mapping.

shape

constexpr const S & shape() const noexcept

The shape, the extents at run time.

cursor

constexpr detail::ViewCursor<T, mapping_type> cursor() const noexcept

Where a traversal starts: the first element, and the strides to move from it.

operator[]

template <index_like... Indices>
constexpr T & operator[](Indices... indices) const noexcept

The element at the given indices: view[i, j, k].

Precondition

Each index is within the extent of its axis; the mapping checks it.

slice

template <std::size_t... Axes>
constexpr auto slice(const TensorView &view, const std::array<std::size_t, sizeof...(Axes)> &indices) noexcept

The view at fixed indices along Axes: what slice gives for a view, another view of the same memory, an axis less for each.

slice checks the axes, increasing, and the indices.

permute

template <std::size_t... Permutation>
constexpr auto permute(const TensorView &view) noexcept

The view with its axes permuted: what permute gives for a view, another view of the same memory, new axis k being old axis Permutation[k].

diagonal

template <std::size_t... Axes>
constexpr auto diagonal(const TensorView &view) noexcept

The view of the diagonal over Axes: what diagonal gives for a view, another view of the same memory, its axes one that moves by all their strides.

Writable.

Precondition

The extents of Axes are equal; diagonal checks it.

block

template <Extent... Sizes>
constexpr auto block(const TensorView &view, const Shape<Sizes...> &sizes, const std::array<std::size_t, sizeof...(Sizes)> &corner) noexcept

The view of the box of shape sizes whose first element is at corner: what block gives for a view, another view of the same memory.

Precondition

The box lies inside the view.

expand

template <std::size_t... Inserted, shape_like Space>
constexpr auto expand(const TensorView &view, const Space &space) noexcept

The view in a shape with new axes: what expand gives for a view, another view of the same memory, its new axes of stride 0, so that along them the same element is read at every position.

Read only: the elements are const, since one element is at many positions.