view

Returns a new view of a slice with the same data, but reshaped to have shape equal to lengths.

pure
view
(
S
size_t N
)
(
S sl
,
ptrdiff_t[N] lengths...
)

Parameters

sl S

n-dimensional slice

lengths ptrdiff_t[N]

A list of lengths for each dimension

Return Value

Type: auto

new view of a slice with the same data

Examples

import std.string : split;
import mir.ndslice.topology : universal, iota;
import mir.ndslice.allocation : slice;
import mir.ndslice.dynamic : transposed;

assert(iota(3, 4).slice.view(-1, 1).shape == [12, 1]);
assert(iota(3, 4).slice.universal.transposed.view(-1, 6).shape == [2, 6]);

Invalid views generate ReshapeError

import std.string : split;
import mir.ndslice.topology : iota;
import mir.ndslice.allocation : slice;

try {
    iota(3, 4).slice.view(2, 1);
} catch (Exception e) {
    assert(e.msg.split(":")[0] == "ReshapeError");
}
try {
    iota(0).slice.view(2, 1);
} catch (Exception e) {
    assert(e.msg.split(":")[0] == "ReshapeError");
}

Meta