Defined in: packages/trie/src/uncompressed-segmented-trie.ts:74
A prefix tree keyed on segment sequences rather than characters, mapping each stored key to a value and resolving queries by longest prefix.
Keys and queries are arrays of strings, the tokens a caller gets from
splitting a delimiter-separated string such as a slug or a dotted path.
Segments compare by exact string equality, so 'Google' and 'google' are
different tokens and the empty string is a legal one, as produced by
splitting on consecutive delimiters. The empty key [] is legal too and
makes longestPrefix answer every query, which suits default entries.
The structure is built once from a known key set and queried many times,
which is the workload longestPrefix exists for: one O(m) walk replaces the
O(m²) scan that repeatedly slices and rejoins ever-shorter prefixes to test
them against a Set.
All operations mutate in place and run in O(m) expected time for a key or
query of m segments. Lookups allocate only the arrays they return. How
keys share structure along a common prefix is each layout’s choice, which
its factory documents.
Mutating methods validate their key and throw on a non-array key or a non-string segment. Reading methods treat such input as a miss instead, because an invalid query cannot corrupt anything. Neither check costs more than the walk itself.
Type Parameters
Section titled “Type Parameters”V
Properties
Section titled “Properties”
readonlykind:"uncompressed"|"compressed"
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:76
Stable identifier for the implementation’s storage layout.
readonlysize:number
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:78
Number of stored keys. O(1).
has: (
segments) =>boolean
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:80
Return true when the exact key is stored. O(m) expected.
Parameters
Section titled “Parameters”segments
Section titled “segments”Returns
Section titled “Returns”boolean
get: (
segments) =>V|undefined
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:85
Return the value stored under the exact key, or undefined when it is
not stored. O(m) expected.
Parameters
Section titled “Parameters”segments
Section titled “segments”Returns
Section titled “Returns”V | undefined
set: (
segments,value) =>void
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:93
Store value under the key, overwriting any value already there without
changing size. O(m) expected.
Parameters
Section titled “Parameters”segments
Section titled “segments”V
Returns
Section titled “Returns”void
Throws
Section titled “Throws”When the key is not an array of strings. Nothing is stored in that case, not even a partial path.
delete
Section titled “delete”delete: (
segments) =>boolean
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:102
Remove the key and its value. Empty branches are pruned, while prefixes of the removed key stay stored. O(m) expected.
Parameters
Section titled “Parameters”segments
Section titled “segments”Returns
Section titled “Returns”boolean
true when a stored key was removed.
Throws
Section titled “Throws”When the key is not an array of strings.
longestPrefix
Section titled “longestPrefix”longestPrefix: (
segments) =>SegmentedTrieEntry<V> |undefined
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:112
Resolve the longest stored key that is a segment-wise prefix of the query, including the query itself and the empty key. O(m) expected.
The walk stops at the first missing segment, because no deeper key can
prefix the query. Returns the matched
key as a fresh prefix of the query array together with its value, or
undefined when no stored key prefixes the query.
Parameters
Section titled “Parameters”segments
Section titled “segments”Returns
Section titled “Returns”SegmentedTrieEntry<V> | undefined
keys: () =>
IterableIterator<SegmentedKey>
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:122
Iterate over every stored key, depth-first with each node’s children in insertion order, so a key precedes its extensions. O(k) total for k stored segments, plus O(n) for the n keys collected.
The keys present when the call was made are collected before the first step, so mutating the trie while iterating neither extends nor disturbs the walk. Each yielded array is fresh and safe to keep.
Returns
Section titled “Returns”IterableIterator<SegmentedKey>
entries
Section titled “entries”entries: () =>
IterableIterator<SegmentedTrieEntry<V>>
Defined in: packages/trie/src/uncompressed-segmented-trie.ts:127
Iterate over every stored entry in the order keys documents, each with its value. Same complexity and snapshot guarantees as keys.
Returns
Section titled “Returns”IterableIterator<SegmentedTrieEntry<V>>