@inst/vscode-bin-darwin
Version:
BINARY ONLY - VSCode binary deployment for macOS
286 lines (284 loc) • 9.38 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
var Touch;
(function (Touch) {
Touch.None = 0;
Touch.First = 1;
Touch.Last = 2;
})(Touch = exports.Touch || (exports.Touch = {}));
var LinkedMap = /** @class */ (function () {
function LinkedMap() {
this._map = new Map();
this._head = undefined;
this._tail = undefined;
this._size = 0;
}
LinkedMap.prototype.clear = function () {
this._map.clear();
this._head = undefined;
this._tail = undefined;
this._size = 0;
};
LinkedMap.prototype.isEmpty = function () {
return !this._head && !this._tail;
};
Object.defineProperty(LinkedMap.prototype, "size", {
get: function () {
return this._size;
},
enumerable: true,
configurable: true
});
LinkedMap.prototype.has = function (key) {
return this._map.has(key);
};
LinkedMap.prototype.get = function (key) {
var item = this._map.get(key);
if (!item) {
return undefined;
}
return item.value;
};
LinkedMap.prototype.set = function (key, value, touch) {
if (touch === void 0) { touch = Touch.None; }
var item = this._map.get(key);
if (item) {
item.value = value;
if (touch !== Touch.None) {
this.touch(item, touch);
}
}
else {
item = { key: key, value: 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++;
}
};
LinkedMap.prototype.delete = function (key) {
var item = this._map.get(key);
if (!item) {
return false;
}
this._map.delete(key);
this.removeItem(item);
this._size--;
return true;
};
LinkedMap.prototype.shift = function () {
if (!this._head && !this._tail) {
return undefined;
}
if (!this._head || !this._tail) {
throw new Error('Invalid list');
}
var item = this._head;
this._map.delete(item.key);
this.removeItem(item);
this._size--;
return item.value;
};
LinkedMap.prototype.forEach = function (callbackfn, thisArg) {
var 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;
}
};
LinkedMap.prototype.forEachReverse = function (callbackfn, thisArg) {
var 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;
}
};
LinkedMap.prototype.values = function () {
var result = [];
var current = this._head;
while (current) {
result.push(current.value);
current = current.next;
}
return result;
};
LinkedMap.prototype.keys = function () {
var result = [];
var current = this._head;
while (current) {
result.push(current.key);
current = current.next;
}
return result;
};
/* JSON RPC run 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;
}
*/
LinkedMap.prototype.addItemFirst = function (item) {
// 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;
};
LinkedMap.prototype.addItemLast = function (item) {
// 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;
};
LinkedMap.prototype.removeItem = function (item) {
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 {
var next = item.next;
var previous = item.previous;
if (!next || !previous) {
throw new Error('Invalid list');
}
next.previous = previous;
previous.next = next;
}
};
LinkedMap.prototype.touch = function (item, touch) {
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;
}
var next = item.next;
var 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;
}
var next = item.next;
var 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;
}
};
return LinkedMap;
}());
exports.LinkedMap = LinkedMap;