src/vs/base/common/prefixTree.ts
259 LOC · 251 covered · 8 uncovered · 61 ranges · 184 concepts · 26 introducers · 116 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
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 related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
prefixTree.ts ×19
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Iterable } from './iterator.js';
const unset = Symbol('unset');
export interface IPrefixTreeNode<T> {
/** Possible children of the node. */
children?: ReadonlyMap<string, Node<T>>;
/** The value if data exists for this node in the tree. Mutable. */
value: T | undefined;
}
/**
* A simple prefix tree implementation where a value is stored based on
* well-defined prefix segments.
*/
export class WellDefinedPrefixTree<V> {
private _size = 0;
/** Tree size, not including the root. */
public get size() {
}
/** Gets the top-level nodes of the tree */
public get nodes(): Iterable<IPrefixTreeNode<V>> {
return this.root.children?.values() || Iterable.empty();
}
/** Gets the top-level nodes of the tree */
public get entries(): Iterable<[string, IPrefixTreeNode<V>]> {
return this.root.children?.entries() || Iterable.empty();
}
/**
* Inserts a new value in the prefix tree.
* @param onNode - called for each node as we descend to the insertion point,
* including the insertion point itself.
*/
insert(key: Iterable<string>, value: V, onNode?: (n: IPrefixTreeNode<V>) => void): void {
}
/** Mutates a value in the prefix tree. */
mutate(key: Iterable<string>, mutate: (value?: V) => V): void {
this.opNode(key, n => n._value = mutate(n._value === unset ? undefined : n._value));
prefixTree.ts ×1
}
/** Mutates nodes along the path in the prefix tree. */
mutatePath(key: Iterable<string>, mutate: (node: IPrefixTreeNode<V>) => void): void {
this.opNode(key, () => { }, n => mutate(n));
}
/** Deletes a node from the prefix tree, returning the value it contained. */
delete(key: Iterable<string>): V | undefined {
if (!path) {
}
let i = path.length - 1;
const value = path[i].node._value;
if (value === unset) {
}
this._size--;
path[i].node._value = unset;
for (; i > 0; i--) {
if (node.children?.size || node._value !== unset) {
break;
}
path[i - 1].node.children!.delete(part);
}
return value;
/** Deletes a subtree from the prefix tree, returning the values they contained. */
*deleteRecursive(key: Iterable<string>): Iterable<V> {
if (!path) {
}
const subtree = path[path.length - 1].node;
// important: run the deletion before we start to yield results, so that
// it still runs even if the caller doesn't consumer the iterator
for (let i = path.length - 1; i > 0; i--) {
parent.node.children!.delete(path[i].part);
if (parent.node.children!.size > 0 || parent.node._value !== unset) {
break;
}
}
for (const node of bfsIterate(subtree)) {
if (node._value !== unset) {
this._size--;
yield node._value;
}
}
// special case for the root note
if (subtree === this.root) {
this.root.children = undefined;
}
/** Gets a value from the tree. */
find(key: Iterable<string>): V | undefined {
for (const segment of key) {
if (!next) {
}
node = next;
}
return node._value === unset ? undefined : node._value;
}
/** Gets whether the tree has the key, or a parent of the key, already inserted. */
hasKeyOrParent(key: Iterable<string>): boolean {
for (const segment of key) {
const next = node.children?.get(segment);
if (!next) {
return false;
}
if (next._value !== unset) {
return true;
}
node = next;
}
return false;
}
/** Gets whether the tree has the given key or any children. */
hasKeyOrChildren(key: Iterable<string>): boolean {
for (const segment of key) {
const next = node.children?.get(segment);
if (!next) {
return false;
}
node = next;
}
return true;
}
/** Gets whether the tree has the given key. */
hasKey(key: Iterable<string>): boolean {
for (const segment of key) {
const next = node.children?.get(segment);
if (!next) {
return false;
}
node = next;
}
return node._value !== unset;
}
private getPathToKey(key: Iterable<string>) {
let i = 0;
for (const part of key) {
if (!node) {
}
path.push({ part, node });
i++;
}
return path;
}
private opNode(key: Iterable<string>, fn: (node: Node<V>) => void, onDescend?: (node: Node<V>) => void): void {
for (const part of key) {
const next = new Node<V>();
node.children = new Map([[part, next]]);
node = next;
} else if (!node.children.has(part)) {
node.children.set(part, next);
node = next;
node = node.children.get(part)!;
}
}
const sizeBefore = node._value === unset ? 0 : 1;
fn(node);
const sizeAfter = node._value === unset ? 0 : 1;
this._size += sizeAfter - sizeBefore;
}
/** Returns an iterable of the tree values in no defined order. */
*values() {
if (_value !== unset) {
yield _value;
}
}
}
const stack = [root];
while (stack.length > 0) {
const node = stack.pop()!;
yield node;
if (node.children) {
for (const child of node.children.values()) {
stack.push(child);
}
}
}
}
public children?: Map<string, Node<T>>;
public get value() {
return this._value === unset ? undefined : this._value;
}
public set value(value: T | undefined) {
this._value = value === undefined ? unset : value;
}
public _value: T | typeof unset = unset;