@throws ― {HeapInvalidInputError} With code DUPLICATE_VALUE when entries
carries one value twice.
minPriorityQueue,
constnumberOrder: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.
@throws ― {HeapInvalidInputError} With code DUPLICATE_VALUE when entries
carries one value twice.
minPriorityQueue(
constnumberOrder: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.
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
@throws ― {HeapInvalidInputError} With code DUPLICATE_VALUE when entries
carries one value twice.
minPriorityQueue,
constnumberOrder: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'
import {
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
@throws ― {HeapInvalidInputError} With code DUPLICATE_VALUE when entries
carries one value twice.
minPriorityQueue(
constnumberOrder: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.
Return a uniformly shuffled copy of values using Fisher-Yates.
Runs in O(values.length) time and uses O(values.length) space for the copy.
The input is never mutated.
@throws ― {PrngInvalidInputError} With code INVALID_SOURCE_VALUE when the
random source returns a value outside [0, 1).
shuffled(
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
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, 'task-7777') // a user is now waiting on task 7777
constqueue:BinaryPriorityQueue<number, string>
queue.
PriorityQueueSurface<number, string>.size: number
Number of entries. O(1). The queue is empty when this is 0.
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() // 'task-7777'
constqueue:BinaryPriorityQueue<number, string>
queue.
PriorityQueueSurface<number, string>.size: number
Number of entries. O(1). The queue is empty when this is 0.
size// 9999
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).