Skip to content

Read the first n items of several sorted lists

Merge sorted inputs lazily, and stop as soon as you have enough.

import {
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'
import {
function mergeSorted<A>(order: Order<A>, sources: Iterable<Iterable<A>>): Generator<A, void, unknown>

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'
const
const shards: number[][]
shards
= [
[1, 4, 9, 12],
[2, 3, 10],
[5, 6, 7, 8],
]
const
const firstFive: number[]
firstFive
: number[] = []
for (const
const id: number
id
of
mergeSorted<number>(order: Order<number>, sources: Iterable<Iterable<number>>): Generator<number, void, unknown>

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
(
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
,
const shards: number[][]
shards
)) {
const firstFive: 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
(
const id: number
id
)
if (
const firstFive: 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
}
}
const firstFive: number[]
firstFive
// [1, 2, 3, 4, 5]

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.

Modules: knotkit/sorted-merge · knotkit/heap · API reference