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_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§
sourcefn view_mut(
&mut self,
start: Coordinate,
end: Coordinate
) -> TooDeeViewMut<'_, T>
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);sourcefn rows_mut(&mut self) -> RowsMut<'_, T> ⓘ
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);sourcefn col_mut(&mut self, col: usize) -> ColMut<'_, T> ⓘ
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);sourceunsafe fn get_unchecked_row_mut(&mut self, row: usize) -> &mut [T]
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.
sourceunsafe fn get_unchecked_mut(&mut self, coord: Coordinate) -> &mut T
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§
sourcefn cells_mut(&mut self) -> CellsMut<'_, T>
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);sourcefn fill(&mut self, fill: T)where
T: Clone,
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));sourcefn swap_cols(&mut self, c1: usize, c2: usize)
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);sourcefn swap_rows(&mut self, r1: usize, r2: usize)
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);sourcefn row_pair_mut(&mut self, r1: usize, r2: usize) -> (&mut [T], &mut [T])
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);