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.
Lazily merge a finite collection of independently iterable sorted sources under the same total order.
Keeps duplicates. Equal items retain their order within each source, with earlier sources winning ties.
Supply a stable order and keep compared values unchanged while the merge is active.
The first advance calls next() once on each source. Each subsequent advance pulls only from the source
of the last emitted value. With k sources, setup takes O(k) time and space, and each output after setup
takes O(log(k + 1)) time, excluding input reads and comparator cost. Individual sources may be infinite.
On early return or failure, calls return() once on each opened, unfinished iterator that exposes it.
Errors from the order and input iterators propagate. A cleanup error surfaces if the merge was otherwise
returning normally. When several iterators fail during cleanup, the first error surfaces after all are tried.
Lazily merge a finite collection of independently iterable sorted sources under the same total order.
Keeps duplicates. Equal items retain their order within each source, with earlier sources winning ties.
Supply a stable order and keep compared values unchanged while the merge is active.
The first advance calls next() once on each source. Each subsequent advance pulls only from the source
of the last emitted value. With k sources, setup takes O(k) time and space, and each output after setup
takes O(log(k + 1)) time, excluding input reads and comparator cost. Individual sources may be infinite.
On early return or failure, calls return() once on each opened, unfinished iterator that exposes it.
Errors from the order and input iterators propagate. A cleanup error surfaces if the merge was otherwise
returning normally. When several iterators fail during cleanup, the first error surfaces after all are tried.
mergeSorted(
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,
constshards:number[][]
shards)) {
constfirstFive:number[]
firstFive.
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(
constid:number
id)
if (
constfirstFive:number[]
firstFive.
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===5) {
break// closes every shard; the rest is never read
}
}
constfirstFive:number[]
firstFive// [1, 2, 3, 4, 5]
import {
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.
Lazily merge a finite collection of independently iterable sorted sources under the same total order.
Keeps duplicates. Equal items retain their order within each source, with earlier sources winning ties.
Supply a stable order and keep compared values unchanged while the merge is active.
The first advance calls next() once on each source. Each subsequent advance pulls only from the source
of the last emitted value. With k sources, setup takes O(k) time and space, and each output after setup
takes O(log(k + 1)) time, excluding input reads and comparator cost. Individual sources may be infinite.
On early return or failure, calls return() once on each opened, unfinished iterator that exposes it.
Errors from the order and input iterators propagate. A cleanup error surfaces if the merge was otherwise
returning normally. When several iterators fail during cleanup, the first error surfaces after all are tried.
mergeSorted } from'knotkit/sorted-merge'
let
let reads:number
reads=0
// 16 shards holding the ids 0 to 65,535 between them, each shard already sorted.
Lazily merge a finite collection of independently iterable sorted sources under the same total order.
Keeps duplicates. Equal items retain their order within each source, with earlier sources winning ties.
Supply a stable order and keep compared values unchanged while the merge is active.
The first advance calls next() once on each source. Each subsequent advance pulls only from the source
of the last emitted value. With k sources, setup takes O(k) time and space, and each output after setup
takes O(log(k + 1)) time, excluding input reads and comparator cost. Individual sources may be infinite.
On early return or failure, calls return() once on each opened, unfinished iterator that exposes it.
Errors from the order and input iterators propagate. A cleanup error surfaces if the merge was otherwise
returning normally. When several iterators fail during cleanup, the first error surfaces after all are tried.
mergeSorted(
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,
constshards:Generator<number, any, any>[]
shards)) {
constpage:number[]
page.
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(
constid:number
id)
if (
constpage:number[]
page.
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===32) {
break// closes every shard
}
}
constpage:number[]
page.
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// 32
constpage:number[]
page.
Array<number>.at(index: number): number |undefined
Returns the item located at the specified index.
@param ― index The zero-based index of the desired code unit. A negative index will count back from the last item.
at(-1) // 31, the last id a 32-row page needs
let reads:number
reads// 47, out of the 65,536 ids the shards hold
The merge reads one item per input to start, then only from the input it last took from. Breaking out of the loop closes every input, so a page of results costs the page, not the whole input.