pub struct Shape { /* private fields */ }Expand description
A shape is a Slice with labeled dimensions and a selection API.
Implementations§
Source§impl Shape
impl Shape
Sourcepub fn new(labels: Vec<String>, slice: Slice) -> Result<Self, ShapeError>
pub fn new(labels: Vec<String>, slice: Slice) -> Result<Self, ShapeError>
Creates a new shape with the provided labels, which describe the provided Slice.
Shapes can also be constructed by way of the [shape] macro, which
creates a by-construction correct slice in row-major order given a set of
sized dimensions.
Sourcepub fn select<R: Into<Range>>(
&self,
label: &str,
range: R,
) -> Result<Self, ShapeError>
pub fn select<R: Into<Range>>( &self, label: &str, range: R, ) -> Result<Self, ShapeError>
Restrict this shape along a named dimension using a Range. The
provided range must be nonempty.
A shape defines a “strided view” where a strided view is a
triple (offset, sizes, strides`). Each coordinate maps to
a flat memory index using the formula:
index = offset + ∑ i_k * strides[k]where i_k is the coordinate in dimension k.
The select(dim, range) operation restricts the view to a
subrange along a single dimension. It refines the shape by
updating the offset, sizes[dim], and strides[dim] to
describe a logically reindexed subregion:
offset += begin x strides[dim]
sizes[dim] = (end - begin) / step
strides[dim] *= stepThis transformation preserves the strided layout and avoids
copying data. After select, the view behaves as if indexing
starts at zero in the selected dimension, with a new length
and stride. From the user’s perspective, nothing changes —
indexing remains zero-based, and the resulting shape can be
used like any other. The transformation is internal: the
view’s offset and stride absorb the selection logic.
select is composable — it can be applied repeatedly, even on
the same dimension, to refine the view incrementally.
Sourcepub fn select_iter(&self, dims: usize) -> Result<SelectIterator<'_>, ShapeError>
pub fn select_iter(&self, dims: usize) -> Result<SelectIterator<'_>, ShapeError>
Produces an iterator over subshapes by fixing the first dims
dimensions.
For a shape of rank n, this yields ∏ sizes[0..dims]
subshapes, each with the first dims dimensions restricted to
size 1. The remaining dimensions are left unconstrained.
This is useful for structured traversal of slices within a
multidimensional shape. See SelectIterator for details and
examples.
§Errors
Returns an error if dims == 0 or dims >= self.rank().
Sourcepub fn index(&self, indices: Vec<(String, usize)>) -> Result<Shape, ShapeError>
pub fn index(&self, indices: Vec<(String, usize)>) -> Result<Shape, ShapeError>
Sub-set this shape by select a particular row of the given indices The resulting shape will no longer have dimensions for the given indices Example shape.index(vec![(“gpu”, 3), (“host”, 0)])
Sourcepub fn coordinates(
&self,
rank: usize,
) -> Result<Vec<(String, usize)>, ShapeError>
pub fn coordinates( &self, rank: usize, ) -> Result<Vec<(String, usize)>, ShapeError>
Return a set of labeled coordinates for the given rank.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Shape
impl<'de> Deserialize<'de> for Shape
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl ReshapeShapeExt for Shape
impl ReshapeShapeExt for Shape
Source§fn reshape(&self, limit: Limit) -> ReshapedShape
fn reshape(&self, limit: Limit) -> ReshapedShape
impl StructuralPartialEq for Shape
Auto Trait Implementations§
impl Freeze for Shape
impl RefUnwindSafe for Shape
impl Send for Shape
impl Sync for Shape
impl Unpin for Shape
impl UnwindSafe for Shape
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more