-module(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(structures@pairing_heap:heap(any())) -> boolean(). is_empty(Queue) -> case structures@pairing_heap:find_min(Queue) of {ok, _} -> false; {error, _} -> true end. -spec new(fun((EZX, EZX) -> gleam@order:order())) -> structures@pairing_heap:heap(EZX). new(Compare) -> structures@pairing_heap:new(Compare). -spec from_list(list(EZQ), fun((EZQ, EZQ) -> gleam@order:order())) -> structures@pairing_heap:heap(EZQ). from_list(List, Compare) -> gleam@list:fold(List, new(Compare), fun structures@pairing_heap:insert/2). -spec pop(structures@pairing_heap:heap(EZZ)) -> {ok, {EZZ, structures@pairing_heap:heap(EZZ)}} | {error, nil}. pop(Queue) -> structures@pairing_heap:delete_min(Queue). -spec peek(structures@pairing_heap:heap(FAE)) -> {ok, FAE} | {error, nil}. peek(Queue) -> structures@pairing_heap:find_min(Queue). -spec push(structures@pairing_heap:heap(FAI), FAI) -> structures@pairing_heap:heap(FAI). push(Queue, Item) -> structures@pairing_heap:insert(Queue, Item). -spec count(structures@pairing_heap:heap(any())) -> integer(). count(Queue) -> case structures@pairing_heap:delete_min(Queue) of {ok, {_, Q}} -> count(Q) + 1; {error, _} -> 0 end. -spec reorder( structures@pairing_heap:heap(FAL), fun((FAL, FAL) -> gleam@order:order()) ) -> structures@pairing_heap:heap(FAL). reorder(Queue, Compare) -> case structures@pairing_heap:delete_min(Queue) of {ok, {X, Q}} -> structures@pairing_heap:insert(reorder(Q, Compare), X); {error, _} -> structures@pairing_heap:new(Compare) end. -spec to_list(structures@pairing_heap:heap(FAO)) -> list(FAO). to_list(Queue) -> case structures@pairing_heap:delete_min(Queue) of {ok, {X, Q}} -> [X | to_list(Q)]; {error, _} -> [] end.