Current section
Files
Jump to
Current section
Files
src/yog@dag@models.erl
-module(yog@dag@models).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yog/dag/models.gleam").
-export([from_graph/1, to_graph/1, add_node/3, remove_node/2, remove_edge/3, add_edge/4]).
-export_type([dag_error/0, dag/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type dag_error() :: cycle_detected.
-opaque dag(KEH, KEI) :: {dag, yog@model:graph(KEH, KEI)}.
-file("src/yog/dag/models.gleam", 14).
?DOC(" The Guard: Uses `is_acyclic` to validate the graph.\n").
-spec from_graph(yog@model:graph(KEJ, KEK)) -> {ok, dag(KEJ, KEK)} |
{error, dag_error()}.
from_graph(Graph) ->
case yog@properties@cyclicity:is_acyclic(Graph) of
true ->
{ok, {dag, Graph}};
false ->
{error, cycle_detected}
end.
-file("src/yog/dag/models.gleam", 22).
?DOC(" The Exit: Unwraps the Dag back into a standard Graph for general use.\n").
-spec to_graph(dag(KER, KES)) -> yog@model:graph(KER, KES).
to_graph(Dag) ->
erlang:element(2, Dag).
-file("src/yog/dag/models.gleam", 27).
?DOC(" Adds a node to the DAG. This cannot create a cycle, so it is safe and guaranteed to return a Dag.\n").
-spec add_node(dag(KEX, KEY), integer(), KEX) -> dag(KEX, KEY).
add_node(Dag, Id, Data) ->
{dag, yog@model:add_node(erlang:element(2, Dag), Id, Data)}.
-file("src/yog/dag/models.gleam", 32).
?DOC(" Removes a node from the DAG. This cannot create a cycle, so it is safe and guaranteed to return a Dag.\n").
-spec remove_node(dag(KFD, KFE), integer()) -> dag(KFD, KFE).
remove_node(Dag, Id) ->
{dag, yog@model:remove_node(erlang:element(2, Dag), Id)}.
-file("src/yog/dag/models.gleam", 37).
?DOC(" Removes an edge from the DAG. This cannot create a cycle, so it is safe and guaranteed to return a Dag.\n").
-spec remove_edge(dag(KFJ, KFK), integer(), integer()) -> dag(KFJ, KFK).
remove_edge(Dag, Src, Dst) ->
{dag, yog@model:remove_edge(erlang:element(2, Dag), Src, Dst)}.
-file("src/yog/dag/models.gleam", 50).
?DOC(
" Adds an edge to the DAG. \n"
" Because adding an edge can potentially create a cycle, this operation must validate the resulting\n"
" graph and returns a `Result(Dag, DagError)`.\n"
"\n"
" **Time Complexity:** O(V+E) (due to required cycle check on insertion).\n"
).
-spec add_edge(dag(KFP, KFQ), integer(), integer(), KFQ) -> {ok, dag(KFP, KFQ)} |
{error, dag_error()}.
add_edge(Dag, Src, Dst, Weight) ->
_pipe = yog@model:add_edge(erlang:element(2, Dag), Src, Dst, Weight),
from_graph(_pipe).