Current section
Files
Jump to
Current section
Files
src/gleamy@structures@priority_queue.erl
-module(gleamy@structures@priority_queue).
-compile([no_auto_import, nowarn_unused_vars]).
-export([is_empty/1, new/1, from_list/2, pop/1, peek/1, push/2, count/1, reorder/2, to_list/1]).
-spec is_empty(gleamy@structures@heap@pairing_heap:heap(any())) -> boolean().
is_empty(Queue) ->
case gleamy@structures@heap@pairing_heap:find_min(Queue) of
{ok, _} ->
false;
{error, _} ->
true
end.
-spec new(fun((GQE, GQE) -> gleam@order:order())) -> gleamy@structures@heap@pairing_heap:heap(GQE).
new(Compare) ->
gleamy@structures@heap@pairing_heap:new(Compare).
-spec from_list(list(GPX), fun((GPX, GPX) -> gleam@order:order())) -> gleamy@structures@heap@pairing_heap:heap(GPX).
from_list(List, Compare) ->
gleam@list:fold(
List,
new(Compare),
fun gleamy@structures@heap@pairing_heap:insert/2
).
-spec pop(gleamy@structures@heap@pairing_heap:heap(GQG)) -> {ok,
{GQG, gleamy@structures@heap@pairing_heap:heap(GQG)}} |
{error, nil}.
pop(Queue) ->
gleamy@structures@heap@pairing_heap:delete_min(Queue).
-spec peek(gleamy@structures@heap@pairing_heap:heap(GQL)) -> {ok, GQL} |
{error, nil}.
peek(Queue) ->
gleamy@structures@heap@pairing_heap:find_min(Queue).
-spec push(gleamy@structures@heap@pairing_heap:heap(GQP), GQP) -> gleamy@structures@heap@pairing_heap:heap(GQP).
push(Queue, Item) ->
gleamy@structures@heap@pairing_heap:insert(Queue, Item).
-spec count(gleamy@structures@heap@pairing_heap:heap(any())) -> integer().
count(Queue) ->
case gleamy@structures@heap@pairing_heap:delete_min(Queue) of
{ok, {_, Q}} ->
count(Q) + 1;
{error, _} ->
0
end.
-spec reorder(
gleamy@structures@heap@pairing_heap:heap(GQS),
fun((GQS, GQS) -> gleam@order:order())
) -> gleamy@structures@heap@pairing_heap:heap(GQS).
reorder(Queue, Compare) ->
case gleamy@structures@heap@pairing_heap:delete_min(Queue) of
{ok, {X, Q}} ->
gleamy@structures@heap@pairing_heap:insert(reorder(Q, Compare), X);
{error, _} ->
gleamy@structures@heap@pairing_heap:new(Compare)
end.
-spec to_list(gleamy@structures@heap@pairing_heap:heap(GQV)) -> list(GQV).
to_list(Queue) ->
case gleamy@structures@heap@pairing_heap:delete_min(Queue) of
{ok, {X, Q}} ->
[X | to_list(Q)];
{error, _} ->
[]
end.