pub trait NVecMut<D: Dim, T>: NVec<D, T> {
// Required methods
fn at_mut<Idx: IntoIdx<D>>(&mut self, idx: Idx) -> &mut T;
fn child_mut(&mut self, i: D::ChildIdx) -> impl NVecMut<D::PrevDim, 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<D>>(&mut self, idx: Idx, value: T) { ... }
fn try_at_mut(&mut self, idx: impl IntoIdx<D>) -> Option<&mut T> { ... }
}
Expand description
Required Methods§
Sourcefn at_mut<Idx: IntoIdx<D>>(&mut self, idx: Idx) -> &mut T
fn at_mut<Idx: IntoIdx<D>>(&mut self, idx: Idx) -> &mut T
Returns a mutable reference to the element at the idx
-th
position of the vector.
Note that the dimensions of the vector and the index are equal; and hence, the result is the scalar.
§Panics
Panics if the idx
is not in_bounds
.
§Examples
use orx_v::*;
let mut vec = vec![
vec![0, 1, 2],
vec![3],
vec![4, 5],
];
*vec.at_mut([0, 1]) = 42;
*vec.at_mut([2, 0]) = 7;
assert_eq!(
vec.equality(&[vec![0, 42, 2], vec![3], vec![7, 5]]),
Equality::Equal
);
Sourcefn child_mut(&mut self, i: D::ChildIdx) -> impl NVecMut<D::PrevDim, T>
fn child_mut(&mut self, i: D::ChildIdx) -> impl NVecMut<D::PrevDim, T>
Returns a mutable reference to the i
-th child of the vector.
Note that child has a dimension that is one less than the dimension of this vector.
§Panics
Panics if i
is out of bounds; i.e., i >= vec.num_children()
.
§Examples
use orx_v::*;
// D2
let mut vec = vec![
vec![0, 1, 2],
vec![3],
vec![4, 5],
];
{
// child is a D1 vec
let mut child = vec.child_mut(2);
*child.at_mut([0]) = 42;
child.set([1], 7);
}
assert_eq!(
vec.equality(&[vec![0, 1, 2], vec![3], vec![42, 7]]),
Equality::Equal
);
Sourcefn mut_all<F>(&mut self, f: F)
fn mut_all<F>(&mut self, f: F)
Applies the mutating function f
over all scalar elements of the vector.
§Example
use orx_v::*;
let mut v1 = [3, 6, 12];
v1.mut_all(|x| *x *= 10);
assert_eq!(v1.equality(&[30, 60, 120]), Equality::Equal);
let mut v2 = vec![
vec![1, 2],
vec![3, 4],
vec![5, 6],
];
v2.mut_all(|x| *x *= 10);
assert_eq!(
v2.equality(&[[10, 20], [30, 40], [50, 60]]),
Equality::Equal,
);
Sourcefn reset_all(&mut self, value: T)
fn reset_all(&mut self, value: T)
Sets all elements of the vector to the given value
.
This method is often used at initialization stage of algorithms.
§Examples
use orx_v::*;
let mut v1 = vec![1, 2, 3, 4, 5];
v1.reset_all(0);
assert_eq!(
v1.equality(&[0, 0, 0, 0, 0]),
Equality::Equal,
);
let mut v2 =[
[1, 2, 3],
[4, 5, 6],
];
v2.reset_all(42);
assert_eq!(
v2.equality(&[[42, 42, 42], [42, 42, 42]]),
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§
Sourcefn set<Idx: IntoIdx<D>>(&mut self, idx: Idx, value: T)
fn set<Idx: IntoIdx<D>>(&mut self, idx: Idx, value: T)
Sets value
of the element at the idx
-th position of the vector.
Note that the dimensions of the vector and the index are equal; and hence, the method sets value of the scalar.
§Panics
Panics if the idx
is not in_bounds
.
§Examples
use orx_v::*;
let mut vec = vec![
vec![0, 1, 2],
vec![3],
vec![4, 5],
];
vec.set([0, 1], 42);
vec.set([2, 0], 7);
assert_eq!(
vec.equality(&[vec![0, 42, 2], vec![3], vec![7, 5]]),
Equality::Equal
);
Sourcefn try_at_mut(&mut self, idx: impl IntoIdx<D>) -> Option<&mut T>
fn try_at_mut(&mut self, idx: impl IntoIdx<D>) -> Option<&mut T>
Returns a mutable reference to the element at the idx
-th
position of the vector if the index is in_bounds
;
returns None otherwise.
§Examples
use orx_v::*;
let mut vec = vec![
vec![0, 1, 2],
vec![3],
vec![4, 5],
];
*vec.try_at_mut([0, 1]).unwrap() = 42;
assert_eq!(vec.at([0, 1]), 42);
// vec.at_mut([1, 1]); // panics!
assert_eq!(vec.try_at_mut([1, 1]), None);
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.