UNPKG

binomial-heap

Version:

No dependency binomial heap, compatible with typescript, can have custom compare function

110 lines (109 loc) 4.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getIndex = exports.equality = exports.getDefaultComparator = exports.mergeComparators = exports.typeComparisonAnalyzer = exports.preSanityCheck = exports.sanityCheck = exports.extendCompare = exports.getValue = void 0; exports.getValue = function (item) { if (item instanceof Date) { return item.getTime(); } switch (typeof item) { case 'function': case 'object': throw Error('Cannot compare functions or objects by default, need a compare function for that'); case 'boolean': case 'undefined': return item ? 1 : 0; case 'bigint': case 'number': return item; case 'string': case 'symbol': return item.toString(); } throw Error('Could not find typeof.. this should never happen!'); }; exports.extendCompare = function (compare) { if (!compare || compare.length === 2) { return compare; } var singleParam = compare; return function (a, b) { return singleParam(a) - singleParam(b); }; }; var treeSanityCheck = function (tree, compare) { return tree .children .reduce(function (p, c) { return !p ? false : compare(c.item, tree.item) >= 0 && treeSanityCheck(c, compare); }, true); }; exports.sanityCheck = function (heap, compare) { return heap.reduce(function (p, c) { return !p ? false : treeSanityCheck(c, compare); }, true); }; exports.preSanityCheck = function (a, b, compare) { return typeof (a[0] && a[0].item) === typeof (b[0] && b[0].item) && !compare; }; exports.typeComparisonAnalyzer = function (items) { return items.reduce(function (p, item) { if (item instanceof Date) { if (p === 'number') { return 'Date'; } } if (typeof item === 'string') { if (isNaN(Date.parse(item))) { return 'string'; } else if (p === 'number' || p === 'Date') { return 'Date'; } return 'string'; } return p; }, 'number'); }; exports.mergeComparators = function (a, b) { if (a === 'string' || b === 'string') { return 'string'; } if (a === 'Date' || b === 'Date') { return 'Date'; } return 'number'; }; exports.getDefaultComparator = function (kindOfCompare) { return function (a, b) { var valueA = exports.getValue(a); var valueB = exports.getValue(b); if (typeof valueA === 'number' && typeof valueB === 'number') { if (kindOfCompare === 'number' || kindOfCompare === 'Date') { return valueA - valueB; } return valueA.toString().localeCompare(valueB.toString()); } if (kindOfCompare === 'Date') { return new Date(valueA).getTime() - new Date(valueB).getTime(); } return valueA.toString().localeCompare(valueB.toString()); }; }; var findInSubTree = function (tree, item, compare) { if (!compare(tree.item, item)) { return tree; } return tree.children.reduce(function (p, c) { return Boolean(p) ? p : (compare(c.item, item) <= 0 ? findInSubTree(c, item, compare) : p); }, null); }; var find = function (heap, item, compare, pass) { return heap.reduce(function (p, tree, i) { if (p || compare(item, tree.item) < 0) { return p; } var found = findInSubTree(tree, item, compare); return found !== null ? [found, pass === undefined ? i : pass] : p; }, null); }; var seek = function (heap, compare, pass) { return heap.reduce(function (p, c, i) { if (Boolean(p)) { return p; } if (compare(c.item)) { return [c, pass === undefined ? i : pass]; } return seek(c.children, compare, i); }, null); }; var treeEquality = function (a, b, compare) { return a.children.length === b.children.length && !compare(a.item, b.item) && a.children.reduce(function (p, c, i) { return !p ? false : b.children[i] && treeEquality(c, b.children[i], compare); }, a.children.length === b.children.length); }; exports.equality = function (a, b, compare) { return a.length === b.length && a.reduce(function (p, c, i) { return !p ? false : treeEquality(c, b[i], compare); }, true); }; exports.getIndex = function (heap, compareTo, compare) { return typeof compareTo === 'function' ? seek(heap, compareTo) : find(heap, compareTo, compare); };