Grid<E> class abstract base mixin Grids

A collection of elements accessible by a two-dimensional index.

Grids are a representation of a two-dimensional matrix, where there is a default, or empty element that initially fills the grid, and is used when the grid is resized, and can be replaced by a different element of the same type, E.

A grid is never empty, and always has a width and height of at least 1.

Implementers

Constructors

Grid.filled(int width, int height, {required E empty, E? fill})
Creates a new ListGrid with the given width and height.
factory
Grid.from(Grid<E> other, {E? empty})
Creates a new ListGrid from an existing grid.
factory
Grid.fromCells(Iterable<E> elements, {required int width, E? empty})
Creates a new ListGrid from elements in row-major order.
factory
Grid.fromRows(Iterable<Iterable<E>> rows, {E? empty})
Each element in rows, a column, must have the same length, and the resulting grid will have a width equal to the number of columns, and a height equal to the length of each column.
factory
Grid.generate(int width, int height, E generator(Pos pos), {required E empty})
Creates a new ListGrid with the given width and height.
factory

Properties

bottomLeft → Pos
The bottom-left position of the grid.
no setter
bottomRight → Pos
The bottom-right position of the grid.
no setter
bounds → Rect
Bounds of the grid, anchored at the topLeft position.
no setter
cells → Iterable<(Pos, E)>
An iterable of positions and elements in the grid, in row-major order.
no setter
columns → Iterable<Iterable<E>>
Columns of the grid.
no setter
empty → E
The default element for the grid, or the "empty" element.
no setter
hashCode → int
The hash code for this object.
no setterinherited
height ↔ int
Number of rows in the grid, and the upper bound for the y-coordinate.
getter/setter pair
isEmpty → bool
Whether the grid is zero-length or is entirely filled with empty.
no setter
isNotEmpty → bool
Whether the grid contains at least one element that is not empty.
no setter
length → int
Total number of elements in the grid.
no setter
rows → Iterable<Iterable<E>>
Rows of the grid.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
topLeft → Pos
The top-left position of the grid.
no setter
topRight → Pos
The top-right position of the grid.
no setter
width ↔ int
Number of columns in the grid, and the upper bound for the x-coordinate.
getter/setter pair

Methods

asUnweighted({Iterable<Pos> directions = Direction.cardinal}) → Walkable<Pos>
Returns a view of this grid as an unweighted walkable.
asWeighted({required double weight(E, E, Pos), Iterable<Pos> directions = Direction.cardinal}) → WeightedWalkable<Pos>
Returns a view of this grid as a weighted walkable.
clear([Rect? bounds]) → void
Clears the grid, setting all elements to empty.
containsPos(Pos pos) → bool
Returns whether a position is within the bounds of the grid.
copyFrom(Grid<E> src, {Rect? source, Pos target = Pos.zero}) → void
Copies the elements from another grid into this grid.
fill(E fill, [Rect? bounds]) → void
Fills the grid with the given element.
get(Pos pos) → E
Returns the element at a position in the grid.
getUnchecked(Pos pos) → E
Returns the element at a position in the grid.
getUnsafe(Pos pos) → E
Returns the element at a position in the grid.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
resize(int width, int height) → void
Resizes the grid to the given width and height.
set(Pos pos, E value) → void
Sets the element at a position in the grid.
setUnchecked(Pos pos, E value) → void
Sets the element at a position in the grid.
setUnsafe(Pos pos, E value) → void
Sets the element at a position in the grid.
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited
operator [](Pos pos) → E
Returns the element at a position in the grid.
operator []=(Pos pos, E value) → void
Sets the element at a position in the grid.

Static Methods

gridToString(Grid<Object?> grid, {String fill = '.', String empty = '#'}) → String
Converts a Grid to a string like toString.