src/vs/platform/instantiation/common/graph.ts

111 LOC · 103 covered · 8 uncovered · 29 ranges · 6084 concepts · 12 introducers · 3058 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.

1 > /*--------------------------------------------------------------------------------------------- graph.ts ×12
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 > export class Node<T> {
7 >
8 >
9 > readonly incoming = new Map<string, Node<T>>();
10 > readonly outgoing = new Map<string, Node<T>>();
11 >
12 > constructor(
13 > readonly key: string, graph.ts ×2
14 > readonly data: T
15 > ) { }
16 > } graph.ts ×12
17 >
18 > export class Graph<T> {
19 >
20 > private readonly _nodes = new Map<string, Node<T>>();
21 >
22 > constructor(private readonly _hashFn: (element: T) => string) {
23 > // empty graph.ts ×1
24 > }
26 > roots(): Node<T>[] {
27 > const ret: Node<T>[] = []; graph.ts ×2
28 > for (const node of this._nodes.values()) {
29 > if (node.outgoing.size === 0) {
30 > ret.push(node); graph.ts ×1
31 > }
32 > } graph.ts ×2
33 > return ret;
34 > }
36 > insertEdge(from: T, to: T): void {
37 > const fromNode = this.lookupOrInsertNode(from); graph.ts ×1
38 > const toNode = this.lookupOrInsertNode(to);
39 >
40 > fromNode.outgoing.set(toNode.key, toNode);
41 > toNode.incoming.set(fromNode.key, fromNode);
42 > }
44 > removeNode(data: T): void {
45 > const key = this._hashFn(data); graph.ts ×2
46 > this._nodes.delete(key);
47 > for (const node of this._nodes.values()) {
48 > node.outgoing.delete(key); graph.ts ×1
49 > node.incoming.delete(key);
50 > }
51 > } graph.ts ×2
53 > lookupOrInsertNode(data: T): Node<T> {
54 > const key = this._hashFn(data); graph.ts ×2
55 > let node = this._nodes.get(key);
56 >
57 > if (!node) {
58 > node = new Node(key, data);
59 > this._nodes.set(key, node);
60 > }
61 >
62 > return node;
63 > }
65 > lookup(data: T): Node<T> | undefined {
66 > return this._nodes.get(this._hashFn(data)); graph.ts ×1
67 > }
69 > isEmpty(): boolean {
70 > return this._nodes.size === 0; graph.ts ×1
71 > }
73 > toString(): string {
74 const data: string[] = [];
75 for (const [key, value] of this._nodes) {
76 data.push(`${key}\n\t(-> incoming)[${[...value.incoming.keys()].join(', ')}]\n\t(outgoing ->)[${[...value.outgoing.keys()].join(',')}]\n`);
77
78 }
79 return data.join('\n');
80 }
82 > /**
83 > * This is brute force and slow and **only** be used
84 > * to trouble shoot.
85 > */
86 > findCycleSlow() {
87 > for (const [id, node] of this._nodes) { graph.ts ×4
88 > const seen = new Set<string>([id]);
89 > const res = this._findCycle(node, seen);
90 > if (res) {
91 > return res;
92 > }
93 > }
94 return undefined;
95 > } graph.ts ×4
97 > private _findCycle(node: Node<T>, seen: Set<string>): string | undefined {
98 > for (const [id, outgoing] of node.outgoing) { graph.ts ×4
99 > if (seen.has(id)) {
100 > return [...seen, id].join(' -> ');
101 > }
102 > seen.add(id);
103 > const value = this._findCycle(outgoing, seen);
104 > if (value) {
105 > return value;
106 > }
107 > seen.delete(id); instantiationService.ts ×6
108 > }
109 > return undefined;
110 > } graph.ts ×4
111 > } graph.ts ×12