pub trait MutLayout: Layout + Clone {
Show 14 methods
// Required methods
fn from_shape(shape: Self::Index<'_>) -> Self;
fn from_shape_and_strides(
shape: Self::Index<'_>,
strides: Self::Index<'_>,
overlap: OverlapPolicy,
) -> Result<Self, FromDataError>;
fn move_axis(&mut self, from: usize, to: usize);
fn permuted(&self, order: Self::Index<'_>) -> Self;
fn resize_dim(&mut self, dim: usize, size: usize);
fn transposed(&self) -> Self;
fn slice<const M: usize>(
&self,
range: &[SliceItem],
) -> Result<(Range<usize>, NdLayout<M>), SliceError>;
fn slice_dyn(
&self,
range: &[SliceItem],
) -> Result<(Range<usize>, DynLayout), SliceError>;
fn squeezed(&self) -> DynLayout;
fn split(
&self,
axis: usize,
mid: usize,
) -> ((Range<usize>, Self), (Range<usize>, Self));
// Provided methods
fn index_axis(
&self,
axis: usize,
index: usize,
) -> (Range<usize>, <Self as RemoveDim>::Output)
where Self: RemoveDim { ... }
fn reshaped_for_view<S: IntoLayout>(
&self,
shape: S,
) -> Result<S::Layout, ReshapeError> { ... }
fn reshaped_for_copy<S: IntoLayout>(
&self,
shape: S,
) -> Result<S::Layout, ReshapeError> { ... }
fn slice_axis(
&self,
axis: usize,
range: Range<usize>,
) -> Result<(Range<usize>, Self), SliceError> { ... }
}
Expand description
MutLayout extends Layout
with methods for creating, modifying and
transforming layouts.
§Strides and internal overlap
Rust requires that only one mutable reference can exist for any value. When creating mutable tensor views or iterators, it is therefore important to know whether multiple elements in the layout may map to the same offset.
Accurately checking this for arbitrary shape and strides is non-trivial. See
notes in mem_overlap.c
in the NumPy source. RTen handles this by using
a conservative check for internal overlap when constructing a layout from
arbitrary strides. Specifically it sorts dimensions by decreasing stride and
then verifies that each dimension fully “steps over” the next one. This
allows for layouts which are transposed or have been sliced, but disallows
some more complex non-overlapping constructions.
When constructing a layout via
from_shape_and_strides
the intended
usage is specified via an OverlapPolicy
.
Required Methods§
Sourcefn from_shape(shape: Self::Index<'_>) -> Self
fn from_shape(shape: Self::Index<'_>) -> Self
Create a new contiguous layout with a given shape.
Sourcefn from_shape_and_strides(
shape: Self::Index<'_>,
strides: Self::Index<'_>,
overlap: OverlapPolicy,
) -> Result<Self, FromDataError>
fn from_shape_and_strides( shape: Self::Index<'_>, strides: Self::Index<'_>, overlap: OverlapPolicy, ) -> Result<Self, FromDataError>
Create a layout with custom strides.
The strides specify the offset gap between successive entries along a
given axis. overlap
controls whether the layout is allowed to map
multiple indices to the same element. This can be true for immutable
views, but must be false for tensors or views that are mutable.
Sourcefn move_axis(&mut self, from: usize, to: usize)
fn move_axis(&mut self, from: usize, to: usize)
Move the axis at position from
to to
by swapping their strides.
Sourcefn permuted(&self, order: Self::Index<'_>) -> Self
fn permuted(&self, order: Self::Index<'_>) -> Self
Return a layout with the axes permuted according to the given order.
fn resize_dim(&mut self, dim: usize, size: usize)
Sourcefn transposed(&self) -> Self
fn transposed(&self) -> Self
Reverse the order of dimensions. This is equivalent to
self.permuted([N-1, N-2, ... 0])
.
Sourcefn slice<const M: usize>(
&self,
range: &[SliceItem],
) -> Result<(Range<usize>, NdLayout<M>), SliceError>
fn slice<const M: usize>( &self, range: &[SliceItem], ) -> Result<(Range<usize>, NdLayout<M>), SliceError>
Slice the layout and return a static-rank layout.
Returns a tuple of (offset_range, sliced_layout)
.
Provided Methods§
Sourcefn index_axis(
&self,
axis: usize,
index: usize,
) -> (Range<usize>, <Self as RemoveDim>::Output)where
Self: RemoveDim,
fn index_axis(
&self,
axis: usize,
index: usize,
) -> (Range<usize>, <Self as RemoveDim>::Output)where
Self: RemoveDim,
Slice a layout by selecting a single entry from a given axis.
Returns an (offset_range, layout)
tuple for the sliced layout.
Sourcefn reshaped_for_view<S: IntoLayout>(
&self,
shape: S,
) -> Result<S::Layout, ReshapeError>
fn reshaped_for_view<S: IntoLayout>( &self, shape: S, ) -> Result<S::Layout, ReshapeError>
Return a new layout formed by reshaping this one to shape
.
This has the same requirements as
reshaped_for_copy
but also requires
that the layout is contiguous.
Sourcefn reshaped_for_copy<S: IntoLayout>(
&self,
shape: S,
) -> Result<S::Layout, ReshapeError>
fn reshaped_for_copy<S: IntoLayout>( &self, shape: S, ) -> Result<S::Layout, ReshapeError>
Return a new layout formed by reshaping this one to shape
.
Sourcefn slice_axis(
&self,
axis: usize,
range: Range<usize>,
) -> Result<(Range<usize>, Self), SliceError>
fn slice_axis( &self, axis: usize, range: Range<usize>, ) -> Result<(Range<usize>, Self), SliceError>
Slice the layout along a given axis.
Returns a tuple of (offset_range, sliced_layout)
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.