pub struct Plane<T: Default, S: BuildHasher = DefaultHashBuilder> { /* private fields */ }Expand description
Stores elements of a certain type in an integer grid, even in negative direction.
Uses Grid<T> type from 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 if you don’t need much space - 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>.
Implementations§
source§impl<T: Default> Plane<T, DefaultHashBuilder>
impl<T: Default> Plane<T, DefaultHashBuilder>
source§impl<T: Default, S: BuildHasher> Plane<T, S>
impl<T: Default, S: BuildHasher> Plane<T, S>
sourcepub fn with_hasher(
x_min: i32,
y_min: i32,
x_max: i32,
y_max: i32,
hasher: S,
) -> Self
pub fn with_hasher( x_min: i32, y_min: i32, x_max: i32, y_max: i32, hasher: S, ) -> 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_with_hasher( grid: Grid<T>, x_min: i32, y_min: i32, hasher: S, ) -> 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 Plane::inner_hash_map, Plane::inner_hash_map_mut, Plane::iter_all, Plane::iter_all_mut or Plane::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 iter_rect(
&self,
x_min: i32,
y_min: i32,
x_max: i32,
y_max: i32,
) -> impl Iterator<Item = ((i32, i32), &T)>
pub fn iter_rect( &self, x_min: i32, y_min: i32, x_max: i32, y_max: i32, ) -> impl Iterator<Item = ((i32, i32), &T)>
Iterates over all the items within the rectangle area inclusively.
Returns Default value if uninitialized element is being accessed.
Order of iteration is deterministic, but can change in future versions.
sourcepub fn iter_rect_mut(
&mut self,
x_min: i32,
y_min: i32,
x_max: i32,
y_max: i32,
) -> impl Iterator<Item = ((i32, i32), &mut T)>
pub fn iter_rect_mut( &mut self, x_min: i32, y_min: i32, x_max: i32, y_max: i32, ) -> impl Iterator<Item = ((i32, i32), &mut T)>
Mutably iterates over all the items within the rectangle area inclusively.
Returns Default value if uninitialized element is being accessed.
Order of iteration is deterministic, 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
Trait Implementations§
Auto Trait Implementations§
impl<T, S> Freeze for Plane<T, S>
impl<T, S> RefUnwindSafe for Plane<T, S>where
T: RefUnwindSafe,
S: RefUnwindSafe,
impl<T, S> Send for Plane<T, S>
impl<T, S> Sync for Plane<T, S>
impl<T, S> Unpin for Plane<T, S>
impl<T, S> UnwindSafe for Plane<T, S>where
T: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)