pub struct Resizable<T> { /* private fields */ }Expand description
A row-major matrix that owns its storage and can change shape in place.
Use it as reusable scratch storage for matrices of different shapes.
Resizable::resize and Resizable::resize_with may reuse the existing
allocation. After a resize, every element holds a valid T, but the values
are unspecified: write the matrix before you read it.
use diskann_utils::views::rowmajor::{Matrix, MatrixMut, Resizable};
let mut scratch = Resizable::<f32>::new();
scratch.resize(2, 3).unwrap();
scratch.as_mut_slice().fill(1.0);
assert_eq!((scratch.nrows(), scratch.ncols()), (2, 3));
assert_eq!(scratch.row(1), &[1.0, 1.0, 1.0]);Implementations§
Source§impl<T> Resizable<T>
impl<T> Resizable<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct an empty 0 x 0 matrix with room for at least capacity
elements.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
The number of elements that the matrix can hold without allocating.
Sourcepub fn resize_with<F>(
&mut self,
nrows: usize,
ncols: usize,
f: F,
) -> Result<(), LayoutError>where
F: FnMut() -> T,
pub fn resize_with<F>(
&mut self,
nrows: usize,
ncols: usize,
f: F,
) -> Result<(), LayoutError>where
F: FnMut() -> T,
Set the shape to nrows x ncols. The element values are unspecified.
f creates the elements that the current storage cannot supply.
Returns an error if nrows x ncols is not a valid Layout. The shape
is then unchanged.
Sourcepub fn resize(&mut self, nrows: usize, ncols: usize) -> Result<(), LayoutError>where
T: Default,
pub fn resize(&mut self, nrows: usize, ncols: usize) -> Result<(), LayoutError>where
T: Default,
Set the shape to nrows x ncols. The element values are unspecified.
Returns an error if nrows x ncols is not a valid Layout. The shape
is then unchanged.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Drop the elements past the current shape and release unused memory.
Trait Implementations§
Source§impl<T> Matrix for Resizable<T>
impl<T> Matrix for Resizable<T>
Source§fn as_nonnull(&self) -> NonNull<T>
fn as_nonnull(&self) -> NonNull<T>
Source§unsafe fn row_unchecked(&self, row: usize) -> &[Self::Element]
unsafe fn row_unchecked(&self, row: usize) -> &[Self::Element]
Source§fn get_row(&self, row: usize) -> Option<&[Self::Element]>
fn get_row(&self, row: usize) -> Option<&[Self::Element]>
row if row < self.nrows(). Otherwise, return None.Source§fn rows(&self) -> Rows<'_, Self::Element> ⓘ
fn rows(&self) -> Rows<'_, Self::Element> ⓘ
Source§unsafe fn element_unchecked(&self, row: usize, col: usize) -> &Self::Element
unsafe fn element_unchecked(&self, row: usize, col: usize) -> &Self::Element
Source§fn subview(&self, rows: Range<usize>) -> Option<Ref<'_, Self::Element>>
fn subview(&self, rows: Range<usize>) -> Option<Ref<'_, Self::Element>>
rows, or None when the range is invalid.Source§fn window_iter(&self, batchsize: NonZeroUsize) -> Windows<'_, Self::Element> ⓘ
fn window_iter(&self, batchsize: NonZeroUsize) -> Windows<'_, Self::Element> ⓘ
batchsize rows with self.ncols() columns. Read moreSource§fn to_rowmajor_owned(&self) -> Owned<Self::Element>
fn to_rowmajor_owned(&self) -> Owned<Self::Element>
Owned with the same shape as self and cloned contents.Source§fn par_rows(&self) -> impl IndexedParallelIterator<Item = &[Self::Element]>
fn par_rows(&self) -> impl IndexedParallelIterator<Item = &[Self::Element]>
Source§fn par_window_iter(
&self,
batchsize: usize,
) -> impl IndexedParallelIterator<Item = Ref<'_, Self::Element>>
fn par_window_iter( &self, batchsize: usize, ) -> impl IndexedParallelIterator<Item = Ref<'_, Self::Element>>
batchsize rows with self.ncols() columns. Read moreSource§impl<T> MatrixMut for Resizable<T>
impl<T> MatrixMut for Resizable<T>
Source§fn as_nonnull_mut(&mut self) -> NonNull<T>
fn as_nonnull_mut(&mut self) -> NonNull<T>
Source§unsafe fn row_unchecked_mut(&mut self, row: usize) -> &mut [Self::Element]
unsafe fn row_unchecked_mut(&mut self, row: usize) -> &mut [Self::Element]
Source§fn as_mut_ptr(&mut self) -> *mut Self::Element
fn as_mut_ptr(&mut self) -> *mut Self::Element
Source§fn as_mut_slice(&mut self) -> &mut [Self::Element]
fn as_mut_slice(&mut self) -> &mut [Self::Element]
Source§fn row_mut(&mut self, row: usize) -> &mut [Self::Element]
fn row_mut(&mut self, row: usize) -> &mut [Self::Element]
row as a mutable slice. Read moreSource§fn get_row_mut(&mut self, row: usize) -> Option<&mut [Self::Element]>
fn get_row_mut(&mut self, row: usize) -> Option<&mut [Self::Element]>
row if row < self.nrows(). Otherwise, return None.Source§fn rows_mut(&mut self) -> RowsMut<'_, Self::Element> ⓘ
fn rows_mut(&mut self) -> RowsMut<'_, Self::Element> ⓘ
Source§unsafe fn element_unchecked_mut(
&mut self,
row: usize,
col: usize,
) -> &mut Self::Element
unsafe fn element_unchecked_mut( &mut self, row: usize, col: usize, ) -> &mut Self::Element
Source§fn as_view_mut(&mut self) -> Mut<'_, Self::Element>
fn as_view_mut(&mut self) -> Mut<'_, Self::Element>
Source§fn subview_mut(&mut self, rows: Range<usize>) -> Option<Mut<'_, Self::Element>>
fn subview_mut(&mut self, rows: Range<usize>) -> Option<Mut<'_, Self::Element>>
rows, or None when the range is invalid.Source§fn par_rows_mut(
&mut self,
) -> impl IndexedParallelIterator<Item = &mut [Self::Element]>
fn par_rows_mut( &mut self, ) -> impl IndexedParallelIterator<Item = &mut [Self::Element]>
Source§fn par_window_iter_mut(
&mut self,
batchsize: usize,
) -> impl IndexedParallelIterator<Item = Mut<'_, Self::Element>>
fn par_window_iter_mut( &mut self, batchsize: usize, ) -> impl IndexedParallelIterator<Item = Mut<'_, Self::Element>>
batchsize rows with self.ncols() columns. Read moreAuto Trait Implementations§
impl<T> Forget for Resizable<T>
impl<T> Freeze for Resizable<T>
impl<T> Move for Resizable<T>
impl<T> RefUnwindSafe for Resizable<T>
impl<T> Send for Resizable<T>
impl<T> Sync for Resizable<T>
impl<T> Unpin for Resizable<T>
impl<T> UnsafeUnpin for Resizable<T>
impl<T> UnwindSafe for Resizable<T>
Blanket Implementations§
impl<T> AsyncFriendly for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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