UNPKG

priority-queue-typed

Version:
29 lines 11 kB
"use strict";var priorityQueueTyped=(()=>{var p=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var x=(i,t,e)=>t in i?p(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var N=(i,t)=>{for(var e in t)p(i,e,{get:t[e],enumerable:!0})},O=(i,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of F(t))!C.call(i,r)&&r!==e&&p(i,r,{get:()=>t[r],enumerable:!(n=M(t,r))||n.enumerable});return i};var $=i=>O(p({},"__esModule",{value:!0}),i);var l=(i,t,e)=>x(i,typeof t!="symbol"?t+"":t,e);var z={};N(z,{DFSOperation:()=>H,ERR:()=>u,FibonacciHeap:()=>b,FibonacciHeapNode:()=>E,Heap:()=>h,MaxHeap:()=>R,MaxPriorityQueue:()=>_,MinHeap:()=>g,MinPriorityQueue:()=>y,PriorityQueue:()=>m,Range:()=>d,raise:()=>c});function c(i,t){throw new i(t)}var u={indexOutOfRange:(i,t,e,n)=>`${n?n+": ":""}Index ${i} is out of range [${t}, ${e}].`,invalidIndex:i=>`${i?i+": ":""}Index must be an integer.`,invalidArgument:(i,t)=>`${t?t+": ":""}${i}`,comparatorRequired:i=>`${i?i+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(i,t)=>`${t?t+": ":""}${i}`,notAFunction:(i,t)=>`${t?t+": ":""}${i} must be a function.`,invalidEntry:i=>`${i?i+": ":""}Each entry must be a [key, value] tuple.`,invalidNaN:i=>`${i?i+": ":""}NaN is not a valid key.`,invalidDate:i=>`${i?i+": ":""}Invalid Date key.`,reduceEmpty:i=>`${i?i+": ":""}Reduce of empty structure with no initial value.`,callbackReturnType:(i,t,e)=>`${e?e+": ":""}Callback must return ${i}; got ${t}.`,invalidOperation:(i,t)=>`${t?t+": ":""}${i}`,matrixDimensionMismatch:i=>`Matrix: Dimensions must be compatible for ${i}.`,matrixSingular:()=>"Matrix: Singular matrix, inverse does not exist.",matrixNotSquare:()=>"Matrix: Must be square for inversion.",matrixNotRectangular:()=>"Matrix: Must be rectangular for transposition.",matrixRowMismatch:(i,t)=>`Matrix: Expected row length ${i}, but got ${t}.`,orderStatisticNotEnabled:(i,t)=>`${t?t+": ":""}${i}() requires enableOrderStatistic: true.`};var H=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(H||{}),d=class{constructor(t,e,n=!0,r=!0){this.low=t;this.high=e;this.includeLow=n;this.includeHigh=r}isInRange(t,e){let n=this.includeLow?e(t,this.low)>=0:e(t,this.low)>0,r=this.includeHigh?e(t,this.high)<=0:e(t,this.high)<0;return n&&r}};var f=class{constructor(t){l(this,"_toElementFn");if(t){let{toElementFn:e}=t;typeof e=="function"?this._toElementFn=e:e&&c(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 r of this)if(e===void 0){if(!t(r,n++,this))return!1}else if(!t.call(e,r,n++,this))return!1;return!0}some(t,e){let n=0;for(let r of this)if(e===void 0){if(t(r,n++,this))return!0}else if(t.call(e,r,n++,this))return!0;return!1}forEach(t,e){let n=0;for(let r of this)e===void 0?t(r,n++,this):t.call(e,r,n++,this)}find(t,e){let n=0;for(let r of this)if(e===void 0){if(t(r,n++,this))return r}else if(t.call(e,r,n++,this))return r}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,r=this[Symbol.iterator](),o;if(arguments.length>=2)o=e;else{let s=r.next();s.done&&c(TypeError,"Reduce of empty structure with no initial value"),o=s.value,n=1}for(let s of r)o=t(o,s,n++,this);return o}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var h=class i extends f{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")&&c(TypeError,u.comparatorRequired("Heap")),e>n?1:e<n?-1:0));l(this,"_comparator",this._DEFAULT_COMPARATOR);if(n){let{comparator:r}=n;r&&(this._comparator=r)}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 i(e,n)}add(e){return this._elements.push(e),this._bubbleUp(this.elements.length-1)}addMany(e){let n=[];for(let r of e)if(this.toElementFn){let o=this.add(this.toElementFn(r));n.push(o)}else{let o=this.add(r);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 r=0;r<this.elements.length;r++)if(this._equals(this.elements[r],e)){n=r;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 r=0;r<this.elements.length;r++)if(e(this.elements[r],r,this)){n=r;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=[],r=o=>{let s=2*o+1,a=s+1;o<this.size&&(e==="IN"?(r(s),n.push(this.elements[o]),r(a)):e==="PRE"?(n.push(this.elements[o]),r(s),r(a)):e==="POST"&&(r(s),r(a),n.push(this.elements[o])))};return r(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 r of this.elements)n.add(r);for(;!n.isEmpty();){let r=n.poll();r!==void 0&&e.push(r)}return e}clone(){let e=this._createInstance();for(let n of this.elements)e.add(n);return e}filter(e,n){let r=this._createInstance(),o=0;for(let s of this)(n===void 0?e(s,o++,this):e.call(n,s,o++,this))?r.add(s):o++;return r}map(e,n,r){let{comparator:o,toElementFn:s,...a}=n!=null?n:{};o||c(TypeError,u.comparatorRequired("Heap.map"));let k=this._createLike([],{...a,comparator:o,toElementFn:s}),I=0;for(let w of this){let v=r===void 0?e(w,I++,this):e.call(r,w,I++,this);k.add(v)}return k}mapSame(e,n){let r=this._createInstance(),o=0;for(let s of this){let a=n===void 0?e(s,o++,this):e.call(n,s,o++,this);r.add(a)}return r}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 r=e-1>>1,o=this.elements[r];if(this.comparator(o,n)<=0)break;this.elements[e]=o,e=r}return this.elements[e]=n,!0}_sinkDown(e,n){let r=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,r)>=0)break;this.elements[e]=a,e=o}return this.elements[e]=r,!0}_createInstance(e){let n=this.constructor;return new n([],{comparator:this.comparator,toElementFn:this.toElementFn,...e!=null?e:{}})}_createLike(e=[],n){let r=this.constructor;return new r(e,n)}_spawnLike(e){return this._createLike([],e)}},E=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"&&c(TypeError,u.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,r=!1;for(;!(n===t&&r);)n===t&&(r=!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,r=e.right,o=n.left;e.right=n,n.left=e,r.left=o,o.right=r}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 E(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,r,o,s;for(let a of e){for(n=a,o=n.degree;t[o];)r=t[o],this.comparator(n.element,r.element)>0&&(s=n,n=r,r=s),this._link(r,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 R=class extends h{constructor(t=[],e){super(t,{comparator:(n,r)=>((typeof n=="object"||typeof r=="object")&&c(TypeError,u.comparatorRequired("MaxHeap")),n<r?1:n>r?-1:0),...e})}};var g=class extends h{constructor(t=[],e){super(t,e)}};var m=class extends h{constructor(t=[],e){super(t,e)}};var y=class extends m{constructor(t=[],e){super(t,e)}};var _=class extends m{constructor(t=[],e){super(t,{comparator:(n,r)=>((typeof n=="object"||typeof r=="object")&&c(TypeError,u.comparatorRequired("MaxPriorityQueue")),n<r?1:n>r?-1:0),...e})}};return $(z);})(); /** * 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 */ /** * @remarks Time O(n log n), Space O(n). * data-structure-typed * @author Kirk Qi * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com> * @license MIT License */ /** * data-structure-typed * * @author Kirk Qi * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com> * @license MIT License */ //# sourceMappingURL=priority-queue-typed.min.js.map