-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityqueue.min.js
6 lines (6 loc) · 2.52 KB
/
priorityqueue.min.js
1
2
3
4
5
6
if("undefined"===typeof net)var net={};"jstege1206"in net||(net.jstege1206={});
(function(){function b(a){this.queue=[];this.queueLength=0;this.comparator="undefined"!=typeof a&&"comparator"in a&&"function"==typeof a.comparator?a.comparator:p;this.equals="undefined"!=typeof a&&"equals"in a&&"function"==typeof a.equals?a.equals:q;"undefined"!=typeof a&&"data"in a&&"object"==typeof a.data&&this.setData(a.data,a.data.length)}function f(a,c,d,b,e){for(var g=b/2|0;a<g;){var h=2*a+1,k=d[h],f=h+1;f<b&&0<e(k,d[f])&&(h=f,k=d[h]);if(0>=e(c,k))break;d[a]=k;a=h}d[a]=c}function m(a,c,d,b){for(;0<
a;){var e=(a-1)/2|0,g=d[e];if(0<=b(c,g))break;d[a]=g;a=e}d[a]=c}function n(a,c,d,b){if(null!=a)for(var e=0;e<d;e++)if(b(a,c[e]))return e;return-1}var p=function(a,c){return null==a?1:null==c?-1:a-c},q=function(a,c){return a==c};b.prototype.clear=function(){this.queue=[];this.queueLength=0};b.prototype.peek=function(){return 0==this.queueLength?null:this.queue[0]};b.prototype.poll=function(){if(0==this.queueLength)return null;this.queueLength--;var a=this.queue[0],c=this.queue[this.queueLength];delete this.queue[this.queueLength];
0!=this.queueLength&&f(0,c,this.queue,this.queueLength,this.comparator);return a};b.prototype.offer=function(a){m(this.queueLength++,a,this.queue,this.comparator)};b.prototype.remove=function(a){a=n(a,this.queue,this.queueLength,this.equals);if(-1==a)return!1;this.queueLength--;var c=this.queue,b=this.queueLength,l=this.comparator,e=this.equals;if(b==a)delete c[b];else{var g=c[b];c[b]=null;f(a,g,c,b,l);e(c[a],g)&&m(a,g,c,l)}return!0};b.prototype.contains=function(a){return-1!=n(a,this.queue,this.queueLength,
this.equals)};b.prototype.getData=function(){return 0<arguments.length&&arguments[0]?this.queue:this.queue.slice()};b.prototype.setData=function(a,b){var d=[];if(a.forEach)a.forEach(function(a){d.push(a)});else for(var f in a)a.hasOwnProperty(f)&&d.push(a[f]);this.queue=d;this.queueLength=b?b:a.length;this.heapify()};b.prototype.getComparator=function(){return this.comparator};b.prototype.getEquals=function(){return this.equals};b.prototype.size=function(){return this.queueLength};b.prototype.heapify=
function(){for(var a=this.queueLength/2|0;0<=a;a--)f(a,this.queue[a],this.queue,this.queueLength,this.comparator)};b.prototype.clone=function(){return new net.jstege1206.PriorityQueue({comparator:this.comparator,equals:this.equals,data:this.queue.slice()})};net.jstege1206.PriorityQueue=b})();"undefined"!==typeof module&&"undefined"!==typeof module.exports&&(module.exports=exports=PriorityQueue=net.jstege1206.PriorityQueue);