Struct Grid2D

Source
pub struct Grid2D<T, S: Storage, const SIZE: usize> { /* private fields */ }
Expand description

A 2D grid, backed by an Array.

Internally the elements are stored in row major order, meaning the elements of each row are stored sequentially.

Implementations§

Source§

impl<T: Clone, const SIZE: usize> Grid2D<T, (), SIZE>

Source

pub fn new(element: T, cols: usize, rows: usize) -> Result<Self>

Returns a 2d grid, allocated in the stack, using element to fill the remaining free data.

§Errors

if the const SIZE doesn’t match cols * rows.

§Examples
use ladata::grid::DirectGrid2D;

let s = DirectGrid2D::<_, 16>::new('.', 4, 4);
Source§

impl<T: Clone, const SIZE: usize> Grid2D<T, Boxed, SIZE>

Source

pub fn new(element: T, cols: usize, rows: usize) -> Result<Self>

Available on crate feature alloc only.

Returns a 2d grid, allocated in the heap, using element to fill the remaining free data.

§Errors

if the const SIZE doesn’t match cols * rows.

§Examples
use ladata::grid::BoxedGrid2D;

let mut s = BoxedGrid2D::<_, 100>::new(' ', 10, 10);
Source§

impl<T, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§general queries

Source

pub const fn len(&self) -> usize

Returns the length of the grid (rows × cols).

Source

pub const fn num_rows(&self) -> usize

Returns the number of rows.

Source

pub const fn num_cols(&self) -> usize

Returns the number of columns.

Source

pub const fn row_len(&self) -> usize

Returns the length of a row (== num_cols).

Source

pub const fn col_len(&self) -> usize

Returns the length of a column (== num_rows).

Source

pub const fn get_index(&self, col: usize, row: usize) -> Result<usize>

Translates 2D col,row coordinates into a 1D index.

§Errors

If out of bounds.

Source

pub const fn get_index_unchecked(&self, col: usize, row: usize) -> usize

Translates 2D col,row coordinates into a 1D index.

This function doesn’t check whether the dimensions are right.

Source

pub const fn get_coords(&self, index: usize) -> Result<(usize, usize)>

Translates 1D index into 2D col,row coordinates.

§Errors

If out of bounds.

Source

pub const fn get_coords_unchecked(&self, index: usize) -> (usize, usize)

Translates 1D index into 2D col,row coordinates.

§Panics

If out of bounds.

Source

pub const fn chunked_capacity(&self, chunk_len: usize) -> usize

Returns the number of chunks (capacity()/chunk_len).

Source

pub const fn chunks_per_row(&self, chunk_len: usize) -> usize

Returns the number of chunks per row.

Source

pub const fn get_chunk_index( &self, chunk_len: usize, col: usize, row: usize, ) -> Result<usize>

Translates 2D col,row coordinates, with chunk length, into a 1D index.

  • it assumes the row_len to be an exact multiple of chunk_len.
  • only full chunks are allowed.
§Errors

If out of bounds.

Source

pub const fn get_chunk_index_unchecked( &self, chunk_len: usize, col: usize, row: usize, ) -> usize

Translates 2D col,row coordinates, with chunk length, into a 1D index.

§Panics

If out of bounds.

Source§

impl<T, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§single element get/set

Source

pub fn get_ref(&self, col: usize, row: usize) -> Result<&T>

Returns a reference to the element at the given row and column.

§Errors

If out of bounds.

Source

pub fn get_ref_unchecked(&self, col: usize, row: usize) -> &T

Returns a reference to the element at the given row and column.

§Panics

If out of bounds.

Source

pub fn get_ref_row_order(&self, index: usize) -> Result<&T>

Returns a reference to the element at the given 1D index, in row major order.

§Errors

If out of bounds.

Source

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

Returns a reference to the element at the given 1D index, in row major order.

§Panics

If out of bounds.

Source

pub fn get_ref_col_order(&self, index: usize) -> Result<&T>

Returns a reference to the element at the given 1D index, in column major order.

§Errors

If out of bounds.

Source

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

Returns a reference to the element at the given 1D index, in column major order.

§Panics

If out of bounds.

Source

pub fn get_ref_mut(&mut self, col: usize, row: usize) -> Result<&mut T>

Returns a mutable reference to the element at the given row and column.

§Errors

If out of bounds.

Source

pub fn get_ref_mut_unchecked(&mut self, col: usize, row: usize) -> &mut T

Returns a mutable reference to the element at the given row and column.

§Panics

If out of bounds.

Source

pub fn get_ref_mut_row_order(&mut self, index: usize) -> Result<&mut T>

Returns a mutable reference to the element at the given 1D index, in row major order.

§Errors

If out of bounds.

Source

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

Returns a mutable reference to the element at the given 1D index, in row major order.

§Panics

If out of bounds.

Source

pub fn get_ref_mut_col_order(&mut self, index: usize) -> Result<&mut T>

Returns a mutable reference to the element at the given 1D index, in column major order.

§Errors

If out of bounds.

Source

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

Returns a mutable reference to the element at the given 1D index, in column major order.

§Panics

If out of bounds.

Source

pub fn set(&mut self, element: T, col: usize, row: usize) -> Result<()>

Sets the element at the given row and column.

§Errors

If out of bounds.

Source

pub fn set_unchecked(&mut self, element: T, col: usize, row: usize)

Sets the element at the given row and column.

§Panics

If out of bounds.

Source

pub fn set_row_order(&mut self, element: T, index: usize) -> Result<()>

Sets the element at the given 1D index, in row major order.

§Errors

If out of bounds.

Source

pub fn set_row_order_unchecked(&mut self, element: T, index: usize)

Sets the element at the given 1D index, in row major order.

§Panics

If out of bounds.

Source

pub fn set_col_order(&mut self, element: T, index: usize) -> Result<()>

Sets the element at the given 1D index, in column major order.

§Errors

If out of bounds.

Source

pub fn set_col_order_unchecked(&mut self, element: T, index: usize)

Returns a mutable reference to the element at the given 1D index, in column major order.

§Panics

If out of bounds.

Source§

impl<T: Copy, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§single element get methods (Copy)

Source

pub fn get(&self, col: usize, row: usize) -> Result<T>

Returns the element at the given row and column.

Source

pub fn get_unchecked(&self, col: usize, row: usize) -> T

Returns the element at the given row and column.

§Panics

If out of bounds.

Source

pub fn get_row_order(&self, index: usize) -> Result<T>

Returns the element at the given 1D index, in row major order.

Source

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

Returns the element at the given 1D index, in row major order.

§Panics

If out of bounds.

Source

pub fn get_col_order(&self, index: usize) -> Result<T>

Returns the element at the given 1D index, in column major order.

Source

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

Returns the element at the given 1D index, in column major order.

§Panics

If out of bounds.

Source§

impl<T, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§iterators

Source

pub fn iter_ref(&self) -> impl DoubleEndedIterator<Item = &T>

Returns an iterator over references to all elements in row major order.

Source

pub fn iter_ref_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T>

Returns an iterator over mutable references to all elements in row major order.

Source

pub fn row_iter_ref( &self, row: usize, ) -> Result<impl DoubleEndedIterator<Item = &T>>

Returns an iterator over references to all elements in the given row.

Source

pub fn row_iter_ref_unchecked( &self, row: usize, ) -> impl DoubleEndedIterator<Item = &T>

Returns an iterator over references to all elements in the given row.

§Panics

If out of bounds.

Source

pub fn row_iter_ref_mut( &mut self, row: usize, ) -> Result<impl DoubleEndedIterator<Item = &mut T>>

Returns an iterator over mutable references to all elements in the given row.

Source

pub fn row_iter_ref_mut_unchecked( &mut self, row: usize, ) -> impl DoubleEndedIterator<Item = &mut T>

Returns an iterator over mutable references to all elements in the given row.

§Panics

If out of bounds.

Source

pub fn col_iter_ref(&self, col: usize) -> Result<impl Iterator<Item = &T>>

Returns an iterator over references to all elements in the given column.

Source

pub fn col_iter_ref_unchecked(&self, col: usize) -> impl Iterator<Item = &T>

Returns an iterator over references to all elements in the given column.

§Panics

If out of bounds.

Source

pub fn col_iter_ref_mut( &mut self, col: usize, ) -> Result<impl Iterator<Item = &mut T>>

Returns an iterator over references to all elements in the given column.

Source

pub fn col_iter_ref_mut_unchecked( &mut self, col: usize, ) -> impl Iterator<Item = &mut T>

Returns an iterator over references to all elements in the given column.

§Panics

If out of bounds.

Source

pub fn rows_iter_ref( &self, ) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T>>

Returns an iterator over all rows.

Each Item is itself another Iterator over references to the elements in that column.

Source

pub fn chunks_iter_ref( &self, chunk_size: usize, ) -> impl DoubleEndedIterator<Item = &[T]>

Returns an iterator over chunk_len references to elements in row major order.

§Panics

If chunk_size is 0.

Source

pub fn chunks_iter_ref_mut( &mut self, chunk_size: usize, ) -> impl DoubleEndedIterator<Item = &mut [T]>

Returns an iterator over chunk_len mutable references to elements in row major order.

§Panics

If chunk_size is 0.

Source§

impl<T: Copy, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§iterators (Copy)

Source

pub fn iter(&self) -> impl DoubleEndedIterator<Item = T> + '_

Returns an iterator over copies of all elements in row major order.

Source

pub fn row_iter( &self, row: usize, ) -> Result<impl DoubleEndedIterator<Item = T> + '_>

Returns an iterator over references to all elements in the given row.

Source

pub fn row_iter_unchecked( &self, row: usize, ) -> impl DoubleEndedIterator<Item = T> + '_

Returns an iterator over references to all elements in the given row.

§Panics

If out of bounds.

Source

pub fn rows_iter( &self, ) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = T> + '_>

Returns an iterator over all rows.

Each Item is itself another Iterator over copies of the elements in that column.

Source§

impl<T: Clone, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§collecting to Vec

Source

pub fn as_rows(&self) -> Vec<Vec<T>>

Available on crate feature alloc only.

Collects the Grid2D into a Vec of rows.

Source

pub fn as_row_order(&self) -> Vec<T>

Available on crate feature alloc only.

Collects the Grid2D into a Vec of elements in row major order.

Source§

impl<T, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§exposing the inner Vec

Source

pub fn into_array(self) -> Array<T, S, SIZE>

Returns the underlying Array.

Source

pub fn ref_array(&self) -> &Array<T, S, SIZE>

Returns a shared reference to the underlying Array.

Source

pub fn mut_array(&mut self) -> &mut Array<T, S, SIZE>

Returns an exclusive reference to the underlying Array.

Source§

impl<T, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§slices

Source

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

Returns a slice of the grid.

§Examples
use ladata::grid::DirectGrid2D;
Source

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

Returns a mutable slice of the grid.

Source

pub fn row_slice(&self, row: usize) -> Result<&[T]>

Returns a slice of requested row.

Source

pub fn row_slice_unchecked(&self, row: usize) -> &[T]

Returns a slice of requested row.

§Panics

If out of bounds.

Source

pub fn row_mut_slice(&mut self, row: usize) -> Result<&mut [T]>

Returns a mutable slice of requested row.

Source

pub fn row_mut_slice_unchecked(&mut self, row: usize) -> &mut [T]

Returns a mutable slice of requested row.

§Panics

If out of bounds.

Source§

impl<T, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§get chunks

Source

pub fn get_chunk( &self, chunk_len: usize, col: usize, row: usize, ) -> Result<&[T]>

Returns a slice of the chunk of elements at the given row and column.

Source

pub fn get_chunk_unchecked( &self, chunk_len: usize, col: usize, row: usize, ) -> &[T]

Returns a slice of the chunk of elements at the given row and column.

§Panics

If out of bounds.

Source

pub fn get_chunk_mut( &mut self, chunk_len: usize, col: usize, row: usize, ) -> Result<&mut [T]>

Returns a mutable slice of the chunk of elements at the given row and column.

Source

pub fn get_chunk_mut_unchecked( &mut self, chunk_len: usize, row: usize, col: usize, ) -> &mut [T]

Returns a mutable slice of the chunk of elements at the given row and column.

§Panics

If out of bounds.

Source§

impl<T: Clone, S: Storage, const SIZE: usize> Grid2D<T, S, SIZE>

§set chunks

Source

pub fn set_chunk( &mut self, chunk_len: usize, row: usize, col: usize, elements: &[T], ) -> Result<()>

Sets the elements on a chunk.

Source

pub fn set_chunk_unchecked( &mut self, chunk_len: usize, row: usize, col: usize, elements: &[T], )

Sets the elements on a chunk.

§Panics

If out of bounds.

Trait Implementations§

Source§

impl<T: Clone, S: Storage, const SIZE: usize> Clone for Grid2D<T, S, SIZE>
where S::Stored<[T; SIZE]>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate 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, S: Storage, const SIZE: usize> Debug for Grid2D<T, S, SIZE>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default, const SIZE: usize> Default for Grid2D<T, (), SIZE>

Source§

fn default() -> Self

Returns a square grid of side SIZE / 2, allocated in the stack, using the default value to fill the data.

§Panics

If SIZE is not even.

§Examples
use ladata::grid::DirectGrid2D;

let g = DirectGrid2D::<u8, 128>::default();
assert_eq![g.num_cols(), 64];
assert_eq![g.num_rows(), 64];
Source§

impl<T: Default, const SIZE: usize> Default for Grid2D<T, Boxed, SIZE>

Available on crate feature alloc only.
Source§

fn default() -> Self

Returns a square grid of side SIZE / 2, allocated in the stack, using the default value to fill the data.

§Panics

If SIZE is not even.

§Examples
use ladata::grid::BoxedGrid2D;

let g = BoxedGrid2D::<u8, 1_024>::default();
assert_eq![g.num_cols(), 512];
assert_eq![g.num_rows(), 512];
Source§

impl<T, S: Storage, const SIZE: usize> Index<(usize, usize)> for Grid2D<T, S, SIZE>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, (col, row): (usize, usize)) -> &Self::Output

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

impl<T, S: Storage, const SIZE: usize> IndexMut<(usize, usize)> for Grid2D<T, S, SIZE>

Source§

fn index_mut(&mut self, (col, row): (usize, usize)) -> &mut Self::Output

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

impl<T: PartialEq, S: Storage, const SIZE: usize> PartialEq for Grid2D<T, S, SIZE>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy, S: Storage, const SIZE: usize> Copy for Grid2D<T, S, SIZE>
where S::Stored<[T; SIZE]>: Copy,

Source§

impl<T: Eq, S: Storage, const SIZE: usize> Eq for Grid2D<T, S, SIZE>
where S::Stored<[T; SIZE]>: Eq,

Auto Trait Implementations§

§

impl<T, S, const SIZE: usize> Freeze for Grid2D<T, S, SIZE>
where <S as Storage>::Stored<[T; SIZE]>: Freeze,

§

impl<T, S, const SIZE: usize> RefUnwindSafe for Grid2D<T, S, SIZE>

§

impl<T, S, const SIZE: usize> Send for Grid2D<T, S, SIZE>
where <S as Storage>::Stored<[T; SIZE]>: Send,

§

impl<T, S, const SIZE: usize> Sync for Grid2D<T, S, SIZE>
where <S as Storage>::Stored<[T; SIZE]>: Sync,

§

impl<T, S, const SIZE: usize> Unpin for Grid2D<T, S, SIZE>
where <S as Storage>::Stored<[T; SIZE]>: Unpin,

§

impl<T, S, const SIZE: usize> UnwindSafe for Grid2D<T, S, SIZE>

Blanket Implementations§

Source§

impl<T> Also for T

Source§

fn also_mut<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value. Read more
Source§

fn also_ref<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value. Read more
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, Res> Apply<Res> for T
where T: ?Sized,

Source§

fn apply<F>(self, f: F) -> Res
where F: FnOnce(Self) -> Res, Self: Sized,

Apply a function which takes the parameter by value.
Source§

fn apply_ref<F>(&self, f: F) -> Res
where F: FnOnce(&Self) -> Res,

Apply a function which takes the parameter by shared reference.
Source§

fn apply_mut<F>(&mut self, f: F) -> Res
where F: FnOnce(&mut Self) -> Res,

Apply a function which takes the parameter by exclusive reference.
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> DataSize for T

Source§

const BYTES: usize = _

The size of this type in bytes, rounded up if it’s not a multiple of 8.
Source§

const UBITS: usize = _

The pointer size in bits for the current platform.
Source§

const UBYTES: usize = 8usize

The pointer size in bytes for the current platform.
Source§

fn pointer_ratio(&self) -> (usize, usize)

Returns the size ratio between UBYTES and BYTES. Read more
Source§

fn stack_byte_size(&self) -> usize

Returns the size in bytes of this type, in the stack. 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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> BitSizeAtLeast<0> for T