orx_v::matrices

Trait MatrixMut

Source
pub trait MatrixMut<T>: Matrix<T> {
    // Required methods
    fn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T;
    fn mut_all<F>(&mut self, f: F)
       where F: FnMut(&mut T);
    fn reset_all(&mut self, value: T)
       where T: PartialEq + Copy;

    // Provided methods
    fn set<Idx: IntoIdx<D2>>(&mut self, idx: Idx, value: T) { ... }
    fn try_at_mut(&mut self, idx: impl IntoIdx<D2>) -> Option<&mut T> { ... }
}
Expand description

A mutable matrix view over a D2 vector with rectangular cardinality, or over a flattened representation by a D1 vector.

A mutable matrix view can be created by:

Alternatively an owned mutable matrix can be crated by

Required Methods§

Source

fn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T

Returns a mutable reference to the element at the given idx of the matrix.

§Panics

Panics if the idx is not in_bounds.

Source

fn mut_all<F>(&mut self, f: F)
where F: FnMut(&mut T),

Applies the mutating function f over all elements of the matrix.

Source

fn reset_all(&mut self, value: T)
where T: PartialEq + Copy,

Sets all elements of the matrix to the given value. This method is often used at initialization stage of algorithms.

§Examples
use orx_v::*;

let mut v2 =[
    [1, 2, 3],
    [4, 5, 6],
];
let mut mat = v2.as_matrix_mut();
mat.reset_all(42);
assert_eq!(
    mat.equality(&[[42, 42, 42], [42, 42, 42]].as_matrix()),
    Equality::<D2>::Equal,
);

Or more practically, consider the Dijkstra’s shortest path algorithm as implemented in the Algorithms repository.

In addition to a priority queue, the implementation uses a distances vector throughout the search. One way to avoid re-allocation of such internal data, we often cache and reuse them. In such a case, we need to set all elements of this vector to infinity on initialization, where reset_all method is useful.

use orx_v::*;

impl WeightedAdjacencyList {
    fn dijkstra(&mut self, start: usize, end: usize) -> Option<(f64, Vec<usize>)> {
        // initialization
        self.distances.reset_all(f64::INFINITY);
        ...

        // search
        while let Some((node, cur_dist)) = pq.pop() {
            ...
        }
        ...
    }
}

Provided Methods§

Source

fn set<Idx: IntoIdx<D2>>(&mut self, idx: Idx, value: T)

Sets the element at the given idx of the matrix to the value.

§Panics

Panics if the idx is not in_bounds.

Source

fn try_at_mut(&mut self, idx: impl IntoIdx<D2>) -> Option<&mut T>

Returns a mutable reference to the element at the idx-th position of the matrix if the index is in_bounds; returns None otherwise.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, M: MatrixMut<T>> MatrixMut<T> for &mut M

Source§

fn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T

Source§

fn mut_all<F>(&mut self, f: F)
where F: FnMut(&mut T),

Source§

fn reset_all(&mut self, value: T)
where T: PartialEq + Copy,

Implementors§

Source§

impl<T, V> MatrixMut<T> for V2MatrixColMajor<T, V>
where V: NVecMut<D2, T>,

Source§

impl<T, V> MatrixMut<T> for V2MatrixRowMajor<T, V>
where V: NVecMut<D2, T>,

Source§

impl<T, V, L> MatrixMut<T> for V1Matrix<T, V, L>
where V: NVecMut<D1, T>, L: V1MatrixLayout,