UNPKG

min-priority-queue-typed

Version:
16 lines 10.2 kB
"use strict";var minPriorityQueueTyped=(()=>{var f=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var M=(r,t,e)=>t in r?f(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var F=(r,t)=>{for(var e in t)f(r,e,{get:t[e],enumerable:!0})},C=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of H(t))!v.call(r,i)&&i!==e&&f(r,i,{get:()=>t[i],enumerable:!(n=I(t,i))||n.enumerable});return r};var x=r=>C(f({},"__esModule",{value:!0}),r);var l=(r,t,e)=>M(r,typeof t!="symbol"?t+"":t,e);var N={};F(N,{DFSOperation:()=>w,ERR:()=>c,FibonacciHeap:()=>b,FibonacciHeapNode:()=>p,Heap:()=>u,MinPriorityQueue:()=>R,PriorityQueue:()=>m,Range:()=>E,raise:()=>h});function h(r,t){throw new r(t)}var c={indexOutOfRange:(r,t,e,n)=>`${n?n+": ":""}Index ${r} is out of range [${t}, ${e}].`,invalidIndex:r=>`${r?r+": ":""}Index must be an integer.`,invalidArgument:(r,t)=>`${t?t+": ":""}${r}`,comparatorRequired:r=>`${r?r+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(r,t)=>`${t?t+": ":""}${r}`,notAFunction:(r,t)=>`${t?t+": ":""}${r} must be a function.`,invalidEntry:r=>`${r?r+": ":""}Each entry must be a [key, value] tuple.`,invalidNaN:r=>`${r?r+": ":""}NaN is not a valid key.`,invalidDate:r=>`${r?r+": ":""}Invalid Date key.`,reduceEmpty:r=>`${r?r+": ":""}Reduce of empty structure with no initial value.`,callbackReturnType:(r,t,e)=>`${e?e+": ":""}Callback must return ${r}; got ${t}.`,invalidOperation:(r,t)=>`${t?t+": ":""}${r}`,matrixDimensionMismatch:r=>`Matrix: Dimensions must be compatible for ${r}.`,matrixSingular:()=>"Matrix: Singular matrix, inverse does not exist.",matrixNotSquare:()=>"Matrix: Must be square for inversion.",matrixNotRectangular:()=>"Matrix: Must be rectangular for transposition.",matrixRowMismatch:(r,t)=>`Matrix: Expected row length ${r}, but got ${t}.`,orderStatisticNotEnabled:(r,t)=>`${t?t+": ":""}${r}() requires enableOrderStatistic: true.`};var w=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(w||{}),E=class{constructor(t,e,n=!0,i=!0){this.low=t;this.high=e;this.includeLow=n;this.includeHigh=i}isInRange(t,e){let n=this.includeLow?e(t,this.low)>=0:e(t,this.low)>0,i=this.includeHigh?e(t,this.high)<=0:e(t,this.high)<0;return n&&i}};var d=class{constructor(t){l(this,"_toElementFn");if(t){let{toElementFn:e}=t;typeof e=="function"?this._toElementFn=e:e&&h(TypeError,"toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...t){yield*this._getIterator(...t)}*values(){for(let t of this)yield t}every(t,e){let n=0;for(let i of this)if(e===void 0){if(!t(i,n++,this))return!1}else if(!t.call(e,i,n++,this))return!1;return!0}some(t,e){let n=0;for(let i of this)if(e===void 0){if(t(i,n++,this))return!0}else if(t.call(e,i,n++,this))return!0;return!1}forEach(t,e){let n=0;for(let i of this)e===void 0?t(i,n++,this):t.call(e,i,n++,this)}find(t,e){let n=0;for(let i of this)if(e===void 0){if(t(i,n++,this))return i}else if(t.call(e,i,n++,this))return i}has(t){for(let e of this)if(e===t)return!0;return!1}includes(t){return this.has(t)}*entries(){let t=0;for(let e of this)yield[t++,e]}*keys(){let t=0;for(let e of this)yield t++}reduce(t,e){let n=0,i=this[Symbol.iterator](),o;if(arguments.length>=2)o=e;else{let s=i.next();s.done&&h(TypeError,"Reduce of empty structure with no initial value"),o=s.value,n=1}for(let s of i)o=t(o,s,n++,this);return o}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var u=class r extends d{constructor(e=[],n){super(n);l(this,"_equals",Object.is);l(this,"_elements",[]);l(this,"_DEFAULT_COMPARATOR",(e,n)=>((typeof e=="object"||typeof n=="object")&&h(TypeError,c.comparatorRequired("Heap")),e>n?1:e<n?-1:0));l(this,"_comparator",this._DEFAULT_COMPARATOR);if(n){let{comparator:i}=n;i&&(this._comparator=i)}this.addMany(e)}get elements(){return this._elements}get size(){return this.elements.length}get leaf(){var e;return(e=this.elements[this.size-1])!=null?e:void 0}static from(e,n){return new this(e,n)}static heapify(e,n){return new r(e,n)}add(e){return this._elements.push(e),this._bubbleUp(this.elements.length-1)}addMany(e){let n=[];for(let i of e)if(this.toElementFn){let o=this.add(this.toElementFn(i));n.push(o)}else{let o=this.add(i);n.push(o)}return n}poll(){return this.pop()}pop(){if(this.elements.length===0)return;let e=this.elements[0],n=this.elements.pop();return this.elements.length&&(this.elements[0]=n,this._sinkDown(0,this.elements.length>>1)),e}peek(){return this.elements[0]}isEmpty(){return this.size===0}clear(){this._elements=[]}has(e){for(let n of this.elements)if(this._equals(n,e))return!0;return!1}delete(e){let n=-1;for(let i=0;i<this.elements.length;i++)if(this._equals(this.elements[i],e)){n=i;break}return n<0?!1:(n===0?this.pop():n===this.elements.length-1?this.elements.pop():(this.elements.splice(n,1,this.elements.pop()),this._bubbleUp(n),this._sinkDown(n,this.elements.length>>1)),!0)}deleteBy(e){return this.deleteWhere(e)}deleteWhere(e){let n=-1;for(let i=0;i<this.elements.length;i++)if(e(this.elements[i],i,this)){n=i;break}return n<0?!1:(n===0?this.pop():n===this.elements.length-1?this.elements.pop():(this.elements.splice(n,1,this.elements.pop()),this._bubbleUp(n),this._sinkDown(n,this.elements.length>>1)),!0)}setEquality(e){return this._equals=e,this}dfs(e="PRE"){let n=[],i=o=>{let s=2*o+1,a=s+1;o<this.size&&(e==="IN"?(i(s),n.push(this.elements[o]),i(a)):e==="PRE"?(n.push(this.elements[o]),i(s),i(a)):e==="POST"&&(i(s),i(a),n.push(this.elements[o])))};return i(0),n}fix(){let e=[];for(let n=Math.floor(this.size/2)-1;n>=0;n--)e.push(this._sinkDown(n,this.elements.length>>1));return e}sort(){let e=[],n=this._createInstance();for(let i of this.elements)n.add(i);for(;!n.isEmpty();){let i=n.poll();i!==void 0&&e.push(i)}return e}clone(){let e=this._createInstance();for(let n of this.elements)e.add(n);return e}filter(e,n){let i=this._createInstance(),o=0;for(let s of this)(n===void 0?e(s,o++,this):e.call(n,s,o++,this))?i.add(s):o++;return i}map(e,n,i){let{comparator:o,toElementFn:s,...a}=n!=null?n:{};o||h(TypeError,c.comparatorRequired("Heap.map"));let g=this._createLike([],{...a,comparator:o,toElementFn:s}),_=0;for(let k of this){let y=i===void 0?e(k,_++,this):e.call(i,k,_++,this);g.add(y)}return g}mapSame(e,n){let i=this._createInstance(),o=0;for(let s of this){let a=n===void 0?e(s,o++,this):e.call(n,s,o++,this);i.add(a)}return i}get comparator(){return this._comparator}*_getIterator(){for(let e of this.elements)yield e}_bubbleUp(e){let n=this.elements[e];for(;e>0;){let i=e-1>>1,o=this.elements[i];if(this.comparator(o,n)<=0)break;this.elements[e]=o,e=i}return this.elements[e]=n,!0}_sinkDown(e,n){let i=this.elements[e];for(;e<n;){let o=e<<1|1,s=o+1,a=this.elements[o];if(s<this.elements.length&&this.comparator(a,this.elements[s])>0&&(o=s,a=this.elements[s]),this.comparator(a,i)>=0)break;this.elements[e]=a,e=o}return this.elements[e]=i,!0}_createInstance(e){let n=this.constructor;return new n([],{comparator:this.comparator,toElementFn:this.toElementFn,...e!=null?e:{}})}_createLike(e=[],n){let i=this.constructor;return new i(e,n)}_spawnLike(e){return this._createLike([],e)}},p=class{constructor(t,e=0){l(this,"element");l(this,"degree");l(this,"left");l(this,"right");l(this,"child");l(this,"parent");l(this,"marked");this.element=t,this.degree=e,this.marked=!1}},b=class{constructor(t){l(this,"_root");l(this,"_size",0);l(this,"_min");l(this,"_comparator");this.clear(),this._comparator=t||this._defaultComparator,typeof this.comparator!="function"&&h(TypeError,c.notAFunction("comparator","FibonacciHeap"))}get root(){return this._root}get size(){return this._size}get min(){return this._min}get comparator(){return this._comparator}clear(){this._root=void 0,this._min=void 0,this._size=0}add(t){return this.push(t),!0}push(t){let e=this.createNode(t);return e.left=e,e.right=e,this.mergeWithRoot(e),(!this.min||this.comparator(e.element,this.min.element)<=0)&&(this._min=e),this._size++,!0}peek(){return this.min?this.min.element:void 0}consumeLinkedList(t){let e=[];if(!t)return e;let n=t,i=!1;for(;!(n===t&&i);)n===t&&(i=!0),e.push(n),n=n.right;return e}mergeWithChild(t,e){t.child?(e.right=t.child.right,e.left=t.child,t.child.right.left=e,t.child.right=e):t.child=e}poll(){return this.pop()}pop(){if(this._size===0)return;let t=this.min;if(t.child){let e=this.consumeLinkedList(t.child);for(let n of e)this.mergeWithRoot(n),n.parent=void 0}return this.removeFromRoot(t),t===t.right?(this._min=void 0,this._root=void 0):(this._min=t.right,this._consolidate()),this._size--,t.element}merge(t){if(t.size!==0){if(this.root&&t.root){let e=this.root,n=t.root,i=e.right,o=n.left;e.right=n,n.left=e,i.left=o,o.right=i}else!this.root&&t.root&&(this._root=t.root);(!this.min||t.min&&this.comparator(t.min.element,this.min.element)<0)&&(this._min=t.min),this._size+=t.size,t.clear()}}createNode(t){return new p(t)}isEmpty(){return this._size===0}_defaultComparator(t,e){return t<e?-1:t>e?1:0}mergeWithRoot(t){this.root?(t.right=this.root.right,t.left=this.root,this.root.right.left=t,this.root.right=t):this._root=t}removeFromRoot(t){this.root===t&&(this._root=t.right),t.left&&(t.left.right=t.right),t.right&&(t.right.left=t.left)}_link(t,e){this.removeFromRoot(t),t.left=t,t.right=t,this.mergeWithChild(e,t),e.degree++,t.parent=e}_consolidate(){let t=new Array(this._size),e=this.consumeLinkedList(this.root),n,i,o,s;for(let a of e){for(n=a,o=n.degree;t[o];)i=t[o],this.comparator(n.element,i.element)>0&&(s=n,n=i,i=s),this._link(i,n),t[o]=void 0,o++;t[o]=n}for(let a=0;a<t.length;a++)t[a]&&(!this.min||this.comparator(t[a].element,this.min.element)<=0)&&(this._min=t[a])}};var m=class extends u{constructor(t=[],e){super(t,e)}};var R=class extends m{constructor(t=[],e){super(t,e)}};return x(N);})(); /** * data-structure-typed * * @author Pablo Zeng * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com> * @license MIT License */ /** * data-structure-typed * * @author Kirk Qi * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com> * @license MIT License */ //# sourceMappingURL=max-priority-queue-typed.min.js.map