mirror of
https://github.com/imezx/Warp.git
synced 2025-04-24 15:10:03 +00:00
687 lines
25 KiB
JavaScript
687 lines
25 KiB
JavaScript
'use strict';
|
|
|
|
/******************************************************************************
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
***************************************************************************** */
|
|
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
|
|
|
|
function __values(o) {
|
|
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
if (m) return m.call(o);
|
|
if (o && typeof o.length === "number") return {
|
|
next: function () {
|
|
if (o && i >= o.length) o = void 0;
|
|
return { value: o && o[i++], done: !o };
|
|
}
|
|
};
|
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
}
|
|
|
|
function __read(o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m) return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
}
|
|
catch (error) { e = { error: error }; }
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
}
|
|
finally { if (e) throw e.error; }
|
|
}
|
|
return ar;
|
|
}
|
|
|
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
var e = new Error(message);
|
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
};
|
|
|
|
/** @ignore */
|
|
var ENTRIES = 'ENTRIES';
|
|
/** @ignore */
|
|
var KEYS = 'KEYS';
|
|
/** @ignore */
|
|
var VALUES = 'VALUES';
|
|
/** @ignore */
|
|
var LEAF = '';
|
|
/**
|
|
* @private
|
|
*/
|
|
var TreeIterator = /** @class */ (function () {
|
|
function TreeIterator(set, type) {
|
|
var node = set._tree;
|
|
var keys = Array.from(node.keys());
|
|
this.set = set;
|
|
this._type = type;
|
|
this._path = keys.length > 0 ? [{ node: node, keys: keys }] : [];
|
|
}
|
|
TreeIterator.prototype.next = function () {
|
|
var value = this.dive();
|
|
this.backtrack();
|
|
return value;
|
|
};
|
|
TreeIterator.prototype.dive = function () {
|
|
if (this._path.length === 0) {
|
|
return { done: true, value: undefined };
|
|
}
|
|
var _a = last$1(this._path), node = _a.node, keys = _a.keys;
|
|
if (last$1(keys) === LEAF) {
|
|
return { done: false, value: this.result() };
|
|
}
|
|
var child = node.get(last$1(keys));
|
|
this._path.push({ node: child, keys: Array.from(child.keys()) });
|
|
return this.dive();
|
|
};
|
|
TreeIterator.prototype.backtrack = function () {
|
|
if (this._path.length === 0) {
|
|
return;
|
|
}
|
|
var keys = last$1(this._path).keys;
|
|
keys.pop();
|
|
if (keys.length > 0) {
|
|
return;
|
|
}
|
|
this._path.pop();
|
|
this.backtrack();
|
|
};
|
|
TreeIterator.prototype.key = function () {
|
|
return this.set._prefix + this._path
|
|
.map(function (_a) {
|
|
var keys = _a.keys;
|
|
return last$1(keys);
|
|
})
|
|
.filter(function (key) { return key !== LEAF; })
|
|
.join('');
|
|
};
|
|
TreeIterator.prototype.value = function () {
|
|
return last$1(this._path).node.get(LEAF);
|
|
};
|
|
TreeIterator.prototype.result = function () {
|
|
switch (this._type) {
|
|
case VALUES: return this.value();
|
|
case KEYS: return this.key();
|
|
default: return [this.key(), this.value()];
|
|
}
|
|
};
|
|
TreeIterator.prototype[Symbol.iterator] = function () {
|
|
return this;
|
|
};
|
|
return TreeIterator;
|
|
}());
|
|
var last$1 = function (array) {
|
|
return array[array.length - 1];
|
|
};
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
var fuzzySearch = function (node, query, maxDistance) {
|
|
var results = new Map();
|
|
if (query === undefined)
|
|
return results;
|
|
// Number of columns in the Levenshtein matrix.
|
|
var n = query.length + 1;
|
|
// Matching terms can never be longer than N + maxDistance.
|
|
var m = n + maxDistance;
|
|
// Fill first matrix row and column with numbers: 0 1 2 3 ...
|
|
var matrix = new Uint8Array(m * n).fill(maxDistance + 1);
|
|
for (var j = 0; j < n; ++j)
|
|
matrix[j] = j;
|
|
for (var i = 1; i < m; ++i)
|
|
matrix[i * n] = i;
|
|
recurse(node, query, maxDistance, results, matrix, 1, n, '');
|
|
return results;
|
|
};
|
|
// Modified version of http://stevehanov.ca/blog/?id=114
|
|
// This builds a Levenshtein matrix for a given query and continuously updates
|
|
// it for nodes in the radix tree that fall within the given maximum edit
|
|
// distance. Keeping the same matrix around is beneficial especially for larger
|
|
// edit distances.
|
|
//
|
|
// k a t e <-- query
|
|
// 0 1 2 3 4
|
|
// c 1 1 2 3 4
|
|
// a 2 2 1 2 3
|
|
// t 3 3 2 1 [2] <-- edit distance
|
|
// ^
|
|
// ^ term in radix tree, rows are added and removed as needed
|
|
var recurse = function (node, query, maxDistance, results, matrix, m, n, prefix) {
|
|
var e_1, _a;
|
|
var offset = m * n;
|
|
try {
|
|
key: for (var _b = __values(node.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var key = _c.value;
|
|
if (key === LEAF) {
|
|
// We've reached a leaf node. Check if the edit distance acceptable and
|
|
// store the result if it is.
|
|
var distance = matrix[offset - 1];
|
|
if (distance <= maxDistance) {
|
|
results.set(prefix, [node.get(key), distance]);
|
|
}
|
|
}
|
|
else {
|
|
// Iterate over all characters in the key. Update the Levenshtein matrix
|
|
// and check if the minimum distance in the last row is still within the
|
|
// maximum edit distance. If it is, we can recurse over all child nodes.
|
|
var i = m;
|
|
for (var pos = 0; pos < key.length; ++pos, ++i) {
|
|
var char = key[pos];
|
|
var thisRowOffset = n * i;
|
|
var prevRowOffset = thisRowOffset - n;
|
|
// Set the first column based on the previous row, and initialize the
|
|
// minimum distance in the current row.
|
|
var minDistance = matrix[thisRowOffset];
|
|
var jmin = Math.max(0, i - maxDistance - 1);
|
|
var jmax = Math.min(n - 1, i + maxDistance);
|
|
// Iterate over remaining columns (characters in the query).
|
|
for (var j = jmin; j < jmax; ++j) {
|
|
var different = char !== query[j];
|
|
// It might make sense to only read the matrix positions used for
|
|
// deletion/insertion if the characters are different. But we want to
|
|
// avoid conditional reads for performance reasons.
|
|
var rpl = matrix[prevRowOffset + j] + +different;
|
|
var del = matrix[prevRowOffset + j + 1] + 1;
|
|
var ins = matrix[thisRowOffset + j] + 1;
|
|
var dist = matrix[thisRowOffset + j + 1] = Math.min(rpl, del, ins);
|
|
if (dist < minDistance)
|
|
minDistance = dist;
|
|
}
|
|
// Because distance will never decrease, we can stop. There will be no
|
|
// matching child nodes.
|
|
if (minDistance > maxDistance) {
|
|
continue key;
|
|
}
|
|
}
|
|
recurse(node.get(key), query, maxDistance, results, matrix, i, n, prefix + key);
|
|
}
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
};
|
|
|
|
/**
|
|
* A class implementing the same interface as a standard JavaScript
|
|
* [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
|
|
* with string keys, but adding support for efficiently searching entries with
|
|
* prefix or fuzzy search. This class is used internally by {@link MiniSearch}
|
|
* as the inverted index data structure. The implementation is a radix tree
|
|
* (compressed prefix tree).
|
|
*
|
|
* Since this class can be of general utility beyond _MiniSearch_, it is
|
|
* exported by the `minisearch` package and can be imported (or required) as
|
|
* `minisearch/SearchableMap`.
|
|
*
|
|
* @typeParam T The type of the values stored in the map.
|
|
*/
|
|
var SearchableMap = /** @class */ (function () {
|
|
/**
|
|
* The constructor is normally called without arguments, creating an empty
|
|
* map. In order to create a {@link SearchableMap} from an iterable or from an
|
|
* object, check {@link SearchableMap.from} and {@link
|
|
* SearchableMap.fromObject}.
|
|
*
|
|
* The constructor arguments are for internal use, when creating derived
|
|
* mutable views of a map at a prefix.
|
|
*/
|
|
function SearchableMap(tree, prefix) {
|
|
if (tree === void 0) { tree = new Map(); }
|
|
if (prefix === void 0) { prefix = ''; }
|
|
this._size = undefined;
|
|
this._tree = tree;
|
|
this._prefix = prefix;
|
|
}
|
|
/**
|
|
* Creates and returns a mutable view of this {@link SearchableMap},
|
|
* containing only entries that share the given prefix.
|
|
*
|
|
* ### Usage:
|
|
*
|
|
* ```javascript
|
|
* let map = new SearchableMap()
|
|
* map.set("unicorn", 1)
|
|
* map.set("universe", 2)
|
|
* map.set("university", 3)
|
|
* map.set("unique", 4)
|
|
* map.set("hello", 5)
|
|
*
|
|
* let uni = map.atPrefix("uni")
|
|
* uni.get("unique") // => 4
|
|
* uni.get("unicorn") // => 1
|
|
* uni.get("hello") // => undefined
|
|
*
|
|
* let univer = map.atPrefix("univer")
|
|
* univer.get("unique") // => undefined
|
|
* univer.get("universe") // => 2
|
|
* univer.get("university") // => 3
|
|
* ```
|
|
*
|
|
* @param prefix The prefix
|
|
* @return A {@link SearchableMap} representing a mutable view of the original
|
|
* Map at the given prefix
|
|
*/
|
|
SearchableMap.prototype.atPrefix = function (prefix) {
|
|
var e_1, _a;
|
|
if (!prefix.startsWith(this._prefix)) {
|
|
throw new Error('Mismatched prefix');
|
|
}
|
|
var _b = __read(trackDown(this._tree, prefix.slice(this._prefix.length)), 2), node = _b[0], path = _b[1];
|
|
if (node === undefined) {
|
|
var _c = __read(last(path), 2), parentNode = _c[0], key = _c[1];
|
|
try {
|
|
for (var _d = __values(parentNode.keys()), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
var k = _e.value;
|
|
if (k !== LEAF && k.startsWith(key)) {
|
|
var node_1 = new Map();
|
|
node_1.set(k.slice(key.length), parentNode.get(k));
|
|
return new SearchableMap(node_1, prefix);
|
|
}
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
}
|
|
return new SearchableMap(node, prefix);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
|
|
*/
|
|
SearchableMap.prototype.clear = function () {
|
|
this._size = undefined;
|
|
this._tree.clear();
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete
|
|
* @param key Key to delete
|
|
*/
|
|
SearchableMap.prototype.delete = function (key) {
|
|
this._size = undefined;
|
|
return remove(this._tree, key);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries
|
|
* @return An iterator iterating through `[key, value]` entries.
|
|
*/
|
|
SearchableMap.prototype.entries = function () {
|
|
return new TreeIterator(this, ENTRIES);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach
|
|
* @param fn Iteration function
|
|
*/
|
|
SearchableMap.prototype.forEach = function (fn) {
|
|
var e_2, _a;
|
|
try {
|
|
for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
|
|
fn(key, value, this);
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
};
|
|
/**
|
|
* Returns a Map of all the entries that have a key within the given edit
|
|
* distance from the search key. The keys of the returned Map are the matching
|
|
* keys, while the values are two-element arrays where the first element is
|
|
* the value associated to the key, and the second is the edit distance of the
|
|
* key to the search key.
|
|
*
|
|
* ### Usage:
|
|
*
|
|
* ```javascript
|
|
* let map = new SearchableMap()
|
|
* map.set('hello', 'world')
|
|
* map.set('hell', 'yeah')
|
|
* map.set('ciao', 'mondo')
|
|
*
|
|
* // Get all entries that match the key 'hallo' with a maximum edit distance of 2
|
|
* map.fuzzyGet('hallo', 2)
|
|
* // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] }
|
|
*
|
|
* // In the example, the "hello" key has value "world" and edit distance of 1
|
|
* // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2
|
|
* // (change "e" to "a", delete "o")
|
|
* ```
|
|
*
|
|
* @param key The search key
|
|
* @param maxEditDistance The maximum edit distance (Levenshtein)
|
|
* @return A Map of the matching keys to their value and edit distance
|
|
*/
|
|
SearchableMap.prototype.fuzzyGet = function (key, maxEditDistance) {
|
|
return fuzzySearch(this._tree, key, maxEditDistance);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
|
|
* @param key Key to get
|
|
* @return Value associated to the key, or `undefined` if the key is not
|
|
* found.
|
|
*/
|
|
SearchableMap.prototype.get = function (key) {
|
|
var node = lookup(this._tree, key);
|
|
return node !== undefined ? node.get(LEAF) : undefined;
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
|
|
* @param key Key
|
|
* @return True if the key is in the map, false otherwise
|
|
*/
|
|
SearchableMap.prototype.has = function (key) {
|
|
var node = lookup(this._tree, key);
|
|
return node !== undefined && node.has(LEAF);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
|
|
* @return An `Iterable` iterating through keys
|
|
*/
|
|
SearchableMap.prototype.keys = function () {
|
|
return new TreeIterator(this, KEYS);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set
|
|
* @param key Key to set
|
|
* @param value Value to associate to the key
|
|
* @return The {@link SearchableMap} itself, to allow chaining
|
|
*/
|
|
SearchableMap.prototype.set = function (key, value) {
|
|
if (typeof key !== 'string') {
|
|
throw new Error('key must be a string');
|
|
}
|
|
this._size = undefined;
|
|
var node = createPath(this._tree, key);
|
|
node.set(LEAF, value);
|
|
return this;
|
|
};
|
|
Object.defineProperty(SearchableMap.prototype, "size", {
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size
|
|
*/
|
|
get: function () {
|
|
if (this._size) {
|
|
return this._size;
|
|
}
|
|
/** @ignore */
|
|
this._size = 0;
|
|
var iter = this.entries();
|
|
while (!iter.next().done)
|
|
this._size += 1;
|
|
return this._size;
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Updates the value at the given key using the provided function. The function
|
|
* is called with the current value at the key, and its return value is used as
|
|
* the new value to be set.
|
|
*
|
|
* ### Example:
|
|
*
|
|
* ```javascript
|
|
* // Increment the current value by one
|
|
* searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1)
|
|
* ```
|
|
*
|
|
* If the value at the given key is or will be an object, it might not require
|
|
* re-assignment. In that case it is better to use `fetch()`, because it is
|
|
* faster.
|
|
*
|
|
* @param key The key to update
|
|
* @param fn The function used to compute the new value from the current one
|
|
* @return The {@link SearchableMap} itself, to allow chaining
|
|
*/
|
|
SearchableMap.prototype.update = function (key, fn) {
|
|
if (typeof key !== 'string') {
|
|
throw new Error('key must be a string');
|
|
}
|
|
this._size = undefined;
|
|
var node = createPath(this._tree, key);
|
|
node.set(LEAF, fn(node.get(LEAF)));
|
|
return this;
|
|
};
|
|
/**
|
|
* Fetches the value of the given key. If the value does not exist, calls the
|
|
* given function to create a new value, which is inserted at the given key
|
|
* and subsequently returned.
|
|
*
|
|
* ### Example:
|
|
*
|
|
* ```javascript
|
|
* const map = searchableMap.fetch('somekey', () => new Map())
|
|
* map.set('foo', 'bar')
|
|
* ```
|
|
*
|
|
* @param key The key to update
|
|
* @param defaultValue A function that creates a new value if the key does not exist
|
|
* @return The existing or new value at the given key
|
|
*/
|
|
SearchableMap.prototype.fetch = function (key, initial) {
|
|
if (typeof key !== 'string') {
|
|
throw new Error('key must be a string');
|
|
}
|
|
this._size = undefined;
|
|
var node = createPath(this._tree, key);
|
|
var value = node.get(LEAF);
|
|
if (value === undefined) {
|
|
node.set(LEAF, value = initial());
|
|
}
|
|
return value;
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
|
|
* @return An `Iterable` iterating through values.
|
|
*/
|
|
SearchableMap.prototype.values = function () {
|
|
return new TreeIterator(this, VALUES);
|
|
};
|
|
/**
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator
|
|
*/
|
|
SearchableMap.prototype[Symbol.iterator] = function () {
|
|
return this.entries();
|
|
};
|
|
/**
|
|
* Creates a {@link SearchableMap} from an `Iterable` of entries
|
|
*
|
|
* @param entries Entries to be inserted in the {@link SearchableMap}
|
|
* @return A new {@link SearchableMap} with the given entries
|
|
*/
|
|
SearchableMap.from = function (entries) {
|
|
var e_3, _a;
|
|
var tree = new SearchableMap();
|
|
try {
|
|
for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
|
|
var _b = __read(entries_1_1.value, 2), key = _b[0], value = _b[1];
|
|
tree.set(key, value);
|
|
}
|
|
}
|
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
finally {
|
|
try {
|
|
if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
|
|
}
|
|
finally { if (e_3) throw e_3.error; }
|
|
}
|
|
return tree;
|
|
};
|
|
/**
|
|
* Creates a {@link SearchableMap} from the iterable properties of a JavaScript object
|
|
*
|
|
* @param object Object of entries for the {@link SearchableMap}
|
|
* @return A new {@link SearchableMap} with the given entries
|
|
*/
|
|
SearchableMap.fromObject = function (object) {
|
|
return SearchableMap.from(Object.entries(object));
|
|
};
|
|
return SearchableMap;
|
|
}());
|
|
var trackDown = function (tree, key, path) {
|
|
var e_4, _a;
|
|
if (path === void 0) { path = []; }
|
|
if (key.length === 0 || tree == null) {
|
|
return [tree, path];
|
|
}
|
|
try {
|
|
for (var _b = __values(tree.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var k = _c.value;
|
|
if (k !== LEAF && key.startsWith(k)) {
|
|
path.push([tree, k]); // performance: update in place
|
|
return trackDown(tree.get(k), key.slice(k.length), path);
|
|
}
|
|
}
|
|
}
|
|
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_4) throw e_4.error; }
|
|
}
|
|
path.push([tree, key]); // performance: update in place
|
|
return trackDown(undefined, '', path);
|
|
};
|
|
var lookup = function (tree, key) {
|
|
var e_5, _a;
|
|
if (key.length === 0 || tree == null) {
|
|
return tree;
|
|
}
|
|
try {
|
|
for (var _b = __values(tree.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var k = _c.value;
|
|
if (k !== LEAF && key.startsWith(k)) {
|
|
return lookup(tree.get(k), key.slice(k.length));
|
|
}
|
|
}
|
|
}
|
|
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_5) throw e_5.error; }
|
|
}
|
|
};
|
|
// Create a path in the radix tree for the given key, and returns the deepest
|
|
// node. This function is in the hot path for indexing. It avoids unnecessary
|
|
// string operations and recursion for performance.
|
|
var createPath = function (node, key) {
|
|
var e_6, _a;
|
|
var keyLength = key.length;
|
|
outer: for (var pos = 0; node && pos < keyLength;) {
|
|
try {
|
|
for (var _b = (e_6 = void 0, __values(node.keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var k = _c.value;
|
|
// Check whether this key is a candidate: the first characters must match.
|
|
if (k !== LEAF && key[pos] === k[0]) {
|
|
var len = Math.min(keyLength - pos, k.length);
|
|
// Advance offset to the point where key and k no longer match.
|
|
var offset = 1;
|
|
while (offset < len && key[pos + offset] === k[offset])
|
|
++offset;
|
|
var child_1 = node.get(k);
|
|
if (offset === k.length) {
|
|
// The existing key is shorter than the key we need to create.
|
|
node = child_1;
|
|
}
|
|
else {
|
|
// Partial match: we need to insert an intermediate node to contain
|
|
// both the existing subtree and the new node.
|
|
var intermediate = new Map();
|
|
intermediate.set(k.slice(offset), child_1);
|
|
node.set(key.slice(pos, pos + offset), intermediate);
|
|
node.delete(k);
|
|
node = intermediate;
|
|
}
|
|
pos += offset;
|
|
continue outer;
|
|
}
|
|
}
|
|
}
|
|
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_6) throw e_6.error; }
|
|
}
|
|
// Create a final child node to contain the final suffix of the key.
|
|
var child = new Map();
|
|
node.set(key.slice(pos), child);
|
|
return child;
|
|
}
|
|
return node;
|
|
};
|
|
var remove = function (tree, key) {
|
|
var _a = __read(trackDown(tree, key), 2), node = _a[0], path = _a[1];
|
|
if (node === undefined) {
|
|
return;
|
|
}
|
|
node.delete(LEAF);
|
|
if (node.size === 0) {
|
|
cleanup(path);
|
|
}
|
|
else if (node.size === 1) {
|
|
var _b = __read(node.entries().next().value, 2), key_1 = _b[0], value = _b[1];
|
|
merge(path, key_1, value);
|
|
}
|
|
};
|
|
var cleanup = function (path) {
|
|
if (path.length === 0) {
|
|
return;
|
|
}
|
|
var _a = __read(last(path), 2), node = _a[0], key = _a[1];
|
|
node.delete(key);
|
|
if (node.size === 0) {
|
|
cleanup(path.slice(0, -1));
|
|
}
|
|
else if (node.size === 1) {
|
|
var _b = __read(node.entries().next().value, 2), key_2 = _b[0], value = _b[1];
|
|
if (key_2 !== LEAF) {
|
|
merge(path.slice(0, -1), key_2, value);
|
|
}
|
|
}
|
|
};
|
|
var merge = function (path, key, value) {
|
|
if (path.length === 0) {
|
|
return;
|
|
}
|
|
var _a = __read(last(path), 2), node = _a[0], nodeKey = _a[1];
|
|
node.set(nodeKey + key, value);
|
|
node.delete(nodeKey);
|
|
};
|
|
var last = function (array) {
|
|
return array[array.length - 1];
|
|
};
|
|
|
|
module.exports = SearchableMap;
|
|
//# sourceMappingURL=SearchableMap.cjs.map
|