prefixTree.ts ×19

Frontier kind: Code frontier

unlabeled · c_973d6c66608b

116 tests · 3451 LOC · 19 files · introduces 0 tests · 73 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
19 ranges73 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
499 ranges3451 lines · 19 files · Browse complete extent
All tests (intent)
116 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 73 introduced LOC across 19 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/base/common/prefixTree.ts 73 introduced LOC · 19 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- prefixTree.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { Iterable } from './iterator.js';
7 >
8 > const unset = Symbol('unset');
9 >
10 > export interface IPrefixTreeNode<T> {
11 > /** Possible children of the node. */
12 > children?: ReadonlyMap<string, Node<T>>;
13 >
14 > /** The value if data exists for this node in the tree. Mutable. */
15 > value: T | undefined;
16 > }
17 >
18 > /**
19 > * A simple prefix tree implementation where a value is stored based on
20 > * well-defined prefix segments.
21 > */
22 > export class WellDefinedPrefixTree<V> {
23 public readonly root = new Node<V>();
24 private _size = 0;
26 > /** Tree size, not including the root. */
27 > public get size() {
28 return this._size;
29 }
31 > /** Gets the top-level nodes of the tree */
32 > public get nodes(): Iterable<IPrefixTreeNode<V>> {
33 return this.root.children?.values() || Iterable.empty();
34 }
36 > /** Gets the top-level nodes of the tree */
37 > public get entries(): Iterable<[string, IPrefixTreeNode<V>]> {
38 return this.root.children?.entries() || Iterable.empty();
39 }
41 > /**
42 > * Inserts a new value in the prefix tree.
43 > * @param onNode - called for each node as we descend to the insertion point,
44 > * including the insertion point itself.
45 > */
46 > insert(key: Iterable<string>, value: V, onNode?: (n: IPrefixTreeNode<V>) => void): void {
47 this.opNode(key, n => n._value = value, onNode);
48 }
50 > /** Mutates a value in the prefix tree. */
51 > mutate(key: Iterable<string>, mutate: (value?: V) => V): void {
52 this.opNode(key, n => n._value = mutate(n._value === unset ? undefined : n._value));
53 }
55 > /** Mutates nodes along the path in the prefix tree. */
56 > mutatePath(key: Iterable<string>, mutate: (node: IPrefixTreeNode<V>) => void): void {
57 this.opNode(key, () => { }, n => mutate(n));
58 }
60 > /** Deletes a node from the prefix tree, returning the value it contained. */
61 > delete(key: Iterable<string>): V | undefined {
62 const path = this.getPathToKey(key);
63 if (!path) {
85 return value;
86 }
88 > /** Deletes a subtree from the prefix tree, returning the values they contained. */
89 > *deleteRecursive(key: Iterable<string>): Iterable<V> {
90 const path = this.getPathToKey(key);
91 if (!path) {
118 }
119 }
121 > /** Gets a value from the tree. */
122 > find(key: Iterable<string>): V | undefined {
123 let node = this.root;
124 for (const segment of key) {
133 return node._value === unset ? undefined : node._value;
134 }
136 > /** Gets whether the tree has the key, or a parent of the key, already inserted. */
137 > hasKeyOrParent(key: Iterable<string>): boolean {
138 let node = this.root;
139 for (const segment of key) {
151 return false;
152 }
154 > /** Gets whether the tree has the given key or any children. */
155 > hasKeyOrChildren(key: Iterable<string>): boolean {
156 let node = this.root;
157 for (const segment of key) {
166 return true;
167 }
169 > /** Gets whether the tree has the given key. */
170 > hasKey(key: Iterable<string>): boolean {
171 let node = this.root;
172 for (const segment of key) {
181 return node._value !== unset;
182 }
184 > private getPathToKey(key: Iterable<string>) {
185 const path = [{ part: '', node: this.root }];
186 let i = 0;
197 return path;
198 }
200 > private opNode(key: Iterable<string>, fn: (node: Node<V>) => void, onDescend?: (node: Node<V>) => void): void {
201 let node = this.root;
202 for (const part of key) {
220 this._size += sizeAfter - sizeBefore;
221 }
223 > /** Returns an iterable of the tree values in no defined order. */
224 > *values() {
225 for (const { _value } of bfsIterate(this.root)) {
226 if (_value !== unset) {
229 }
230 }
231 > } prefixTree.ts
232 >
233 function* bfsIterate<T>(root: Node<T>): Iterable<Node<T>> {
234 const stack = [root];
244 }
245 }
247 class Node<T> implements IPrefixTreeNode<T> {
248 public children?: Map<string, Node<T>>;
257
258 public _value: T | typeof unset = unset;
259 > } prefixTree.ts