pub struct Extent { /* private fields */ }Expand description
Extent defines the logical shape of a multidimensional space by
assigning a size to each named dimension. It abstracts away memory
layout and focuses solely on structure — what dimensions exist and
how many elements each contains.
Conceptually, it corresponds to a coordinate space in the mathematical sense.
Implementations§
Source§impl Extent
impl Extent
Sourcepub fn new(labels: Vec<String>, sizes: Vec<usize>) -> Result<Self, ExtentError>
pub fn new(labels: Vec<String>, sizes: Vec<usize>) -> Result<Self, ExtentError>
Creates a new Extent from the given labels and sizes.
pub fn unity() -> Extent
Sourcepub fn size(&self, label: &str) -> Option<usize>
pub fn size(&self, label: &str) -> Option<usize>
Returns the size of the dimension with the given label, if it exists.
Sourcepub fn position(&self, label: &str) -> Option<usize>
pub fn position(&self, label: &str) -> Option<usize>
Returns the position of the dimension with the given label, if it exists exists.
pub fn rank_of_coords(&self, coords: &[usize]) -> Result<usize, PointError>
Sourcepub fn point(&self, coords: Vec<usize>) -> Result<Point, PointError>
pub fn point(&self, coords: Vec<usize>) -> Result<Point, PointError>
Creates a Point in this extent from the given coordinate
vector.
The coordinates are interpreted in row-major order against
self.sizes(). This constructor does not store the
coordinates; it computes the linear rank and returns a
Point that stores { rank, extent }.
§Errors
Returns:
PointError::DimMismatchifcoords.len() != self.len().PointError::OutOfRangeIndexif any coordinate `coords[i]= self.sizes()[i]`.
§Examples
use ndslice::extent;
let ext = extent!(x = 2, y = 3, z = 4);
let p = ext.point(vec![1, 2, 3]).unwrap();
assert_eq!(p.rank(), 1 * (3 * 4) + 2 * 4 + 3); // row-major
assert_eq!(p.coords(), vec![1, 2, 3]);Dimension mismatch:
use ndslice::PointError;
use ndslice::extent;
let ext = extent!(x = 2, y = 3);
let err = ext.point(vec![1]).unwrap_err();
matches!(err, PointError::DimMismatch { .. });Coordinate out of range:
use ndslice::PointError;
use ndslice::extent;
let ext = extent!(x = 2, y = 3);
let err = ext.point(vec![1, 3]).unwrap_err(); // y size is 3, max index is 2
matches!(err, PointError::OutOfRangeIndex { .. });Sourcepub fn point_of_rank(&self, rank: usize) -> Result<Point, PointError>
pub fn point_of_rank(&self, rank: usize) -> Result<Point, PointError>
Returns the Point corresponding to the given linearized
rank within this extent, using row-major order.
§Errors
Returns PointError::OutOfRangeRank if rank >= self.num_ranks(), i.e. when the requested rank lies outside
the bounds of this extent.
§Examples
use ndslice::extent;
let ext = extent!(x = 2, y = 3);
assert_eq!(ext.num_ranks(), 6);
let p = ext.point_of_rank(4).unwrap();
assert_eq!(p.coords(), vec![1, 1]); // row-major: x=1, y=1
assert_eq!(p.rank(), 4);
assert!(ext.point_of_rank(6).is_err()); // out of rangeSourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of dimensions in this extent.
For example, an extent defined as (x=2, y=3, z=4) has
dimensionality 3.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this extent has zero dimensions.
A 0-dimensional extent corresponds to the scalar case: a
coordinate space with exactly one rank (the empty tuple []).
Sourcepub fn num_ranks(&self) -> usize
pub fn num_ranks(&self) -> usize
Returns the total number of ranks (points) in this extent.
This is the product of all dimension sizes, i.e. the number of distinct coordinates in row-major order.
Sourcepub fn into_inner(self) -> (Vec<String>, Vec<usize>)
pub fn into_inner(self) -> (Vec<String>, Vec<usize>)
Convert this extent into its labels and sizes.
Sourcepub fn iter(&self) -> impl Iterator<Item = (String, usize)> + use<'_>
pub fn iter(&self) -> impl Iterator<Item = (String, usize)> + use<'_>
Iterate over this extent’s labels and sizes.
Sourcepub fn points(&self) -> ExtentPointsIterator<'_> ⓘ
pub fn points(&self) -> ExtentPointsIterator<'_> ⓘ
Iterate points in this extent.
Sourcepub fn concat(&self, other: &Extent) -> Result<Self, ExtentError>
pub fn concat(&self, other: &Extent) -> Result<Self, ExtentError>
Append the dimensions of other to this extent, preserving order.
Duplicate labels are not allowed: if any label in other already appears
in self, this returns ExtentError::OverlappingLabel.
This operation is not commutative: a.concat(&b) may differ from
b.concat(&a).
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Extent
impl<'de> Deserialize<'de> for Extent
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 Display for Extent
Formats an Extent as a compact map‐literal:
impl Display for Extent
Formats an Extent as a compact map‐literal:
{label: size, label: size, ...}§Grammar
Extent := "{" [ Pair ( "," Pair )* ] "}"
Pair := Label ": " Size
Label := SafeIdent | Quoted
SafeIdent:= [A-Za-z0-9_]+
Quoted := "\"" ( [^"\\] | "\\" . )* "\""
Size := [0-9]+§Quoting rules
- Labels that are not
SafeIdentare rendered using Rust string literal syntax (viaformat!("{:?}", label)), e.g."dim/0"or"x y". - “Safe” means ASCII alphanumeric or underscore (
[A-Za-z0-9_]+). Everything else is quoted. This keeps common identifiers unquoted and unambiguous.
§Examples
{x: 4, y: 5, z: 6}
{"dim/0": 3, "dim,1": 5}
{}Implementation note: label rendering goes through fmt_label,
which emits the label as-is for safe idents, otherwise as a Rust
string literal.
Source§impl View for Extent
An Extent is also a View.
impl View for Extent
An Extent is also a View.
impl Eq for Extent
impl StructuralPartialEq for Extent
Auto Trait Implementations§
impl Freeze for Extent
impl RefUnwindSafe for Extent
impl Send for Extent
impl Sync for Extent
impl Unpin for Extent
impl UnwindSafe for Extent
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 moreSource§impl<T> ViewExt for Twhere
T: View,
impl<T> ViewExt for Twhere
T: View,
Source§fn range<R>(&self, dim: &str, range: R) -> Result<<T as View>::View, ViewError>
fn range<R>(&self, dim: &str, range: R) -> Result<<T as View>::View, ViewError>
Source§fn group_by(
&self,
dim: &str,
) -> Result<impl Iterator<Item = <T as View>::View>, ViewError>
fn group_by( &self, dim: &str, ) -> Result<impl Iterator<Item = <T as View>::View>, ViewError>
dim. The returned iterator enumerates all groups
as views in the extent of dim to the last dimension of the view. Read more