Initial Sample.

This commit is contained in:
anand committed 2024-06-03 20:23:50 +05:30
1 parent ef2b65f673
commit 5269ec3c66
2575 files changed
+282312

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
'use strict';
// A [trie](https://en.wikipedia.org/wiki/Trie) data structure that holds
// object keys weakly, yet can also hold non-object keys, unlike the
// native `WeakMap`.
// If no makeData function is supplied, the looked-up data will be an empty,
// null-prototype Object.
var defaultMakeData = function () { return Object.create(null); };
// Useful for processing arguments objects as well as arrays.
var _a = Array.prototype, forEach = _a.forEach, slice = _a.slice;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Trie = /** @class */ (function () {
function Trie(weakness, makeData) {
if (weakness === void 0) { weakness = true; }
if (makeData === void 0) { makeData = defaultMakeData; }
this.weakness = weakness;
this.makeData = makeData;
}
Trie.prototype.lookup = function () {
return this.lookupArray(arguments);
};
Trie.prototype.lookupArray = function (array) {
var node = this;
forEach.call(array, function (key) { return node = node.getChildTrie(key); });
return hasOwnProperty.call(node, "data")
? node.data
: node.data = this.makeData(slice.call(array));
};
Trie.prototype.peek = function () {
return this.peekArray(arguments);
};
Trie.prototype.peekArray = function (array) {
var node = this;
for (var i = 0, len = array.length; node && i < len; ++i) {
var map = node.mapFor(array[i], false);
node = map && map.get(array[i]);
}
return node && node.data;
};
Trie.prototype.remove = function () {
return this.removeArray(arguments);
};
Trie.prototype.removeArray = function (array) {
var data;
if (array.length) {
var head = array[0];
var map = this.mapFor(head, false);
var child = map && map.get(head);
if (child) {
data = child.removeArray(slice.call(array, 1));
if (!child.data && !child.weak && !(child.strong && child.strong.size)) {
map.delete(head);
}
}
}
else {
data = this.data;
delete this.data;
}
return data;
};
Trie.prototype.getChildTrie = function (key) {
var map = this.mapFor(key, true);
var child = map.get(key);
if (!child)
map.set(key, child = new Trie(this.weakness, this.makeData));
return child;
};
Trie.prototype.mapFor = function (key, create) {
return this.weakness && isObjRef(key)
? this.weak || (create ? this.weak = new WeakMap : void 0)
: this.strong || (create ? this.strong = new Map : void 0);
};
return Trie;
}());
function isObjRef(value) {
switch (typeof value) {
case "object":
if (value === null)
break;
// Fall through to return true...
case "function":
return true;
}
return false;
}
exports.Trie = Trie;
//# sourceMappingURL=bundle.cjs.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"bundle.cjs","sources":["es5/index.js"],"sourcesContent":["// A [trie](https://en.wikipedia.org/wiki/Trie) data structure that holds\n// object keys weakly, yet can also hold non-object keys, unlike the\n// native `WeakMap`.\n// If no makeData function is supplied, the looked-up data will be an empty,\n// null-prototype Object.\nvar defaultMakeData = function () { return Object.create(null); };\n// Useful for processing arguments objects as well as arrays.\nvar _a = Array.prototype, forEach = _a.forEach, slice = _a.slice;\nvar hasOwnProperty = Object.prototype.hasOwnProperty;\nvar Trie = /** @class */ (function () {\n function Trie(weakness, makeData) {\n if (weakness === void 0) { weakness = true; }\n if (makeData === void 0) { makeData = defaultMakeData; }\n this.weakness = weakness;\n this.makeData = makeData;\n }\n Trie.prototype.lookup = function () {\n return this.lookupArray(arguments);\n };\n Trie.prototype.lookupArray = function (array) {\n var node = this;\n forEach.call(array, function (key) { return node = node.getChildTrie(key); });\n return hasOwnProperty.call(node, \"data\")\n ? node.data\n : node.data = this.makeData(slice.call(array));\n };\n Trie.prototype.peek = function () {\n return this.peekArray(arguments);\n };\n Trie.prototype.peekArray = function (array) {\n var node = this;\n for (var i = 0, len = array.length; node && i < len; ++i) {\n var map = node.mapFor(array[i], false);\n node = map && map.get(array[i]);\n }\n return node && node.data;\n };\n Trie.prototype.remove = function () {\n return this.removeArray(arguments);\n };\n Trie.prototype.removeArray = function (array) {\n var data;\n if (array.length) {\n var head = array[0];\n var map = this.mapFor(head, false);\n var child = map && map.get(head);\n if (child) {\n data = child.removeArray(slice.call(array, 1));\n if (!child.data && !child.weak && !(child.strong && child.strong.size)) {\n map.delete(head);\n }\n }\n }\n else {\n data = this.data;\n delete this.data;\n }\n return data;\n };\n Trie.prototype.getChildTrie = function (key) {\n var map = this.mapFor(key, true);\n var child = map.get(key);\n if (!child)\n map.set(key, child = new Trie(this.weakness, this.makeData));\n return child;\n };\n Trie.prototype.mapFor = function (key, create) {\n return this.weakness && isObjRef(key)\n ? this.weak || (create ? this.weak = new WeakMap : void 0)\n : this.strong || (create ? this.strong = new Map : void 0);\n };\n return Trie;\n}());\nexport { Trie };\nfunction isObjRef(value) {\n switch (typeof value) {\n case \"object\":\n if (value === null)\n break;\n // Fall through to return true...\n case \"function\":\n return true;\n }\n return false;\n}\n//# sourceMappingURL=index.js.map"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA,IAAI,eAAe,GAAG,YAAY,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;AAClE;AACA,IAAI,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,OAAO,GAAG,EAAE,CAAC,OAAO,EAAE,KAAK,GAAG,EAAE,CAAC,KAAK,CAAC;AACjE,IAAI,cAAc,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;AAClD,IAAC,IAAI,kBAAkB,YAAY;AACtC,IAAI,SAAS,IAAI,CAAC,QAAQ,EAAE,QAAQ,EAAE;AACtC,QAAQ,IAAI,QAAQ,KAAK,KAAK,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAC,EAAE;AACrD,QAAQ,IAAI,QAAQ,KAAK,KAAK,CAAC,EAAE,EAAE,QAAQ,GAAG,eAAe,CAAC,EAAE;AAChE,QAAQ,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,QAAQ,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,KAAK;AACL,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,YAAY;AACxC,QAAQ,OAAO,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;AAC3C,KAAK,CAAC;AACN,IAAI,IAAI,CAAC,SAAS,CAAC,WAAW,GAAG,UAAU,KAAK,EAAE;AAClD,QA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/Trie) data structure that holds
// object keys weakly, yet can also hold non-object keys, unlike the
// native `WeakMap`.
// If no makeData function is supplied, the looked-up data will be an empty,
// null-prototype Object.
var defaultMakeData = function () { return Object.create(null); };
// Useful for processing arguments objects as well as arrays.
var _a = Array.prototype, forEach = _a.forEach, slice = _a.slice;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Trie = /** @class */ (function () {
function Trie(weakness, makeData) {
if (weakness === void 0) { weakness = true; }
if (makeData === void 0) { makeData = defaultMakeData; }
this.weakness = weakness;
this.makeData = makeData;
}
Trie.prototype.lookup = function () {
return this.lookupArray(arguments);
};
Trie.prototype.lookupArray = function (array) {
var node = this;
forEach.call(array, function (key) { return node = node.getChildTrie(key); });
return hasOwnProperty.call(node, "data")
? node.data
: node.data = this.makeData(slice.call(array));
};
Trie.prototype.peek = function () {
return this.peekArray(arguments);
};
Trie.prototype.peekArray = function (array) {
var node = this;
for (var i = 0, len = array.length; node && i < len; ++i) {
var map = node.mapFor(array[i], false);
node = map && map.get(array[i]);
}
return node && node.data;
};
Trie.prototype.remove = function () {
return this.removeArray(arguments);
};
Trie.prototype.removeArray = function (array) {
var data;
if (array.length) {
var head = array[0];
var map = this.mapFor(head, false);
var child = map && map.get(head);
if (child) {
data = child.removeArray(slice.call(array, 1));
if (!child.data && !child.weak && !(child.strong && child.strong.size)) {
map.delete(head);
}
}
}
else {
data = this.data;
delete this.data;
}
return data;
};
Trie.prototype.getChildTrie = function (key) {
var map = this.mapFor(key, true);
var child = map.get(key);
if (!child)
map.set(key, child = new Trie(this.weakness, this.makeData));
return child;
};
Trie.prototype.mapFor = function (key, create) {
return this.weakness && isObjRef(key)
? this.weak || (create ? this.weak = new WeakMap : void 0)
: this.strong || (create ? this.strong = new Map : void 0);
};
return Trie;
}());
function isObjRef(value) {
switch (typeof value) {
case "object":
if (value === null)
break;
// Fall through to return true...
case "function":
return true;
}
return false;
}
exports.Trie = Trie;
//# sourceMappingURL=bundle.cjs.map
+16
View File
@@ -0,0 +1,16 @@
export declare class Trie<Data> {
private weakness;
private makeData;
private weak?;
private strong?;
private data?;
constructor(weakness?: boolean, makeData?: (array: any[]) => Data);
lookup<T extends any[]>(...array: T): Data;
lookupArray<T extends IArguments | any[]>(array: T): Data;
peek<T extends any[]>(...array: T): Data | undefined;
peekArray<T extends IArguments | any[]>(array: T): Data | undefined;
remove(...array: any[]): Data | undefined;
removeArray<T extends IArguments | any[]>(array: T): Data | undefined;
private getChildTrie;
private mapFor;
}
+82
View File
@@ -0,0 +1,82 @@
// A [trie](https://en.wikipedia.org/wiki/Trie) data structure that holds
// object keys weakly, yet can also hold non-object keys, unlike the
// native `WeakMap`.
// If no makeData function is supplied, the looked-up data will be an empty,
// null-prototype Object.
const defaultMakeData = () => Object.create(null);
// Useful for processing arguments objects as well as arrays.
const { forEach, slice } = Array.prototype;
const { hasOwnProperty } = Object.prototype;
export class Trie {
constructor(weakness = true, makeData = defaultMakeData) {
this.weakness = weakness;
this.makeData = makeData;
}
lookup() {
return this.lookupArray(arguments);
}
lookupArray(array) {
let node = this;
forEach.call(array, key => node = node.getChildTrie(key));
return hasOwnProperty.call(node, "data")
? node.data
: node.data = this.makeData(slice.call(array));
}
peek() {
return this.peekArray(arguments);
}
peekArray(array) {
let node = this;
for (let i = 0, len = array.length; node && i < len; ++i) {
const map = node.mapFor(array[i], false);
node = map && map.get(array[i]);
}
return node && node.data;
}
remove() {
return this.removeArray(arguments);
}
removeArray(array) {
let data;
if (array.length) {
const head = array[0];
const map = this.mapFor(head, false);
const child = map && map.get(head);
if (child) {
data = child.removeArray(slice.call(array, 1));
if (!child.data && !child.weak && !(child.strong && child.strong.size)) {
map.delete(head);
}
}
}
else {
data = this.data;
delete this.data;
}
return data;
}
getChildTrie(key) {
const map = this.mapFor(key, true);
let child = map.get(key);
if (!child)
map.set(key, child = new Trie(this.weakness, this.makeData));
return child;
}
mapFor(key, create) {
return this.weakness && isObjRef(key)
? this.weak || (create ? this.weak = new WeakMap : void 0)
: this.strong || (create ? this.strong = new Map : void 0);
}
}
function isObjRef(value) {
switch (typeof value) {
case "object":
if (value === null)
break;
// Fall through to return true...
case "function":
return true;
}
return false;
}
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,oEAAoE;AACpE,oBAAoB;AAEpB,4EAA4E;AAC5E,yBAAyB;AACzB,MAAM,eAAe,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAElD,6DAA6D;AAC7D,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,SAAS,CAAC;AAC3C,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC,SAAS,CAAC;AAE5C,MAAM,OAAO,IAAI;IAQf,YACU,WAAW,IAAI,EACf,WAAmC,eAAe;QADlD,aAAQ,GAAR,QAAQ,CAAO;QACf,aAAQ,GAAR,QAAQ,CAA0C;IACzD,CAAC;IAGG,MAAM;QACX,OAAO,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IACrC,CAAC;IAEM,WAAW,CAA+B,KAAQ;QACvD,IAAI,IAAI,GAAe,IAAI,CAAC;QAC5B,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC,IAAI,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1D,OAAO,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC;YACtC,CAAC,CAAC,IAAI,CAAC,IAAY;YACnB,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACnD,CAAC;IAGM,IAAI;QACT,OAAO,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAEM,SAAS,CA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