@param ― order - Total ordering over the elements.
@param ― items - Optional initial elements, in iteration order.
minBinaryHeap,
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.
@param ― order - Total ordering over the elements.
@param ― items - Optional initial elements, in iteration order.
minBinaryHeap(
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)
for (const
constlatency:number
latencyof [120, 45, 300, 87, 210, 95, 410]) {
if (
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.size: number
Number of elements. O(1). The heap is empty when this is 0.
size<
constk:3
k) {
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.push: (value:number) =>void
Insert an element and restore the heap invariant. O(log n).
Declared in property syntax on purpose: method-syntax parameters compare
bivariantly, which would let BinaryHeap<Dog> widen into
Heap<Animal> through the union's apparent members and defeat the
invariance in out A exists to enforce. Property syntax keeps the
parameter contravariant under strictFunctionTypes. Every callable
member is declared the same way, so the interface reads uniformly.
push(
constlatency:number
latency)
} elseif (
constlatency:number
latency>
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.peek: () => number
Return the extreme element without removing it. O(1).
Property syntax for the same variance reason as
push
.
@throws ― {HeapInvalidInputError} With code EMPTY when the heap is empty.
peek()) {
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.replaceRoot: (value:number) => number
Return the current root and replace it with the supplied value, preserving size. O(log n).
@throws ― {HeapInvalidInputError} With code EMPTY when the heap is empty.
replaceRoot(
constlatency:number
latency)
}
}
const
consttop:number[]
top:number[] = []
while (
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.size: number
Number of elements. O(1). The heap is empty when this is 0.
size>0) {
consttop:number[]
top.
Array<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@param ― items New elements to add to the array.
push(
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.pop: () => number
Remove and return the extreme element — the minimum for min kinds, the
maximum for max kinds, under the factory order. O(log n).
Property syntax for the same variance reason as
push
.
@throws ― {HeapInvalidInputError} With code EMPTY when the heap is empty.
@param ― order - Total ordering over the elements.
@param ― items - Optional initial elements, in iteration order.
minBinaryHeap,
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
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
uniformInt } from'knotkit/prng'
const
constk:3
k=3
const
constrandom: () =>number
random=
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
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
@param ― start The zero-based location in the array from which to start removing elements.
@param ― deleteCount The number of elements to remove. If value of this argument is either a negative number, zero,
undefined, or a type that cannot be converted to an integer, the function will evaluate the argument as zero and
not remove any elements.
@param ― items Elements to insert into the array in place of the deleted elements.
@returns ― An array containing the elements that were deleted.
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
uniformInt(
constrandom: () =>number
random,
constlatencies:number[]
latencies.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length), 0, 1_500)
constlatencies:number[]
latencies.
Array<number>.indexOf(searchElement: number, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
@param ― order - Total ordering over the elements.
@param ― items - Optional initial elements, in iteration order.
minBinaryHeap(
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)
for (const
constlatency:number
latencyof
constlatencies:number[]
latencies) {
if (
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.size: number
Number of elements. O(1). The heap is empty when this is 0.
size<
constk:3
k) {
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.push: (value:number) =>void
Insert an element and restore the heap invariant. O(log n).
Declared in property syntax on purpose: method-syntax parameters compare
bivariantly, which would let BinaryHeap<Dog> widen into
Heap<Animal> through the union's apparent members and defeat the
invariance in out A exists to enforce. Property syntax keeps the
parameter contravariant under strictFunctionTypes. Every callable
member is declared the same way, so the interface reads uniformly.
push(
constlatency:number
latency)
} elseif (
constlatency:number
latency>
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.peek: () => number
Return the extreme element without removing it. O(1).
Property syntax for the same variance reason as
push
.
@throws ― {HeapInvalidInputError} With code EMPTY when the heap is empty.
peek()) {
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.replaceRoot: (value:number) => number
Return the current root and replace it with the supplied value, preserving size. O(log n).
@throws ― {HeapInvalidInputError} With code EMPTY when the heap is empty.
replaceRoot(
constlatency:number
latency)
}
}
const
consttop:number[]
top:number[] = []
while (
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.size: number
Number of elements. O(1). The heap is empty when this is 0.
size>0) {
consttop:number[]
top.
Array<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@param ― items New elements to add to the array.
push(
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.pop: () => number
Remove and return the extreme element — the minimum for min kinds, the
maximum for max kinds, under the factory order. O(log n).
Property syntax for the same variance reason as
push
.
@throws ― {HeapInvalidInputError} With code EMPTY when the heap is empty.
pop())
}
// The heap held three values through 20,001 arrivals.
consttop:number[]
top// [1000, 1000, 1500]
consttop:number[]
top[2] // 1500, the outlier survived every replacement
constlargest:BinaryHeap<number>
largest.
BinaryHeap<number>.size: number
Number of elements. O(1). The heap is empty when this is 0.
size// 0
Each value costs one comparison against the root and, when it is admitted, one sift of O(log k). Memory stays at k values however long the stream runs.