@amaui/heap
Version:
7 lines (6 loc) • 6.71 kB
JavaScript
/** @license AmauiHeap v1.0.11117
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AmauiHeap={})}(this,(function(e){"use strict";var t="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function r(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var a={exports:{}};!function(e,r){Object.defineProperty(r,"__esModule",{value:!0});const a={},n="undefined"!=typeof window&&void 0!==window.document,i=!(void 0===t||!e.exports);r.default=function e(t,r,s={}){var o;const u=Object.assign(Object.assign({},a),s),{variant:l}=u,c=r&&"object"==typeof r&&Object.getPrototypeOf(r);switch(t){case"string":return"string"==typeof r;case"number":return"number"==typeof r&&!Number.isNaN(r);case"boolean":return"boolean"==typeof r;case"array":return Array.isArray(r);case"object":return"object"==typeof r&&!!r&&r.constructor===Object;case"object-like":return"object"==typeof r&&(null===r||r.constructor!==Object);case"class":return("object"==typeof r||"function"==typeof r)&&(/class/gi.test(String(r))||/class/gi.test(String(null==r?void 0:r.constructor)));case"function":return!!(r&&r instanceof Function);case"async":return!(!e("function",r)||!(n?"AsyncFunction"===r.constructor.name:r()instanceof Promise));case"map":return!(c!==Map.prototype);case"weakmap":return!(c!==WeakMap.prototype);case"set":return!(c!==Set.prototype);case"weakset":return!(c!==WeakSet.prototype);case"promise":return!(c!==Promise.prototype);case"int8array":return!(c!==Int8Array.prototype);case"uint8array":return!(c!==Uint8Array.prototype);case"uint8clampedarray":return!(c!==Uint8ClampedArray.prototype);case"int16array":return!(c!==Int16Array.prototype);case"uint16array":return!(c!==Uint16Array.prototype);case"int32array":return!(c!==Int32Array.prototype);case"uint32array":return!(c!==Uint32Array.prototype);case"float32array":return!(c!==Float32Array.prototype);case"float64array":return!(c!==Float64Array.prototype);case"bigint64array":return!(c!==BigInt64Array.prototype);case"biguint64array":return!(c!==BigUint64Array.prototype);case"typedarray":return e("int8array",r)||e("uint8array",r)||e("uint8clampedarray",r)||e("int16array",r)||e("uint16array",r)||e("int32array",r)||e("uint32array",r)||e("float32array",r)||e("float64array",r)||e("bigint64array",r)||e("biguint64array",r);case"dataview":return!(c!==DataView.prototype);case"arraybuffer":return!(c!==ArrayBuffer.prototype);case"sharedarraybuffer":return"undefined"!=typeof SharedArrayBuffer&&!(c!==SharedArrayBuffer.prototype);case"symbol":return!("symbol"!=typeof r);case"error":return!!(r&&r instanceof Error);case"date":return!!(r&&r instanceof Date);case"regexp":return!!(r&&r instanceof RegExp);case"arguments":return!(!r||"[object Arguments]"!==r.toString());case"null":return null===r;case"undefined":return void 0===r;case"blob":return n&&r instanceof Blob;case"buffer":return!(!i||"function"!=typeof(null===(o=null==r?void 0:r.constructor)||void 0===o?void 0:o.isBuffer)||!r.constructor.isBuffer(r));case"element":if(r)switch(l){case void 0:case"html":case"element":return n&&("object"==typeof HTMLElement?r instanceof HTMLElement:r&&"object"==typeof r&&null!==r&&1===r.nodeType&&"string"==typeof r.nodeName);case"node":return n&&("object"==typeof Node?r instanceof Node:r&&"object"==typeof r&&null!==r&&"number"==typeof r.nodeType&&"string"==typeof r.nodeName);case"react":return r.elementType||r.hasOwnProperty("$$typeof");default:return!1}return!1;case"simple":return e("string",r,u)||e("number",r,u)||e("boolean",r,u)||e("undefined",r,u)||e("null",r,u);case"not-array-object":return!e("array",r,u)&&!e("object",r,u);default:return!1}}}(a,a.exports);var n=r(a.exports);class i{constructor(e){this.value=e}}class s{static get min(){return new s("min")}static get max(){return new s("max")}static make(e,t){return new s(t).make(e)}static left(e){return 2*e+1}static right(e){return 2*e+2}static parent(e){return Math.floor((e-1)/2)}static isPriority(e){return 0===e}static isLeft(e){return!!(0!==e&&e%2)}static isRight(e){return!(0===e||e%2)}static isLeaf(e,t){return e>=Math.floor(t.length/2)&&e<=t.length-1}constructor(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"min";var t,r,a;this.variant=e,a=[],(r="values")in(t=this)?Object.defineProperty(t,r,{value:a,enumerable:!0,configurable:!0,writable:!0}):t[r]=a}get array(){return this.values.map((e=>e.value))}get first(){return this.values[0]}get leafs(){return this.values.filter(((e,t)=>s.isLeaf(t,this.values)))}get remove(){if(!this.values.length)return;const e=this.first,t=this.values.pop();return this.values[0]=t,this.heapifyDown(),e}add(e){const t=e instanceof i?e:new i(e);return this.values.push(t),this.heapifyUp(),this}swap(e,t){return[this.values[e],this.values[t]]=[this.values[t],this.values[e]],this}make(e){this.values=e.map((e=>e instanceof i?e:new i(e)));for(let e=Math.floor(this.values.length/2);e>=0;e--)this.heapifyDown(e);return this}forEach(e){n("function",e)&&this.values.forEach(((t,r)=>{const a=this.values[s.parent(r)],n=this.values[s.left(r)],i=this.values[s.right(r)],o=s.isPriority(r),u=s.isLeaf(r,this.values),l=s.isLeft(r),c=s.isRight(r);e(t,r,a,n,i,o,u,l,c)}))}heapifyUp(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.values.length-1;if(this.values.length>0){let i=e,o=s.parent(i);for(;i>0&&("min"===this.variant?(null===(t=this.values[o])||void 0===t?void 0:t.value)>(null===(r=this.values[i])||void 0===r?void 0:r.value):(null===(a=this.values[o])||void 0===a?void 0:a.value)<(null===(n=this.values[i])||void 0===n?void 0:n.value));){var t,r,a,n;this.swap(o,i),i=o,o=s.parent(i)}}}heapifyDown(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;if(!s.isLeaf(e,this.values)){var t,r,a,n,i,o,u,l;const c=e,f=s.left(c),p=s.right(c);let y=c;("min"===this.variant?(null===(t=this.values[f])||void 0===t?void 0:t.value)<(null===(r=this.values[y])||void 0===r?void 0:r.value):(null===(a=this.values[f])||void 0===a?void 0:a.value)>(null===(n=this.values[y])||void 0===n?void 0:n.value))&&(y=f),("min"===this.variant?(null===(i=this.values[p])||void 0===i?void 0:i.value)<(null===(o=this.values[y])||void 0===o?void 0:o.value):(null===(u=this.values[p])||void 0===u?void 0:u.value)>(null===(l=this.values[y])||void 0===l?void 0:l.value))&&(y=p),c!==y&&(this.swap(c,y),this.heapifyDown(y))}}}e.AmauiHeap=s,e.AmauiNode=i,Object.defineProperty(e,"__esModule",{value:!0})}));