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>
impl<'data, T> BlockRef<'data, T>
Sourcepub fn new(data: &'data [T], pitch: usize) -> Self
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.
Sourcepub fn split_at_col(
self,
mid: usize,
) -> (BlockRef<'data, T>, BlockRef<'data, T>)
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);Sourcepub fn split_at_col_checked(
self,
mid: usize,
) -> Option<(BlockRef<'data, T>, BlockRef<'data, T>)>
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.
Sourcepub fn split_at_row(
self,
mid: usize,
) -> (BlockRef<'data, T>, BlockRef<'data, T>)
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);Sourcepub fn split_at_row_checked(
self,
mid: usize,
) -> Option<(BlockRef<'data, T>, BlockRef<'data, T>)>
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.
Sourcepub fn row(self, row: usize) -> VecRef<'data, T>
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);Sourcepub fn col(self, col: usize) -> VecRef<'data, T>
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);Sourcepub fn select_rows<R>(self, range: R) -> Option<BlockRef<'data, T>>where
R: MatrixIndex,
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);Sourcepub fn select_cols<R>(self, range: R) -> Option<BlockRef<'data, T>>where
R: MatrixIndex,
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.
Sourcepub fn select(
self,
row_range: impl MatrixIndex,
col_range: impl MatrixIndex,
) -> Option<BlockRef<'data, T>>
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.
Sourcepub fn into_contiguous_slice(self) -> Option<&'data [T]>
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());Sourcepub fn into_array_rows_checked<const N: usize>(self) -> Option<&'data [[T; N]]>
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.
Trait Implementations§
Source§impl<T> Default for BlockRef<'_, T>
Creates an empty block reference, within a matrix of a dangling slice.
impl<T> Default for BlockRef<'_, T>
Creates an empty block reference, within a matrix of a dangling slice.