pub struct Map<Tile> { /* private fields */ }
Expand description
A Map keeps track of a tile grid.
Its coordinate system assumes that the origin is in the lower left,
for compatibility with Direction
.
While it is possible to clone a map, it is generally safe to assume that doing so is a sign that there’s a better approach possible.
§Entry Points
Map::new
is most useful when the problem involves cartography.- When a map is provided as the day’s input, use
Map::try_from
§Panics
Several internal methods assume that the width and height of the map can be
represented in an i32
. Very large maps may panic if that assumption is violated.
Implementations§
source§impl<Tile> Map<Tile>
impl<Tile> Map<Tile>
sourcepub fn procedural(
width: usize,
height: usize,
procedure: impl Fn(Point) -> Tile,
) -> Map<Tile>
pub fn procedural( width: usize, height: usize, procedure: impl Fn(Point) -> Tile, ) -> Map<Tile>
Procedurally create a new Map
from a function.
sourcepub fn procedural_offset(
offset: Point,
width: usize,
height: usize,
procedure: impl Fn(Point) -> Tile,
) -> Map<Tile>
pub fn procedural_offset( offset: Point, width: usize, height: usize, procedure: impl Fn(Point) -> Tile, ) -> Map<Tile>
Procedurally create a new Map
from a function, with an offset origin.
This offset can reduce dead space when the interesting part of a map is far from the origin.
sourcepub fn high_x(&self) -> i32
pub fn high_x(&self) -> i32
Highest x coordinate which is in bounds of this map.
Note that this is inclusive; use ..=
when using this to bound a range.
sourcepub fn high_y(&self) -> i32
pub fn high_y(&self) -> i32
Highest y coordinate which is in bounds of this map.
Note that this is inclusive; use ..=
when using this to bound a range.
sourcepub fn bottom_left(&self) -> Point
pub fn bottom_left(&self) -> Point
The coordinates of the bottom left corner of this map.
This is inclusive; it is a valid index into the map.
sourcepub fn top_left(&self) -> Point
pub fn top_left(&self) -> Point
The coordinates of the top left corner of this map.
This is inclusive; it is a valid index into the map.
sourcepub fn bottom_right(&self) -> Point
pub fn bottom_right(&self) -> Point
The coordinates of the bottom right corner of this map.
This is inclusive; it is a valid index into the map.
sourcepub fn top_right(&self) -> Point
pub fn top_right(&self) -> Point
The coordinates of the top right corner of this map.
This is inclusive; it is a valid index into the map.
sourcepub fn iter(&self) -> impl Iterator<Item = (Point, &Tile)>
pub fn iter(&self) -> impl Iterator<Item = (Point, &Tile)>
Iterate over the points and tiles of this map.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (Point, &mut Tile)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (Point, &mut Tile)>
Iterate over the points of this tiles in this map, with mutable access to the tiles.
sourcepub fn points(&self) -> impl Iterator<Item = Point>
pub fn points(&self) -> impl Iterator<Item = Point>
Iterate over the points of this map without depending on the lifetime of self
.
sourcepub fn in_bounds(&self, point: Point) -> bool
pub fn in_bounds(&self, point: Point) -> bool
true
when a point is legal within the bounds of this map.
sourcepub fn make_in_bounds(&self) -> impl Fn(Point) -> bool
pub fn make_in_bounds(&self) -> impl Fn(Point) -> bool
Make a function which returns true
when the parameter is within the bounds of this map,
without depending on the lifetime of self
.
sourcepub fn adjacencies(&self, point: Point) -> impl Iterator<Item = Point>
pub fn adjacencies(&self, point: Point) -> impl Iterator<Item = Point>
Return an iterator of all legal points adjacent to the given point.
This iterator will return up to 8 elements; it includes diagonals.
sourcepub fn orthogonal_adjacencies(
&self,
point: Point,
) -> impl Iterator<Item = Point>
pub fn orthogonal_adjacencies( &self, point: Point, ) -> impl Iterator<Item = Point>
Return an iterator of all legal points orthogonally adjacent to the given point,
This iterator will return up to 4 elements; it does not include diagonals.
sourcepub fn make_adjacencies(&self, point: Point) -> impl Iterator<Item = Point>where
Tile: 'static,
pub fn make_adjacencies(&self, point: Point) -> impl Iterator<Item = Point>where
Tile: 'static,
Return an iterator of all legal points adjacent to the given point,
without depending on the lifetime of self
.
This iterator will return up to 8 elements; it includes diagonals.
This introduces a bound that the Tile
type must not contain any references.
It is also slightly less efficient than [self.adjacencies
]. In general,
that function should be preferred unless there are lifetime conflicts.
sourcepub fn make_orthogonal_adjacencies(
&self,
point: Point,
) -> impl Iterator<Item = Point>where
Tile: 'static,
pub fn make_orthogonal_adjacencies(
&self,
point: Point,
) -> impl Iterator<Item = Point>where
Tile: 'static,
Return an iterator of all legal points orthogonally adjacent to the given point,
without depending on the lifetime of self
.
This iterator will return up to 4 elements; it does not include diagonals.
This introduces a bound that the Tile
type must not contain any references.
It is also slightly less efficient than [self.orthogonal_adjacencies
]. In general,
that function should be preferred unless there are lifetime conflicts.
sourcepub fn project(
&self,
origin: Point,
dx: i32,
dy: i32,
) -> impl Iterator<Item = Point>
pub fn project( &self, origin: Point, dx: i32, dy: i32, ) -> impl Iterator<Item = Point>
Return an iterator of all legal points arrived at by applying the given deltas to the origin.
The origin point is always the first item in this iteration.
sourcepub fn edge(&self, direction: Direction) -> Edge ⓘ
pub fn edge(&self, direction: Direction) -> Edge ⓘ
Create an iterator over the points on the edge of this map.
Note that this iterator returns the points which are coordinates for each point on the edge,
not the items of this map. You can use the Iterator::map
combinator to map it to items from
the map, if desired.
The edge is traversed in increasing order. It is a std::iter::DoubleEndedIterator
, though, so
it can be reversed if desired.
The input direction
indicates which edge should be traversed.
sourcepub fn translate(&mut self, dx: i32, dy: i32)
pub fn translate(&mut self, dx: i32, dy: i32)
Translate all points in this map by a given amount.
Completes in O(1)
.
§Example
Using the symbols X
and O
to indicate tiles, and .
to indicate out-of-bounds space
away from the origin, we start with this map:
XOOX
OXOX
After applying translate(2, 1)
, we end with this map:
..XOOX
..OXOX
......
sourcepub fn convert_tile_type<NewTile>(self) -> Map<NewTile>where
Tile: Into<NewTile>,
pub fn convert_tile_type<NewTile>(self) -> Map<NewTile>where
Tile: Into<NewTile>,
Convert the underlying tile type of a map.
This produces a new map whose tiles are of a new underlying type.
source§impl<Tile: Clone + Default> Map<Tile>
impl<Tile: Clone + Default> Map<Tile>
sourcepub fn new(width: usize, height: usize) -> Map<Tile>
pub fn new(width: usize, height: usize) -> Map<Tile>
Create a new map of the specified dimensions.
Its lower left corner is at (0, 0)
.
sourcepub fn new_offset(offset: Point, width: usize, height: usize) -> Map<Tile>
pub fn new_offset(offset: Point, width: usize, height: usize) -> Map<Tile>
Create a new map of the specified dimensions.
Its lower left corner is at offset
.
sourcepub fn flip_vertical(&self) -> Map<Tile>
pub fn flip_vertical(&self) -> Map<Tile>
Create a copy of this map which has been flipped vertically: the axis of symmetry is horizontal.
This does not adjust the offset; the corners remain where they previously were.
sourcepub fn flip_horizontal(&self) -> Map<Tile>
pub fn flip_horizontal(&self) -> Map<Tile>
Create a copy of this map which has been flipped horizontally; the axis of symmetry is vertical.
This does not adjust the offset; the corners remain where they previously were.
sourcepub fn rotate_left(&self) -> Map<Tile>
pub fn rotate_left(&self) -> Map<Tile>
Create a copy of this map which has been rotated counter-clockwise.
This maintains the invariant that all points are in the positive quadrant, and assumes that
the offset is (0, 0)
. If necessary, apply Self::translate
before and after this operation to
produce an appropriate new offset.
§Panics
If the offset is not (0, 0)
.
sourcepub fn rotate_right(&self) -> Map<Tile>
pub fn rotate_right(&self) -> Map<Tile>
Create a copy of this map which has been rotated clockwise.
This maintains the invariant that all points are in the positive quadrant, and assumes that
the offset is (0, 0)
. If necessary, apply Self::translate
before and after this operation to
produce an appropriate new offset.
§Panics
If the offset is not (0, 0)
.
source§impl<Tile> Map<Tile>
impl<Tile> Map<Tile>
sourcepub fn try_from<R>(input: R) -> Result<Self, MapConversionErr>where
R: BufRead,
pub fn try_from<R>(input: R) -> Result<Self, MapConversionErr>where
R: BufRead,
Try to convert the contents of a reader into a map.
We don’t actually impl<T, R> TryFrom<R> for Map<T>
because there’s a
coherence conflict with the stdlib blanket impl
impl<T, U> std::convert::TryFrom<U> for T where U: std::convert::Into<T>;
Because there’s a chance that R
also implements Into<Map<T>>
, we can’t do it.
That doesn’t stop us from doing it here, and implementing the official trait for a few concrete types
source§impl<Tile> Map<Tile>where
Tile: Display + DisplayWidth,
impl<Tile> Map<Tile>where
Tile: Display + DisplayWidth,
sourcepub fn to_string_with_override(
&self,
override_tiles: impl Fn(Point, &Tile) -> Option<String>,
) -> String
pub fn to_string_with_override( &self, override_tiles: impl Fn(Point, &Tile) -> Option<String>, ) -> String
Produce a string from this Map
, with certain tiles overridden.
This does not edit the map, just adjusts its presentation.
§Panics
- If an override value’s returned string is not the same length as
Tile::DISPLAY_WIDTH
.
source§impl<Tile> Map<Tile>where
Tile: ToRgb,
impl<Tile> Map<Tile>where
Tile: ToRgb,
sourcepub fn render(&self, output: &Path, style: Style) -> Result<(), RenderError>
pub fn render(&self, output: &Path, style: Style) -> Result<(), RenderError>
Render this map as a still image into an output file.
Depends on the map-render
feature.
The output image is a gif under all circumstances. It is useful, though
unenforced, that the output file name matches *.gif
.
sourcepub fn prepare_animation(
&self,
output: &Path,
frame_duration: Duration,
style: Style,
) -> Result<Animation, RenderError>
pub fn prepare_animation( &self, output: &Path, frame_duration: Duration, style: Style, ) -> Result<Animation, RenderError>
Prepare an animation from this map.
Depends on the map-render
feature.
This returns an Animation
object which can have frames added to it.
This method does not automatically render this Map
frame to the Animation
.
This enables you to set up the animation ahead of time with dummy data.
The major constraint is that all subsequent maps added as frames must have dimensions identical to this Map’s.
The animation will loop infinitely, displaying each frame for
frame_duration
.
source§impl<Tile: Clone + ContextInto<Traversable>> Map<Tile>
impl<Tile: Clone + ContextInto<Traversable>> Map<Tile>
sourcepub fn reachable_from_ctx(
&self,
context: &<Tile as ContextInto<Traversable>>::Context,
point: Point,
visit: impl FnMut(Point, &Tile) -> bool,
)
pub fn reachable_from_ctx( &self, context: &<Tile as ContextInto<Traversable>>::Context, point: Point, visit: impl FnMut(Point, &Tile) -> bool, )
Visit every non-obstructed tile reachable from the initial point.
If the visitor ever returns true, processing halts and no further points are visited.
navigate between the given points using A*
Trait Implementations§
impl<Tile: Eq> Eq for Map<Tile>
Auto Trait Implementations§
impl<Tile> Freeze for Map<Tile>
impl<Tile> RefUnwindSafe for Map<Tile>where
Tile: RefUnwindSafe,
impl<Tile> Send for Map<Tile>where
Tile: Send,
impl<Tile> Sync for Map<Tile>where
Tile: Sync,
impl<Tile> Unpin for Map<Tile>where
Tile: Unpin,
impl<Tile> UnwindSafe for Map<Tile>where
Tile: 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
)source§impl<A, B> ContextFrom<A> for Bwhere
B: From<A>,
impl<A, B> ContextFrom<A> for Bwhere
B: From<A>,
source§impl<A, B> ContextInto<B> for Awhere
B: ContextFrom<A>,
impl<A, B> ContextInto<B> for Awhere
B: ContextFrom<A>,
§impl<T> Conv for T
impl<T> Conv for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.