PriorityQueue

lib/PriorityQueue.gob

A queue that pops its least element first: push and pop take O(log n) time, peek O(1), and fromArray builds a queue in O(n).

new orders the elements by their Ord. Tuples compare element by element, so a priority paired with a value is a tuple with the priority first, and Dijkstra's search pushes (distance, node):

let frontier = PriorityQueue.new()
PriorityQueue.push(frontier, (0, start))
while let Some((d, here)) = PriorityQueue.pop(frontier) { ... }

newBy takes the order instead, as Vec.sortBy does; the greatest first is newBy(fun(x, y) = Vec.threeWay(y, x)). Elements the order calls equal pop in no particular order.

There is no way to lower an element's priority in place. Push it again with the new priority, and skip the stale entry when it pops.

type PriorityQueue a
fun new : [Ord a] fun() -> PriorityQueue a
fun newBy : fun(fun(a, a) -> Ordering) -> PriorityQueue a
fun fromArray : [Ord a] fun(Array a) -> PriorityQueue a

Copies xs, which is left as it was.

fun fromArrayBy : fun(Array a, fun(a, a) -> Ordering) -> PriorityQueue a
fun push : fun(PriorityQueue a, a) -> Unit
fun pop : fun(PriorityQueue a) -> Option a

Removes and returns the least element.

fun peek : fun(PriorityQueue a) -> Option a

The least element, left in the queue.

fun isEmpty : fun(PriorityQueue a) -> Bool
fun clear : fun(PriorityQueue a) -> Unit
fun drain : fun(PriorityQueue a) -> Array a

Removes every element and returns them in the order pop would, least first, leaving the queue empty.

fun toArray : fun(PriorityQueue a) -> Array a

The elements in no particular order, copied, leaving the queue as it was.

instance Length (PriorityQueue a)
instance Semigroup (PriorityQueue a)

A new queue holding the elements of both, ordered as p is; q's order is dropped. There is no Monoid: an empty queue would have to choose an order, and as the left operand it would impose it on the right one, so it could not be an identity for a queue made by newBy.

instance Debug (PriorityQueue a) : Debug a

The elements, in no particular order, as a call to fromArray: PriorityQueue.fromArray([x, ...]). That call orders by Ord, so for a queue made by newBy it builds a different queue; the order is a function and does not print.

instance Show (PriorityQueue a) : Debug a

What its Debug prints: a container's text shows its elements' Debug, so that a string in it is quoted.