forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollectionShims.ts
More file actions
267 lines (239 loc) · 10.9 KB
/
collectionShims.ts
File metadata and controls
267 lines (239 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* @internal */
namespace ts {
type GetIteratorCallback = <I extends readonly any[] | ReadonlySetShim<any> | ReadonlyMapShim<any, any> | undefined>(iterable: I) => IteratorShim<
I extends ReadonlyMapShim<infer K, infer V> ? [K, V] :
I extends ReadonlySetShim<infer T> ? T :
I extends readonly (infer T)[] ? T :
I extends undefined ? undefined :
never>;
type IteratorResultShim<T> =
| { value: T, done?: false }
| { value: void, done: true };
interface IteratorShim<T> {
next(): IteratorResultShim<T>;
}
interface ReadonlyMapShim<K, V> {
readonly size: number;
get(key: K): V | undefined;
has(key: K): boolean;
keys(): IteratorShim<K>;
values(): IteratorShim<V>;
entries(): IteratorShim<[K, V]>;
forEach(action: (value: V, key: K) => void): void;
}
interface MapShim<K, V> extends ReadonlyMapShim<K, V> {
set(key: K, value: V): this;
delete(key: K): boolean;
clear(): void;
}
type MapShimConstructor = new <K, V>(iterable?: readonly (readonly [K, V])[] | ReadonlyMapShim<K, V>) => MapShim<K, V>;
interface ReadonlySetShim<T> {
readonly size: number;
has(value: T): boolean;
keys(): IteratorShim<T>;
values(): IteratorShim<T>;
entries(): IteratorShim<[T, T]>;
forEach(action: (value: T, key: T) => void): void;
}
interface SetShim<T> extends ReadonlySetShim<T> {
add(value: T): this;
delete(value: T): boolean;
clear(): void;
}
type SetShimConstructor = new <T>(iterable?: readonly T[] | ReadonlySetShim<T>) => SetShim<T>;
interface MapData<K, V> {
size: number;
readonly head: MapEntry<K, V>;
tail: MapEntry<K, V>;
}
interface MapEntry<K, V> {
readonly key?: K;
value?: V;
/**
* Specifies the next entry in the linked list.
*/
next?: MapEntry<K, V>;
/**
* Specifies the previous entry in the linked list.
* Must be set when the entry is part of a Map/Set.
* When 'undefined', iterators should skip the next entry.
* This will be set to 'undefined' when an entry is deleted.
* See https://github.com/Microsoft/TypeScript/pull/27292 for more information.
*/
prev?: MapEntry<K, V>;
}
interface IteratorData<K, V, U extends (K | V | [K, V])> {
current?: MapEntry<K, V>;
selector: (key: K, value: V) => U;
}
function createMapData<K, V>(): MapData<K, V> {
const sentinel: MapEntry<K, V> = {};
sentinel.prev = sentinel;
return { head: sentinel, tail: sentinel, size: 0 };
}
function createMapEntry<K, V>(key: K, value: V): MapEntry<K, V> {
return { key, value, next: undefined, prev: undefined };
}
function sameValueZero(x: unknown, y: unknown) {
// Treats -0 === 0 and NaN === NaN
return x === y || x !== x && y !== y;
}
function getPrev<K, V>(entry: MapEntry<K, V>) {
const prev = entry.prev;
// Entries without a 'prev' have been removed from the map.
// An entry whose 'prev' points to itself is the head of the list and is invalid here.
if (!prev || prev === entry) throw new Error("Illegal state");
return prev;
}
function getNext<K, V>(entry: MapEntry<K, V> | undefined) {
while (entry) {
// Entries without a 'prev' have been removed from the map. Their 'next'
// pointer should point to the previous entry prior to deletion and
// that entry should be skipped to resume iteration.
const skipNext = !entry.prev;
entry = entry.next;
if (skipNext) {
continue;
}
return entry;
}
}
function getEntry<K, V>(data: MapData<K, V>, key: K): MapEntry<K, V> | undefined {
// We walk backwards from 'tail' to prioritize recently added entries.
// We skip 'head' because it is an empty entry used to track iteration start.
for (let entry = data.tail; entry !== data.head; entry = getPrev(entry)) {
if (sameValueZero(entry.key, key)) {
return entry;
}
}
}
function addOrUpdateEntry<K, V>(data: MapData<K, V>, key: K, value: V): MapEntry<K, V> | undefined {
const existing = getEntry(data, key);
if (existing) {
existing.value = value;
return;
}
const entry = createMapEntry(key, value);
entry.prev = data.tail;
data.tail.next = entry;
data.tail = entry;
data.size++;
return entry;
}
function deleteEntry<K, V>(data: MapData<K, V>, key: K): MapEntry<K, V> | undefined {
// We walk backwards from 'tail' to prioritize recently added entries.
// We skip 'head' because it is an empty entry used to track iteration start.
for (let entry = data.tail; entry !== data.head; entry = getPrev(entry)) {
// all entries in the map should have a 'prev' pointer.
if (entry.prev === undefined) throw new Error("Illegal state");
if (sameValueZero(entry.key, key)) {
if (entry.next) {
entry.next.prev = entry.prev;
}
else {
// an entry in the map without a 'next' pointer must be the 'tail'.
if (data.tail !== entry) throw new Error("Illegal state");
data.tail = entry.prev;
}
entry.prev.next = entry.next;
entry.next = entry.prev;
entry.prev = undefined;
data.size--;
return entry;
}
}
}
function clearEntries<K, V>(data: MapData<K, V>) {
let node = data.tail;
while (node !== data.head) {
const prev = getPrev(node);
node.next = data.head;
node.prev = undefined;
node = prev;
}
data.head.next = undefined;
data.tail = data.head;
data.size = 0;
}
function forEachEntry<K, V>(data: MapData<K, V>, action: (value: V, key: K) => void) {
let entry: MapEntry<K, V> | undefined = data.head;
while (entry) {
entry = getNext(entry);
if (entry) {
action(entry.value!, entry.key!);
}
}
}
function forEachIteration<T>(iterator: IteratorShim<T> | undefined, action: (value: any) => void) {
if (iterator) {
for (let step = iterator.next(); !step.done; step = iterator.next()) {
action(step.value);
}
}
}
function createIteratorData<K, V, U extends (K | V | [K, V])>(data: MapData<K, V>, selector: (key: K, value: V) => U): IteratorData<K, V, U> {
return { current: data.head, selector };
}
function iteratorNext<K, V, U extends (K | V | [K, V])>(data: IteratorData<K, V, U>): IteratorResultShim<U> {
// Navigate to the next entry.
data.current = getNext(data.current);
if (data.current) {
return { value: data.selector(data.current.key!, data.current.value!), done: false };
}
else {
return { value: undefined as never, done: true };
}
}
/* @internal */
export namespace ShimCollections {
export function createMapShim(getIterator: GetIteratorCallback): MapShimConstructor {
class MapIterator<K, V, U extends (K | V | [K, V])> {
private _data: IteratorData<K, V, U>;
constructor(data: MapData<K, V>, selector: (key: K, value: V) => U) {
this._data = createIteratorData(data, selector);
}
next() { return iteratorNext(this._data); }
}
return class Map<K, V> implements MapShim<K, V> {
private _mapData = createMapData<K, V>();
constructor(iterable?: readonly (readonly [K, V])[] | ReadonlyMapShim<K, V>) {
forEachIteration(getIterator(iterable), ([key, value]) => this.set(key, value));
}
get size() { return this._mapData.size; }
get(key: K): V | undefined { return getEntry(this._mapData, key)?.value; }
set(key: K, value: V): this { return addOrUpdateEntry(this._mapData, key, value), this; }
has(key: K): boolean { return !!getEntry(this._mapData, key); }
delete(key: K): boolean { return !!deleteEntry(this._mapData, key); }
clear(): void { clearEntries(this._mapData); }
keys(): IteratorShim<K> { return new MapIterator(this._mapData, (key, _value) => key); }
values(): IteratorShim<V> { return new MapIterator(this._mapData, (_key, value) => value); }
entries(): IteratorShim<[K, V]> { return new MapIterator(this._mapData, (key, value) => [key, value]); }
forEach(action: (value: V, key: K) => void): void { forEachEntry(this._mapData, action); }
};
}
export function createSetShim(getIterator: GetIteratorCallback): SetShimConstructor {
class SetIterator<K, V, U extends (K | V | [K, V])> {
private _data: IteratorData<K, V, U>;
constructor(data: MapData<K, V>, selector: (key: K, value: V) => U) {
this._data = createIteratorData(data, selector);
}
next() { return iteratorNext(this._data); }
}
return class Set<T> implements SetShim<T> {
private _mapData = createMapData<T, T>();
constructor(iterable?: readonly T[] | ReadonlySetShim<T>) {
forEachIteration(getIterator(iterable), value => this.add(value));
}
get size() { return this._mapData.size; }
add(value: T): this { return addOrUpdateEntry(this._mapData, value, value), this; }
has(value: T): boolean { return !!getEntry(this._mapData, value); }
delete(value: T): boolean { return !!deleteEntry(this._mapData, value); }
clear(): void { clearEntries(this._mapData); }
keys(): IteratorShim<T> { return new SetIterator(this._mapData, (key, _value) => key); }
values(): IteratorShim<T> { return new SetIterator(this._mapData, (_key, value) => value); }
entries(): IteratorShim<[T, T]> { return new SetIterator(this._mapData, (key, value) => [key, value]); }
forEach(action: (value: T, key: T) => void): void { forEachEntry(this._mapData, action); }
};
}
}
}