Struct DynamicMatrix

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

Source

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]);
Source

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

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

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

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

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

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

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

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();
Source

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();
Source

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);
}
Source

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]);
Source

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]);
Source

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]);
Source

pub unsafe fn from_raw_parts( vec_parts: (*mut T, usize, usize), cols: usize, ) -> Self

Creates a DynamicMatrix from it’s underlying raw components

Source

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]);
Source

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]);
Source

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();
Source

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>

Source§

fn clone(&self) -> DynamicMatrix<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for DynamicMatrix<T>

Source§

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

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

impl<T> Index<(usize, usize)> for DynamicMatrix<T>

Source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Returns 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[(row, col)], 3 * row + col + 1);
    }
}
Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<(usize, usize)> for DynamicMatrix<T>

Source§

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]);

Auto Trait Implementations§

§

impl<T> Freeze for DynamicMatrix<T>

§

impl<T> RefUnwindSafe for DynamicMatrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for DynamicMatrix<T>
where T: Send,

§

impl<T> Sync for DynamicMatrix<T>
where T: Sync,

§

impl<T> Unpin for DynamicMatrix<T>
where T: Unpin,

§

impl<T> UnwindSafe for DynamicMatrix<T>
where T: UnwindSafe,

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

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.