Skip to main content

Resizable

Struct Resizable 

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

Source

pub fn new() -> Self

Construct an empty 0 x 0 matrix. No memory is allocated.

Source

pub fn with_capacity(capacity: usize) -> Self

Construct an empty 0 x 0 matrix with room for at least capacity elements.

Source

pub fn capacity(&self) -> usize

The number of elements that the matrix can hold without allocating.

Source

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.

Source

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.

Source

pub fn clear(&mut self)

Set the shape to 0 x 0. The allocation is kept.

Source

pub fn shrink_to_fit(&mut self)

Drop the elements past the current shape and release unused memory.

Trait Implementations§

Source§

impl<T: Clone> Clone for Resizable<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Resizable<T>

Source§

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

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

impl<T> Default for Resizable<T>

Source§

fn default() -> Self

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

impl<T> From<Owned<T>> for Resizable<T>

Source§

fn from(matrix: Owned<T>) -> Self

Take the storage of matrix without copying it.

Source§

impl<T> From<Resizable<T>> for Owned<T>

Source§

fn from(matrix: Resizable<T>) -> Self

Keep the elements of the current shape and drop the rest.

Source§

impl<T> Matrix for Resizable<T>

Source§

type Element = T

The type of the element stored in the matrix.
Source§

fn as_nonnull(&self) -> NonNull<T>

Return the base pointer for the matrix.
Source§

fn layout(&self) -> Layout<T>

Return the Layout for the matrix.
Source§

fn nrows(&self) -> usize

Return the number of rows in the matrix.
Source§

fn ncols(&self) -> usize

Return the number of columns in the matrix.
Source§

unsafe fn row_unchecked(&self, row: usize) -> &[Self::Element]

Returns the requested row without bounds checking. Read more
Source§

fn as_ptr(&self) -> *const Self::Element

Return a pointer to the base of the matrix.
Source§

fn as_slice(&self) -> &[Self::Element]

Return the underlying data as a slice.
Source§

fn row(&self, row: usize) -> &[Self::Element]

Return row row as a slice. Read more
Source§

fn get_row(&self, row: usize) -> Option<&[Self::Element]>

Return row row if row < self.nrows(). Otherwise, return None.
Source§

fn rows(&self) -> Rows<'_, Self::Element> ⓘ

Return a iterator over all rows in the matrix. Read more
Source§

unsafe fn element_unchecked(&self, row: usize, col: usize) -> &Self::Element

Returns a reference to an element without bounds checking. Read more
Source§

fn get_element(&self, row: usize, col: usize) -> Option<&Self::Element>

Return the value at the specified row and col. Read more
Source§

fn element(&self, row: usize, col: usize) -> &Self::Element

Return the value at the specified row and col. Read more
Source§

fn as_view(&self) -> Ref<'_, Self::Element>

Return a view over the matrix.
Source§

fn subview(&self, rows: Range<usize>) -> Option<Ref<'_, Self::Element>>

Return a view over the rows in rows, or None when the range is invalid.
Source§

fn window_iter(&self, batchsize: NonZeroUsize) -> Windows<'_, Self::Element> ⓘ

Return an iterator that divides the matrix into sub-matrices with (up to) batchsize rows with self.ncols() columns. Read more
Source§

fn to_rowmajor_owned(&self) -> Owned<Self::Element>
where Self::Element: Clone,

Return an Owned with the same shape as self and cloned contents.
Source§

fn try_map<F, R>(&self, f: F) -> Result<Owned<R>, LayoutError>
where F: FnMut(&Self::Element) -> R,

Create a new Matrix by applying the closure f to each element. Read more
Source§

fn map<F, R>(&self, f: F) -> Owned<R>
where F: FnMut(&Self::Element) -> R,

Create a new Matrix by applying the closure f to each element. Read more
Source§

fn transpose(&self) -> Owned<Self::Element>
where Self::Element: Clone,

Transpose the elements in self.
Source§

fn par_rows(&self) -> impl IndexedParallelIterator<Item = &[Self::Element]>
where Self::Element: Sync,

Return a parallel iterator over the rows of the matrix.
Source§

fn par_window_iter( &self, batchsize: usize, ) -> impl IndexedParallelIterator<Item = Ref<'_, Self::Element>>
where Self::Element: Sync,

Return a parallel iterator that divides the matrix into sub-matrices with (up to) batchsize rows with self.ncols() columns. Read more
Source§

impl<T> MatrixMut for Resizable<T>

Source§

fn as_nonnull_mut(&mut self) -> NonNull<T>

Return the base pointer for the matrix. Read more
Source§

unsafe fn row_unchecked_mut(&mut self, row: usize) -> &mut [Self::Element]

Returns the requested row without bounds checking. Read more
Source§

fn as_mut_ptr(&mut self) -> *mut Self::Element

Return a pointer to the base of the matrix.
Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Return the underlying data as a mutable slice.
Source§

fn row_mut(&mut self, row: usize) -> &mut [Self::Element]

Return row row as a mutable slice. Read more
Source§

fn get_row_mut(&mut self, row: usize) -> Option<&mut [Self::Element]>

Return row row if row < self.nrows(). Otherwise, return None.
Source§

fn rows_mut(&mut self) -> RowsMut<'_, Self::Element> ⓘ

Return a mutable iterator over all rows in the matrix. Read more
Source§

unsafe fn element_unchecked_mut( &mut self, row: usize, col: usize, ) -> &mut Self::Element

Returns a mutable reference to an element without bounds checking. Read more
Source§

fn get_element_mut( &mut self, row: usize, col: usize, ) -> Option<&mut Self::Element>

Return the value at the specified row and col. Read more
Source§

fn element_mut(&mut self, row: usize, col: usize) -> &mut Self::Element

Return the value at the specified row and col. Read more
Source§

fn as_view_mut(&mut self) -> Mut<'_, Self::Element>

Return a view over the matrix.
Source§

fn subview_mut(&mut self, rows: Range<usize>) -> Option<Mut<'_, Self::Element>>

Return a mutable view over the rows in rows, or None when the range is invalid.
Source§

fn par_rows_mut( &mut self, ) -> impl IndexedParallelIterator<Item = &mut [Self::Element]>
where Self::Element: Send,

Return a parallel iterator over the rows of the matrix. Read more
Source§

fn par_window_iter_mut( &mut self, batchsize: usize, ) -> impl IndexedParallelIterator<Item = Mut<'_, Self::Element>>
where Self::Element: Send,

Return a parallel iterator that divides the matrix into mutable sub-matrices with (up to) batchsize rows with self.ncols() columns. Read more
Source§

impl<T> PartialEq for Resizable<T>
where T: PartialEq,

Source§

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

Compare the shapes and the elements of the shapes.

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a, T> Reborrow<'a> for Resizable<T>

Source§

type Target = Ref<'a, T>

Source§

fn reborrow(&'a self) -> Self::Target

Borrow self into a generalized reference type and reborrow
Source§

impl<'a, T> ReborrowMut<'a> for Resizable<T>

Source§

type Target = Mut<'a, T>

Source§

fn reborrow_mut(&'a mut self) -> Self::Target

Mutably borrow self into a generalized reference type and reborrow.

Auto Trait Implementations§

§

impl<T> Forget for Resizable<T>

§

impl<T> Freeze for Resizable<T>
where Vec<T>: Freeze, Layout<T>: Freeze,

§

impl<T> Move for Resizable<T>

§

impl<T> RefUnwindSafe for Resizable<T>

§

impl<T> Send for Resizable<T>
where Vec<T>: Send, Layout<T>: Send,

§

impl<T> Sync for Resizable<T>
where Vec<T>: Sync, Layout<T>: Sync,

§

impl<T> Unpin for Resizable<T>
where Vec<T>: Unpin, Layout<T>: Unpin,

§

impl<T> UnsafeUnpin for Resizable<T>

§

impl<T> UnwindSafe for Resizable<T>

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> AsyncFriendly for T
where T: Send + Sync + 'static,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V