pub struct DynamicMatrix<T> { /* private fields */ }
Expand description
A dynamic matrix in stored in row-major order.
Adding a new row is cheap while adding a new column is expensive.
use dynamic_matrix::{dynamic_matrix, DynamicMatrix};
let mut mat = dynamic_matrix![
1, 2;
4, 5;
];
// let mat: DynamicMatrix<isize> = DynamicMatrix::new([[1, 2], [4, 5]]);
assert_eq!(mat.shape(), (2, 2));
mat.push_row(vec![7, 8]).unwrap();
mat.push_col(vec![3, 6, 10]).unwrap();
assert_eq!(mat.shape(), (3, 3));
assert_eq!(mat[(1, 2)], 6);
mat[(2, 2)] = 9;
assert_eq!(mat.as_slice(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
Implementations§
Source§impl<T> DynamicMatrix<T>
impl<T> DynamicMatrix<T>
Sourcepub fn new<const COLS: usize, const ROWS: usize>(
data: [[T; COLS]; ROWS],
) -> Self
pub fn new<const COLS: usize, const ROWS: usize>( data: [[T; COLS]; ROWS], ) -> Self
Constructs a new DynamicMatrix from a nested array
let mat: DynamicMatrix<isize> = DynamicMatrix::new([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
assert_eq!(mat.shape(), (3, 3));
assert_eq!(mat.as_slice(), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
Sourcepub fn new_with_cols(cols: usize) -> Self
pub fn new_with_cols(cols: usize) -> Self
Constructs a new empty DynamicMatrix with a set number of columns
let mat: DynamicMatrix<isize> = DynamicMatrix::new_with_cols(3);
assert_eq!(mat.rows(), 0);
assert_eq!(mat.cols(), 3);
Sourcepub fn with_capacity(shape: (usize, usize)) -> Self
pub fn with_capacity(shape: (usize, usize)) -> Self
Constructs a new DynamicMatrix and allocates enough space to accomodate a matrix of the provided shape without reallocation
let mat: DynamicMatrix<isize> = DynamicMatrix::with_capacity((3, 3));
assert_eq!(mat.rows(), 0);
assert_eq!(mat.cols(), 3);
assert_eq!(mat.capacity(), 9);
Sourcepub fn rows(&self) -> usize
pub fn rows(&self) -> usize
Returns the number of rows in the DynamicMatrix
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
assert_eq!(mat.rows(), 3);
Sourcepub fn cols(&self) -> usize
pub fn cols(&self) -> usize
Returns the number of columns in the DynamicMatrix
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
assert_eq!(mat.cols(), 3);
Sourcepub fn shape(&self) -> (usize, usize)
pub fn shape(&self) -> (usize, usize)
Returns a tuple containing the number of rows as the first element and number of columns as the second element
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
assert_eq!(mat.shape(), (3, 3));
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the underlying Vec
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
assert_eq!(mat.len(), 9);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity of the underlying Vec
let mat: DynamicMatrix<isize> = DynamicMatrix::with_capacity((3, 3));
assert_eq!(mat.capacity(), 9);
Sourcepub fn push_row(&mut self, row: Vec<T>) -> Result<(), ShapeError>
pub fn push_row(&mut self, row: Vec<T>) -> Result<(), ShapeError>
Appends a new row to the DynamicMatrix
let mut mat: DynamicMatrix<isize> = DynamicMatrix::new_with_cols(3);
mat.push_row(vec![1, 2, 3]).unwrap();
mat.push_row(vec![4, 5, 6]).unwrap();
mat.push_row(vec![7, 8, 9]).unwrap();
assert_eq!(mat.as_slice(), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert_eq!(mat.rows(), 3);
Trying to append a new row with unequal number of columns will return a ShapeError
:
let mut mat: DynamicMatrix<isize> = DynamicMatrix::new_with_cols(3);
// Trying to push a vector with length 4 into a matrix with only 3 columns
mat.push_row(vec![1, 2, 3, 4]).unwrap();
Sourcepub fn push_col(&mut self, col: Vec<T>) -> Result<(), ShapeError>
pub fn push_col(&mut self, col: Vec<T>) -> Result<(), ShapeError>
Appends a new columns to the DynamicMatrix
let mut mat: DynamicMatrix<isize> = DynamicMatrix::new_with_cols(2);
mat.push_row(vec![1, 2]).unwrap();
mat.push_row(vec![4, 5]).unwrap();
mat.push_row(vec![7, 8]).unwrap();
mat.push_col(vec![3, 6, 9]).unwrap();
assert_eq!(mat.as_slice(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert_eq!(mat.cols(), 3);
Trying to append a new row with unequal number of columns will return a ShapeError
:
let mut mat: DynamicMatrix<isize> = DynamicMatrix::new_with_cols(2);
mat.push_row(vec![1, 2]).unwrap();
mat.push_row(vec![4, 5]).unwrap();
mat.push_row(vec![7, 8]).unwrap();
// Trying to push a column with less elements than the number of rows
mat.push_col(vec![3, 6]).unwrap();
Sourcepub fn as_ptr(&self) -> *const T
pub fn as_ptr(&self) -> *const T
Gives a raw pointer to the underlying Vec’s buffer
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
let mat_ptr = mat.as_ptr();
for i in 0..(mat.rows() * mat.cols()) {
assert_eq!(unsafe { *mat_ptr.add(i) }, i as isize + 1);
}
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Gives a raw mutable pointer to the underlying Vec’s buffer
let mut mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
let mat_ptr = mat.as_mut_ptr();
for i in 0..(mat.rows() * mat.cols()) {
unsafe {
*mat_ptr.add(i) = i as isize + 10;
}
}
assert_eq!(mat.as_slice(), &[10, 11, 12, 13, 14, 15, 16, 17, 18]);
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Extracts a slice containing the underlying Vec
let mut mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
assert_eq!(mat.as_slice(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Extracts a mut slice containing the underlying Vec
let mut mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
let mut mat_slice = mat.as_mut_slice();
mat_slice[0] = 10;
mat_slice[1] = 11;
mat_slice[2] = 12;
assert_eq!(mat.as_slice(), &[10, 11, 12, 4, 5, 6, 7, 8, 9]);
Sourcepub unsafe fn from_raw_parts(
vec_parts: (*mut T, usize, usize),
cols: usize,
) -> Self
pub unsafe fn from_raw_parts( vec_parts: (*mut T, usize, usize), cols: usize, ) -> Self
Creates a DynamicMatrix from it’s underlying raw components
Sourcepub fn into_boxed_slice(self) -> (Box<[T]>, usize)
pub fn into_boxed_slice(self) -> (Box<[T]>, usize)
Decomposes the DynamicMatrix into the boxed slice of it’s underlying Vec
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
let (slice, cols) = mat.into_boxed_slice();
assert_eq!(cols, 3);
assert_eq!(slice.as_ref(), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
Sourcepub fn from_boxed_slice(boxed_slice: Box<[T]>, cols: usize) -> Self
pub fn from_boxed_slice(boxed_slice: Box<[T]>, cols: usize) -> Self
Creates a DynamicMatrix from a Boxed slice
let boxed_slice = Box::new([1, 2, 3, 4, 5, 6, 7, 8, 9]);
let mat = DynamicMatrix::from_boxed_slice(boxed_slice, 3);
assert_eq!(mat.cols(), 3);
assert_eq!(mat.as_slice(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
Sourcepub fn get(&self, index: (usize, usize)) -> Result<&T, IndexingError>
pub fn get(&self, index: (usize, usize)) -> Result<&T, IndexingError>
Returns a Result
containing a shared reference to the value at the given index
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
for row in 0..mat.rows() {
for col in 0..mat.cols() {
assert_eq!(*mat.get((row, col)).unwrap(), 3 * row + col + 1);
}
}
Indexing outside bounds will return an IndexingError
.
let mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
mat.get((3, 3)).unwrap();
Sourcepub fn get_mut(
&mut self,
index: (usize, usize),
) -> Result<&mut T, IndexingError>
pub fn get_mut( &mut self, index: (usize, usize), ) -> Result<&mut T, IndexingError>
Returns a Result
containing an exclusive reference to the value at the given index
let mut mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
for row in 0..mat.rows() {
for col in 0..mat.cols() {
*mat.get_mut((row, col)).unwrap() += 9;
}
}
assert_eq!(mat.as_slice(), &[10, 11, 12, 13, 14, 15, 16, 17, 18]);
Indexing outside bounds will return an IndexingError
.
let mut mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
*mat.get_mut((3, 3)).unwrap() += 1;
Trait Implementations§
Source§impl<T: Clone> Clone for DynamicMatrix<T>
impl<T: Clone> Clone for DynamicMatrix<T>
Source§fn clone(&self) -> DynamicMatrix<T>
fn clone(&self) -> DynamicMatrix<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug> Debug for DynamicMatrix<T>
impl<T: Debug> Debug for DynamicMatrix<T>
Source§impl<T> Index<(usize, usize)> for DynamicMatrix<T>
impl<T> Index<(usize, usize)> for DynamicMatrix<T>
Source§impl<T> IndexMut<(usize, usize)> for DynamicMatrix<T>
impl<T> IndexMut<(usize, usize)> for DynamicMatrix<T>
Source§fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output
Returns an exclusive reference to the value at the given index
let mut mat = dynamic_matrix![1, 2, 3; 4, 5, 6; 7, 8, 9];
for row in 0..mat.rows() {
for col in 0..mat.cols() {
mat[(row, col)] += 9;
}
}
assert_eq!(mat.as_slice(), &[10, 11, 12, 13, 14, 15, 16, 17, 18]);