UNPKG

@heap-data-structure/binomial-heap

Version:
3 lines (2 loc) 5.09 kB
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t||self).binomialHeap={})}(this,function(t){function n(t,n){this.value=t,this.children=n}function e(t,n){this.value=t,this.children=n,this.parent=null}function i(t,n){this.value=t,this.next=n}function l(){this.top=null,this.bottom=null}n.prototype.merge=function(t,n){return t(this.value,n.value)<=0?(this.children=this.children.concat(n),this):(n.children=n.children.concat(this),n)},n.prototype.detach=function(){return this.children.splice(0),this},n.prototype.setparent=function(t){},e.prototype.rank=function(){return this.children.length},e.prototype.merge=function(t,n){return t(this.value,n.value)<=0?(this.children=this.children.concat(n),n.parent=this,this):(n.children=n.children.concat(this),this.parent=n,n)},e.prototype.detach=function(){return this.children.splice(0),this.parent=null,this},e.prototype.setparent=function(t){this.parent=t},l.prototype.empty=function(){return null===this.top},l.prototype.push=function(t){this.top=new i(t,this.top),null===this.bottom&&(this.bottom=this.top)},l.prototype.pop=function(){var t=this.top.value;return this.top=this.top.next,null===this.top&&(this.bottom=null),t},l.prototype.meld=function(t){null===this.bottom?this.top=t.top:this.bottom.next=t.top,this.bottom=t.bottom},t.BinomialHeap=function(t){var n=function(t,n,e,i){for(var l=i-n.length;l-- >0;)n.push(null);var r=n.length;for(l=i;l<r&&null!==n[l];++l)e=e.merge(t,n[l]),n[l]=null;l===r&&n.push(null),n[l]=e},e=function(t,n,e){if(0!==e.length){for(var i=e.length-n.length;i-- >0;)n.push(null);var l=null,r=n.length;for(i=0;i<r;++i)i>=e.length||null===e[i]?null!==l&&(null===n[i]?(n[i]=l,l=null):(l=l.merge(t,n[i]),n[i]=null)):null!==l?l=l.merge(t,e[i]):null!==n[i]?(l=n[i].merge(t,e[i]),n[i]=null):n[i]=e[i];null!==l&&n.push(l)}},i=function(t,n,e,i){for(;e<i-1&&null===n[e];++e);var l=e,r=n[e].value;for(++e;e<i;++e){var o=n[e];if(null!==o){var h=o.value;t(h,r)<0&&(l=e,r=h)}}return l},l=function(t,n,i,l){var r=n[i].children;n[i]=null,o(null,r),i===l-1&&n.pop(),e(t,n,r)},r=function(t,n){var e=n.length,r=i(t,n,0,e),o=n[r];return l(t,n,r,e),o},o=function(t,n){for(var e=n.length,i=0;i<e;++i)n[i].setparent(t)},h=function(t,n){var e=n.children;n.children=t.children,t.children=e;var i=n.rank();return t.children[i]=n,t.parent=n.parent,o(t,t.children),o(n,n.children),t.parent},u=function(t,n,e){!function(t,n){var e=n.parent;if(null!==e){for(;null!==(e=h(n,e));)e.children[n.rank()]=n;t[n.rank()]=n}}(n,e),l(t,n,e.rank(),n.length),e.detach()},s=function(t){this.compare=t,this.length=0,this.list=[]};return s.prototype.head=function(){if(0!==this.length){var t=i(this.compare,this.list,0,this.list.length);return this.list[t].value}},s.prototype.headreference=function(){if(0===this.length)return null;var t=i(this.compare,this.list,0,this.list.length);return this.list[t]},s.prototype.pop=function(){if(0!==this.length)return--this.length,r(this.compare,this.list).value},s.prototype.popreference=function(){return 0===this.length?null:(--this.length,r(this.compare,this.list).detach())},s.prototype.push=function(n){var e=new t(n,[]);return this.pushreference(e),e},s.prototype.pushreference=function(t){++this.length,n(this.compare,this.list,t,0)},s.prototype.merge=function(t){return e(this.compare,this.list,t.list),this.length+=t.length,this},s.prototype.update=function(t,n){var e=this.compare(n,t.value);e<0?this.decreasekey(t,n):e>0?this.increasekey(t,n):t.value=n},s.prototype.decreasekey=function(t,n){!function(t,n,e,i){e.value=i;var l=e.parent;if(null!==l){for(;;){if(t(i,l.value)>=0)return;if(null===(l=h(e,l)))break;l.children[e.rank()]=e}n[e.rank()]=e}}(this.compare,this.list,t,n)},s.prototype.increasekey=function(t,e){u(this.compare,this.list,t),t.value=e,n(this.compare,this.list,t,0)},s.prototype.delete=function(t){--this.length,u(this.compare,this.list,t)},s},t.BinomialTree=n,t.BinomialTreeWithParent=e,t.LazyBinomialHeap=function(t){var n=function(t,n,e){if(0!==e.length){for(var i=e.length-n.length;i-- >0;)n.push(null);var l=null,r=n.length;for(i=0;i<r;++i)i>=e.length||null===e[i]?null!==l&&(null===n[i]?(n[i]=l,l=null):(l=l.merge(t,n[i]),n[i]=null)):null!==l?l=l.merge(t,e[i]):null!==n[i]?(l=n[i].merge(t,e[i]),n[i]=null):n[i]=e[i];null!==l&&n.push(l)}},e=function(t){this.compare=t,this.length=0,this.list=[],this.lazy=new l};return e.prototype.pop=function(){if(0!==this.length)return--this.length,function(t,e,i){for(;!i.empty();)n(t,e,i.pop());for(var l=e.length,r=0;r<l-1&&null===e[r];++r);var o=r,h=e[r].value;for(++r;r<l;++r){var u=e[r];if(null!==u){var s=u.value;t(s,h)<0&&(o=r,h=s)}}var p=e[o].children;return e[o]=null,o===l-1&&e.pop(),i.push(p),h}(this.compare,this.list,this.lazy)},e.prototype.push=function(n){return++this.length,function(t,n,e){for(var i=[],l=0;l--;)i.push(null);i.push(n),t.push(i)}(this.lazy,new t(n,[]))},e.prototype.merge=function(t){return this.lazy.meld(t.lazy),this.length+=t.length,this},e},t.LazyNode=i,t.LazyStack=l}); //# sourceMappingURL=index.umd.js.map