-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityqueue.min.js
4 lines (4 loc) · 1.2 KB
/
priorityqueue.min.js
1
2
3
4
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).PriorityQueue=i()}(this,(function(){"use strict";
/**
* @copyright can-dy-jack 2023
*/var t=function(t,i){if(void 0===t&&(t=[]),void 0===i&&(i=function(t,i){return t===i?0:t>i?1:-1}),this.data=t,this.compare=i,this.size=t.length,this.size>0)for(var s=Math.floor(this.size/2)-1;s>=0;s--)this._up(s)};return t.prototype.push=function(t){if(this.data.push(t),this.size++,1!==this.size){for(var i=this.size-1;i>0;){var s=Math.floor((i-1)/2),a=this.data[s];if(this.compare(t,a)>=0)break;this.data[i]=a,i=s}this.data[i]=t}},t.prototype.peek=function(){return this.data[0]},t.prototype.pop=function(){if(!this.isEmpty()){var t=this.data[0];return this.size--,this.isEmpty()?this.data.pop():(this.data[0]=this.data.pop(),this._up(0),t)}},t.prototype._up=function(t){for(var i=Math.floor(this.size/2),s=this.data[t];t<i;){var a=2*t+1,e=a+2;if(e<this.size&&this.compare(this.data[e],this.data[a])<0&&(a=e),this.compare(this.data[a],s)>=0)break;this.data[t]=this.data[a],t=a}this.data[t]=s},t.prototype.isEmpty=function(){return 0===this.size},t}));