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
startandendindexes 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: isizeFirst 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
impl SliceRange
Sourcepub fn new(start: isize, end: Option<isize>, step: isize) -> SliceRange
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.
Sourcepub fn steps(&self, dim_size: usize) -> usize
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.
Sourcepub fn clamp(&self, dim_size: usize) -> SliceRange
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.
pub fn step(&self) -> isize
Sourcepub fn resolve_clamped(&self, dim_size: usize) -> Range<usize>
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.
Sourcepub fn resolve(&self, dim_size: usize) -> Option<Range<usize>>
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
impl Clone for SliceRange
Source§fn clone(&self) -> SliceRange
fn clone(&self) -> SliceRange
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SliceRange
impl Debug for SliceRange
Source§impl<T> From<Range<T>> for SliceRange
impl<T> From<Range<T>> for SliceRange
Source§fn from(r: Range<T>) -> SliceRange
fn from(r: Range<T>) -> SliceRange
Source§impl<T> From<RangeFrom<T>> for SliceRange
impl<T> From<RangeFrom<T>> for SliceRange
Source§fn from(r: RangeFrom<T>) -> SliceRange
fn from(r: RangeFrom<T>) -> SliceRange
Source§impl From<RangeFull> for SliceRange
impl From<RangeFull> for SliceRange
Source§fn from(_: RangeFull) -> SliceRange
fn from(_: RangeFull) -> SliceRange
Source§impl<T> From<RangeTo<T>> for SliceRange
impl<T> From<RangeTo<T>> for SliceRange
Source§fn from(r: RangeTo<T>) -> SliceRange
fn from(r: RangeTo<T>) -> SliceRange
Source§impl IndexCount for SliceRange
impl IndexCount for SliceRange
Source§impl IsIndex for SliceRange
impl IsIndex for SliceRange
Source§impl PartialEq for SliceRange
impl PartialEq for SliceRange
impl Copy for SliceRange
impl StructuralPartialEq for SliceRange
Auto Trait Implementations§
impl Freeze for SliceRange
impl RefUnwindSafe for SliceRange
impl Send for SliceRange
impl Sync for SliceRange
impl Unpin for SliceRange
impl UnwindSafe for SliceRange
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