-module(gleamy@priority_queue). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new/1, pop/1, peek/1, push/2, is_empty/1, count/1, reorder/2, from_list/2, to_list/1]). -spec new(fun((GVM, GVM) -> gleam@order:order())) -> gleamy@pairing_heap:heap(GVM). new(Compare) -> gleamy@pairing_heap:new(Compare). -spec pop(gleamy@pairing_heap:heap(GVO)) -> {ok, {GVO, gleamy@pairing_heap:heap(GVO)}} | {error, nil}. pop(Queue) -> gleamy@pairing_heap:delete_min(Queue). -spec peek(gleamy@pairing_heap:heap(GVT)) -> {ok, GVT} | {error, nil}. peek(Queue) -> gleamy@pairing_heap:find_min(Queue). -spec push(gleamy@pairing_heap:heap(GVX), GVX) -> gleamy@pairing_heap:heap(GVX). push(Queue, Item) -> gleamy@pairing_heap:insert(Queue, Item). -spec is_empty(gleamy@pairing_heap:heap(any())) -> boolean(). is_empty(Queue) -> case gleamy@pairing_heap:find_min(Queue) of {ok, _} -> false; {error, _} -> true end. -spec count(gleamy@pairing_heap:heap(any())) -> integer(). count(Queue) -> case gleamy@pairing_heap:delete_min(Queue) of {ok, {_, Q}} -> count(Q) + 1; {error, _} -> 0 end. -spec reorder( gleamy@pairing_heap:heap(GWE), fun((GWE, GWE) -> gleam@order:order()) ) -> gleamy@pairing_heap:heap(GWE). reorder(Queue, Compare) -> case gleamy@pairing_heap:delete_min(Queue) of {ok, {X, Q}} -> gleamy@pairing_heap:insert(reorder(Q, Compare), X); {error, _} -> gleamy@pairing_heap:new(Compare) end. -spec from_list(list(GWH), fun((GWH, GWH) -> gleam@order:order())) -> gleamy@pairing_heap:heap(GWH). from_list(List, Compare) -> gleam@list:fold(List, new(Compare), fun gleamy@pairing_heap:insert/2). -spec to_list(gleamy@pairing_heap:heap(GWK)) -> list(GWK). to_list(Queue) -> case gleamy@pairing_heap:delete_min(Queue) of {ok, {X, Q}} -> [X | to_list(Q)]; {error, _} -> [] end.