UNPKG

@difizen/mana-core

Version:

1,196 lines (1,189 loc) 47.2 kB
var _Symbol$toStringTag, _Symbol$iterator2, _Symbol$toStringTag2, _Symbol$iterator3; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* eslint-disable @typescript-eslint/no-this-alias */ /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { CharCode, compare, compareIgnoreCase, compareSubstring, compareSubstringIgnoreCase } from '@difizen/mana-common'; import { URI } from 'vscode-uri'; export function getOrSet(map, key, value) { var result = map.get(key); if (result === undefined) { result = value; map.set(key, result); } return result; } export var StringIterator = /*#__PURE__*/function () { function StringIterator() { _classCallCheck(this, StringIterator); this._value = ''; this._pos = 0; } _createClass(StringIterator, [{ key: "reset", value: function reset(key) { this._value = key; this._pos = 0; return this; } }, { key: "next", value: function next() { this._pos += 1; return this; } }, { key: "hasNext", value: function hasNext() { return this._pos < this._value.length - 1; } }, { key: "cmp", value: function cmp(a) { var aCode = a.charCodeAt(0); var thisCode = this._value.charCodeAt(this._pos); return aCode - thisCode; } }, { key: "value", value: function value() { return this._value[this._pos]; } }]); return StringIterator; }(); export var ConfigKeysIterator = /*#__PURE__*/function () { function ConfigKeysIterator() { var _caseSensitive = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; _classCallCheck(this, ConfigKeysIterator); this._caseSensitive = true; this._caseSensitive = _caseSensitive; } _createClass(ConfigKeysIterator, [{ key: "reset", value: function reset(key) { this._value = key; this._from = 0; this._to = 0; return this.next(); } }, { key: "hasNext", value: function hasNext() { return this._to < this._value.length; } }, { key: "next", value: function next() { // this._data = key.split(/[\\/]/).filter(s => !!s); this._from = this._to; var justSeps = true; for (; this._to < this._value.length; this._to++) { var ch = this._value.charCodeAt(this._to); if (ch === CharCode.Period) { if (justSeps) { this._from++; } else { break; } } else { justSeps = false; } } return this; } }, { key: "cmp", value: function cmp(a) { return this._caseSensitive ? compareSubstring(a, this._value, 0, a.length, this._from, this._to) : compareSubstringIgnoreCase(a, this._value, 0, a.length, this._from, this._to); } }, { key: "value", value: function value() { return this._value.substring(this._from, this._to); } }]); return ConfigKeysIterator; }(); export var PathIterator = /*#__PURE__*/function () { function PathIterator() { var _splitOnBackslash = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var _caseSensitive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; _classCallCheck(this, PathIterator); this._splitOnBackslash = true; this._caseSensitive = true; this._splitOnBackslash = _splitOnBackslash; this._caseSensitive = _caseSensitive; } _createClass(PathIterator, [{ key: "reset", value: function reset(key) { this._value = key.replace(/\\$|\/$/, ''); this._from = 0; this._to = 0; return this.next(); } }, { key: "hasNext", value: function hasNext() { return this._to < this._value.length; } }, { key: "next", value: function next() { // this._data = key.split(/[\\/]/).filter(s => !!s); this._from = this._to; var justSeps = true; for (; this._to < this._value.length; this._to++) { var ch = this._value.charCodeAt(this._to); if (ch === CharCode.Slash || this._splitOnBackslash && ch === CharCode.Backslash) { if (justSeps) { this._from++; } else { break; } } else { justSeps = false; } } return this; } }, { key: "cmp", value: function cmp(a) { return this._caseSensitive ? compareSubstring(a, this._value, 0, a.length, this._from, this._to) : compareSubstringIgnoreCase(a, this._value, 0, a.length, this._from, this._to); } }, { key: "value", value: function value() { return this._value.substring(this._from, this._to); } }]); return PathIterator; }(); var UriIteratorState = /*#__PURE__*/function (UriIteratorState) { UriIteratorState[UriIteratorState["Scheme"] = 1] = "Scheme"; UriIteratorState[UriIteratorState["Authority"] = 2] = "Authority"; UriIteratorState[UriIteratorState["Path"] = 3] = "Path"; UriIteratorState[UriIteratorState["Query"] = 4] = "Query"; UriIteratorState[UriIteratorState["Fragment"] = 5] = "Fragment"; return UriIteratorState; }(UriIteratorState || {}); export var UriIterator = /*#__PURE__*/function () { // eslint-disable-next-line @typescript-eslint/no-unused-vars function UriIterator(_ignorePathCasing) { _classCallCheck(this, UriIterator); this._states = []; this._stateIdx = 0; this._ignorePathCasing = _ignorePathCasing; } _createClass(UriIterator, [{ key: "reset", value: function reset(key) { this._value = key; this._states = []; if (this._value.scheme) { this._states.push(UriIteratorState.Scheme); } if (this._value.authority) { this._states.push(UriIteratorState.Authority); } if (this._value.path) { this._pathIterator = new PathIterator(false, !this._ignorePathCasing(key)); this._pathIterator.reset(key.path); if (this._pathIterator.value()) { this._states.push(UriIteratorState.Path); } } if (this._value.query) { this._states.push(UriIteratorState.Query); } if (this._value.fragment) { this._states.push(UriIteratorState.Fragment); } this._stateIdx = 0; return this; } }, { key: "next", value: function next() { if (this._states[this._stateIdx] === UriIteratorState.Path && this._pathIterator.hasNext()) { this._pathIterator.next(); } else { this._stateIdx += 1; } return this; } }, { key: "hasNext", value: function hasNext() { return this._states[this._stateIdx] === UriIteratorState.Path && this._pathIterator.hasNext() || this._stateIdx < this._states.length - 1; } }, { key: "cmp", value: function cmp(a) { if (this._states[this._stateIdx] === UriIteratorState.Scheme) { return compareIgnoreCase(a, this._value.scheme); } if (this._states[this._stateIdx] === UriIteratorState.Authority) { return compareIgnoreCase(a, this._value.authority); } if (this._states[this._stateIdx] === UriIteratorState.Path) { return this._pathIterator.cmp(a); } if (this._states[this._stateIdx] === UriIteratorState.Query) { return compare(a, this._value.query); } if (this._states[this._stateIdx] === UriIteratorState.Fragment) { return compare(a, this._value.fragment); } throw new Error(); } }, { key: "value", value: function value() { if (this._states[this._stateIdx] === UriIteratorState.Scheme) { return this._value.scheme; } if (this._states[this._stateIdx] === UriIteratorState.Authority) { return this._value.authority; } if (this._states[this._stateIdx] === UriIteratorState.Path) { return this._pathIterator.value(); } if (this._states[this._stateIdx] === UriIteratorState.Query) { return this._value.query; } if (this._states[this._stateIdx] === UriIteratorState.Fragment) { return this._value.fragment; } throw new Error(); } }]); return UriIterator; }(); var TernarySearchTreeNode = /*#__PURE__*/function () { function TernarySearchTreeNode() { _classCallCheck(this, TernarySearchTreeNode); } _createClass(TernarySearchTreeNode, [{ key: "isEmpty", value: function isEmpty() { return !this.left && !this.mid && !this.right && !this.value; } }]); return TernarySearchTreeNode; }(); export var TernarySearchTree = /*#__PURE__*/function (_Symbol$iterator) { function TernarySearchTree(segments) { _classCallCheck(this, TernarySearchTree); this._iter = segments; } _createClass(TernarySearchTree, [{ key: "clear", value: function clear() { this._root = undefined; } }, { key: "set", value: function set(key, element) { var iter = this._iter.reset(key); var node; if (!this._root) { this._root = new TernarySearchTreeNode(); this._root.segment = iter.value(); } node = this._root; // eslint-disable-next-line no-constant-condition while (true) { var val = iter.cmp(node.segment); if (val > 0) { // left if (!node.left) { node.left = new TernarySearchTreeNode(); node.left.segment = iter.value(); } node = node.left; } else if (val < 0) { // right if (!node.right) { node.right = new TernarySearchTreeNode(); node.right.segment = iter.value(); } node = node.right; } else if (iter.hasNext()) { // mid iter.next(); if (!node.mid) { node.mid = new TernarySearchTreeNode(); node.mid.segment = iter.value(); } node = node.mid; } else { break; } } var oldElement = node.value; node.value = element; node.key = key; return oldElement; } }, { key: "get", value: function get(key) { var _this$_getNode; return (_this$_getNode = this._getNode(key)) === null || _this$_getNode === void 0 ? void 0 : _this$_getNode.value; } }, { key: "_getNode", value: function _getNode(key) { var iter = this._iter.reset(key); var node = this._root; while (node) { var val = iter.cmp(node.segment); if (val > 0) { // left node = node.left; } else if (val < 0) { // right node = node.right; } else if (iter.hasNext()) { // mid iter.next(); node = node.mid; } else { break; } } return node; } }, { key: "has", value: function has(key) { var node = this._getNode(key); return !((node === null || node === void 0 ? void 0 : node.value) === undefined && (node === null || node === void 0 ? void 0 : node.mid) === undefined); } }, { key: "delete", value: function _delete(key) { return this._delete(key, false); } }, { key: "deleteSuperstr", value: function deleteSuperstr(key) { return this._delete(key, true); } }, { key: "_delete", value: function _delete(key, superStr) { var iter = this._iter.reset(key); var stack = []; var node = this._root; // find and unset node while (node) { var val = iter.cmp(node.segment); if (val > 0) { // left stack.push([1, node]); node = node.left; } else if (val < 0) { // right stack.push([-1, node]); node = node.right; } else if (iter.hasNext()) { // mid iter.next(); stack.push([0, node]); node = node.mid; } else { if (superStr) { // remove children node.left = undefined; node.mid = undefined; node.right = undefined; } else { // remove element node.value = undefined; } // clean up empty nodes while (stack.length > 0 && node.isEmpty()) { var _ref = stack.pop(), _ref2 = _slicedToArray(_ref, 2), dir = _ref2[0], parent = _ref2[1]; // eslint-disable-next-line default-case switch (dir) { case 1: parent.left = undefined; break; case 0: parent.mid = undefined; break; case -1: parent.right = undefined; break; } node = parent; } break; } } } }, { key: "findSubstr", value: function findSubstr(key) { var iter = this._iter.reset(key); var node = this._root; var candidate; while (node) { var val = iter.cmp(node.segment); if (val > 0) { // left node = node.left; } else if (val < 0) { // right node = node.right; } else if (iter.hasNext()) { // mid iter.next(); candidate = node.value || candidate; node = node.mid; } else { break; } } return node && node.value || candidate; } }, { key: "findSuperstr", value: function findSuperstr(key) { var iter = this._iter.reset(key); var node = this._root; while (node) { var val = iter.cmp(node.segment); if (val > 0) { // left node = node.left; } else if (val < 0) { // right node = node.right; } else if (iter.hasNext()) { // mid iter.next(); node = node.mid; } else { // collect if (!node.mid) { return undefined; } return this._entries(node.mid); } } return undefined; } }, { key: "forEach", value: function forEach(callback) { var _iterator = _createForOfIteratorHelper(this), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _step$value = _slicedToArray(_step.value, 2), _key = _step$value[0], _value = _step$value[1]; callback(_value, _key); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }, { key: _Symbol$iterator, value: /*#__PURE__*/_regeneratorRuntime().mark(function value() { return _regeneratorRuntime().wrap(function value$(_context) { while (1) switch (_context.prev = _context.next) { case 0: return _context.delegateYield(this._entries(this._root), "t0", 1); case 1: case "end": return _context.stop(); } }, value, this); }) }, { key: "_entries", value: /*#__PURE__*/_regeneratorRuntime().mark(function _entries(node) { return _regeneratorRuntime().wrap(function _entries$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: if (!node) { _context2.next = 7; break; } return _context2.delegateYield(this._entries(node.left), "t0", 2); case 2: if (!node.value) { _context2.next = 5; break; } _context2.next = 5; return [node.key, node.value]; case 5: return _context2.delegateYield(this._entries(node.mid), "t1", 6); case 6: return _context2.delegateYield(this._entries(node.right), "t2", 7); case 7: case "end": return _context2.stop(); } }, _entries, this); }) }], [{ key: "forUris", value: function forUris() { var ignorePathCasing = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () { return false; }; return new TernarySearchTree(new UriIterator(ignorePathCasing)); } }, { key: "forPaths", value: function forPaths() { return new TernarySearchTree(new PathIterator()); } }, { key: "forStrings", value: function forStrings() { return new TernarySearchTree(new StringIterator()); } }, { key: "forConfigKeys", value: function forConfigKeys() { return new TernarySearchTree(new ConfigKeysIterator()); } }]); return TernarySearchTree; }(Symbol.iterator); _Symbol$toStringTag = Symbol.toStringTag; _Symbol$iterator2 = Symbol.iterator; export var ResourceMap = /*#__PURE__*/function () { function ResourceMap(mapOrKeyFn, toKey) { _classCallCheck(this, ResourceMap); this[_Symbol$toStringTag] = 'ResourceMap'; if (mapOrKeyFn instanceof ResourceMap) { this.map = new Map(mapOrKeyFn.map); this.toKey = toKey !== null && toKey !== void 0 ? toKey : ResourceMap.defaultToKey; } else { this.map = new Map(); this.toKey = mapOrKeyFn !== null && mapOrKeyFn !== void 0 ? mapOrKeyFn : ResourceMap.defaultToKey; } } _createClass(ResourceMap, [{ key: "set", value: function set(resource, value) { this.map.set(this.toKey(resource), value); return this; } }, { key: "get", value: function get(resource) { return this.map.get(this.toKey(resource)); } }, { key: "has", value: function has(resource) { return this.map.has(this.toKey(resource)); } }, { key: "size", get: function get() { return this.map.size; } }, { key: "clear", value: function clear() { this.map.clear(); } }, { key: "delete", value: function _delete(resource) { return this.map.delete(this.toKey(resource)); } }, { key: "forEach", value: function forEach(clb, thisArg) { if (typeof thisArg !== 'undefined') { clb = clb.bind(thisArg); } var _iterator2 = _createForOfIteratorHelper(this.map), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _step2$value = _slicedToArray(_step2.value, 2), _index = _step2$value[0], _value2 = _step2$value[1]; clb(_value2, URI.parse(_index), this); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } }, { key: "values", value: function values() { return this.map.values(); } }, { key: "keys", value: /*#__PURE__*/_regeneratorRuntime().mark(function keys() { var _iterator3, _step3, _key2; return _regeneratorRuntime().wrap(function keys$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _iterator3 = _createForOfIteratorHelper(this.map.keys()); _context3.prev = 1; _iterator3.s(); case 3: if ((_step3 = _iterator3.n()).done) { _context3.next = 9; break; } _key2 = _step3.value; _context3.next = 7; return URI.parse(_key2); case 7: _context3.next = 3; break; case 9: _context3.next = 14; break; case 11: _context3.prev = 11; _context3.t0 = _context3["catch"](1); _iterator3.e(_context3.t0); case 14: _context3.prev = 14; _iterator3.f(); return _context3.finish(14); case 17: case "end": return _context3.stop(); } }, keys, this, [[1, 11, 14, 17]]); }) }, { key: "entries", value: /*#__PURE__*/_regeneratorRuntime().mark(function entries() { var _iterator4, _step4, tuple; return _regeneratorRuntime().wrap(function entries$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: _iterator4 = _createForOfIteratorHelper(this.map.entries()); _context4.prev = 1; _iterator4.s(); case 3: if ((_step4 = _iterator4.n()).done) { _context4.next = 9; break; } tuple = _step4.value; _context4.next = 7; return [URI.parse(tuple[0]), tuple[1]]; case 7: _context4.next = 3; break; case 9: _context4.next = 14; break; case 11: _context4.prev = 11; _context4.t0 = _context4["catch"](1); _iterator4.e(_context4.t0); case 14: _context4.prev = 14; _iterator4.f(); return _context4.finish(14); case 17: case "end": return _context4.stop(); } }, entries, this, [[1, 11, 14, 17]]); }) }, { key: _Symbol$iterator2, value: /*#__PURE__*/_regeneratorRuntime().mark(function value() { var _iterator5, _step5, item; return _regeneratorRuntime().wrap(function value$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: _iterator5 = _createForOfIteratorHelper(this.map); _context5.prev = 1; _iterator5.s(); case 3: if ((_step5 = _iterator5.n()).done) { _context5.next = 9; break; } item = _step5.value; _context5.next = 7; return [URI.parse(item[0]), item[1]]; case 7: _context5.next = 3; break; case 9: _context5.next = 14; break; case 11: _context5.prev = 11; _context5.t0 = _context5["catch"](1); _iterator5.e(_context5.t0); case 14: _context5.prev = 14; _iterator5.f(); return _context5.finish(14); case 17: case "end": return _context5.stop(); } }, value, this, [[1, 11, 14, 17]]); }) }]); return ResourceMap; }(); ResourceMap.defaultToKey = function (resource) { return resource.toString(); }; export var Touch = /*#__PURE__*/function (Touch) { Touch[Touch["None"] = 0] = "None"; Touch[Touch["AsOld"] = 1] = "AsOld"; Touch[Touch["AsNew"] = 2] = "AsNew"; return Touch; }({}); _Symbol$toStringTag2 = Symbol.toStringTag; _Symbol$iterator3 = Symbol.iterator; export var LinkedMap = /*#__PURE__*/function () { function LinkedMap() { _classCallCheck(this, LinkedMap); this[_Symbol$toStringTag2] = 'LinkedMap'; this._map = new Map(); this._head = undefined; this._tail = undefined; this._size = 0; this._state = 0; } _createClass(LinkedMap, [{ key: "clear", value: function clear() { this._map.clear(); this._head = undefined; this._tail = undefined; this._size = 0; this._state++; } }, { key: "isEmpty", value: function isEmpty() { return !this._head && !this._tail; } }, { key: "size", get: function get() { return this._size; } }, { key: "first", get: function get() { var _this$_head; return (_this$_head = this._head) === null || _this$_head === void 0 ? void 0 : _this$_head.value; } }, { key: "last", get: function get() { var _this$_tail; return (_this$_tail = this._tail) === null || _this$_tail === void 0 ? void 0 : _this$_tail.value; } }, { key: "has", value: function has(key) { return this._map.has(key); } }, { key: "get", value: function get(key) { var touch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Touch.None; var item = this._map.get(key); if (!item) { return undefined; } if (touch !== Touch.None) { this.touch(item, touch); } return item.value; } }, { key: "set", value: function set(key, value) { var touch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 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.AsOld: this.addItemFirst(item); break; case Touch.AsNew: this.addItemLast(item); break; default: this.addItemLast(item); break; } this._map.set(key, item); this._size++; } return this; } }, { key: "delete", value: function _delete(key) { return !!this.remove(key); } }, { key: "remove", value: function remove(key) { var item = this._map.get(key); if (!item) { return undefined; } this._map.delete(key); this.removeItem(item); this._size--; return item.value; } }, { key: "shift", value: function shift() { 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; } }, { key: "forEach", value: function forEach(callbackfn, thisArg) { var state = this._state; var current = this._head; while (current) { if (thisArg) { callbackfn.bind(thisArg)(current.value, current.key, this); } else { callbackfn(current.value, current.key, this); } if (this._state !== state) { throw new Error("LinkedMap got modified during iteration."); } current = current.next; } } }, { key: "keys", value: function keys() { var map = this; var state = this._state; var current = this._head; var iterator = _defineProperty(_defineProperty({}, Symbol.iterator, function () { return iterator; }), "next", function next() { if (map._state !== state) { throw new Error("LinkedMap got modified during iteration."); } if (current) { var result = { value: current.key, done: false }; current = current.next; return result; } return { value: undefined, done: true }; }); return iterator; } }, { key: "values", value: function values() { var map = this; var state = this._state; var current = this._head; var iterator = _defineProperty(_defineProperty({}, Symbol.iterator, function () { return iterator; }), "next", function next() { if (map._state !== state) { throw new Error("LinkedMap got modified during iteration."); } if (current) { var result = { value: current.value, done: false }; current = current.next; return result; } return { value: undefined, done: true }; }); return iterator; } }, { key: "entries", value: function entries() { var map = this; var state = this._state; var current = this._head; var iterator = _defineProperty(_defineProperty({}, Symbol.iterator, function () { return iterator; }), "next", function next() { if (map._state !== state) { throw new Error("LinkedMap got modified during iteration."); } if (current) { var result = { value: [current.key, current.value], done: false }; current = current.next; return result; } return { value: undefined, done: true }; }); return iterator; } }, { key: _Symbol$iterator3, value: function value() { return this.entries(); } }, { key: "trimOld", value: function trimOld(newSize) { if (newSize >= this.size) { return; } if (newSize === 0) { this.clear(); return; } var current = this._head; var currentSize = this.size; while (current && currentSize > newSize) { this._map.delete(current.key); current = current.next; currentSize--; } this._head = current; this._size = currentSize; if (current) { current.previous = undefined; } this._state++; } }, { key: "addItemFirst", value: function addItemFirst(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; this._state++; } }, { key: "addItemLast", value: function addItemLast(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; this._state++; } }, { key: "removeItem", value: function removeItem(item) { if (item === this._head && item === this._tail) { this._head = undefined; this._tail = undefined; } else if (item === this._head) { // This can only happend if size === 1 which is handle // by the case above. if (!item.next) { throw new Error('Invalid list'); } item.next.previous = undefined; this._head = item.next; } else if (item === this._tail) { // This can only happend if size === 1 which is handle // by the case above. if (!item.previous) { throw new Error('Invalid list'); } item.previous.next = undefined; 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; } item.next = undefined; item.previous = undefined; this._state++; } }, { key: "touch", value: function touch(item, _touch) { if (!this._head || !this._tail) { throw new Error('Invalid list'); } if (_touch !== Touch.AsOld && _touch !== Touch.AsNew) { return; } if (_touch === Touch.AsOld) { 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; this._state++; } else if (_touch === Touch.AsNew) { 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; this._state++; } } }, { key: "toJSON", value: function toJSON() { var data = []; this.forEach(function (value, key) { data.push([key, value]); }); return data; } }, { key: "fromJSON", value: function fromJSON(data) { this.clear(); var _iterator9 = _createForOfIteratorHelper(data), _step6; try { for (_iterator9.s(); !(_step6 = _iterator9.n()).done;) { var _step6$value = _slicedToArray(_step6.value, 2), _key3 = _step6$value[0], _value3 = _step6$value[1]; this.set(_key3, _value3); } } catch (err) { _iterator9.e(err); } finally { _iterator9.f(); } } }]); return LinkedMap; }();