Create an interval index backed by a red-black tree keyed by (start, id), each node augmented with the largest
end in its subtree, plus a map from id to node.
add and delete take O(log n) time and has and get O(1). atPoint and overlapping reporting k entries
take O(min(n, (k + 1) log n)) time: a subtree is skipped when its largest end sorts before the query start, and the
walk stops at the first start past the query end. A full walk takes O(n). Space is O(n).
@throws ― {IntervalIndexInvalidInputError} With INVALID_INTERVAL when an initial entry's start sorts after its end.
@see ― Cormen , Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd ed., section 14.3, "Interval trees".
Compare two byte strings in unsigned lexicographic order, the order an encoded key sorts in.
A proper prefix sorts before every longer string that extends it. The result is -1, 0, or 1, so the function
is an Order<Uint8Array> for knotkit/heap, knotkit/ordered-map, knotkit/sorted-merge, or Array.prototype.sort.
Runs in O(min(left.byteLength, right.byteLength)) and does not validate its arguments, because it sits on the hot
path of every comparison-based structure; pass Uint8Array values.
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Uint8Array=>new
var TextEncoder:new () =>TextEncoder
The TextEncoder interface takes a stream of code points as input and emits a stream of UTF-8 bytes.
The TextEncoder.encode() method takes a string as input, and returns a Global_Objects/Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.
Create an interval index backed by a red-black tree keyed by (start, id), each node augmented with the largest
end in its subtree, plus a map from id to node.
add and delete take O(log n) time and has and get O(1). atPoint and overlapping reporting k entries
take O(min(n, (k + 1) log n)) time: a subtree is skipped when its largest end sorts before the query start, and the
walk stops at the first start past the query end. A full walk takes O(n). Space is O(n).
@throws ― {IntervalIndexInvalidInputError} With INVALID_INTERVAL when an initial entry's start sorts after its end.
@see ― Cormen , Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd ed., section 14.3, "Interval trees".
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Compare two byte strings in unsigned lexicographic order, the order an encoded key sorts in.
A proper prefix sorts before every longer string that extends it. The result is -1, 0, or 1, so the function
is an Order<Uint8Array> for knotkit/heap, knotkit/ordered-map, knotkit/sorted-merge, or Array.prototype.sort.
Runs in O(min(left.byteLength, right.byteLength)) and does not validate its arguments, because it sits on the hot
path of every comparison-based structure; pass Uint8Array values.
IntervalIndex<Uint8Array<ArrayBufferLike>, string>.add: (start:Uint8Array<ArrayBufferLike>, end:Uint8Array<ArrayBufferLike>, value:string) => number
Store the closed interval [start, end] and return its id: a nonnegative integer, larger than every id this index
returned before, never reused, including after clear. Initial entries take ids 0, 1, … in input order.
@throws ― {IntervalIndexInvalidInputError} With INVALID_INTERVAL when start sorts after end.
IntervalIndex<Uint8Array<ArrayBufferLike>, string>.add: (start:Uint8Array<ArrayBufferLike>, end:Uint8Array<ArrayBufferLike>, value:string) => number
Store the closed interval [start, end] and return its id: a nonnegative integer, larger than every id this index
returned before, never reused, including after clear. Initial entries take ids 0, 1, … in input order.
@throws ― {IntervalIndexInvalidInputError} With INVALID_INTERVAL when start sorts after end.
IntervalIndex<Uint8Array<ArrayBufferLike>, string>.add: (start:Uint8Array<ArrayBufferLike>, end:Uint8Array<ArrayBufferLike>, value:string) => number
Store the closed interval [start, end] and return its id: a nonnegative integer, larger than every id this index
returned before, never reused, including after clear. Initial entries take ids 0, 1, … in input order.
@throws ― {IntervalIndexInvalidInputError} With INVALID_INTERVAL when start sorts after end.
add(
constkey: (text:string) =>Uint8Array
key('fig'),
constkey: (text:string) =>Uint8Array
key('grape'), 'sst-3')
// A point read probes every table whose fences hold the key, oldest start first.
Walk every entry whose interval holds point: start <= point <= end under the order.
@throws ― {IntervalIndexInvalidInputError} With MODIFIED_DURING_ITERATION on advancement after an add, a
successful delete, or a clear of a nonempty index.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Walk every entry whose interval shares at least one point with the closed range [start, end]:
entry.start <= end and start <= entry.end. Touching endpoints overlap. The range is checked when the call is
made, not on the first step.
@throws ― {IntervalIndexInvalidInputError} With INVALID_INTERVAL when start sorts after end, or
MODIFIED_DURING_ITERATION on advancement after an add, a successful delete, or a clear of a nonempty index.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
intervals are added and deleted dynamically (such as L0 tables arriving on flush and leaving on compaction) and n exceeds a few hundred
one tree node with three links, a color, and a subtree maximum end per entry; 4.1 ms against sorted-array's 39.5 ms for 1,000 rounds of one flush, four point reads, and a compaction every fourth round over 1,000 tables, and 1.6 to 1.7 times slower than sorted-array on static queries over 1,000 to 10,000 tables
sortedArrayIntervalIndex
kind sorted-array
the interval set is built once or changes infrequently relative to queries (such as immutable LSM level manifests)
two array slots per entry; 0.43 ms for 1,000 point queries over 10,000 tables against 0.72 ms for augmented-red-black-tree, but every query after an out-of-order add or a deletion first pays an O(n log n) or O(n) rebuild, and a single long interval near the start can make a query scan every entry after it
Red-black tree keyed by (start, id) with each node's subtree maximum end, maintained on every insertion, deletion, and rotation, plus a map from id to node.
One array of entries sorted by (start, id) with a parallel running maximum of end, rebuilt by the first query after an out-of-order add or a deletion, plus a map from id to entry.
Cost per member. Open a row for every operation.
augmentedRedBlackIntervalIndex10 members
Member
Cost
build
O(n log n)
size
O(1)
add
O(log n)
delete
O(log n)
has
O(1)
get
O(1)
atPoint
O(min(n, (k + 1) log n)) for k entries reported
overlapping
O(min(n, (k + 1) log n)) for k entries reported
entries
O(n)
clear
O(1)
sortedArrayIntervalIndex10 members
Member
Cost
build
O(n log n)
size
O(1)
add
O(1) amortized; an add whose start sorts before the last stored start defers an O(n log n) rebuild to the next query
delete
O(1) amortized, O(n) when half the entries are deleted; nonempty queries rebuild prefix maxima in O(n) after deletions alone
has
O(1)
get
O(1)
atPoint
O(log n + m) once settled, where m counts entries from the first whose running maximum end reaches the query start to the last starting at or before the query end; k <= m <= n
overlapping
O(log n + m) once settled, where m counts entries from the first whose running maximum end reaches the query start to the last starting at or before the query end; k <= m <= n
Every error this module throws is IntervalIndexInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.
Measured by the suite in packages/interval-index/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.
Use knotkit/interval-index for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/interval-index'.
Exports:
- `augmentedRedBlackIntervalIndex` → `IntervalIndex<K, V>`: Red-black tree keyed by (start, id) with each node's subtree maximum end, maintained on every insertion, deletion, and rotation, plus a map from id to node.
- `sortedArrayIntervalIndex` → `IntervalIndex<K, V>`: One array of entries sorted by (start, id) with a parallel running maximum of end, rebuilt by the first query after an out-of-order add or a deletion, plus a map from id to entry.
Pick the layout by these rules:
- `augmentedRedBlackIntervalIndex` (kind 'augmented-red-black-tree') when intervals are added and deleted dynamically (such as L0 tables arriving on flush and leaving on compaction) and n exceeds a few hundred
- `sortedArrayIntervalIndex` (kind 'sorted-array') when the interval set is built once or changes infrequently relative to queries (such as immutable LSM level manifests)
Inputs and their sources:
- key order: `Order<K>` from `knotkit/heap`
Use something else when:
- look up, rank, or scan single keys rather than ranges → knotkit/ordered-map
- membership or range counts over unsigned 32-bit integer ids → knotkit/bitset
- a compact immutable block of sorted byte-keyed entries with seeks → knotkit/sorted-block
- the longest stored prefix of a delimiter-separated key → knotkit/trie
- overlap queries over rectangles or boxes in two or more dimensions → an R-tree library such as rbush
Errors are IntervalIndexInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/interval-index.md
Types: node_modules/knotkit/interval-index/ and https://knotkit.jkomyno.dev/reference/knotkit/interval-index/