Skip to main content

BlockRef

Struct BlockRef 

Source
pub struct BlockRef<'a, T> { /* private fields */ }
Expand description

A reference to a block of a matrix with shared access to elements.

Implementations§

Source§

impl<'data, T> BlockRef<'data, T>

Source

pub fn new(data: &'data [T], pitch: usize) -> Self

Create a new block reference from a raw slice and pitch.

The resulting block refers to the whole matrix.

§Panics

Panics if the length of data is not a multiple of pitch.

Source

pub fn rows(&self) -> usize

Number of rows in this block.

Source

pub fn cols(&self) -> usize

Number of columns in this block.

Source

pub fn split_at_col( self, mid: usize, ) -> (BlockRef<'data, T>, BlockRef<'data, T>)

Divide into two blocks at the given column.

§Examples
let data = &[
    [0, 1, 2],
    [3, 4, 5],
];

let block = matrix_slice::from_array_rows(data);
let (left, right) = block.split_at_col(2);

assert_eq!(left[(1, 0)], 3);
assert_eq!(right[(1, 0)], 5);
Source

pub fn split_at_col_checked( self, mid: usize, ) -> Option<(BlockRef<'data, T>, BlockRef<'data, T>)>

Divide into two blocks at the given column.

See Self::split_at_col but returns None if out of bounds.

Source

pub fn split_at_row( self, mid: usize, ) -> (BlockRef<'data, T>, BlockRef<'data, T>)

Divide into two blocks at the given row.

§Examples
let data = &[
    [0, 1, 2],
    [3, 4, 5],
];

let block = matrix_slice::from_array_rows(data);
let (top, bot) = block.split_at_row(1);

assert_eq!(top[(0, 2)], 2);
assert_eq!(bot[(0, 2)], 5);
Source

pub fn split_at_row_checked( self, mid: usize, ) -> Option<(BlockRef<'data, T>, BlockRef<'data, T>)>

Divide into two blocks at the given row.

See Self::split_at_row but returns None if out of bounds.

Source

pub fn row(self, row: usize) -> VecRef<'data, T>

Choose a single row and refer to its data.

§Examples
let data = &[
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
];

let block = matrix_slice::from_array_rows(data);
let row = block.row(1);
assert_eq!(row[0], 3);
Source

pub fn row_checked(self, row: usize) -> Option<VecRef<'data, T>>

Choose a single row and refer to its data.

See Self::row but returns None if out of bounds.

Source

pub fn col(self, col: usize) -> VecRef<'data, T>

Choose a single column and refer to its data.

§Examples
let data = &[
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
];

let block = matrix_slice::from_array_rows(data);
let row = block.col(1);
assert_eq!(row[0], 1);
Source

pub fn col_checked(self, col: usize) -> Option<VecRef<'data, T>>

Choose a single column and refer to its data.

See Self::col but returns None if out of bounds.

Source

pub fn select_rows<R>(self, range: R) -> Option<BlockRef<'data, T>>
where R: MatrixIndex,

Choose a range of rows and contract the block to that.

The argument type is flexible, allowing ranges (1..3), half open ranges (2.. and ..2) among others. See the MatrixIndex trait, which is sealed though as its details are not yet finalized.

§Examples
let data = &[
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
];

let block = matrix_slice::from_array_rows(data);

let center = block.select_rows(1..2).unwrap();
assert_eq!(center.rows(), 1);
assert_eq!(center.cols(), 3);
assert_eq!(center[(0, 1)], 4);
Source

pub fn select_cols<R>(self, range: R) -> Option<BlockRef<'data, T>>
where R: MatrixIndex,

Choose a range of columns and contract the block to that.

The argument type is flexible, allowing ranges (1..3), half open ranges (2.. and ..2) among others. See the MatrixIndex trait, which is sealed though as its details are not yet finalized.

§Examples
let data = &[
    [0, 1],
    [3, 4],
    [6, 7],
];

let mut block = matrix_slice::from_array_rows(data);

assert!(block.reborrow().select_cols(1..).is_some_and(|b| b.cols() == 1));
assert!(block.reborrow().select_cols(2..).is_some_and(|b| b.cols() == 0));
assert!(block.reborrow().select_cols(3..).is_none());
Source

pub fn select( self, row_range: impl MatrixIndex, col_range: impl MatrixIndex, ) -> Option<BlockRef<'data, T>>

Choose a sub-block by its range of rows and columns.

Source

pub fn into_contiguous_slice(self) -> Option<&'data [T]>

Extract a contiguous underlying slice of elements if the block is contiguous.

§Examples
let data = &[[0u32; 3]; 3];
let block = matrix_slice::from_array_rows(data);

let (block, _) = block.split_at_row(2);
assert!(block.into_contiguous_slice().is_some());

let (pre, post) = block.split_at_col(2);
assert!(pre.into_contiguous_slice().is_none());
assert!(post.into_contiguous_slice().is_none());

let (same, _) = block.split_at_col(3);
assert!(same.into_contiguous_slice().is_some());
Source

pub fn into_array_rows_checked<const N: usize>(self) -> Option<&'data [[T; N]]>

Extract access as a slice of arrays if the block is contiguous.

The caller must choose N matching the number of columns.

Source

pub fn iter_rows(self) -> IterRows<'data, T>

Iterate over the rows of this block.

Source

pub fn reborrow(&self) -> BlockRef<'_, T>

Create a reference to this block with a shorter lifetime.

Trait Implementations§

Source§

impl<'a, T: Clone> Clone for BlockRef<'a, T>

Source§

fn clone(&self) -> BlockRef<'a, T>

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<T: Debug> Debug for BlockRef<'_, T>

Source§

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

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

impl<T> Default for BlockRef<'_, T>

Creates an empty block reference, within a matrix of a dangling slice.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Index<(usize, usize)> for BlockRef<'_, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T: Copy> Copy for BlockRef<'a, T>

Source§

impl<T> Send for BlockRef<'_, T>
where T: Sync,

Source§

impl<T> Sync for BlockRef<'_, T>
where T: Sync,

Auto Trait Implementations§

§

impl<'a, T> Freeze for BlockRef<'a, T>

§

impl<'a, T> RefUnwindSafe for BlockRef<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Unpin for BlockRef<'a, T>

§

impl<'a, T> UnwindSafe for BlockRef<'a, T>
where T: RefUnwindSafe,

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, 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.