src/vs/base/common/linkedList.ts

151 LOC · 142 covered · 9 uncovered · 45 ranges · 17534 concepts · 24 introducers · 10859 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 > /*--------------------------------------------------------------------------------------------- linkedList.ts ×13
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 > class Node<E> {
7 >
8 > static readonly Undefined = new Node<unknown>(undefined);
9 >
10 > element: E;
11 > next: Node<E> | typeof Node.Undefined;
12 > prev: Node<E> | typeof Node.Undefined;
13 >
14 > constructor(element: E) {
15 > this.element = element;
16 > this.next = Node.Undefined;
17 > this.prev = Node.Undefined;
18 > }
19 > }
20 >
21 > export class LinkedList<E> {
23 > private _first: Node<E> | typeof Node.Undefined = Node.Undefined;
24 > private _last: Node<E> | typeof Node.Undefined = Node.Undefined;
25 > private _size: number = 0;
27 > get size(): number {
28 > return this._size; linkedList.ts ×1
29 > }
31 > isEmpty(): boolean {
32 > return this._first === Node.Undefined; commands.ts ×3
33 > }
35 > clear(): void {
36 > let node = this._first; linkedList.ts ×2
37 > while (node !== Node.Undefined) {
38 > const next = node.next; event.ts ×1
39 > node.prev = Node.Undefined;
40 > node.next = Node.Undefined;
41 > node = next;
42 > }
44 > this._first = Node.Undefined;
45 > this._last = Node.Undefined;
46 > this._size = 0;
47 > }
49 > unshift(element: E): () => void {
50 > return this._insert(element, false); linkedList.ts ×1
51 > }
53 > push(element: E): () => void {
54 > return this._insert(element, true); linkedList.ts ×1
55 > }
57 > private _insert(element: E, atTheEnd: boolean): () => void {
58 > const newNode = new Node(element); linkedList.ts ×3
59 > if (this._first === Node.Undefined) {
60 > this._first = newNode;
61 > this._last = newNode;
62 >
63 > } else if (atTheEnd) {
64 > // push linkedList.ts ×1
65 > const oldLast = this._last;
66 > this._last = newNode;
67 > newNode.prev = oldLast;
68 > oldLast.next = newNode;
69 >
70 > } else { linkedList.ts ×1
71 > // unshift linkedList.ts ×1
72 > const oldFirst = this._first;
73 > this._first = newNode;
74 > newNode.next = oldFirst;
75 > oldFirst.prev = newNode;
76 > }
77 > this._size += 1; linkedList.ts ×3
78 >
79 > let didRemove = false;
80 > return () => {
81 > if (!didRemove) { linkedList.ts ×1
82 > didRemove = true;
83 > this._remove(newNode);
84 > }
85 > };
88 > shift(): E | undefined {
89 > if (this._first === Node.Undefined) { linkedList.ts ×2
90 return undefined;
91 > } else { linkedList.ts ×2
92 > const res = this._first.element;
93 > this._remove(this._first);
94 > return res as E;
95 > }
96 > }
98 > pop(): E | undefined {
99 > if (this._last === Node.Undefined) { linkedList.ts ×2
100 return undefined;
101 > } else { linkedList.ts ×2
102 > const res = this._last.element;
103 > this._remove(this._last);
104 > return res as E;
105 > }
106 > }
108 > peek(): E | undefined {
109 if (this._last === Node.Undefined) {
110 return undefined;
111 } else {
112 const res = this._last.element;
113 return res as E;
114 }
115 }
117 > private _remove(node: Node<E> | typeof Node.Undefined): void {
118 > if (node.prev !== Node.Undefined && node.next !== Node.Undefined) { linkedList.ts ×4
119 > // middle linkedList.ts ×1
120 > const anchor = node.prev;
121 > anchor.next = node.next;
122 > node.next.prev = anchor;
123 >
124 > } else if (node.prev === Node.Undefined && node.next === Node.Undefined) { linkedList.ts ×4
125 > // only node linkedList.ts ×1
126 > this._first = Node.Undefined;
127 > this._last = Node.Undefined;
128 >
129 > } else if (node.next === Node.Undefined) { linkedList.ts ×4
130 > // last linkedList.ts ×1
131 > this._last = this._last.prev!;
132 > this._last.next = Node.Undefined;
133 >
134 > } else if (node.prev === Node.Undefined) { linkedList.ts ×1
135 > // first linkedList.ts ×1
136 > this._first = this._first.next!;
137 > this._first.prev = Node.Undefined;
138 > }
140 > // done
141 > this._size -= 1;
142 > }
144 > *[Symbol.iterator](): Iterator<E> {
145 > let node = this._first; linkedList.ts ×2
146 > while (node !== Node.Undefined) {
147 > yield node.element as E; linkedList.ts ×1
148 > node = node.next; linkedList.ts ×1
149 > }