map.ts 18.3 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

B
wip  
Benjamin Pasero 已提交
8 9
import URI from 'vs/base/common/uri';

S
Sandeep Somavarapu 已提交
10
export interface Key {
J
Johannes Rieken 已提交
11
	toString(): string;
S
Sandeep Somavarapu 已提交
12 13 14 15
}

export interface Entry<K, T> {
	key: K;
16 17 18
	value: T;
}

19 20 21
export function values<K, V>(map: Map<K, V>): V[] {
	const result: V[] = [];
	map.forEach(value => result.push(value));
B
Benjamin Pasero 已提交
22

23 24
	return result;
}
S
Sandeep Somavarapu 已提交
25

26 27 28
export function keys<K, V>(map: Map<K, V>): K[] {
	const result: K[] = [];
	map.forEach((value, key) => result.push(key));
B
Benjamin Pasero 已提交
29

30 31 32
	return result;
}

33 34 35 36 37
export function getOrSet<K, V>(map: Map<K, V>, key: K, value: V): V {
	let result = map.get(key);
	if (result === void 0) {
		result = value;
		map.set(key, result);
S
Sandeep Somavarapu 已提交
38
	}
B
Benjamin Pasero 已提交
39

40
	return result;
S
Sandeep Somavarapu 已提交
41 42
}

43 44 45 46
export interface ISerializedBoundedLinkedMap<T> {
	entries: { key: string; value: T }[];
}

B
Benjamin Pasero 已提交
47
interface LinkedEntry<K, T> extends Entry<K, T> {
48 49 50 51
	next?: LinkedEntry<K, T>;
	prev?: LinkedEntry<K, T>;
}

52 53
/**
 * A simple Map<T> that optionally allows to set a limit of entries to store. Once the limit is hit,
B
Benjamin Pasero 已提交
54 55
 * the cache will remove the entry that was last recently added. Or, if a ratio is provided below 1,
 * all elements will be removed until the ratio is full filled (e.g. 0.75 to remove 25% of old elements).
56
 */
57
export class BoundedMap<T> {
B
Benjamin Pasero 已提交
58
	private map: Map<string, LinkedEntry<string, T>>;
59 60 61

	private head: LinkedEntry<string, T>;
	private tail: LinkedEntry<string, T>;
B
Benjamin Pasero 已提交
62
	private ratio: number;
63

64
	constructor(private limit = Number.MAX_VALUE, ratio = 1, value?: ISerializedBoundedLinkedMap<T>) {
B
Benjamin Pasero 已提交
65
		this.map = new Map<string, LinkedEntry<string, T>>();
B
Benjamin Pasero 已提交
66
		this.ratio = limit * ratio;
67 68 69 70 71 72 73 74 75 76 77 78 79 80

		if (value) {
			value.entries.forEach(entry => {
				this.set(entry.key, entry.value);
			});
		}
	}

	public setLimit(limit: number): void {
		if (limit < 0) {
			return; // invalid limit
		}

		this.limit = limit;
B
Benjamin Pasero 已提交
81
		while (this.map.size > this.limit) {
82 83 84 85 86 87 88
			this.trim();
		}
	}

	public serialize(): ISerializedBoundedLinkedMap<T> {
		const serialized: ISerializedBoundedLinkedMap<T> = { entries: [] };

B
Benjamin Pasero 已提交
89 90 91
		this.map.forEach(entry => {
			serialized.entries.push({ key: entry.key, value: entry.value });
		});
92 93

		return serialized;
94 95 96
	}

	public get size(): number {
B
Benjamin Pasero 已提交
97
		return this.map.size;
98 99 100
	}

	public set(key: string, value: T): boolean {
B
Benjamin Pasero 已提交
101
		if (this.map.has(key)) {
102 103 104
			return false; // already present!
		}

105
		const entry: LinkedEntry<string, T> = { key, value };
B
Benjamin Pasero 已提交
106
		this.push(entry);
107

B
Benjamin Pasero 已提交
108
		if (this.size > this.limit) {
109 110 111 112 113 114 115
			this.trim();
		}

		return true;
	}

	public get(key: string): T {
B
Benjamin Pasero 已提交
116
		const entry = this.map.get(key);
117 118 119 120

		return entry ? entry.value : null;
	}

B
Benjamin Pasero 已提交
121 122 123 124 125 126 127 128 129 130 131
	public getOrSet(k: string, t: T): T {
		const res = this.get(k);
		if (res) {
			return res;
		}

		this.set(k, t);

		return t;
	}

B
Benjamin Pasero 已提交
132
	public delete(key: string): T {
B
Benjamin Pasero 已提交
133
		const entry = this.map.get(key);
134 135

		if (entry) {
B
Benjamin Pasero 已提交
136
			this.map.delete(key);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

			if (entry.next) {
				entry.next.prev = entry.prev; // [A]<-[x]<-[C] = [A]<-[C]
			} else {
				this.head = entry.prev; // [A]-[x] = [A]
			}

			if (entry.prev) {
				entry.prev.next = entry.next; // [A]->[x]->[C] = [A]->[C]
			} else {
				this.tail = entry.next; // [x]-[A] = [A]
			}

			return entry.value;
		}

		return null;
	}

B
Benjamin Pasero 已提交
156
	public has(key: string): boolean {
B
Benjamin Pasero 已提交
157
		return this.map.has(key);
B
Benjamin Pasero 已提交
158 159
	}

160
	public clear(): void {
B
Benjamin Pasero 已提交
161
		this.map.clear();
162 163 164 165
		this.head = null;
		this.tail = null;
	}

B
Benjamin Pasero 已提交
166
	private push(entry: LinkedEntry<string, T>): void {
167 168 169 170 171 172 173 174 175 176 177 178
		if (this.head) {
			// [A]-[B] = [A]-[B]->[X]
			entry.prev = this.head;
			this.head.next = entry;
		}

		if (!this.tail) {
			this.tail = entry;
		}

		this.head = entry;

B
Benjamin Pasero 已提交
179
		this.map.set(entry.key, entry);
180 181 182 183 184
	}

	private trim(): void {
		if (this.tail) {

B
Benjamin Pasero 已提交
185 186 187 188 189 190 191
			// Remove all elements until ratio is reached
			if (this.ratio < this.limit) {
				let index = 0;
				let current = this.tail;
				while (current.next) {

					// Remove the entry
B
Benjamin Pasero 已提交
192
					this.map.delete(current.key);
B
Benjamin Pasero 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

					// if we reached the element that overflows our ratio condition
					// make its next element the new tail of the Map and adjust the size
					if (index === this.ratio) {
						this.tail = current.next;
						this.tail.prev = null;

						break;
					}

					// Move on
					current = current.next;
					index++;
				}
			}

			// Just remove the tail element
			else {
B
Benjamin Pasero 已提交
211
				this.map.delete(this.tail.key);
B
Benjamin Pasero 已提交
212 213 214

				// [x]-[B] = [B]
				this.tail = this.tail.next;
215 216 217
				if (this.tail) {
					this.tail.prev = null;
				}
B
Benjamin Pasero 已提交
218
			}
219 220
		}
	}
B
Benjamin Pasero 已提交
221 222
}

223 224 225
export interface IKeySegements {
	reset(key: string): this;
	join(segments: string[]): string;
J
Johannes Rieken 已提交
226
	hasNext(): boolean;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	next(): string;
}

export class StringSegments implements IKeySegements {
	private _value: string;
	private _pos: number;

	reset(key: string): this {
		this._value = key;
		this._pos = 0;
		return this;
	}
	join(segments: string[]): string {
		return segments.join('');
	}
J
Johannes Rieken 已提交
242 243
	hasNext(): boolean {
		return this._pos < this._value.length;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	}
	next(): string {
		return this._value[this._pos++];
	}
}

export class PathSegments implements IKeySegements {

	private static _fwd = '/'.charCodeAt(0);
	private static _bwd = '\\'.charCodeAt(0);

	private _value: string;
	private _pos: number;

	reset(key: string): this {
		this._value = key;
		this._pos = 0;
		return this;
	}
	join(segments: string[]): string {
		return segments.join('/');
	}
J
Johannes Rieken 已提交
266 267
	hasNext(): boolean {
		return this._pos < this._value.length;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	}
	next(): string {
		// this._data = key.split(/[\\/]/).filter(s => !!s);
		let pos = this._pos;
		loop: for (; pos < this._value.length; pos++) {
			switch (this._value.charCodeAt(pos)) {
				case PathSegments._fwd:
				case PathSegments._bwd:
					// found it
					break loop;
			}
		}

		if (pos > this._pos) {
			// did advance
			let ret = this._value.substring(this._pos, pos);
			this._pos = pos + 1;
			return ret;

		} else {
			// maybe just separators in a row
			this._pos += 1;
J
Johannes Rieken 已提交
290
			return this.hasNext()
291 292 293 294 295 296 297 298 299 300 301 302
				? this.next()
				: undefined;
		}
	}
}

class TernarySearchTreeNode<E> {
	str: string;
	element: E;
	left: TernarySearchTreeNode<E>;
	mid: TernarySearchTreeNode<E>;
	right: TernarySearchTreeNode<E>;
J
Johannes Rieken 已提交
303 304 305 306

	isEmpty(): boolean {
		return !this.left && !this.mid && !this.right && !this.element;
	}
307 308 309 310
}

export class TernarySearchTree<E> {

311 312 313 314 315 316 317 318
	static forPaths<E>(): TernarySearchTree<E> {
		return new TernarySearchTree<E>(new PathSegments());
	}

	static forStrings<E>(): TernarySearchTree<E> {
		return new TernarySearchTree<E>(new StringSegments());
	}

J
Johannes Rieken 已提交
319
	private _segments: IKeySegements;
320 321
	private _root: TernarySearchTreeNode<E>;

322
	constructor(segments: IKeySegements) {
J
Johannes Rieken 已提交
323
		this._segments = segments;
324 325 326
	}

	set(key: string, element: E): void {
J
Johannes Rieken 已提交
327
		const segements = this._segments.reset(key);
328 329 330
		this._root = this._set(this._root, segements.next(), segements, element);
	}

J
Johannes Rieken 已提交
331
	private _set(node: TernarySearchTreeNode<E>, key: string, segments: IKeySegements, element: E): TernarySearchTreeNode<E> {
332 333 334 335 336 337 338 339

		if (!node) {
			node = new TernarySearchTreeNode<E>();
			node.str = key;
		}

		if (node.str > key) {
			// left
J
Johannes Rieken 已提交
340
			node.left = this._set(node.left, key, segments, element);
341 342
		} else if (node.str < key) {
			// right
J
Johannes Rieken 已提交
343 344
			node.right = this._set(node.right, key, segments, element);
		} else if (segments.hasNext()) {
345
			// mid
J
Johannes Rieken 已提交
346 347 348
			node.mid = this._set(node.mid, segments.next(), segments, element);
		} else {
			node.element = element;
349 350 351 352 353 354
		}

		return node;
	}

	get(key: string): E {
J
Johannes Rieken 已提交
355
		const segements = this._segments.reset(key);
356 357 358
		return this._get(this._root, segements.next(), segements);
	}

J
Johannes Rieken 已提交
359
	private _get(node: TernarySearchTreeNode<E>, key: string, segments: IKeySegements): E {
360 361 362 363
		if (!node) {
			return undefined;
		} else if (node.str > key) {
			// left
J
Johannes Rieken 已提交
364
			return this._get(node.left, key, segments);
365 366
		} else if (node.str < key) {
			// right
J
Johannes Rieken 已提交
367 368 369 370
			return this._get(node.right, key, segments);
		} else if (segments.hasNext()) {
			// mid
			return this._get(node.mid, segments.next(), segments);
371
		} else {
J
Johannes Rieken 已提交
372
			return node.element;
373 374 375
		}
	}

J
Johannes Rieken 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389
	delete(key: string): void {
		const segments = this._segments.reset(key);
		this._delete(this._root, segments.next(), segments);
	}

	private _delete(node: TernarySearchTreeNode<E>, key: string, segments: IKeySegements): TernarySearchTreeNode<E> {
		if (!node) {
			return undefined;
		} else if (node.str > key) {
			// left
			node.left = this._delete(node.left, key, segments);
		} else if (node.str < key) {
			// right
			node.right = this._delete(node.right, key, segments);
J
Johannes Rieken 已提交
390
		} else if (segments.hasNext()) {
J
Johannes Rieken 已提交
391 392
			// mid
			node.mid = this._delete(node.mid, segments.next(), segments);
J
Johannes Rieken 已提交
393 394 395
		} else {
			// remove element
			node.element = undefined;
J
Johannes Rieken 已提交
396 397 398 399 400
		}

		return node.isEmpty() ? undefined : node;
	}

401
	findSubstr(key: string): E {
J
Johannes Rieken 已提交
402
		const segements = this._segments.reset(key);
403 404 405
		return this._findSubstr(this._root, segements.next(), segements, undefined);
	}

J
Johannes Rieken 已提交
406
	private _findSubstr(node: TernarySearchTreeNode<E>, key: string, segments: IKeySegements, candidate: E): E {
407 408 409 410
		if (!node) {
			return candidate;
		} else if (node.str > key) {
			// left
J
Johannes Rieken 已提交
411
			return this._findSubstr(node.left, key, segments, candidate);
412 413
		} else if (node.str < key) {
			// right
J
Johannes Rieken 已提交
414 415 416 417
			return this._findSubstr(node.right, key, segments, candidate);
		} else if (segments.hasNext()) {
			// mid
			return this._findSubstr(node.mid, segments.next(), segments, node.element || candidate);
418
		} else {
J
Johannes Rieken 已提交
419
			return node.element || candidate;
420 421 422
		}
	}

423
	findSuperstr(key: string): TernarySearchTree<E> {
J
Johannes Rieken 已提交
424
		const segements = this._segments.reset(key);
425
		return this._findSuperstr(this._root, segements.next(), segements);
J
Johannes Rieken 已提交
426 427
	}

428
	private _findSuperstr(node: TernarySearchTreeNode<E>, key: string, segments: IKeySegements): TernarySearchTree<E> {
J
Johannes Rieken 已提交
429
		if (!node) {
430
			return undefined;
J
Johannes Rieken 已提交
431 432
		} else if (node.str > key) {
			// left
433
			return this._findSuperstr(node.left, key, segments);
J
Johannes Rieken 已提交
434 435
		} else if (node.str < key) {
			// right
436
			return this._findSuperstr(node.right, key, segments);
J
Johannes Rieken 已提交
437 438
		} else if (segments.hasNext()) {
			// mid
439
			return this._findSuperstr(node.mid, segments.next(), segments);
J
Johannes Rieken 已提交
440
		} else {
J
Johannes Rieken 已提交
441
			// collect
442 443
			if (!node.mid) {
				return undefined;
J
Johannes Rieken 已提交
444
			}
445 446 447
			let ret = new TernarySearchTree<E>(this._segments);
			ret._root = node.mid;
			return ret;
J
Johannes Rieken 已提交
448 449 450
		}
	}

451 452 453 454 455 456 457 458 459 460 461 462 463
	forEach(callback: (entry: [string, E]) => any) {
		this._forEach(this._root, [], callback);
	}

	private _forEach(node: TernarySearchTreeNode<E>, parts: string[], callback: (entry: [string, E]) => any) {
		if (!node) {
			return;
		}
		this._forEach(node.left, parts, callback);
		this._forEach(node.right, parts, callback);
		let newParts = parts.slice();
		newParts.push(node.str);
		if (node.element) {
J
Johannes Rieken 已提交
464
			callback([this._segments.join(newParts), node.element]);
465 466 467 468 469
		}
		this._forEach(node.mid, newParts, callback);
	}
}

B
wip  
Benjamin Pasero 已提交
470
export class ResourceMap<T> {
471 472

	protected map: Map<string, T>;
B
wip  
Benjamin Pasero 已提交
473

474
	constructor(private ignoreCase?: boolean) {
B
wip  
Benjamin Pasero 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
		this.map = new Map<string, T>();
	}

	public set(resource: URI, value: T): void {
		this.map.set(this.toKey(resource), value);
	}

	public get(resource: URI): T {
		return this.map.get(this.toKey(resource));
	}

	public has(resource: URI): boolean {
		return this.map.has(this.toKey(resource));
	}

	public get size(): number {
		return this.map.size;
	}

	public clear(): void {
		this.map.clear();
	}

	public delete(resource: URI): boolean {
		return this.map.delete(this.toKey(resource));
	}

	public forEach(clb: (value: T) => void): void {
		this.map.forEach(clb);
	}

B
Benjamin Pasero 已提交
506
	public values(): T[] {
507
		return values(this.map);
B
Benjamin Pasero 已提交
508 509
	}

B
wip  
Benjamin Pasero 已提交
510
	private toKey(resource: URI): string {
511
		let key = resource.toString();
512 513 514 515
		if (this.ignoreCase) {
			key = key.toLowerCase();
		}

B
wip  
Benjamin Pasero 已提交
516 517
		return key;
	}
D
Dirk Baeumer 已提交
518 519
}

520 521 522 523 524 525 526 527 528 529 530 531
export class StrictResourceMap<T> extends ResourceMap<T> {

	constructor() {
		super();
	}

	public keys(): URI[] {
		return keys(this.map).map(key => URI.parse(key));
	}

}

D
Dirk Baeumer 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
// We should fold BoundedMap and LinkedMap. See https://github.com/Microsoft/vscode/issues/28496

interface Item<K, V> {
	previous: Item<K, V> | undefined;
	next: Item<K, V> | undefined;
	key: K;
	value: V;
}

export namespace Touch {
	export const None: 0 = 0;
	export const First: 1 = 1;
	export const Last: 2 = 2;
}

export type Touch = 0 | 1 | 2;

export class LinkedMap<K, V> {

	private _map: Map<K, Item<K, V>>;
	private _head: Item<K, V> | undefined;
	private _tail: Item<K, V> | undefined;
	private _size: number;

	constructor() {
		this._map = new Map<K, Item<K, V>>();
		this._head = undefined;
		this._tail = undefined;
		this._size = 0;
	}

	public clear(): void {
		this._map.clear();
		this._head = undefined;
		this._tail = undefined;
		this._size = 0;
	}

	public isEmpty(): boolean {
		return !this._head && !this._tail;
	}

	public get size(): number {
		return this._size;
	}

	public has(key: K): boolean {
		return this._map.has(key);
	}

	public get(key: K): V | undefined {
		const item = this._map.get(key);
		if (!item) {
			return undefined;
		}
		return item.value;
	}

	public set(key: K, value: V, touch: Touch = Touch.None): void {
		let item = this._map.get(key);
		if (item) {
			item.value = value;
			if (touch !== Touch.None) {
				this.touch(item, touch);
			}
		} else {
			item = { key, value, next: undefined, previous: undefined };
			switch (touch) {
				case Touch.None:
					this.addItemLast(item);
					break;
				case Touch.First:
					this.addItemFirst(item);
					break;
				case Touch.Last:
					this.addItemLast(item);
					break;
				default:
					this.addItemLast(item);
					break;
			}
			this._map.set(key, item);
			this._size++;
		}
	}

	public delete(key: K): boolean {
619 620 621 622
		return !!this.remove(key);
	}

	public remove(key: K): V | undefined {
D
Dirk Baeumer 已提交
623 624
		const item = this._map.get(key);
		if (!item) {
625
			return undefined;
D
Dirk Baeumer 已提交
626 627 628 629
		}
		this._map.delete(key);
		this.removeItem(item);
		this._size--;
630
		return item.value;
D
Dirk Baeumer 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	}

	public shift(): V | undefined {
		if (!this._head && !this._tail) {
			return undefined;
		}
		if (!this._head || !this._tail) {
			throw new Error('Invalid list');
		}
		const item = this._head;
		this._map.delete(item.key);
		this.removeItem(item);
		this._size--;
		return item.value;
	}

	public forEach(callbackfn: (value: V, key: K, map: LinkedMap<K, V>) => void, thisArg?: any): void {
		let current = this._head;
		while (current) {
			if (thisArg) {
				callbackfn.bind(thisArg)(current.value, current.key, this);
			} else {
				callbackfn(current.value, current.key, this);
			}
			current = current.next;
		}
	}

	public forEachReverse(callbackfn: (value: V, key: K, map: LinkedMap<K, V>) => void, thisArg?: any): void {
		let current = this._tail;
		while (current) {
			if (thisArg) {
				callbackfn.bind(thisArg)(current.value, current.key, this);
			} else {
				callbackfn(current.value, current.key, this);
			}
			current = current.previous;
		}
	}

	public values(): V[] {
		let result: V[] = [];
		let current = this._head;
		while (current) {
			result.push(current.value);
			current = current.next;
		}
		return result;
	}

	public keys(): K[] {
		let result: K[] = [];
		let current = this._head;
		while (current) {
			result.push(current.key);
			current = current.next;
		}
		return result;
	}

	/* VS Code / Monaco editor runs on es5 which has no Symbol.iterator
	public keys(): IterableIterator<K> {
		let current = this._head;
		let iterator: IterableIterator<K> = {
			[Symbol.iterator]() {
				return iterator;
			},
			next():IteratorResult<K> {
				if (current) {
					let result = { value: current.key, done: false };
					current = current.next;
					return result;
				} else {
					return { value: undefined, done: true };
				}
			}
		};
		return iterator;
	}

	public values(): IterableIterator<V> {
		let current = this._head;
		let iterator: IterableIterator<V> = {
			[Symbol.iterator]() {
				return iterator;
			},
			next():IteratorResult<V> {
				if (current) {
					let result = { value: current.value, done: false };
					current = current.next;
					return result;
				} else {
					return { value: undefined, done: true };
				}
			}
		};
		return iterator;
	}
	*/

	private addItemFirst(item: Item<K, V>): void {
		// First time Insert
		if (!this._head && !this._tail) {
			this._tail = item;
		} else if (!this._head) {
			throw new Error('Invalid list');
		} else {
			item.next = this._head;
			this._head.previous = item;
		}
		this._head = item;
	}

	private addItemLast(item: Item<K, V>): void {
		// First time Insert
		if (!this._head && !this._tail) {
			this._head = item;
		} else if (!this._tail) {
			throw new Error('Invalid list');
		} else {
			item.previous = this._tail;
			this._tail.next = item;
		}
		this._tail = item;
	}

	private removeItem(item: Item<K, V>): void {
		if (item === this._head && item === this._tail) {
			this._head = undefined;
			this._tail = undefined;
		}
		else if (item === this._head) {
			this._head = item.next;
		}
		else if (item === this._tail) {
			this._tail = item.previous;
		}
		else {
			const next = item.next;
			const previous = item.previous;
			if (!next || !previous) {
				throw new Error('Invalid list');
			}
			next.previous = previous;
			previous.next = next;
		}
	}

	private touch(item: Item<K, V>, touch: Touch): void {
		if (!this._head || !this._tail) {
			throw new Error('Invalid list');
		}
		if ((touch !== Touch.First && touch !== Touch.Last)) {
			return;
		}

		if (touch === Touch.First) {
			if (item === this._head) {
				return;
			}

			const next = item.next;
			const previous = item.previous;

			// Unlink the item
			if (item === this._tail) {
				// previous must be defined since item was not head but is tail
				// So there are more than on item in the map
				previous!.next = undefined;
				this._tail = previous;
			}
			else {
				// Both next and previous are not undefined since item was neither head nor tail.
				next!.previous = previous;
				previous!.next = next;
			}

			// Insert the node at head
			item.previous = undefined;
			item.next = this._head;
			this._head.previous = item;
			this._head = item;
		} else if (touch === Touch.Last) {
			if (item === this._tail) {
				return;
			}

			const next = item.next;
			const previous = item.previous;

			// Unlink the item.
			if (item === this._head) {
				// next must be defined since item was not tail but is head
				// So there are more than on item in the map
				next!.previous = undefined;
				this._head = next;
			} else {
				// Both next and previous are not undefined since item was neither head nor tail.
				next!.previous = previous;
				previous!.next = next;
			}
			item.next = undefined;
			item.previous = this._tail;
			this._tail.next = item;
			this._tail = item;
		}
	}
838
}