Trait toodee::TooDeeOpsMut

source ·
pub trait TooDeeOpsMut<T>: TooDeeOps<T> + IndexMut<usize, Output = [T]> + IndexMut<Coordinate, Output = T> {
    // Required methods
    fn view_mut(
        &mut self,
        start: Coordinate,
        end: Coordinate
    ) -> TooDeeViewMut<'_, T>;
    fn rows_mut(&mut self) -> RowsMut<'_, T> ;
    fn col_mut(&mut self, col: usize) -> ColMut<'_, T> ;
    unsafe fn get_unchecked_row_mut(&mut self, row: usize) -> &mut [T];
    unsafe fn get_unchecked_mut(&mut self, coord: Coordinate) -> &mut T;

    // Provided methods
    fn cells_mut(&mut self) -> CellsMut<'_, T> { ... }
    fn fill(&mut self, fill: T)
       where T: Clone { ... }
    fn swap_cols(&mut self, c1: usize, c2: usize) { ... }
    fn swap(&mut self, cell1: Coordinate, cell2: Coordinate) { ... }
    fn swap_rows(&mut self, r1: usize, r2: usize) { ... }
    fn row_pair_mut(&mut self, r1: usize, r2: usize) -> (&mut [T], &mut [T]) { ... }
}
Expand description

Defines operations common to both TooDee and TooDeeViewMut. Default implementations are provided where possible/practical.

Required Methods§

source

fn view_mut( &mut self, start: Coordinate, end: Coordinate ) -> TooDeeViewMut<'_, T>

Returns a mutable view (or subset) of the current area based on the coordinates provided.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let view = toodee.view_mut((1, 1), (9, 4));
assert_eq!(view.num_cols(), 8);
assert_eq!(view.num_rows(), 3);
source

fn rows_mut(&mut self) -> RowsMut<'_, T>

Returns a mutable iterator of slices, where each slice represents an entire row.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
for (i, r) in toodee.rows_mut().enumerate() {
   r.iter_mut().for_each(|c| *c -= i as u32);
}
assert_eq!(toodee.cells().sum::<u32>(), 42*50 - 10 - 20 - 30 - 40);
source

fn col_mut(&mut self, col: usize) -> ColMut<'_, T>

Returns a mutable iterator over a single column. Note that the ColMut iterator is indexable.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
for c in toodee.col_mut(4) {
    *c /= 2;
}
assert_eq!(toodee.cells().sum::<u32>(), 42*45 + 21*5);
source

unsafe fn get_unchecked_row_mut(&mut self, row: usize) -> &mut [T]

Returns a mutable row without checking that the row is valid. Generally it’s best to use indexing instead, e.g., toodee[row]

Safety

This is generally not recommended, use with caution! Calling this method with an invalid row is [undefined behavior] even if the resulting reference is not used.

source

unsafe fn get_unchecked_mut(&mut self, coord: Coordinate) -> &mut T

Returns a mutable cell without checking that the cell coordinate is valid. Generally it’s best to use indexing instead, e.g., toodee[(col, row)]

Safety

This is generally not recommended, use with caution! Calling this method with an invalid coordinate is [undefined behavior] even if the resulting reference is not used.

Provided Methods§

source

fn cells_mut(&mut self) -> CellsMut<'_, T>

Returns an iterator that traverses all cells within the area.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
for c in toodee.cells_mut() {
    *c -= 1;
}
assert_eq!(toodee.cells().sum::<u32>(), 41*50);
source

fn fill(&mut self, fill: T)where T: Clone,

Fills the entire area with the specified value.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
let mut view = toodee.view_mut((1, 1), (9, 4));
view.fill(0);
assert_eq!(toodee.cells().sum::<u32>(), 42*(50 - 8*3));
source

fn swap_cols(&mut self, c1: usize, c2: usize)

Swap/exchange the data between two columns.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
for c in toodee.col_mut(2) {
    *c = 1;
}
assert_eq!(toodee[(4, 0)], 42);
toodee.swap_cols(2, 4);
assert_eq!(toodee[(4, 0)], 1);
source

fn swap(&mut self, cell1: Coordinate, cell2: Coordinate)

Swap/exchange two cells in the array.

Panics

Panics if either cell coordinate is out of bounds.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee = TooDee::from_vec(3, 3, (0u32..9).collect());
toodee.swap((0,0),(2, 2));
assert_eq!(toodee.data(), &[8, 1, 2, 3, 4, 5, 6, 7, 0]);
source

fn swap_rows(&mut self, r1: usize, r2: usize)

Swap/exchange the data between two rows. Note that this method is overridden in both TooDee and TooDeeOpsMut. This implementation remains in place for other types that may wish to implement the trait.

Panics

Panics if either row index is out of bounds.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
toodee[0].iter_mut().for_each(|v| *v = 1);
assert_eq!(toodee[(0, 2)], 42);
toodee.view_mut((0, 0), (10, 5)).swap_rows(0, 2);
assert_eq!(toodee[(0, 2)], 1);
source

fn row_pair_mut(&mut self, r1: usize, r2: usize) -> (&mut [T], &mut [T])

Return the specified rows as mutable slices.

Panics

Will panic if r1 and r2 are equal, or if either row index is out of bounds.

Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
let (r1, r2) = toodee.row_pair_mut(0, 4);
// do something with the row pair
r1.swap_with_slice(r2);

Implementors§

source§

impl<'a, T> TooDeeOpsMut<T> for TooDeeViewMut<'a, T>

source§

impl<T> TooDeeOpsMut<T> for TooDee<T>