Current section

Files

Jump to
yog src yog@internal@util.erl
Raw

src/yog@internal@util.erl

-module(yog@internal@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yog/internal/util.gleam").
-export([range/2, dict_update_inner/4, norm_diff/3, array_from_list/1, array_to_list/2, array_get/2, array_set/3, list_at/2, shuffle/2, compare_frontier/3, compare_distance_frontier/3, compare_a_star_frontier/3, should_explore_node/4, exit/1]).
-export_type([norm_type/0, array/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(false).
-type norm_type() :: l1 | l2 | max.
-type array(EBF) :: any() | {gleam_phantom, EBF}.
-file("src/yog/internal/util.gleam", 24).
?DOC(false).
-spec do_range(integer(), integer(), list(integer())) -> list(integer()).
do_range(Current, End, Acc) ->
case Current > End of
true ->
Acc;
false ->
do_range(Current + 1, End, [Current | Acc])
end.
-file("src/yog/internal/util.gleam", 19).
?DOC(false).
-spec range(integer(), integer()) -> list(integer()).
range(Start, End) ->
_pipe = do_range(Start, End, []),
lists:reverse(_pipe).
-file("src/yog/internal/util.gleam", 32).
?DOC(false).
-spec dict_update_inner(
gleam@dict:dict(EBJ, gleam@dict:dict(EBK, EBL)),
EBJ,
EBK,
fun((gleam@dict:dict(EBK, EBL), EBK) -> gleam@dict:dict(EBK, EBL))
) -> gleam@dict:dict(EBJ, gleam@dict:dict(EBK, EBL)).
dict_update_inner(Outer, Key1, Key2, Fun) ->
case gleam_stdlib:map_get(Outer, Key1) of
{ok, Inner} ->
gleam@dict:insert(Outer, Key1, Fun(Inner, Key2));
{error, _} ->
Outer
end.
-file("src/yog/internal/util.gleam", 92).
?DOC(false).
-spec norm_diff_l1(gleam@dict:dict(ECD, float()), gleam@dict:dict(ECD, float())) -> float().
norm_diff_l1(M1, M2) ->
_pipe = gleam@dict:combine(M1, M2, fun(V1, V2) -> V1 - V2 end),
gleam@dict:fold(
_pipe,
+0.0,
fun(Acc, _, V) -> Acc + gleam@float:absolute_value(V) end
).
-file("src/yog/internal/util.gleam", 97).
?DOC(false).
-spec norm_diff_l2(gleam@dict:dict(ECI, float()), gleam@dict:dict(ECI, float())) -> float().
norm_diff_l2(M1, M2) ->
Sum_sq = begin
_pipe = gleam@dict:combine(M1, M2, fun(V1, V2) -> V1 - V2 end),
gleam@dict:fold(_pipe, +0.0, fun(Acc, _, V) -> Acc + (V * V) end)
end,
_pipe@1 = gleam@float:square_root(Sum_sq),
gleam@result:unwrap(_pipe@1, +0.0).
-file("src/yog/internal/util.gleam", 105).
?DOC(false).
-spec norm_diff_max(
gleam@dict:dict(ECN, float()),
gleam@dict:dict(ECN, float())
) -> float().
norm_diff_max(M1, M2) ->
_pipe = gleam@dict:combine(M1, M2, fun(V1, V2) -> V1 - V2 end),
gleam@dict:fold(
_pipe,
+0.0,
fun(Acc, _, V) ->
D = gleam@float:absolute_value(V),
case D > Acc of
true ->
D;
false ->
Acc
end
end
).
-file("src/yog/internal/util.gleam", 80).
?DOC(false).
-spec norm_diff(
gleam@dict:dict(EBY, float()),
gleam@dict:dict(EBY, float()),
norm_type()
) -> float().
norm_diff(M1, M2, Norm_type) ->
case Norm_type of
l1 ->
norm_diff_l1(M1, M2);
l2 ->
norm_diff_l2(M1, M2);
max ->
norm_diff_max(M1, M2)
end.
-file("src/yog/internal/util.gleam", 127).
?DOC(false).
-spec array_from_list(list(ECS)) -> array(ECS).
array_from_list(List) ->
yog_internal_util:array_from_list(List).
-file("src/yog/internal/util.gleam", 132).
?DOC(false).
-spec array_to_list(array(ECV), integer()) -> list(ECV).
array_to_list(Arr, Size) ->
yog_internal_util:array_to_list(Arr, Size).
-file("src/yog/internal/util.gleam", 137).
?DOC(false).
-spec array_get(array(ECY), integer()) -> ECY.
array_get(Arr, Index) ->
yog_internal_util:array_get(Arr, Index).
-file("src/yog/internal/util.gleam", 142).
?DOC(false).
-spec array_set(array(EDA), integer(), EDA) -> array(EDA).
array_set(Arr, Index, Value) ->
yog_internal_util:array_set(Arr, Index, Value).
-file("src/yog/internal/util.gleam", 146).
?DOC(false).
-spec list_at(list(EDD), integer()) -> {ok, EDD} | {error, nil}.
list_at(Lst, Index) ->
case {Index, Lst} of
{0, [First | _]} ->
{ok, First};
{N, [_ | Rest]} when N > 0 ->
list_at(Rest, N - 1);
{_, _} ->
{error, nil}
end.
-file("src/yog/internal/util.gleam", 181).
?DOC(false).
-spec do_shuffle(array(EDK), integer(), yog@internal@random:rng()) -> {array(EDK),
yog@internal@random:rng()}.
do_shuffle(Arr, I, Rng) ->
case I =< 0 of
true ->
{Arr, Rng};
false ->
{J, Next_rng} = yog@internal@random:next_int(Rng, I + 1),
Temp_i = yog_internal_util:array_get(Arr, I),
Temp_j = yog_internal_util:array_get(Arr, J),
Swapped = begin
_pipe = Arr,
_pipe@1 = yog_internal_util:array_set(_pipe, I, Temp_j),
yog_internal_util:array_set(_pipe@1, J, Temp_i)
end,
do_shuffle(Swapped, I - 1, Next_rng)
end.
-file("src/yog/internal/util.gleam", 169).
?DOC(false).
-spec shuffle(list(EDH), yog@internal@random:rng()) -> {list(EDH),
yog@internal@random:rng()}.
shuffle(List, Rng) ->
N = erlang:length(List),
case N =< 1 of
true ->
{List, Rng};
false ->
Arr = yog_internal_util:array_from_list(List),
{Shuffled_arr, Final_rng} = do_shuffle(Arr, N - 1, Rng),
{yog_internal_util:array_to_list(Shuffled_arr, N), Final_rng}
end.
-file("src/yog/internal/util.gleam", 202).
?DOC(false).
-spec compare_frontier(
{EDN, list(EDO)},
{EDN, list(EDO)},
fun((EDN, EDN) -> gleam@order:order())
) -> gleam@order:order().
compare_frontier(A, B, Cmp) ->
Cmp(erlang:element(1, A), erlang:element(1, B)).
-file("src/yog/internal/util.gleam", 211).
?DOC(false).
-spec compare_distance_frontier(
{EDR, EDS},
{EDR, EDS},
fun((EDR, EDR) -> gleam@order:order())
) -> gleam@order:order().
compare_distance_frontier(A, B, Cmp) ->
Cmp(erlang:element(1, A), erlang:element(1, B)).
-file("src/yog/internal/util.gleam", 220).
?DOC(false).
-spec compare_a_star_frontier(
{EDT, EDT, list(EDU)},
{EDT, EDT, list(EDU)},
fun((EDT, EDT) -> gleam@order:order())
) -> gleam@order:order().
compare_a_star_frontier(A, B, Cmp) ->
Cmp(erlang:element(1, A), erlang:element(1, B)).
-file("src/yog/internal/util.gleam", 229).
?DOC(false).
-spec should_explore_node(
gleam@dict:dict(EDX, EDY),
EDX,
EDY,
fun((EDY, EDY) -> gleam@order:order())
) -> boolean().
should_explore_node(Visited, Node, New_dist, Compare) ->
case gleam_stdlib:map_get(Visited, Node) of
{ok, Prev_dist} ->
case Compare(New_dist, Prev_dist) of
lt ->
true;
_ ->
false
end;
{error, nil} ->
true
end.
-file("src/yog/internal/util.gleam", 254).
?DOC(false).
-spec exit(integer()) -> nil.
exit(Status) ->
erlang:halt(Status).