pub struct Plane<T: Default, S: Default + BuildHasher = RandomState> { /* private fields */ }Expand description
Stores elements of a certain type in a 2D grid structure on the whole 2D plane-2d, even in negative direction.
Uses Grid<T> type in a grid crate
and a
HashMap<(i32, i32), T>
to store data on the heap.
Data in Grid<T> is stored inside one dimensional array using Vec<T>.
This is cash efficient, so it is recommended to store there dense regions of data,
but it is memory inefficient - it keeps memory for rows * cols cells,
so if there are only two cells in use - one is placed on coordinate (0,0) and other is on (100,100),
there is space reserved for at least 10000 elements.
Using HashMap solves that problem - it stores data outside the grid bounds.
Note that if the size of the Grid is zero, this data structure is identical to the
HashMap<(i32, i32), T>,
and it’ll be more effective to just use
HashMap<(i32, i32), T>,
since in this case you’ll get rid of any unnecessary checks.
T should implement Default trait, because the plain is infinitely large,
and you can access any point of it at any time.
Whenever uninitialized cell is accessed, default value is returned.
For optionally initialized data use Option<T>.
The size limit for the grid is rows * cols < usize.
Implementations§
source§impl<T: Default, S: Default + BuildHasher> Plane<T, S>
impl<T: Default, S: Default + BuildHasher> Plane<T, S>
pub fn rows_cols( x_min: i32, y_min: i32, x_max: i32, y_max: i32, ) -> (usize, usize)
sourcepub fn new(x_min: i32, y_min: i32, x_max: i32, y_max: i32) -> Self
pub fn new(x_min: i32, y_min: i32, x_max: i32, y_max: i32) -> Self
Returns Plane whose array-based grid is within specified bounds
pub fn inner_grid(&self) -> &Grid<T>
pub fn inner_grid_mut(&mut self) -> &mut Grid<T>
pub fn inner_hash_map(&self) -> &HashMap<(i32, i32), T, S>
pub fn inner_hash_map_mut(&mut self) -> &mut HashMap<(i32, i32), T, S>
pub fn from_hash_map( map: HashMap<(i32, i32), T, S>, x_min: i32, y_min: i32, x_max: i32, y_max: i32, ) -> Self
pub fn from_grid(grid: Grid<T>, x_min: i32, y_min: i32) -> Self
sourcepub fn from_grid_and_hash_map(
grid: Grid<T>,
map: HashMap<(i32, i32), T, S>,
x_min: i32,
y_min: i32,
) -> Self
pub fn from_grid_and_hash_map( grid: Grid<T>, map: HashMap<(i32, i32), T, S>, x_min: i32, y_min: i32, ) -> Self
Creates instance of Plane<T> from Grid<T> and [HashMap<(Scalar, Scalar), T>]
§Note
Doesn’t remove items from map if they are initialized and overlapping with grid. Their existence will be ignored.
When you are calling [inner_hash_map], [inner_hash_map_mut], [iter_all], [iter_all_mut] or [into_iter_all]
those values may or may not still exist in the hash map.
pub fn into_hash_map(self) -> HashMap<(i32, i32), T, S>
pub fn global_coordinates_from_grid(&self, x: usize, y: usize) -> (i32, i32)
pub fn grid_coordinates_from_global( &self, x: i32, y: i32, ) -> Option<(usize, usize)>
sourcepub unsafe fn get_unchecked(&self, x: i32, y: i32) -> &T
pub unsafe fn get_unchecked(&self, x: i32, y: i32) -> &T
Returns a reference to an element that should be contained in Grid<T> container without performing bound checks.
Generally not recommended, use with caution!
§Safety
Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
sourcepub unsafe fn get_unchecked_mut(&mut self, x: i32, y: i32) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self, x: i32, y: i32) -> &mut T
Returns a mutable reference to an element that should be contained in Grid<T> container without performing bound checks.
Generally not recommended, use with caution!
§Safety
Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
sourcepub fn get(&self, x: i32, y: i32) -> &T
pub fn get(&self, x: i32, y: i32) -> &T
Access a certain element on the plane-2d.
Returns [default] value if uninitialized element is being accessed.
sourcepub fn get_mut(&mut self, x: i32, y: i32) -> &mut T
pub fn get_mut(&mut self, x: i32, y: i32) -> &mut T
Mutable access to a certain element on the plane-2d.
Returns [default] value if uninitialized element is being accessed.
sourcepub fn insert(&mut self, value: T, x: i32, y: i32) -> T
pub fn insert(&mut self, value: T, x: i32, y: i32) -> T
Insert element at the coordinate.
Returns [default] value if uninitialized element is being accessed.
sourcepub fn relocate_grid(&mut self, x_min: i32, y_min: i32, x_max: i32, y_max: i32)
pub fn relocate_grid(&mut self, x_min: i32, y_min: i32, x_max: i32, y_max: i32)
Changes position of the base grid structure. Iterates over all elements in previous grid and all elements in new grid
sourcepub fn foreach_in_area(
&self,
x_min: i32,
y_min: i32,
x_max: i32,
y_max: i32,
f: impl FnMut(&T, i32, i32),
)
pub fn foreach_in_area( &self, x_min: i32, y_min: i32, x_max: i32, y_max: i32, f: impl FnMut(&T, i32, i32), )
Iterates over all the items within the rectangle area inclusively.
Returns [default] value if uninitialized element is being accessed.
Order of iteration deterministic for now, but can change in future versions .
sourcepub fn foreach_in_area_mut(
&mut self,
x_min: i32,
y_min: i32,
x_max: i32,
y_max: i32,
f: impl FnMut(&mut T, i32, i32),
)
pub fn foreach_in_area_mut( &mut self, x_min: i32, y_min: i32, x_max: i32, y_max: i32, f: impl FnMut(&mut T, i32, i32), )
Mutably iterates over all the items within the rectangle area inclusively.
Returns [default] value if uninitialized element is being accessed.
Order of iteration deterministic for now, but can change in future versions .
sourcepub fn iter_all(&self) -> impl Iterator<Item = ((i32, i32), &T)>
pub fn iter_all(&self) -> impl Iterator<Item = ((i32, i32), &T)>
Iterate over all the elements stored inside the grid and hashmap. May return value from HashMap even if it is overlapping with Grid
sourcepub fn iter_all_mut(&mut self) -> impl Iterator<Item = ((i32, i32), &mut T)>
pub fn iter_all_mut(&mut self) -> impl Iterator<Item = ((i32, i32), &mut T)>
Mutably iterate over all the elements stored inside the grid and hashmap. May return value from HashMap even if it is overlapping with Grid
sourcepub fn into_iter_all(self) -> impl Iterator<Item = ((i32, i32), T)>
pub fn into_iter_all(self) -> impl Iterator<Item = ((i32, i32), T)>
Iterate over all the elements stored inside the grid and hashmap. May return value from HashMap even if it is overlapping with Grid