[−][src]Struct psyche_utils::grid::Grid
Collection that holds data in 2d grid-like manner. Grid can be:
- accessed by inspection of each element;
- filled with same value for all fields;
- filled with values got from closure that produces value for each field individually;
- sampled with any type that implements
GridSampler
trai.
Methods
impl<T> Grid<T>
[src]
pub fn new(cols: usize, rows: usize, value: T) -> Self where
T: Clone,
[src]
T: Clone,
Creates new grid.
Arguments
cols
- Number of columns.rows
- Number of rows.value
- Initial value applied for each field.
Return
Instance of grid.
Example
use psyche_utils::grid::{Grid, GridSamplerCluster}; let grid = Grid::new(2, 2, 1.0); let sampler = GridSamplerCluster::new((0, 0), (1, 1)); assert_eq!(grid.sample(sampler).unwrap(), (4.0, 4));
pub fn cols(&self) -> usize
[src]
Gets number of columns.
Return
Number of columns.
Example
use psyche_utils::grid::Grid; let grid = Grid::new(2, 2, 1.0); assert_eq!(grid.cols(), 2);
pub fn rows(&self) -> usize
[src]
Gets number of rows.
Return
Number of rows.
Example
use psyche_utils::grid::Grid; let grid = Grid::new(2, 2, 1.0); assert_eq!(grid.rows(), 2);
pub fn fields(&self) -> &[T]
[src]
Gets slice of fields.
Return
Reference to slice of fields that holds.
Example
use psyche_utils::grid::Grid; let grid = Grid::new(2, 2, 1.0); assert_eq!(grid.fields(), &[1.0, 1.0, 1.0, 1.0]);
pub fn fields_mut(&mut self) -> &mut [T]
[src]
Gets slice of fields.
Return
Mutable reference to slice of fields that holds.
Example
use psyche_utils::grid::Grid; let mut grid = Grid::new(2, 2, 0.0); let mut i = 1.0; for field in grid.fields_mut() { *field = i; i += 1.0; } assert_eq!(grid.fields(), &[1.0, 2.0, 3.0, 4.0]);
pub fn fill_all(&mut self, value: T) where
T: Clone,
[src]
T: Clone,
Fiils grid with same value.
Arguments
value
- Value that will be applied to each field.
Example
use psyche_utils::grid::Grid; let mut grid = Grid::new(2, 2, 0.0); grid.fill_all(1.0); assert_eq!(grid.fields(), &[1.0, 1.0, 1.0, 1.0]);
pub fn fill(&mut self, col_row: (usize, usize), size: (usize, usize), value: T) where
T: Clone,
[src]
T: Clone,
Fiils grid with same value to fields contained by specified bounds.
Arguments
col_row
- Starting column and row.size
- Number of columns and rows of bounds.value
- Value that will be applied to each field.
Example
use psyche_utils::grid::Grid; let mut grid = Grid::new(2, 2, 0.0); grid.fill((1, 0), (1, 2), 1.0); assert_eq!(grid.fields(), &[0.0, 1.0, 0.0, 1.0]);
pub fn fill_with<F>(&mut self, with: F) where
F: FnMut(usize, usize) -> Option<T>,
[src]
F: FnMut(usize, usize) -> Option<T>,
Fiils grid with values got from producer closure.
Arguments
with
- Closure that will produce value for each field based on their col-row coords.
Example
use psyche_utils::grid::Grid; let mut grid = Grid::new(2, 2, 0.0); grid.fill_with(|col, row| Some((col + row) as f32)); assert_eq!(grid.fields(), &[0.0, 1.0, 1.0, 2.0]);
pub fn with<F>(&mut self, with: F) where
F: FnMut(usize, usize, &mut T),
[src]
F: FnMut(usize, usize, &mut T),
Inspect and/or edit fields with closure.
Arguments
with
- Closure that will inspect and allow to edit each field.
Example
use psyche_utils::grid::Grid; let mut grid = Grid::new(2, 2, 0.0); grid.with(|col, row, field| *field = (col + row) as f32); assert_eq!(grid.fields(), &[0.0, 1.0, 1.0, 2.0]);
pub fn sample<S, W>(&self, sampler: S) -> Option<(T, W)> where
S: GridSampler<T, W>,
[src]
S: GridSampler<T, W>,
Sample grid fields using given sampler.
Arguments
sampler
- Sampler object that implementsGridSampler
trait.
Example
use psyche_utils::grid::{Grid, GridSamplerCluster}; let grid = Grid::new(2, 2, 1.0); let sampler = GridSamplerCluster::new((0, 0), (1, 1)); assert_eq!(grid.sample(sampler).unwrap(), (4.0, 4));
Trait Implementations
impl<T: Clone> Clone for Grid<T>
[src]
fn clone(&self) -> Grid<T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<T: Default> Default for Grid<T>
[src]
impl<T> Index<(usize, usize)> for Grid<T>
[src]
impl<T> Index<[usize; 2]> for Grid<T>
[src]
impl<T> IndexMut<(usize, usize)> for Grid<T>
[src]
impl<T> IndexMut<[usize; 2]> for Grid<T>
[src]
impl<T> Serialize for Grid<T> where
T: Serialize,
[src]
T: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de, T> Deserialize<'de> for Grid<T> where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,