UNPKG

@heap-data-structure/binomial-heap

Version:
3 lines (2 loc) 4.93 kB
function t(t,n){this.value=t,this.children=n}function n(t,n){this.value=t,this.children=n,this.parent=null}function e(t,n){this.value=t,this.next=n}function i(){this.top=null,this.bottom=null}t.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)},t.prototype.detach=function(){return this.children.splice(0),this},t.prototype.setparent=function(t){},n.prototype.rank=function(){return this.children.length},n.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)},n.prototype.detach=function(){return this.children.splice(0),this.parent=null,this},n.prototype.setparent=function(t){this.parent=t},i.prototype.empty=function(){return null===this.top},i.prototype.push=function(t){this.top=new e(t,this.top),null===this.bottom&&(this.bottom=this.top)},i.prototype.pop=function(){var t=this.top.value;return this.top=this.top.next,null===this.top&&(this.bottom=null),t},i.prototype.meld=function(t){null===this.bottom?this.top=t.top:this.bottom.next=t.top,this.bottom=t.bottom},exports.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 h=n[e];if(null!==h){var o=h.value;t(o,r)<0&&(l=e,r=o)}}return l},l=function(t,n,i,l){var r=n[i].children;n[i]=null,h(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),h=n[r];return l(t,n,r,e),h},h=function(t,n){for(var e=n.length,i=0;i<e;++i)n[i].setparent(t)},o=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,h(t,t.children),h(n,n.children),t.parent},u=function(t,n,e){!function(t,n){var e=n.parent;if(null!==e){for(;null!==(e=o(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=o(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},exports.BinomialTree=t,exports.BinomialTreeWithParent=n,exports.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 i};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 h=r,o=e[r].value;for(++r;r<l;++r){var u=e[r];if(null!==u){var s=u.value;t(s,o)<0&&(h=r,o=s)}}var p=e[h].children;return e[h]=null,h===l-1&&e.pop(),i.push(p),o}(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},exports.LazyNode=e,exports.LazyStack=i; //# sourceMappingURL=index.cjs.map