Struct plane_2d::Plane

source ·
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>

source

pub fn new(x_min: i32, y_min: i32, x_max: i32, y_max: i32) -> Self

source§

impl<T: Default, S: Default + BuildHasher> Plane<T, S>

source

pub fn default_hasher(x_min: i32, y_min: i32, x_max: i32, y_max: i32) -> Self

source

pub fn from_grid(grid: Grid<T>, x_min: i32, y_min: i32) -> Self

source§

impl<T: Default, S: BuildHasher> Plane<T, S>

source

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

source

pub fn inner_grid(&self) -> &Grid<T>

source

pub fn inner_grid_mut(&mut self) -> &mut Grid<T>

source

pub fn inner_hash_map(&self) -> &HashMap<(i32, i32), T, S>

source

pub fn inner_hash_map_mut(&mut self) -> &mut HashMap<(i32, i32), T, S>

source

pub fn from_hash_map( map: HashMap<(i32, i32), T, S>, x_min: i32, y_min: i32, x_max: i32, y_max: i32, ) -> Self

source

pub fn from_grid_with_hasher( grid: Grid<T>, x_min: i32, y_min: i32, hasher: S, ) -> Self

source

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.

source

pub fn into_hash_map(self) -> HashMap<(i32, i32), T, S>

source

pub fn global_coordinates_from_grid(&self, x: usize, y: usize) -> (i32, i32)

source

pub fn grid_coordinates_from_global( &self, x: i32, y: i32, ) -> Option<(usize, usize)>

source

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.

source

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.

source

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.

source

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.

source

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.

source

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

source

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.

source

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 .

source

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

source

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

source

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

source§

impl<T, S: BuildHasher> Plane<Option<T>, S>

source

pub fn iter(&self) -> impl Iterator<Item = ((i32, i32), &T)>

Iterate over all the initialized elements

source

pub fn iter_mut(&mut self) -> impl Iterator<Item = ((i32, i32), &mut T)>

Mutably iterate over all the initialized elements

source

pub fn into_iter(self) -> impl Iterator<Item = ((i32, i32), T)>

Consume plane-2d to get all the initialized elements

Trait Implementations§

source§

impl<T: Clone + Default, S: Clone + BuildHasher> Clone for Plane<T, S>

source§

fn clone(&self) -> Plane<T, S>

Returns a copy 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 + Default, S: Debug + BuildHasher> Debug for Plane<T, S>

source§

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

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

impl<T: Default, S: Default + BuildHasher> Default for Plane<T, S>

source§

fn default() -> Self

Creates new Plane<T> with internal Grid<T> of size 0. Basically identical to HashMap<(i32, i32), T>, better use it instead, because Plane will be doing unnecessary comparisons

source§

impl<T: Default, S: Default + BuildHasher> From<Grid<T>> for Plane<T, S>

source§

fn from(value: Grid<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Default, S: BuildHasher> From<HashMap<(i32, i32), T, S>> for Plane<T, S>

source§

fn from(value: HashMap<(i32, i32), T, S>) -> Self

Converts to this type from the input type.
source§

impl<T: Default, S: BuildHasher> Index<(i32, i32)> for Plane<T, S>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, (x, y): (i32, i32)) -> &Self::Output

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

impl<T: Default, S: BuildHasher> IndexMut<(i32, i32)> for Plane<T, S>

source§

fn index_mut(&mut self, (x, y): (i32, i32)) -> &mut Self::Output

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

Auto Trait Implementations§

§

impl<T, S> Freeze for Plane<T, S>
where T: Freeze, S: Freeze,

§

impl<T, S> RefUnwindSafe for Plane<T, S>

§

impl<T, S> Send for Plane<T, S>
where T: Send, S: Send,

§

impl<T, S> Sync for Plane<T, S>
where T: Sync, S: Sync,

§

impl<T, S> Unpin for Plane<T, S>
where T: Unpin, S: Unpin,

§

impl<T, S> UnwindSafe for Plane<T, S>
where T: UnwindSafe, S: 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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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

§

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

§

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.