[][src]Struct psyche_utils::grid::Grid

pub struct Grid<T> { /* fields omitted */ }

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]

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]

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]

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]

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]

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]

Sample grid fields using given sampler.

Arguments

  • sampler - Sampler object that implements GridSampler 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_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]

type Output = T

The returned type after indexing.

impl<T> Index<[usize; 2]> for Grid<T>[src]

type Output = T

The returned type after indexing.

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]

impl<'de, T> Deserialize<'de> for Grid<T> where
    T: Deserialize<'de>, 
[src]

Auto Trait Implementations

impl<T> Send for Grid<T> where
    T: Send

impl<T> Sync for Grid<T> where
    T: Sync

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]