orx_v

Trait NVecMut

Source
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

A D dimensional mutable vector.

V1Mut, V2Mut, etc. are type aliases for NVecMut<D1, T>, NVecMut<D2, T>, and so on.

Required Methods§

Source

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

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

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

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

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

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§

Source

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

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

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.

Implementations on Foreign Types§

Source§

impl<'a, C, T> NVecMut<D2, T> for &'a mut [C]
where C: NVecMut<<D2 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D2 as Dim>::ChildIdx, ) -> impl NVecMut<<D2 as Dim>::PrevDim, 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,

Source§

impl<'a, C, T> NVecMut<D3, T> for &'a mut [C]
where C: NVecMut<<D3 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D3 as Dim>::ChildIdx, ) -> impl NVecMut<<D3 as Dim>::PrevDim, 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,

Source§

impl<'a, C, T> NVecMut<D4, T> for &'a mut [C]
where C: NVecMut<<D4 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D4 as Dim>::ChildIdx, ) -> impl NVecMut<<D4 as Dim>::PrevDim, 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,

Source§

impl<'a, T> NVecMut<D1, T> for &'a mut [T]
where T: Copy,

Source§

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

Source§

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

Source§

fn child_mut(&mut self, _: IdxNever) -> impl NVecMut<<D1 as Dim>::PrevDim, 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,

Source§

impl<T> NVecMut<D1, T> for Vec<T>
where T: Copy,

Source§

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

Source§

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

Source§

fn child_mut(&mut self, _: IdxNever) -> impl NVecMut<<D1 as Dim>::PrevDim, 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,

Source§

impl<T, C> NVecMut<D2, T> for Vec<C>
where C: NVecMut<<D2 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D2 as Dim>::ChildIdx, ) -> impl NVecMut<<D2 as Dim>::PrevDim, 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,

Source§

impl<T, C> NVecMut<D3, T> for Vec<C>
where C: NVecMut<<D3 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D3 as Dim>::ChildIdx, ) -> impl NVecMut<<D3 as Dim>::PrevDim, 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,

Source§

impl<T, C> NVecMut<D4, T> for Vec<C>
where C: NVecMut<<D4 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D4 as Dim>::ChildIdx, ) -> impl NVecMut<<D4 as Dim>::PrevDim, 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,

Source§

impl<T, D: Dim, V: NVecMut<D, T>> NVecMut<D, T> for &mut V

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D as Dim>::ChildIdx, ) -> impl NVecMut<<D as Dim>::PrevDim, 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,

Source§

impl<const N: usize, C, T> NVecMut<D2, T> for [C; N]
where C: NVecMut<<D2 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D2 as Dim>::ChildIdx, ) -> impl NVecMut<<D2 as Dim>::PrevDim, 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,

Source§

impl<const N: usize, C, T> NVecMut<D3, T> for [C; N]
where C: NVecMut<<D3 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D3 as Dim>::ChildIdx, ) -> impl NVecMut<<D3 as Dim>::PrevDim, 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,

Source§

impl<const N: usize, C, T> NVecMut<D4, T> for [C; N]
where C: NVecMut<<D4 as Dim>::PrevDim, T>,

Source§

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

Source§

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

Source§

fn child_mut( &mut self, i: <D4 as Dim>::ChildIdx, ) -> impl NVecMut<<D4 as Dim>::PrevDim, 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,

Source§

impl<const N: usize, T> NVecMut<D1, T> for [T; N]
where T: Copy,

Source§

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

Source§

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

Source§

fn child_mut(&mut self, _: IdxNever) -> impl NVecMut<<D1 as Dim>::PrevDim, 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<D: Dim, T> NVecMut<D, T> for EmptyVec<D, T>

Source§

impl<T, L, C> NVecMut<D1, T> for SparseVec<D1, T, C, L>
where T: Copy + Default, L: Lookup<<D1 as Dim>::Idx, T>, C: Card<D1>,

Source§

impl<T, L, C> NVecMut<D2, T> for SparseVec<D2, T, C, L>
where T: Copy, L: Lookup<<D2 as Dim>::Idx, T>, C: Card<D2>,

Source§

impl<T, L, C> NVecMut<D3, T> for SparseVec<D3, T, C, L>
where T: Copy, L: Lookup<<D3 as Dim>::Idx, T>, C: Card<D3>,

Source§

impl<T, L, C> NVecMut<D4, T> for SparseVec<D4, T, C, L>
where T: Copy, L: Lookup<<D4 as Dim>::Idx, T>, C: Card<D4>,

Source§

impl<V, I, T> NVecMut<D2, T> for FlatJagged<V, I, T>
where V: NVec<D1, T> + NVecMut<D1, T>, I: NVec<D1, usize>,