pub trait TooDeeOps<T>: Index<usize, Output = [T]> + Index<Coordinate, Output = T> {
// Required methods
fn num_cols(&self) -> usize;
fn num_rows(&self) -> usize;
fn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>;
fn rows(&self) -> Rows<'_, T> ⓘ;
fn col(&self, col: usize) -> Col<'_, T> ⓘ;
unsafe fn get_unchecked_row(&self, row: usize) -> &[T];
unsafe fn get_unchecked(&self, coord: Coordinate) -> &T;
// Provided methods
fn size(&self) -> (usize, usize) { ... }
fn is_empty(&self) -> bool { ... }
fn cells(&self) -> Cells<'_, T> { ... }
}
Expand description
Defines operations common to both TooDee
and TooDeeView
. Default implementations are provided
where possible/practical.
Required Methods§
sourcefn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>
fn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>
Returns a view (or subset) of the current area based on the coordinates provided.
Examples
use toodee::{TooDee,TooDeeOps};
let toodee : TooDee<u32> = TooDee::new(10, 5);
let view = toodee.view((1, 1), (9, 4));
assert_eq!(view.num_cols(), 8);
assert_eq!(view.num_rows(), 3);
sourcefn rows(&self) -> Rows<'_, T> ⓘ
fn rows(&self) -> Rows<'_, T> ⓘ
Returns an iterator of slices, where each slice represents an entire row.
Examples
use toodee::{TooDee,TooDeeOps};
let toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
let mut sum = 0u32;
for r in toodee.rows() {
sum += r.iter().sum::<u32>();
}
assert_eq!(sum, 42*50);
sourcefn col(&self, col: usize) -> Col<'_, T> ⓘ
fn col(&self, col: usize) -> Col<'_, T> ⓘ
Returns an iterator over a single column. Note that the Col
iterator is indexable.
Examples
use toodee::{TooDee,TooDeeOps};
let toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
let mut sum = 0u32;
for c in toodee.col(1) {
sum += c;
}
assert_eq!(sum, 42*5);
sourceunsafe fn get_unchecked_row(&self, row: usize) -> &[T]
unsafe fn get_unchecked_row(&self, row: usize) -> &[T]
Returns a 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(&self, coord: Coordinate) -> &T
unsafe fn get_unchecked(&self, coord: Coordinate) -> &T
Returns a 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.