priorityq

Types

A priority queue implemented with a max pairing heap.

pub opaque type PriorityQueue(t)

Functions

pub fn from_list(
  ls: List(a),
  cmp: fn(a, a) -> Order,
) -> PriorityQueue(a)

Creates a priority queue from a list.

Runs in linear time.

Examples

import gleam/int

from_list([1, 10, 5], int.compare) // -> PriorityQueue(Int)
pub fn is_empty(pq: PriorityQueue(a)) -> Bool

Returns whether the priority queue is empty.

Runs in constant time.

Examples

import gleam/int

new(int.compare) |> is_empty // -> True
from_list([0], int.compare) |> is_empty // -> False
pub fn new(cmp: fn(a, a) -> Order) -> PriorityQueue(a)

Creates an empty priority queue.

Examples

import gleam/int

new(int.compare) // -> PriorityQueue(Int)
pub fn peek(pq: PriorityQueue(a)) -> Option(a)

Returns the maximum value in the priority queue.

Examples

import gleam/int

new(int.compare) |> peek // -> None
from_list([1, 10, 5], int.compare) |> peek // -> 10
pub fn pop(pq: PriorityQueue(a)) -> PriorityQueue(a)

Pops the maximum value from the priority queue.

Runs in amortized logarithmic time.

Examples

import gleam/int

from_list([0], int.compare) |> pop // -> PriorityQueue(Int)
pub fn push(pq: PriorityQueue(a), val: a) -> PriorityQueue(a)

Pushes a value into the priority queue.

Runs in constant time.

Examples

import gleam/int

new(int.compare) |> push(10) // -> PriorityQueue(Int)
pub fn size(pq: PriorityQueue(a)) -> Int

Returns the number of elements in the priority queue.

Runs in constant time.

Examples

import gleam/int

from_list([1, 2, 3], int.compare) |> size // -> 3
Search Document