BlockGrid

Struct BlockGrid 

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

Source

pub fn from_raw_vec(rows: usize, cols: usize, elems: Vec<T>) -> Result<Self, ()>

Constructs a BlockGrid<T, B> by consuming a Vec<T>.

The ordering of the memory is taken as is in the vector.

§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.

Source

pub fn take_raw_vec(self) -> Vec<T>

Converts a BlockGrid<T, B> to a Vec<T> in memory order.

Source

pub fn rows(&self) -> usize

Returns the nuumber of rows.

Source

pub fn cols(&self) -> usize

Returns the number of columns.

Source

pub fn size(&self) -> usize

Returns the number of elements.

Source

pub fn row_blocks(&self) -> usize

Returns the number of blocks in the vertical direction.

Source

pub fn col_blocks(&self) -> usize

Returns the number of blocks in the horizontal direction.

Source

pub fn blocks(&self) -> usize

Returns the total number of blocks.

Source

pub fn contains(&self, (row, col): Coords) -> bool

Returns true if the given coordinates are valid.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Returns all elements as a slice in memory order.

Source

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

Returns all elements as a mutable slice in memory order.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

Source§

impl<T: Clone + Default, B: BlockDim> BlockGrid<T, B>

Source

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

Constructs a BlockGrid<T, B> by filling with the default value of T.

§Errors

If rows and cols do not divide evenly into the block size B.

Trait Implementations§

Source§

impl<T: Clone, B: Clone + BlockDim> Clone for BlockGrid<T, B>

Source§

fn clone(&self) -> BlockGrid<T, B>

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: Debug, B: Debug + BlockDim> Debug for BlockGrid<T, B>

Source§

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

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

impl<T: Hash, B: Hash + BlockDim> Hash for BlockGrid<T, B>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, B: BlockDim> Index<(usize, usize)> for BlockGrid<T, B>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, coords: Coords) -> &Self::Output

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

impl<T, B: BlockDim> IndexMut<(usize, usize)> for BlockGrid<T, B>

Source§

fn index_mut(&mut self, coords: Coords) -> &mut Self::Output

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

impl<T: PartialEq, B: PartialEq + BlockDim> PartialEq for BlockGrid<T, B>

Source§

fn eq(&self, other: &BlockGrid<T, B>) -> 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: Eq, B: Eq + BlockDim> Eq for BlockGrid<T, B>

Source§

impl<T, B: BlockDim> StructuralPartialEq for BlockGrid<T, B>

Auto Trait Implementations§

§

impl<T, B> Freeze for BlockGrid<T, B>

§

impl<T, B> RefUnwindSafe for BlockGrid<T, B>

§

impl<T, B> Send for BlockGrid<T, B>
where B: Send, T: Send,

§

impl<T, B> Sync for BlockGrid<T, B>
where B: Sync, T: Sync,

§

impl<T, B> Unpin for BlockGrid<T, B>
where B: Unpin, T: Unpin,

§

impl<T, B> UnwindSafe for BlockGrid<T, B>
where B: UnwindSafe, 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.