pub struct BlockGrid<T, B: BlockDim> { /* private fields */ }Expand description
A fixed-size 2D array with a blocked memory representation.
See crate-level documentation for general usage info.
If your dimensions are not a multiple of the block size, you can use the helper function
BlockDim::round_up_to_valid to generate larger, valid dimensions.
Implementations§
Source§impl<T, B: BlockDim> BlockGrid<T, B>
impl<T, B: BlockDim> BlockGrid<T, B>
Sourcepub fn take_raw_vec(self) -> Vec<T>
pub fn take_raw_vec(self) -> Vec<T>
Converts a BlockGrid<T, B> to a Vec<T> in memory order.
Sourcepub fn row_blocks(&self) -> usize
pub fn row_blocks(&self) -> usize
Returns the number of blocks in the vertical direction.
Sourcepub fn col_blocks(&self) -> usize
pub fn col_blocks(&self) -> usize
Returns the number of blocks in the horizontal direction.
Sourcepub fn contains(&self, (row, col): Coords) -> bool
pub fn contains(&self, (row, col): Coords) -> bool
Returns true if the given coordinates are valid.
Sourcepub fn get(&self, coords: Coords) -> Option<&T>
pub fn get(&self, coords: Coords) -> Option<&T>
Returns a reference to the element at the given coordinates, or None if they are
out-of-bounds.
Sourcepub fn get_mut(&mut self, coords: Coords) -> Option<&mut T>
pub fn get_mut(&mut self, coords: Coords) -> Option<&mut T>
Returns a mutable reference to the element at the given coordinates, or None if they
are out-of-bounds.
Sourcepub unsafe fn get_unchecked(&self, coords: Coords) -> &T
pub unsafe fn get_unchecked(&self, coords: Coords) -> &T
Returns a reference to the element at the given coordinates, without bounds checking.
§Safety
Calling this method with out-of-bounds coordinates is undefined-behaviour.
Sourcepub unsafe fn get_unchecked_mut(&mut self, coords: Coords) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self, coords: Coords) -> &mut T
Returns a mutable reference to the element at the given coordinates, without bounds checking.
§Safety
Calling this method with out-of-bounds coordinates is undefined-behaviour.
Sourcepub fn each_iter(&self) -> EachIter<'_, T, B> ⓘ
pub fn each_iter(&self) -> EachIter<'_, T, B> ⓘ
Returns an iterator over all the elements in memory order.
If you wanna visit each element arbitrarily, this would be the best way. If you also need
coordinates while iterating, follow up with a chained .coords() call.
Sourcepub fn each_iter_mut(&mut self) -> EachIterMut<'_, T, B> ⓘ
pub fn each_iter_mut(&mut self) -> EachIterMut<'_, T, B> ⓘ
Returns a mutable iterator over all the elements in memory order.
If you wanna mutably visit each element arbitrarily, this would be the best way. If you
also need coordinates while iterating, follow up with a chained .coords() call.
Sourcepub fn block_iter(&self) -> BlockIter<'_, T, B> ⓘ
pub fn block_iter(&self) -> BlockIter<'_, T, B> ⓘ
Returns an iterator over all blocks in memory order, yielding Blocks.
If you need the block coordinates while iterating, follow up with a chained
.coords() call. In this case, note that the 2D coordinates yielded are of the
actual entire block. If you instead need the coordinates of the first (top-left) element
in the block, see Block::starts_at.
Sourcepub fn block_iter_mut(&mut self) -> BlockIterMut<'_, T, B> ⓘ
pub fn block_iter_mut(&mut self) -> BlockIterMut<'_, T, B> ⓘ
Returns a mutable iterator over all blocks in memory order, yielding BlockMuts.
If you need the block coordinates while iterating, follow up with a chained
.coords() call. In this case, note that the 2D coordinates yielded are of the
actual entire block. If you instead need the coordinates of the first (top-left) element
in the block, see BlockMut::starts_at.
Sourcepub fn row_major_iter(&self) -> RowMajorIter<'_, T, B> ⓘ
pub fn row_major_iter(&self) -> RowMajorIter<'_, T, B> ⓘ
Returns an iterator over all the elements in row-major order.
This ordering is what you’re probably used to with usual 2D arrays. This method may be
useful for converting between array types or general IO. If you also need the coordinates
while iterating, follow up with a chained .coords() call.
Sourcepub fn row_major_iter_mut(&mut self) -> RowMajorIterMut<'_, T, B> ⓘ
pub fn row_major_iter_mut(&mut self) -> RowMajorIterMut<'_, T, B> ⓘ
Returns an mutable iterator over all the elements in row-major order.
If you also need the coordinates while iterating, follow up with a chained
.coords() call.
Source§impl<T: Clone, B: BlockDim> BlockGrid<T, B>
impl<T: Clone, B: BlockDim> BlockGrid<T, B>
Sourcepub fn filled(rows: usize, cols: usize, elem: T) -> Result<Self, ()>
pub fn filled(rows: usize, cols: usize, elem: T) -> Result<Self, ()>
Constructs a BlockGrid<T, B> by filling with a single element.
§Errors
If rows and cols do not divide evenly into the block size B.
Sourcepub fn from_row_major(rows: usize, cols: usize, elems: &[T]) -> Result<Self, ()>
pub fn from_row_major(rows: usize, cols: usize, elems: &[T]) -> Result<Self, ()>
Constructs a BlockGrid<T, B> from a slice in row-major order.
This method may be useful for converting from a typical 2D array.
§Errors
If invalid dimensions, either because rows and cols do not divide evenly into the block
size B or the length of elems does not match rows * cols.
Sourcepub fn from_col_major(rows: usize, cols: usize, elems: &[T]) -> Result<Self, ()>
pub fn from_col_major(rows: usize, cols: usize, elems: &[T]) -> Result<Self, ()>
Constructs a BlockGrid<T, B> from a slice in column-major order.
2D arrays are not usually stored like this, but occasionally they are.
§Errors
If invalid dimensions, either because rows and cols do not divide evenly into the block
size B or the length of elems does not match rows * cols.