[−][src]Struct gridly_grids::SparseGrid
A sparse grid, where most of the cells are some default grid.
Sparse grids are backed by a hash table and a default value, and all elements not present in the hash table are considered to be the default value. These are colloquially called "unoccupied cells", though from the point of view of the gridly interface, they are indistinguishable from other cells. When reading from the grid, references to unoccupied cells will (usually) be to the same stored default value.
Whenever possible, cells that are set to the default value (as determined by
PartialEq
) will be removed from the hash table. Conversely, getting a
mutable reference to an unnocupied cell will insert a clone of the default
at that location, which can then be mutated.
Note about interior mutability: When a user gets a reference to an unoccupied
cell, the reference will (usually) be to the stored default value. This means
that if the user mutates the cell somehow (for instance, with a RefCell
),
those changes will appear in all unoccupied cells.
This is a trivial implementation of a Sparse Grid, intended for simple use
cases and as an example Grid
implementation. More complex implementations
are possible that track dirtied cells and clear them from the internal
storage more aggressively.
Methods
impl<T: Clone + PartialEq> SparseGrid<T>
[src]
pub fn new_default(dimensions: Vector, default: T) -> Self
[src]
Create a new SparseGrid
with the given dimensions, rooted at (0, 0)
,
filled with the given default value
pub fn new_rooted_default(
root: Location,
dimensions: Vector,
default: T
) -> Self
[src]
root: Location,
dimensions: Vector,
default: T
) -> Self
Create a new SparseGrid
with the given dimensions and root location,
filled with the default value
pub fn get_default(&self) -> &T
[src]
Get a reference to the default value. Most cells in the grid have this value.
pub fn clean(&mut self)
[src]
Remove all entries from the underlying hash table that compare equal to the default
pub fn clear(&mut self)
[src]
Remove all non-default entries from the grid
pub fn occuppied_entries(
&self
) -> impl Iterator<Item = (&Location, &T)> + FusedIterator + Clone
[src]
&self
) -> impl Iterator<Item = (&Location, &T)> + FusedIterator + Clone
Get an iterator over all of the occupied (non-default) entries in the grid, in an arbitrary order.
pub fn occuppied_entries_mut(
&mut self
) -> impl Iterator<Item = (&Location, &mut T)> + FusedIterator
[src]
&mut self
) -> impl Iterator<Item = (&Location, &mut T)> + FusedIterator
Get an iterator of mutable references to the occupied (non-default) entries in the grid, in an arbitrary order.
pub fn occuppied_entries_mut_cleaned(
&mut self
) -> impl Iterator<Item = (&Location, &mut T)> + FusedIterator + ExactSizeIterator
[src]
&mut self
) -> impl Iterator<Item = (&Location, &mut T)> + FusedIterator + ExactSizeIterator
Get an iterator of mutable references to the occupied (non-default) entries in the grid, in an arbitrary order.
The difference between this method and occuppied_entries_mut
is that
this one first cleans the underlying storage.
This means there's a higher up-front cost, but has the benefit of
providing an ExactSizeIterator
.
impl<T: Clone + PartialEq + Default> SparseGrid<T>
[src]
pub fn new_rooted(root: Location, dimensions: Vector) -> Self
[src]
Create a new SparseGrid
with the given dimensions and root location,
filled with the default value for T
pub fn new(dimensions: Vector) -> Self
[src]
Create a new SparseGrid
with the given dimensions, rooted at (0, 0)
,
filled with the default value for T
Trait Implementations
impl<T: Clone + PartialEq> Clone for SparseGrid<T>
[src]
fn clone(&self) -> SparseGrid<T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: Clone + PartialEq, L: LocationLike> Index<L> for SparseGrid<T>
[src]
impl<T: Clone + PartialEq, L: LocationLike> IndexMut<L> for SparseGrid<T>
[src]
impl<T: Debug + Clone + PartialEq> Debug for SparseGrid<T>
[src]
impl<T: Clone + PartialEq> BaseGridBounds for SparseGrid<T>
[src]
impl<T: Clone + PartialEq> BaseGridSetter for SparseGrid<T>
[src]
unsafe fn replace_unchecked(
&mut self,
location: &Location,
value: Self::Item
) -> Self::Item
[src]
&mut self,
location: &Location,
value: Self::Item
) -> Self::Item
Set the value of a cell in the grid. If this value compares equal to the default, remove it from the underlying hash table. Return the previous value (which may be a clone of the default value if the cell was unoccupied)
unsafe fn set_unchecked(&mut self, location: &Location, value: T)
[src]
Set the value of a cell in the grid. If this value compares equal to the default, remove it from the underlying hash table.
impl<T: Clone + PartialEq> BaseGrid for SparseGrid<T>
[src]
type Item = T
unsafe fn get_unchecked(&self, loc: &Location) -> &T
[src]
Get a reference to a value in the grid. If the location is not present in the hash table, return a reference to the grid's default value.
impl<T: Clone + PartialEq> BaseGridMut for SparseGrid<T>
[src]
unsafe fn get_unchecked_mut(&mut self, location: &Location) -> &mut T
[src]
Get a mutable reference to a cell in the grid. If this cell is unoccupied, the default is cloned and inserted into the underlying hash table at this location.
Auto Trait Implementations
impl<T> Send for SparseGrid<T> where
T: Send,
T: Send,
impl<T> Unpin for SparseGrid<T> where
T: Unpin,
T: Unpin,
impl<T> Sync for SparseGrid<T> where
T: Sync,
T: Sync,
impl<T> UnwindSafe for SparseGrid<T> where
T: UnwindSafe,
T: UnwindSafe,
impl<T> RefUnwindSafe for SparseGrid<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<G> GridBounds for G where
G: BaseGridBounds,
[src]
G: BaseGridBounds,
fn outer_root(&self) -> Location
[src]
fn num_rows(&self) -> Rows
[src]
fn num_columns(&self) -> Columns
[src]
fn dimension<C>(&self) -> C where
C: Component,
[src]
C: Component,
fn root_row(&self) -> Row
[src]
fn root_column(&self) -> Column
[src]
fn root_component<C>(&self) -> C where
C: Component,
[src]
C: Component,
fn range<C>(&self) -> ComponentRange<C> where
C: Component,
[src]
C: Component,
fn row_range(&self) -> ComponentRange<Row>
[src]
fn column_range(&self) -> ComponentRange<Column>
[src]
fn check_component<C>(&self, c: C) -> Result<C, RangeError<C>> where
C: Component,
[src]
C: Component,
fn check_row(&self, row: impl Into<Row>) -> Result<Row, RangeError<Row>>
[src]
fn check_column(
&self,
column: impl Into<Column>
) -> Result<Column, RangeError<Column>>
[src]
&self,
column: impl Into<Column>
) -> Result<Column, RangeError<Column>>
fn component_in_bounds<C>(&self, c: C) -> bool where
C: Component,
[src]
C: Component,
fn row_in_bounds(&self, row: impl Into<Row>) -> bool
[src]
fn column_in_bounds(&self, column: impl Into<Column>) -> bool
[src]
fn check_location(
&self,
location: impl LocationLike
) -> Result<Location, BoundsError>
[src]
&self,
location: impl LocationLike
) -> Result<Location, BoundsError>
fn location_in_bounds(&self, location: impl LocationLike) -> bool
[src]
impl<G> GridSetter for G where
G: BaseGridSetter,
<G as BaseGrid>::Item: Sized,
[src]
G: BaseGridSetter,
<G as BaseGrid>::Item: Sized,
fn replace(
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<Self::Item, BoundsError>
[src]
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<Self::Item, BoundsError>
fn set(
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<(), BoundsError>
[src]
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<(), BoundsError>
impl<G> Grid for G where
G: BaseGrid,
[src]
G: BaseGrid,
fn get(&self, location: impl LocationLike) -> Result<&Self::Item, BoundsError>
[src]
fn view<T>(&self) -> View<Self, T> where
T: Component,
[src]
T: Component,
fn rows(&self) -> View<Self, Row>
[src]
fn columns(&self) -> View<Self, Column>
[src]
unsafe fn single_view_unchecked<T>(&self, index: T) -> SingleView<Self, T> where
T: Component,
[src]
T: Component,
unsafe fn row_unchecked(&self, row: Row) -> SingleView<Self, Row>
[src]
unsafe fn column_unchecked(&self, column: Column) -> SingleView<Self, Column>
[src]
fn single_view<T>(&self, index: T) -> Result<SingleView<Self, T>, RangeError<T>> where
T: Component,
[src]
T: Component,
fn row(
&self,
row: impl Into<Row>
) -> Result<SingleView<Self, Row>, RangeError<Row>>
[src]
&self,
row: impl Into<Row>
) -> Result<SingleView<Self, Row>, RangeError<Row>>
fn column(
&self,
column: impl Into<Column>
) -> Result<SingleView<Self, Column>, RangeError<Column>>
[src]
&self,
column: impl Into<Column>
) -> Result<SingleView<Self, Column>, RangeError<Column>>
impl<G> GridMut for G where
G: BaseGridMut,
[src]
G: BaseGridMut,
fn get_mut(
&mut self,
location: impl LocationLike
) -> Result<&mut Self::Item, BoundsError>
[src]
&mut self,
location: impl LocationLike
) -> Result<&mut Self::Item, BoundsError>