Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/test/fixtures/map.summarized.ts
13406 views
1
2
class ResourceMapEntry<T> {
3
constructor(readonly uri: URI, readonly value: T) { }
4
}
5
6
export class ResourceMap<T> implements Map<URI, T> {
7
8
constructor(mapOrKeyFn?: ResourceMap<T> | ResourceMapKeyFn, toKey?: ResourceMapKeyFn) {}
9
}
10
11
export class ResourceSet implements Set<URI> {
12
constructor(entriesOrKey?: readonly URI[] | ResourceMapKeyFn, toKey?: ResourceMapKeyFn) {}
13
14
forEach(callbackfn: (value: URI, value2: URI, set: Set<URI>) => void, thisArg?: any): void {}
15
16
has(value: URI): boolean {}
17
18
entries(): IterableIterator<[URI, URI]> {}
19
20
keys(): IterableIterator<URI> {}
21
22
values(): IterableIterator<URI> {}
23
24
[Symbol.iterator](): IterableIterator<URI> {}
25
}
26
27
28
interface Item<K, V> {
29
previous: Item<K, V> | undefined;
30
next: Item<K, V> | undefined;
31
key: K;
32
value: V;
33
}
34
35
export const enum Touch {
36
None = 0,
37
AsOld = 1,
38
AsNew = 2
39
}
40
41
export class LinkedMap<K, V> implements Map<K, V> {
42
43
readonly [Symbol.toStringTag] = 'LinkedMap';
44
45
private _map: Map<K, Item<K, V>>;
46
private _head: Item<K, V> | undefined;
47
private _tail: Item<K, V> | undefined;
48
private _size: number;
49
50
private _state: number;
51
52
constructor() {}
53
54
clear(): void {}
55
56
isEmpty(): boolean {}
57
58
get size(): number {}
59
60
get first(): V | undefined {}
61
62
get last(): V | undefined {}
63
64
has(key: K): boolean {}
65
66
get(key: K, touch: Touch = Touch.None): V | undefined {
67
return item.value;
68
}
69
70
set(key: K, value: V, touch: Touch = Touch.None): this {
71
let item = this._map.get(key);
72
if (item) {} else {}
73
return this;
74
}
75
76
delete(key: K): boolean {
77
return !!this.remove(key);
78
}
79
80
remove(key: K): V | undefined {
81
const item = this._map.get(key);
82
if (!item) {}
83
this._map.delete(key);
84
this.removeItem(item);
85
this._size--;
86
return item.value;
87
}
88
89
shift(): V | undefined {
90
if (!this._head && !this._tail) {}
91
if (!this._head || !this._tail) {}
92
const item = this._head;
93
this._map.delete(item.key);
94
this.removeItem(item);
95
this._size--;
96
return item.value;
97
}
98
99
forEach(callbackfn: (value: V, key: K, map: LinkedMap<K, V>) => void, thisArg?: any): void {
100
const state = this._state;
101
let current = this._head;
102
while (current) {
103
if (thisArg) {} else {}
104
if (this._state !== state) {}
105
current = current.next;
106
}
107
}
108
109
keys(): IterableIterator<K> {
110
const map = this;
111
const state = this._state;
112
let current = this._head;
113
const iterator: IterableIterator<K> = {
114
[Symbol.iterator]() {},
115
next(): IteratorResult<K> {}
116
};
117
return iterator;
118
}
119
120
values(): IterableIterator<V> {
121
const map = this;
122
const state = this._state;
123
let current = this._head;
124
const iterator: IterableIterator<V> = {
125
[Symbol.iterator]() {},
126
next(): IteratorResult<V> {}
127
};
128
return iterator;
129
}
130
131
entries(): IterableIterator<[K, V]> {
132
const map = this;
133
const state = this._state;
134
let current = this._head;
135
const iterator: IterableIterator<[K, V]> = {
136
[Symbol.iterator]() {},
137
next(): IteratorResult<[K, V]> {
138
if (current) {} else {}
139
}
140
};
141
return iterator;
142
}
143
144
[Symbol.iterator](): IterableIterator<[K, V]> {
145
return this.entries();
146
}
147
148
protected trimOld(newSize: number) {
149
if (newSize >= this.size) {
150
return;
151
}
152
if (newSize === 0) {
153
this.clear();
154
return;
155
}
156
let current = this._head;
157
let currentSize = this.size;
158
while (current && currentSize > newSize) {
159
this._map.delete(current.key);
160
current = current.next;
161
currentSize--;
162
}
163
this._head = current;
164
this._size = currentSize;
165
if (current) {
166
current.previous = undefined;
167
}
168
this._state++;
169
}
170
171
private addItemFirst(item: Item<K, V>): void {
172
// First time Insert
173
if (!this._head && !this._tail) {
174
this._tail = item;
175
} else if (!this._head) {
176
throw new Error('Invalid list');
177
} else {
178
item.next = this._head;
179
this._head.previous = item;
180
}
181
this._head = item;
182
this._state++;
183
}
184
185
private addItemLast(item: Item<K, V>): void {
186
// First time Insert
187
if (!this._head && !this._tail) {
188
this._head = item;
189
} else if (!this._tail) {
190
throw new Error('Invalid list');
191
} else {
192
item.previous = this._tail;
193
this._tail.next = item;
194
}
195
this._tail = item;
196
this._state++;
197
}
198
199
private removeItem(item: Item<K, V>): void {
200
if (item === this._head && item === this._tail) {
201
this._head = undefined;
202
this._tail = undefined;
203
}
204
else if (item === this._head) {
205
// This can only happen if size === 1 which is handled
206
// by the case above.
207
if (!item.next) {
208
throw new Error('Invalid list');
209
}
210
item.next.previous = undefined;
211
this._head = item.next;
212
}
213
else if (item === this._tail) {
214
// This can only happen if size === 1 which is handled
215
// by the case above.
216
if (!item.previous) {
217
throw new Error('Invalid list');
218
}
219
item.previous.next = undefined;
220
this._tail = item.previous;
221
}
222
else {
223
const next = item.next;
224
const previous = item.previous;
225
if (!next || !previous) {
226
throw new Error('Invalid list');
227
}
228
next.previous = previous;
229
previous.next = next;
230
}
231
item.next = undefined;
232
item.previous = undefined;
233
this._state++;
234
}
235
236
private touch(item: Item<K, V>, touch: Touch): void {
237
if (!this._head || !this._tail) {
238
throw new Error('Invalid list');
239
}
240
if ((touch !== Touch.AsOld && touch !== Touch.AsNew)) {
241
return;
242
}
243
244
if (touch === Touch.AsOld) {
245
if (item === this._head) {
246
return;
247
}
248
249
const next = item.next;
250
const previous = item.previous;
251
252
// Unlink the item
253
if (item === this._tail) {
254
// previous must be defined since item was not head but is tail
255
// So there are more than on item in the map
256
previous!.next = undefined;
257
this._tail = previous;
258
}
259
else {
260
// Both next and previous are not undefined since item was neither head nor tail.
261
next!.previous = previous;
262
previous!.next = next;
263
}
264
265
// Insert the node at head
266
item.previous = undefined;
267
item.next = this._head;
268
this._head.previous = item;
269
this._head = item;
270
this._state++;
271
} else if (touch === Touch.AsNew) {
272
if (item === this._tail) {
273
return;
274
}
275
276
const next = item.next;
277
const previous = item.previous;
278
279
// Unlink the item.
280
if (item === this._head) {
281
// next must be defined since item was not tail but is head
282
// So there are more than on item in the map
283
next!.previous = undefined;
284
this._head = next;
285
} else {
286
// Both next and previous are not undefined since item was neither head nor tail.
287
next!.previous = previous;
288
previous!.next = next;
289
}
290
item.next = undefined;
291
item.previous = this._tail;
292
this._tail.next = item;
293
this._tail = item;
294
this._state++;
295
}
296
}
297
298
toJSON(): [K, V][] {
299
const data: [K, V][] = [];
300
301
this.forEach((value, key) => {
302
data.push([key, value]);
303
});
304
305
return data;
306
}
307
308
fromJSON(data: [K, V][]): void {
309
this.clear();
310
311
for (const [key, value] of data) {
312
this.set(key, value);
313
}
314
}
315
}
316
317
export class LRUCache<K, V> extends LinkedMap<K, V> {
318
319
private _limit: number;
320
private _ratio: number;
321
322
constructor(limit: number, ratio: number = 1) {
323
super();
324
this._limit = limit;
325
this._ratio = Math.min(Math.max(0, ratio), 1);
326
}
327
328
get limit(): number {
329
return this._limit;
330
}
331
332
set limit(limit: number) {
333
this._limit = limit;
334
this.checkTrim();
335
}
336
337
get ratio(): number {
338
return this._ratio;
339
}
340
341
set ratio(ratio: number) {
342
this._ratio = Math.min(Math.max(0, ratio), 1);
343
this.checkTrim();
344
}
345
346
override get(key: K, touch: Touch = Touch.AsNew): V | undefined {
347
return super.get(key, touch);
348
}
349
350
peek(key: K): V | undefined {
351
return super.get(key, Touch.None);
352
}
353
354
override set(key: K, value: V): this {
355
super.set(key, value, Touch.AsNew);
356
this.checkTrim();
357
return this;
358
}
359
360
private checkTrim() {
361
if (this.size > this._limit) {
362
this.trimOld(Math.round(this._limit * this._ratio));
363
}
364
}
365
}
366
367
export class CounterSet<T> {
368
369
private map = new Map<T, number>();
370
371
add(value: T): CounterSet<T> {
372
this.map.set(value, (this.map.get(value) || 0) + 1);
373
return this;
374
}
375
376
delete(value: T): boolean {
377
let counter = this.map.get(value) || 0;
378
379
if (counter === 0) {
380
return false;
381
}
382
383
counter--;
384
385
if (counter === 0) {
386
this.map.delete(value);
387
} else {
388
this.map.set(value, counter);
389
}
390
391
return true;
392
}
393
394
has(value: T): boolean {
395
return this.map.has(value);
396
}
397
}
398
399
/**
400
* A map that allows access both by keys and values.
401
* **NOTE**: values need to be unique.
402
*/
403
export class BidirectionalMap<K, V> {
404
405
private readonly _m1 = new Map<K, V>();
406
private readonly _m2 = new Map<V, K>();
407
408
constructor(entries?: readonly (readonly [K, V])[]) {
409
if (entries) {
410
for (const [key, value] of entries) {
411
this.set(key, value);
412
}
413
}
414
}
415
416
clear(): void {
417
this._m1.clear();
418
this._m2.clear();
419
}
420
421
set(key: K, value: V): void {
422
this._m1.set(key, value);
423
this._m2.set(value, key);
424
}
425
426
get(key: K): V | undefined {
427
return this._m1.get(key);
428
}
429
430
getKey(value: V): K | undefined {
431
return this._m2.get(value);
432
}
433
434
delete(key: K): boolean {
435
const value = this._m1.get(key);
436
if (value === undefined) {
437
return false;
438
}
439
this._m1.delete(key);
440
this._m2.delete(value);
441
return true;
442
}
443
444
forEach(callbackfn: (value: V, key: K, map: BidirectionalMap<K, V>) => void, thisArg?: any): void {
445
this._m1.forEach((value, key) => {
446
callbackfn.call(thisArg, value, key, this);
447
});
448
}
449
450
keys(): IterableIterator<K> {
451
return this._m1.keys();
452
}
453
454
values(): IterableIterator<V> {
455
return this._m1.values();
456
}
457
}
458
459