Skip to content

Change the priority of queued work

An indexed priority queue finds a queued item by its value and moves it in place.

import {
function minPriorityQueue<K, V>(keyOrder: Order<K>, entries?: Iterable<readonly [K, V]>): BinaryPriorityQueue<K, V>

Create a min priority queue over a binary heap: the entry with the smallest key per keyOrder is at the root, and pop extracts in ascending key order.

@example

const pq = minPriorityQueue(numberOrder, [[5, 'a'], [1, 'b'], [3, 'c']])
pq.peek() // 'b'
pq.peekEntry() // [1, 'b']

@param ― keyOrder - Total ordering over the keys.

@param ― entries - Optional initial [key, value] pairs, in iteration order.

@throws ― {HeapInvalidInputError} With code DUPLICATE_VALUE when entries carries one value twice.

minPriorityQueue
,
const numberOrder: Order<number>

A total order on number.

NaN is totally ordered as greater than every other number and equal only to itself; 0 equals -0. Making the order total, rather than returning 0 for every NaN comparison the way </> do, is what lets heaps containing NaN keep a coherent invariant: with the partial rule, NaN would compare equal to both 1 and 3 while 1 < 3, and a sift could strand a smaller number behind it. NaN therefore drains last from a min heap and first from a max heap.

Under this order -Infinity is the least element and NaN the greatest, so the full chain reads -Infinity < -0 = 0 < 1 < Infinity < NaN.

numberOrder
} from 'knotkit/heap'
// [priority, task]: the lowest priority runs first.
const
const queue: BinaryPriorityQueue<number, string>
queue
=
minPriorityQueue<number, string>(keyOrder: Order<number>, entries?: Iterable<readonly [number, string]> | undefined): BinaryPriorityQueue<number, string>

Create a min priority queue over a binary heap: the entry with the smallest key per keyOrder is at the root, and pop extracts in ascending key order.

@example

const pq = minPriorityQueue(numberOrder, [[5, 'a'], [1, 'b'], [3, 'c']])
pq.peek() // 'b'
pq.peekEntry() // [1, 'b']

@param ― keyOrder - Total ordering over the keys.

@param ― entries - Optional initial [key, value] pairs, in iteration order.

@throws ― {HeapInvalidInputError} With code DUPLICATE_VALUE when entries carries one value twice.

minPriorityQueue
(
const numberOrder: Order<number>

A total order on number.

NaN is totally ordered as greater than every other number and equal only to itself; 0 equals -0. Making the order total, rather than returning 0 for every NaN comparison the way </> do, is what lets heaps containing NaN keep a coherent invariant: with the partial rule, NaN would compare equal to both 1 and 3 while 1 < 3, and a sift could strand a smaller number behind it. NaN therefore drains last from a min heap and first from a max heap.

Under this order -Infinity is the least element and NaN the greatest, so the full chain reads -Infinity < -0 = 0 < 1 < Infinity < NaN.

numberOrder
, [
[5, 'render'],
[1, 'parse'],
[3, 'layout'],
])
const queue: BinaryPriorityQueue<number, string>
queue
.
PriorityQueueSurface<number, string>.updateKey: (key: number, value: string) => void

Replace the key of a resident value and restore the heap invariant in the one direction the change requires. O(log n) binary, O(k log_k n) k-ary; O(1) when the new key compares equal to the old, in which case nothing moves and only the stored key (the whole entry object) is replaced.

Property syntax for the same variance reason as

push

.

@throws ― {HeapInvalidInputError} With code VALUE_NOT_FOUND when value is not resident.

updateKey
(0, 'layout') // a user is now waiting on layout
const queue: BinaryPriorityQueue<number, string>
queue
.
PriorityQueueSurface<number, string>.pop: () => string

Remove and return the value of the extreme entry. O(log n) binary, O(k log_k n) k-ary.

Property syntax for the same variance reason as

push

.

@throws ― {HeapInvalidInputError} With code EMPTY when the queue is empty.

pop
() // 'layout'
const queue: BinaryPriorityQueue<number, string>
queue
.
PriorityQueueSurface<number, string>.pop: () => string

Remove and return the value of the extreme entry. O(log n) binary, O(k log_k n) k-ary.

Property syntax for the same variance reason as

push

.

@throws ― {HeapInvalidInputError} With code EMPTY when the queue is empty.

pop
() // 'parse'
const queue: BinaryPriorityQueue<number, string>
queue
.
PriorityQueueSurface<number, string>.size: number

Number of entries. O(1). The queue is empty when this is 0.

size
// 1

Use an indexed queue when jobs can change priority while they wait. The first key lookup or update builds the index in O(n). After that, a binary queue finds an entry by value and changes its priority in O(log n).

Modules: knotkit/heap · API reference