# SegmentedTrie

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:74](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L74)

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

### V

`V`

## Properties

### kind

> `readonly` **kind**: `"uncompressed"` | `"compressed"`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:76](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L76)

Stable identifier for the implementation’s storage layout.

***

### size

> `readonly` **size**: `number`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:78](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L78)

Number of stored keys. O(1).

***

### has

> **has**: (`segments`) => `boolean`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:80](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L80)

Return `true` when the exact key is stored. O(m) expected.

#### Parameters

##### segments

[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)

#### Returns

`boolean`

***

### get

> **get**: (`segments`) => `V` | `undefined`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:85](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L85)

Return the value stored under the exact key, or `undefined` when it is not stored. O(m) expected.

#### Parameters

##### segments

[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)

#### Returns

`V` | `undefined`

***

### set

> **set**: (`segments`, `value`) => `void`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:93](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L93)

Store `value` under the key, overwriting any value already there without changing `size`. O(m) expected.

#### Parameters

##### segments

[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)

##### value

`V`

#### Returns

`void`

#### Throws

When the key is not an array of strings. Nothing is stored in that case, not even a partial path.

***

### delete

> **delete**: (`segments`) => `boolean`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:102](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L102)

Remove the key and its value. Empty branches are pruned, while prefixes of the removed key stay stored. O(m) expected.

#### Parameters

##### segments

[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)

#### Returns

`boolean`

`true` when a stored key was removed.

#### Throws

When the key is not an array of strings.

***

### longestPrefix

> **longestPrefix**: (`segments`) => [`SegmentedTrieEntry`](/reference/knotkit/trie/interfaces/segmentedtrieentry/)<`V`> | `undefined`

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:112](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L112)

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

##### segments

[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)

#### Returns

[`SegmentedTrieEntry`](/reference/knotkit/trie/interfaces/segmentedtrieentry/)<`V`> | `undefined`

***

### keys

> **keys**: () => `IterableIterator`<[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)>

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:122](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L122)

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

`IterableIterator`<[`SegmentedKey`](/reference/knotkit/trie/type-aliases/segmentedkey/)>

***

### entries

> **entries**: () => `IterableIterator`<[`SegmentedTrieEntry`](/reference/knotkit/trie/interfaces/segmentedtrieentry/)<`V`>>

Defined in: [packages/trie/src/uncompressed-segmented-trie.ts:127](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/trie/src/uncompressed-segmented-trie.ts#L127)

Iterate over every stored entry in the order [keys](/reference/knotkit/trie/interfaces/segmentedtrie/#keys) documents, each with its value. Same complexity and snapshot guarantees as [keys](/reference/knotkit/trie/interfaces/segmentedtrie/#keys).

#### Returns

`IterableIterator`<[`SegmentedTrieEntry`](/reference/knotkit/trie/interfaces/segmentedtrieentry/)<`V`>>
