CircularMut

Trait CircularMut 

Source
pub trait CircularMut<'a, const N: usize, T> {
Show 16 methods // Required methods fn get_mut(&mut self, index: [usize; N]) -> &mut T; fn get_mut_raw(&mut self, index: [usize; N]) -> &mut T; fn push_front(&'a mut self, axis: usize, el: &'a [T]); fn push_front_init(&'a mut self, axis: usize, n: usize, init: T) where T: Clone; fn push_front_iter<'b, I>(&'a mut self, axis: usize, el: I) where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b; fn push_front_raw(&'a mut self, axis: usize, el: &'a [T]); fn push_front_raw_iter<'b, I>(&'a mut self, axis: usize, el: I) where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b; fn push_back(&'a mut self, axis: usize, el: &'a [T]); fn push_back_init(&'a mut self, axis: usize, n: usize, init: T); fn push_back_iter<'b, I>(&'a mut self, axis: usize, el: I) where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b; fn push_back_raw(&'a mut self, axis: usize, el: &'a [T]); fn push_back_raw_iter<'b, I>(&'a mut self, axis: usize, el: I) where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b; fn translate_front<'b, F>( &'a mut self, axis: usize, n: usize, origin: [usize; N], el_fn: F, ) where T: 'b, F: FnMut([Range<usize>; N]) -> &'b [T]; fn translate_back<'b, F>( &'a mut self, axis: usize, n: usize, origin: [usize; N], el_fn: F, ) where T: 'b, F: FnMut([Range<usize>; N]) -> &'b [T]; // Provided methods fn translate_front_init(&'a mut self, axis: usize, n: usize, init: T) where T: Clone { ... } fn translate_back_init(&'a mut self, axis: usize, n: usize, init: T) where T: Clone { ... }
}
Expand description

Mutating CircularArray operations.

Required Methods§

Source

fn get_mut(&mut self, index: [usize; N]) -> &mut T

Get a mutable reference to the element at the given index, aligned to the offset.

§Example
let mut array = CircularArray::new_offset([3, 3], [1, 0], vec![
    2, 0, 1,
    5, 3, 4,
    8, 6, 7,
]);
assert_eq!(array.get_mut([0, 0]), &mut 0);
Source

fn get_mut_raw(&mut self, index: [usize; N]) -> &mut T

Get a mutable reference to the element at the given index. This does not account for the offset. See CircularArray::offset.

§Example
let mut array = CircularArray::new_offset([3, 3], [1, 0], vec![
    2, 0, 1,
    5, 3, 4,
    8, 6, 7,
]);
assert_eq!(array.get_mut_raw([0, 0]), &mut 2);
Source

fn push_front(&'a mut self, axis: usize, el: &'a [T])

Push elements to the front of the given axis, aligned to the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new([3, 3], vec![
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
]);

array.push_front(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     3,  4,  5,
     6,  7,  8,
     9, 10, 11
]);
Source

fn push_front_init(&'a mut self, axis: usize, n: usize, init: T)
where T: Clone,

Push n slices of elements to the front of the given axis, aligned to the offset. Elements are initialized with the provided init value.

§Example
let mut array = CircularArray::new([3, 3], vec![
    false, false, false,
    false, false, false,
    false, false, false,
]);

array.push_front_init(1, 1, true);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
    false, false, false,
    false, false, false,
     true,  true,  true,
]);
Source

fn push_front_iter<'b, I>(&'a mut self, axis: usize, el: I)
where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b,

Push elements to the front of the given axis, aligned to the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new([3, 3], vec![
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
]);

array.push_front_iter(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     3,  4,  5,
     6,  7,  8,
     9, 10, 11
]);
Source

fn push_front_raw(&'a mut self, axis: usize, el: &'a [T])

Push elements to the front of the given axis, ignoring the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new_offset([3, 3], [1, 0], vec![
    2, 0, 1,
    5, 3, 4,
    8, 6, 7,
]);

array.push_front_raw(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     3,  4,  5,
     6,  7,  8,
    10, 11,  9,
]);
assert_eq!(array.iter_raw().cloned().collect::<Vec<_>>(), &[
     9, 10, 11,
     5,  3,  4,
     8,  6,  7,
]);
Source

fn push_front_raw_iter<'b, I>(&'a mut self, axis: usize, el: I)
where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b,

Push elements to the front of the given axis, ignoring the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new_offset([3, 3], [1, 0], vec![
    2, 0, 1,
    5, 3, 4,
    8, 6, 7,
]);

array.push_front_raw_iter(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     3,  4,  5,
     6,  7,  8,
    10, 11,  9,
]);
assert_eq!(array.iter_raw().cloned().collect::<Vec<_>>(), &[
     9, 10, 11,
     5,  3,  4,
     8,  6,  7,
]);
Source

fn push_back(&'a mut self, axis: usize, el: &'a [T])

Push elements to the back of the given axis, aligned to the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new([3, 3], vec![
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
]);

array.push_back(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     9, 10, 11,
     0,  1,  2,
     3,  4,  5,
]);
Source

fn push_back_init(&'a mut self, axis: usize, n: usize, init: T)

Push n slices of elements to the back of the given axis, aligned to the offset. Elements are initialized with the provided init value.

§Example
let mut array = CircularArray::new([3, 3], vec![
    false, false, false,
    false, false, false,
    false, false, false,
]);

array.push_back_init(1, 1, true);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     true,  true,  true,
    false, false, false,
    false, false, false,
]);
Source

fn push_back_iter<'b, I>(&'a mut self, axis: usize, el: I)
where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b,

Push elements to the back of the given axis, aligned to the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new([3, 3], vec![
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
]);

array.push_back_iter(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
     9, 10, 11,
     0,  1,  2,
     3,  4,  5,
]);
Source

fn push_back_raw(&'a mut self, axis: usize, el: &'a [T])

Push elements to the back of the given axis, ignoring the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

§Example
let mut array = CircularArray::new_offset([3, 3], [1, 0], vec![
    2, 0, 1,
    5, 3, 4,
    8, 6, 7,
]);

array.push_back_raw(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
    10, 11,  9,
     0,  1,  2,
     3,  4,  5,
]);
assert_eq!(array.iter_raw().cloned().collect::<Vec<_>>(), &[
     2,  0,  1,
     5,  3,  4,
     9, 10, 11,
]);
Source

fn push_back_raw_iter<'b, I>(&'a mut self, axis: usize, el: I)
where I: IntoIterator<IntoIter: ExactSizeIterator, Item = &'b T>, T: 'b,

Push elements to the back of the given axis, ignoring the offset. Elements must be an exact multiple of the slice size for the given axis. See CircularArray::slice_len.

_raw suffixed methods do not update the offset and will always start insertion at the axis bound.

§Example
let mut array = CircularArray::new_offset([3, 3], [1, 0], vec![
    2, 0, 1,
    5, 3, 4,
    8, 6, 7,
]);

array.push_back_raw_iter(1, &[9, 10, 11]);
assert_eq!(array.iter().cloned().collect::<Vec<_>>(), &[
    10, 11,  9,
     0,  1,  2,
     3,  4,  5,
]);
assert_eq!(array.iter_raw().cloned().collect::<Vec<_>>(), &[
     2,  0,  1,
     5,  3,  4,
     9, 10, 11,
]);
Source

fn translate_front<'b, F>( &'a mut self, axis: usize, n: usize, origin: [usize; N], el_fn: F, )
where T: 'b, F: FnMut([Range<usize>; N]) -> &'b [T],

Translate the array by n on the given axis, inserting elements to the front of the array.

Requires specifying the array origin of the CircularArray relative to translation. N dimensional index ranges ([Range<usize>; N]) will be passed to the el_fn for slicing a source buffer to retrieve the new elements. Note that the caller should ensure that a translation of n is within the source array bounds prior to calling this function.

In the following example, we pre-calculate the Strides of the source array to flatten the N dimensional index into a contiguous range (requires feature flag strides). Alternatively, the index range can be passed to 3rd party crates for slicing operations.

// A [5, 5] source array.
let src = [
     0,  1,  2,  3,  4,
     5,  6,  7,  8,  9,
    10, 11, 12, 13, 14,
    15, 16, 17, 18, 19,
    20, 21, 22, 23, 24,
];
// Strides used for flattening `N` dimensional indices.
let src_strides = Strides::new(&[5, 5]);

// Slice function.
let el_fn = |mut index: [Range<usize>; 2]| {
    &src[src_strides.flatten_range(index)]
};

// A [3, 3] circular array positioned at `[0, 0]`.
let mut origin = [0, 0];
let mut dst = CircularArray::new([3, 3], vec![
     0,  1,  2,
     5,  6,  7,
    10, 11, 12
]);

// Translate by 2 on axis 0 (Pushes 2 columns to front of axis 0).
let (axis, n) = (0, 2);
dst.translate_front(axis, n, origin, el_fn);
origin[axis] += n as usize;

assert_eq!(dst.iter().cloned().collect::<Vec<usize>>(), &[
     2,  3,  4,
     7,  8,  9,
    12, 13, 14,
]);

// Translate by 1 on axis 1 (Pushes 1 row to front of axis 1).
let (axis, n) = (1, 1);
dst.translate_front(axis, n, origin, el_fn);
origin[axis] += n as usize;

assert_eq!(dst.iter().cloned().collect::<Vec<usize>>(), &[
     7,  8,  9,
    12, 13, 14,
    17, 18, 19,
]);
Source

fn translate_back<'b, F>( &'a mut self, axis: usize, n: usize, origin: [usize; N], el_fn: F, )
where T: 'b, F: FnMut([Range<usize>; N]) -> &'b [T],

Translate the array by -n on the given axis, inserting elements to the back of the array.

Requires specifying the array origin of the CircularArray relative to translation. N dimensional index range ([Range<usize>; N]) will be passed to the el_fn for slicing a source buffer to retrieve the new elements. Note that the caller should ensure that a translation of n is within the source array bounds prior to calling this function.

In the following example, we pre-calculate the Strides of the source array to flatten the N dimensional index into a contiguous range (requires feature flag strides). Alternatively, the index range can be passed to 3rd party crates for slicing operations.

// A [5, 5] source array.
let src = [
     0,  1,  2,  3,  4,
     5,  6,  7,  8,  9,
    10, 11, 12, 13, 14,
    15, 16, 17, 18, 19,
    20, 21, 22, 23, 24,
];
// Strides used for flattening `N` dimensional indices.
let src_strides = Strides::new(&[5, 5]);

// Slice function.
let el_fn = |mut index: [Range<usize>; 2]| {
    &src[src_strides.flatten_range(index)]
};

// A [3, 3] circular array positioned at `[2, 2]`.
let mut origin = [2, 2];
let mut dst = CircularArray::new([3, 3], vec![
    12, 13, 14,
    17, 18, 19,
    22, 23, 24,
]);

// Translate by -2 on axis 0 (Pushes 2 columns to back of axis 0).
let (axis, n) = (0, 2);
dst.translate_back(axis, n, origin, el_fn);
origin[axis] -= n;

assert_eq!(dst.iter().cloned().collect::<Vec<usize>>(), &[
    10, 11, 12,
    15, 16, 17,
    20, 21, 22,
]);

// Translate by -1 on axis 1 (Pushes 1 row to back of axis 1).
let (axis, n) = (1, 1);
dst.translate_back(axis, n, origin, el_fn);
origin[axis] -= n;

assert_eq!(dst.iter().cloned().collect::<Vec<usize>>(), &[
     5,  6,  7,
    10, 11, 12,
    15, 16, 17,
]);

Provided Methods§

Source

fn translate_front_init(&'a mut self, axis: usize, n: usize, init: T)
where T: Clone,

Translate the array by n on the given axis, inserting elements to the front of the array. See CircularMut::push_front_init.

Source

fn translate_back_init(&'a mut self, axis: usize, n: usize, init: T)
where T: Clone,

Translate the array by n on the given axis, inserting elements to the back of the array. See CircularMut::push_back_init.

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.

Implementors§

Source§

impl<'a, const N: usize, A: AsRef<[T]> + AsMut<[T]>, T: Clone + 'a> CircularMut<'a, N, T> for CircularArray<N, A, T>