pub unsafe trait Matrix {
type Element;
Show 22 methods
// Required methods
fn as_nonnull(&self) -> NonNull<Self::Element>;
fn layout(&self) -> Layout<Self::Element>;
// Provided methods
fn nrows(&self) -> usize { ... }
fn ncols(&self) -> usize { ... }
unsafe fn row_unchecked(&self, row: usize) -> &[Self::Element] { ... }
fn as_ptr(&self) -> *const Self::Element { ... }
fn as_slice(&self) -> &[Self::Element] { ... }
fn row(&self, row: usize) -> &[Self::Element] { ... }
fn get_row(&self, row: usize) -> Option<&[Self::Element]> { ... }
fn rows(&self) -> Rows<'_, Self::Element> ⓘ { ... }
unsafe fn element_unchecked(&self, row: usize, col: usize) -> &Self::Element { ... }
fn get_element(&self, row: usize, col: usize) -> Option<&Self::Element> { ... }
fn element(&self, row: usize, col: usize) -> &Self::Element { ... }
fn as_view(&self) -> Ref<'_, Self::Element> { ... }
fn subview(&self, rows: Range<usize>) -> Option<Ref<'_, Self::Element>> { ... }
fn window_iter(&self, batchsize: NonZeroUsize) -> Windows<'_, Self::Element> ⓘ { ... }
fn to_rowmajor_owned(&self) -> Owned<Self::Element>
where Self::Element: Clone { ... }
fn try_map<F, R>(&self, f: F) -> Result<Owned<R>, LayoutError>
where F: FnMut(&Self::Element) -> R { ... }
fn map<F, R>(&self, f: F) -> Owned<R>
where F: FnMut(&Self::Element) -> R { ... }
fn transpose(&self) -> Owned<Self::Element>
where Self::Element: Clone { ... }
fn par_rows(&self) -> impl IndexedParallelIterator<Item = &[Self::Element]>
where Self::Element: Sync { ... }
fn par_window_iter(
&self,
batchsize: usize,
) -> impl IndexedParallelIterator<Item = Ref<'_, Self::Element>>
where Self::Element: Sync { ... }
}Expand description
Tools for treating contiguous regions of memory as a dense, row-major matrix.
See also: MatrixMut, Owned, Ref, Mut.
§Safety
For any shared reference matrix: &Self, the following must hold for the entire lifetime
of that reference:
-
Every call to
matrix.as_nonnull()returns the same pointer. -
Every call to
matrix.layout()returns the same layout. -
The pointer and layout describe the same valid initialized span throughout that lifetime. Given:
let data = matrix.as_nonnull(); let layout = matrix.layout();constructing the following slice is valid:
unsafe { std::slice::from_raw_parts(data.as_ptr().cast_const(), layout.num_elements()) };In particular:
-
datamust be properly aligned, including whenlayout.num_elements() == 0. -
If both
layout.num_elements()andsize_of::<Self::Element>()are nonzero, the described span must lie within a single allocation. -
The span must contain
layout.num_elements()consecutive, properly initialized values of typeSelf::Element. -
The referenced memory must not be mutated for the lifetime of
matrix, except through anUnsafeCell.
-
Required Associated Types§
Required Methods§
Sourcefn as_nonnull(&self) -> NonNull<Self::Element>
fn as_nonnull(&self) -> NonNull<Self::Element>
Return the base pointer for the matrix.
Provided Methods§
Sourceunsafe fn row_unchecked(&self, row: usize) -> &[Self::Element]
unsafe fn row_unchecked(&self, row: usize) -> &[Self::Element]
Returns the requested row without bounds checking.
§Safety
The following conditions must hold to avoid undefined behavior:
row < self.nrows().
Sourcefn get_row(&self, row: usize) -> Option<&[Self::Element]>
fn get_row(&self, row: usize) -> Option<&[Self::Element]>
Return row row if row < self.nrows(). Otherwise, return None.
Sourcefn rows(&self) -> Rows<'_, Self::Element> ⓘ
fn rows(&self) -> Rows<'_, Self::Element> ⓘ
Return a iterator over all rows in the matrix.
Rows are yielded sequentially beginning with row 0.
Sourceunsafe fn element_unchecked(&self, row: usize, col: usize) -> &Self::Element
unsafe fn element_unchecked(&self, row: usize, col: usize) -> &Self::Element
Returns a reference to an element without bounds checking.
§Safety
The following conditions must hold to avoid undefined behavior:
row < self.nrows().col < self.ncols().
Sourcefn get_element(&self, row: usize, col: usize) -> Option<&Self::Element>
fn get_element(&self, row: usize, col: usize) -> Option<&Self::Element>
Return the value at the specified row and col.
If either index is out-of-bounds, return None.
Sourcefn element(&self, row: usize, col: usize) -> &Self::Element
fn element(&self, row: usize, col: usize) -> &Self::Element
Return the value at the specified row and col.
§Panics
Panics if either row or col is out-of-bounds.
Sourcefn subview(&self, rows: Range<usize>) -> Option<Ref<'_, Self::Element>>
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.
Sourcefn window_iter(&self, batchsize: NonZeroUsize) -> Windows<'_, Self::Element> ⓘ
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.
It is possible for yielded sub-matrices to have fewer than batchsize rows if the
number of rows in the parent matrix is not evenly divisible by batchsize.
Sourcefn to_rowmajor_owned(&self) -> Owned<Self::Element>
fn to_rowmajor_owned(&self) -> Owned<Self::Element>
Return an Owned with the same shape as self and cloned contents.
Sourcefn try_map<F, R>(&self, f: F) -> Result<Owned<R>, LayoutError>
fn try_map<F, R>(&self, f: F) -> Result<Owned<R>, LayoutError>
Create a new Matrix by applying the closure f to each element.
The returned matrix has the same shape as self.
Sourcefn par_rows(&self) -> impl IndexedParallelIterator<Item = &[Self::Element]>
fn par_rows(&self) -> impl IndexedParallelIterator<Item = &[Self::Element]>
Return a parallel iterator over the rows of the matrix.
Sourcefn 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>>
Return a parallel iterator that divides the matrix into sub-matrices with (up to)
batchsize rows with self.ncols() columns.
This allows workers in parallel algorithms to work on dense subsets of the whole matrix for better locality.
It is possible for yielded sub-matrices to have fewer than batchsize rows if the
number of rows in the parent matrix is not evenly divisible by batchsize.
§Panics
Panics if batchsize = 0.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".