Yog.Builder.GridGraph (YogEx v0.70.0)

Copy Markdown View Source

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 graph
  • rows - Number of rows in the grid
  • cols - Number of columns in the grid
  • topology - 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

t()

@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

coord_to_id(grid_graph, row, col)

@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

from_map(map)

@spec from_map(map()) :: t()

Backward compatibility: convert from legacy map format.

get_cell(grid_graph, row, col)

@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}

id_to_coord(grid_graph, node_id)

@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}

new(graph, rows, cols)

@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

new(graph, rows, cols, topology)

@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

to_graph(grid_graph)

@spec to_graph(t()) :: Yog.graph()

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

to_map(grid)

@spec to_map(t()) :: map()

Convert to legacy map format.

valid_coord?(grid_graph, row, col)

@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