Struct dynamic_matrix::DynamicMatrix[][src]

pub struct DynamicMatrix<T> { /* fields omitted */ }
Expand description

A dynamic matrix in stored in row-major order.

Adding a new row is cheap while adding a new column is expensive.

Implementations

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

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

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

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

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

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

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

Returns the capacity of the underlying Vec

let mat: DynamicMatrix<isize> = DynamicMatrix::with_capacity((3, 3));

assert_eq!(mat.capacity(), 9);

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

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

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

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

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

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

Creates a DynamicMatrix from it’s underlying raw components

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

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

The returned type after indexing.

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.