Struct sark_grids::sparse_grid::SparseGrid
source · pub struct SparseGrid<T> { /* private fields */ }
Expand description
A sparse grid that stores elements in a BTreeMap.
Implementations§
source§impl<T: Clone> SparseGrid<T>
impl<T: Clone> SparseGrid<T>
sourcepub fn new(size: impl GridPoint) -> Self
pub fn new(size: impl GridPoint) -> Self
Creates a new SparseGrid
sourcepub fn iter(&self) -> impl Iterator<Item = (&usize, &T)>
pub fn iter(&self) -> impl Iterator<Item = (&usize, &T)>
An iterator over all elements in the grid.
Yields (&usize,&mut T)
where usize
is the 1d position of the element in the grid.
sourcepub fn iter_values(&self) -> impl Iterator<Item = &T>
pub fn iter_values(&self) -> impl Iterator<Item = &T>
An iterator over just the values in the grid.
Yields &T
.
sourcepub fn iter_values_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_values_mut(&mut self) -> impl Iterator<Item = &mut T>
A mutable iterator over just the values in the grid.
Yields &mut T
.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&usize, &mut T)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&usize, &mut T)>
A mutable iterator over all elements in the grid.
Yields (&usize,&mut T)
where usize
is the 1d position of the element in the grid.
sourcepub fn iter_2d(&self) -> impl Iterator<Item = (IVec2, &T)>
pub fn iter_2d(&self) -> impl Iterator<Item = (IVec2, &T)>
A 2d iterator over all elements in the grid.
Yields (IVec2,&mut T)
where IVec2
is the 2d position of the element in the grid.
sourcepub fn iter_mut_2d(&mut self) -> impl Iterator<Item = (IVec2, &mut T)>
pub fn iter_mut_2d(&mut self) -> impl Iterator<Item = (IVec2, &mut T)>
A mutable iterator over all elements in the grid.
Yields (IVec,&mut T)
where IVec2
is the 2d position of the element in the grid.
sourcepub fn insert_row(
&mut self,
y: usize,
row: impl IntoIterator<Item = T> + Iterator<Item = T>
)
pub fn insert_row( &mut self, y: usize, row: impl IntoIterator<Item = T> + Iterator<Item = T> )
Insert into a row of the grid using an iterator.
Will insert up to the length of a row.
sourcepub fn insert_row_at(
&mut self,
xy: impl GridPoint,
row: impl IntoIterator<Item = T> + Iterator<Item = T>
)
pub fn insert_row_at( &mut self, xy: impl GridPoint, row: impl IntoIterator<Item = T> + Iterator<Item = T> )
Insert into a row of the grid using an iterator.
Will insert up to the length of a row.
sourcepub fn insert_column(
&mut self,
x: usize,
column: impl IntoIterator<Item = T> + Iterator<Item = T>
)
pub fn insert_column( &mut self, x: usize, column: impl IntoIterator<Item = T> + Iterator<Item = T> )
Insert into a column of the grid using an iterator.
Will insert up to the height of a column.
sourcepub fn insert_column_at(
&mut self,
xy: impl GridPoint,
column: impl IntoIterator<Item = T> + Iterator<Item = T>
)
pub fn insert_column_at( &mut self, xy: impl GridPoint, column: impl IntoIterator<Item = T> + Iterator<Item = T> )
Insert into a column of the grid starting from some point using an iterator.
Will insert up to the height of a column.
sourcepub fn remove(&mut self, pos: impl GridPoint) -> Option<T>
pub fn remove(&mut self, pos: impl GridPoint) -> Option<T>
Remove the element/tile at the given position.
Returns the removed element if one was present.
sourcepub fn remove_index(&mut self, index: usize) -> Option<T>
pub fn remove_index(&mut self, index: usize) -> Option<T>
Remove the element/tile at the given 1d index.
Returns the removed element if one was present.
pub fn width(&self) -> usize
pub fn height(&self) -> usize
pub fn size(&self) -> IVec2
pub fn is_empty(&self) -> bool
sourcepub fn transform_lti(&self, pos: impl GridPoint) -> usize
pub fn transform_lti(&self, pos: impl GridPoint) -> usize
Converts a 2d grid position to it’s corresponding 1D index.
sourcepub fn transform_itl(&self, index: usize) -> impl GridPoint
pub fn transform_itl(&self, index: usize) -> impl GridPoint
Converts a 1d index to it’s corresponding grid position.
sourcepub fn side_index(&self, side: Side) -> usize
pub fn side_index(&self, side: Side) -> usize
Gets the index for a given side.
sourcepub fn in_bounds(&self, pos: impl GridPoint) -> bool
pub fn in_bounds(&self, pos: impl GridPoint) -> bool
Returns true if the position is in the bounds of the grid. Note this doesn’t necessarily mean a tile exists at that point - just that it’s in bounds.
sourcepub fn insert_index(&mut self, index: usize, value: T) -> Option<T>
pub fn insert_index(&mut self, index: usize, value: T) -> Option<T>
Insert a value in the grid at the given 1d index.
Returns None
if no value was already present. Otherwise the old value
is returned.
sourcepub fn insert(&mut self, pos: impl GridPoint, value: T) -> Option<T>
pub fn insert(&mut self, pos: impl GridPoint, value: T) -> Option<T>
Insert a value in the grid.
Returns None
if no value was already present. Otherwise the old value
is returned.
sourcepub fn get_index(&self, index: usize) -> Option<&T>
pub fn get_index(&self, index: usize) -> Option<&T>
Retrieve a value in the grid from it’s 1d index.
Returns None
if there is no value at the index.
sourcepub fn get_mut_index(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut_index(&mut self, index: usize) -> Option<&mut T>
Retrieve a mutable value in the grid from it’s 1d index.
Returns None
if there is no value at the index.
Trait Implementations§
source§impl<T: Clone> Clone for SparseGrid<T>
impl<T: Clone> Clone for SparseGrid<T>
source§fn clone(&self) -> SparseGrid<T>
fn clone(&self) -> SparseGrid<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more