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:
- calling
as_matrix_mut
oras_matrix_col_major_mut
methods on aD2
vector implementingNVecMut<D2, _>
, or equivalently,V2Mut<_>
; or by: - calling
as_matrix_mut
oras_matrix_col_major_mut
methods on aD1
vector implementingNVecMut<D1, _>
, or equivalently,V1Mut<_>
.
Alternatively an owned mutable matrix can be crated by
- calling
into_matrix
method on aD2
mutable vector; or by: - calling
v1_into_matrix
method on aD1
mutable vector.
Required Methods§
Sourcefn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T
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
.
Sourcefn mut_all<F>(&mut self, f: F)
fn mut_all<F>(&mut self, f: F)
Applies the mutating function f
over all elements of the matrix.
Sourcefn reset_all(&mut self, value: T)
fn reset_all(&mut self, value: T)
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§
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.