SliceRange

Struct SliceRange 

Source
pub struct SliceRange {
    pub start: isize,
    pub end: Option<isize>,
    /* private fields */
}
Expand description

A range for slicing a Tensor or NdTensor.

This has two main differences from Range.

  • A non-zero step between indices can be specified. The step can be negative, which means that the dimension should be traversed in reverse order.
  • The start and end indexes can also be negative, in which case they count backwards from the end of the array.

This system for specifying slicing and indexing follows NumPy, which in turn strongly influenced slicing in ONNX.

Fields§

§start: isize

First index in range.

§end: Option<isize>

Last index (exclusive) in range, or None if the range extends to the end of a dimension.

Implementations§

Source§

impl SliceRange

Source

pub fn new(start: isize, end: Option<isize>, step: isize) -> SliceRange

Create a new range from start to end. The start index is inclusive and the end value is exclusive. If end is None, the range spans to the end of the dimension.

Panics if the step size is 0.

Source

pub fn steps(&self, dim_size: usize) -> usize

Return the number of elements that would be retained if using this range to slice a dimension of size dim_size.

Source

pub fn clamp(&self, dim_size: usize) -> SliceRange

Return a copy of this range with indexes adjusted so that they are valid for a tensor dimension of size dim_size.

Valid indexes depend on direction that the dimension is traversed (forwards if self.step is positive or backwards if negative). They start at the first element going in that direction and end after the last element.

Source

pub fn step(&self) -> isize

Source

pub fn resolve_clamped(&self, dim_size: usize) -> Range<usize>

Clamp this range so that it is valid for a dimension of size dim_size and resolve it to a positive range.

This method is useful for implementing Python/NumPy-style slicing where range endpoints can be out of bounds.

Source

pub fn resolve(&self, dim_size: usize) -> Option<Range<usize>>

Resolve the range endpoints to a positive range in [0, dim_size).

Returns the range if resolved or None if out of bounds.

If self.step is positive, the returned range counts forwards from the first index of the dimension, otherwise it counts backwards from the last index.

Trait Implementations§

Source§

impl Clone for SliceRange

Source§

fn clone(&self) -> SliceRange

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SliceRange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<Range<T>> for SliceRange
where T: TryInto<isize>, <T as TryInto<isize>>::Error: Debug,

Source§

fn from(r: Range<T>) -> SliceRange

Converts to this type from the input type.
Source§

impl<T> From<RangeFrom<T>> for SliceRange
where T: TryInto<isize>, <T as TryInto<isize>>::Error: Debug,

Source§

fn from(r: RangeFrom<T>) -> SliceRange

Converts to this type from the input type.
Source§

impl From<RangeFull> for SliceRange

Source§

fn from(_: RangeFull) -> SliceRange

Converts to this type from the input type.
Source§

impl<T> From<RangeTo<T>> for SliceRange
where T: TryInto<isize>, <T as TryInto<isize>>::Error: Debug,

Source§

fn from(r: RangeTo<T>) -> SliceRange

Converts to this type from the input type.
Source§

impl IndexCount for SliceRange

Source§

type Count = U0

Type representing the count value.
Source§

fn index_count(&self) -> Option<usize>

Returns Count as a numeric value.
Source§

impl IsIndex for SliceRange

Source§

type IsIndex = U0

Associated type that is either U0 or U1 indicating whether this type is an index. Read more
Source§

impl PartialEq for SliceRange

Source§

fn eq(&self, other: &SliceRange) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for SliceRange

Source§

impl StructuralPartialEq for SliceRange

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoSliceItems for T
where T: Into<SliceItem>,

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.