Current section
Files
Jump to
Current section
Files
src/yog@pathfinding@path.erl
-module(yog@pathfinding@path).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yog/pathfinding/path.gleam").
-export([hydrate_path/2]).
-export_type([path/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" Shared types and utilities for pathfinding algorithms.\n").
-type path(JHA) :: {path, list(integer()), JHA}.
-file("src/yog/pathfinding/path.gleam", 35).
-spec do_hydrate_path(
yog@model:graph(any(), JHI),
list(integer()),
list({integer(), integer(), JHI})
) -> list({integer(), integer(), JHI}).
do_hydrate_path(Graph, Node_ids, Acc) ->
case Node_ids of
[U, V | Rest] ->
Acc@1 = case yog@model:edge_data(Graph, U, V) of
{ok, Data} ->
[{U, V, Data} | Acc];
{error, _} ->
Acc
end,
do_hydrate_path(Graph, [V | Rest], Acc@1);
_ ->
lists:reverse(Acc)
end.
-file("src/yog/pathfinding/path.gleam", 28).
?DOC(
" Hydrates a list of node IDs with the actual edge data between consecutive nodes.\n"
"\n"
" This is useful when you have a path from a pathfinding algorithm and need to\n"
" reconstruct the full sequence of edges with their weights/attributes.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let graph =\n"
" model.new(model.Directed)\n"
" |> model.add_edge_ensure(from: 1, to: 2, with: \"CAR\", default: Nil)\n"
" |> model.add_edge_ensure(from: 2, to: 3, with: \"BUS\", default: Nil)\n"
"\n"
" let path = [1, 2, 3]\n"
" let edges = path.hydrate_path(graph, path)\n"
" // => [#(1, 2, \"CAR\"), #(2, 3, \"BUS\")]\n"
" ```\n"
).
-spec hydrate_path(yog@model:graph(any(), JHC), list(integer())) -> list({integer(),
integer(),
JHC}).
hydrate_path(Graph, Node_ids) ->
do_hydrate_path(Graph, Node_ids, []).