Grid graph builder result.
A grid graph is a structured graph where nodes are arranged in a 2D grid with edges connecting adjacent cells according to a specified topology.
Fields
graph- The underlying Yog graphrows- Number of rows in the gridcols- Number of columns in the gridtopology- Connection pattern (:rook,:queen,:king, etc.)predicate- Optional function to filter valid cells
Topologies
:rook- 4-connected (up, down, left, right):queen- 8-connected (rook + diagonals):king- Same as queen:bishop- Diagonal connections only
Examples
iex> grid = %Yog.Builder.GridGraph{
...> graph: graph,
...> rows: 3,
...> cols: 3,
...> topology: :rook
...> }
iex> grid.rows
3
iex> Yog.Builder.GridGraph.coord_to_id(grid, 0, 0)
0
Summary
Functions
Converts grid coordinates to a node ID.
Backward compatibility: convert from legacy map format.
Gets the cell data at a specific grid coordinate.
Converts a node ID back to grid coordinates.
Creates a new grid graph result.
Creates a new grid graph result with topology.
Unwraps the grid graph to return the plain graph.
Convert to legacy map format.
Checks if a coordinate is within the grid bounds.
Types
@type t() :: %Yog.Builder.GridGraph{ cols: non_neg_integer(), graph: Yog.graph(), predicate: (non_neg_integer(), non_neg_integer() -> boolean()) | nil, rows: non_neg_integer(), topology: atom() }
Functions
@spec coord_to_id(t(), non_neg_integer(), non_neg_integer()) :: Yog.Model.node_id()
Converts grid coordinates to a node ID.
The default mapping is: row * cols + col
Examples
iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 4}
iex> Yog.Builder.GridGraph.coord_to_id(grid, 0, 0)
0
iex> Yog.Builder.GridGraph.coord_to_id(grid, 1, 2)
6
iex> Yog.Builder.GridGraph.coord_to_id(grid, 2, 3)
11
Backward compatibility: convert from legacy map format.
@spec get_cell(t(), non_neg_integer(), non_neg_integer()) :: {:ok, term()} | {:error, nil}
Gets the cell data at a specific grid coordinate.
Returns {:ok, data} if the cell exists, or {:error, nil} otherwise.
Examples
iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 3}
iex> Yog.Builder.GridGraph.get_cell(grid, 1, 1)
{:ok, some_data}
iex> Yog.Builder.GridGraph.get_cell(grid, 10, 10)
{:error, nil}
@spec id_to_coord(t(), Yog.Model.node_id()) :: {non_neg_integer(), non_neg_integer()}
Converts a node ID back to grid coordinates.
Returns {row, col}.
Examples
iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 4}
iex> Yog.Builder.GridGraph.id_to_coord(grid, 0)
{0, 0}
iex> Yog.Builder.GridGraph.id_to_coord(grid, 6)
{1, 2}
iex> Yog.Builder.GridGraph.id_to_coord(grid, 11)
{2, 3}
@spec new(Yog.graph(), non_neg_integer(), non_neg_integer()) :: t()
Creates a new grid graph result.
Examples
iex> graph = Yog.undirected()
iex> grid = Yog.Builder.GridGraph.new(graph, 3, 4)
iex> grid.rows
3
iex> grid.cols
4
iex> grid.topology
:rook
@spec new(Yog.graph(), non_neg_integer(), non_neg_integer(), atom()) :: t()
Creates a new grid graph result with topology.
Examples
iex> graph = Yog.undirected()
iex> grid = Yog.Builder.GridGraph.new(graph, 3, 4, :queen)
iex> grid.topology
:queen
Unwraps the grid graph to return the plain graph.
Examples
iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 2, cols: 2}
iex> Yog.Builder.GridGraph.to_graph(grid)
graph
Convert to legacy map format.
@spec valid_coord?(t(), non_neg_integer(), non_neg_integer()) :: boolean()
Checks if a coordinate is within the grid bounds.
Examples
iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 4}
iex> Yog.Builder.GridGraph.valid_coord?(grid, 1, 2)
true
iex> Yog.Builder.GridGraph.valid_coord?(grid, 5, 5)
false