Struct Grid

Source
pub struct Grid<T> { /* private fields */ }
Expand description

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.

Implementations§

Source§

impl<T> Grid<T>

Source

pub fn new(cols: usize, rows: usize, value: T) -> Self
where 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));
Source

pub fn cols(&self) -> usize

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);
Source

pub fn rows(&self) -> usize

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);
Source

pub fn fields(&self) -> &[T]

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]);
Source

pub fn fields_mut(&mut self) -> &mut [T]

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]);
Source

pub fn fill_all(&mut self, value: T)
where 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]);
Source

pub fn fill(&mut self, col_row: (usize, usize), size: (usize, usize), value: T)
where 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]);
Source

pub fn fill_with<F>(&mut self, with: F)
where 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]);
Source

pub fn with<F>(&mut self, with: F)
where 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]);
Source

pub fn sample<S, W>(&self, sampler: S) -> Option<(T, W)>
where S: GridSampler<T, W>,

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§

Source§

impl<T: Clone> Clone for Grid<T>

Source§

fn clone(&self) -> Grid<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Default> Default for Grid<T>

Source§

fn default() -> Grid<T>

Returns the “default value” for a type. Read more
Source§

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

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

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

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: [usize; 2]) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<(usize, usize)> for Grid<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize)) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<[usize; 2]> for Grid<T>

Source§

fn index_mut(&mut self, index: [usize; 2]) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<(usize, usize)> for Grid<T>

Source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> Serialize for Grid<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Grid<T>

§

impl<T> RefUnwindSafe for Grid<T>
where T: RefUnwindSafe,

§

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

§

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

§

impl<T> Unpin for Grid<T>
where T: Unpin,

§

impl<T> UnwindSafe for Grid<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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