sorted-queue
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -1,2 +0,2 @@ | ||
var e=function(e){void 0===e&&(e=i),this._cmp=e,this._array=[]};e.prototype.push=function(e){var t=new r(e,this,this._array.length);return this._array.push(t),function(e,r,t){for(;r._index>0;){var i=e[r._index/2>>>0];if(t(i.value,r.value)<=0)return;n(e,i,r)}}(this._array,t,this._cmp),t},e.prototype.peek=function(){return this._array.length>0?this._array[0]:void 0},e.prototype.pop=function(){var e=this.peek();return e?(e.pop(),e):e};var r=function(e,r,t){this.value=e,this._queue=r,this._index=t};r.prototype.pop=function(){var e=this._queue;if(!e)return!1;var r=e._array.pop();return r&&r!==this&&(r._index=this._index,e._array[this._index]=r,function(e,r,t){for(;;){var i=2*r._index+1,u=i+1;if(u<e.length&&t(e[u].value,r.value)<=0)n(e,e[u],r);else{if(!(i<e.length&&t(e[i].value,r.value)<=0))return;n(e,e[i],r)}}}(e._array,r,e._cmp)),this._queue=null,!0};var t=r;function i(e,r){return e===r?0:e!=e?r!=r?0:-1:r<e||r!=r?1:-1}function n(e,r,t){var i=r._index,n=t._index;e[i]=t,e[n]=r,r._index=n,t._index=i}exports.SortedQueue=e,exports.SortedQueueItem=t; | ||
var r=function(r){void 0===r&&(r=n),this._cmp=r,this._array=[]};r.prototype.push=function(r){var t=new e(r,this,this._array.length);return this._array.push(t),function(r,e,t){for(;e._index>0;){var n=r[e._index-1>>>1];if(t(n.value,e.value)<=0)return;i(r,n,e)}}(this._array,t,this._cmp),t},r.prototype.peek=function(){return this._array.length>0?this._array[0]:void 0},r.prototype.pop=function(){var r=this.peek();return r?(r.pop(),r):r};var e=function(r,e,t){this.value=r,this._queue=e,this._index=t};e.prototype.pop=function(){var r=this._queue;if(!r)return!1;var e=r._array.pop();return e&&e!==this&&(e._index=this._index,r._array[this._index]=e,function(r,e,t){for(;;){var n=2*e._index+1,u=n+1;if(n>=r.length)return;var a=r[n];if(u<r.length&&t(r[u].value,a.value)<0&&(a=r[u]),!(t(a.value,e.value)<=0))return;i(r,a,e)}}(r._array,e,r._cmp)),this._queue=null,!0};var t=e;function n(r,e){return r===e?0:r!=r?e!=e?0:-1:e<r||e!=e?1:-1}function i(r,e,t){var n=e._index,i=t._index;r[n]=t,r[i]=e,e._index=i,t._index=n}exports.SortedQueue=r,exports.SortedQueueItem=t; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.sortedQueue={})}(this,function(e){var t=function(e){void 0===e&&(e=i),this._cmp=e,this._array=[]};t.prototype.push=function(e){var t=new n(e,this,this._array.length);return this._array.push(t),function(e,t,n){for(;t._index>0;){var r=e[t._index/2>>>0];if(n(r.value,t.value)<=0)return;u(e,r,t)}}(this._array,t,this._cmp),t},t.prototype.peek=function(){return this._array.length>0?this._array[0]:void 0},t.prototype.pop=function(){var e=this.peek();return e?(e.pop(),e):e};var n=function(e,t,n){this.value=e,this._queue=t,this._index=n};n.prototype.pop=function(){var e=this._queue;if(!e)return!1;var t=e._array.pop();return t&&t!==this&&(t._index=this._index,e._array[this._index]=t,function(e,t,n){for(;;){var r=2*t._index+1,i=r+1;if(i<e.length&&n(e[i].value,t.value)<=0)u(e,e[i],t);else{if(!(r<e.length&&n(e[r].value,t.value)<=0))return;u(e,e[r],t)}}}(e._array,t,e._cmp)),this._queue=null,!0};var r=n;function i(e,t){return e===t?0:e!=e?t!=t?0:-1:t<e||t!=t?1:-1}function u(e,t,n){var r=t._index,i=n._index;e[r]=n,e[i]=t,t._index=i,n._index=r}e.SortedQueue=t,e.SortedQueueItem=r}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.sortedQueue={})}(this,function(e){var t=function(e){void 0===e&&(e=i),this._cmp=e,this._array=[]};t.prototype.push=function(e){var t=new r(e,this,this._array.length);return this._array.push(t),function(e,t,r){for(;t._index>0;){var n=e[t._index-1>>>1];if(r(n.value,t.value)<=0)return;u(e,n,t)}}(this._array,t,this._cmp),t},t.prototype.peek=function(){return this._array.length>0?this._array[0]:void 0},t.prototype.pop=function(){var e=this.peek();return e?(e.pop(),e):e};var r=function(e,t,r){this.value=e,this._queue=t,this._index=r};r.prototype.pop=function(){var e=this._queue;if(!e)return!1;var t=e._array.pop();return t&&t!==this&&(t._index=this._index,e._array[this._index]=t,function(e,t,r){for(;;){var n=2*t._index+1,i=n+1;if(n>=e.length)return;var o=e[n];if(i<e.length&&r(e[i].value,o.value)<0&&(o=e[i]),!(r(o.value,t.value)<=0))return;u(e,o,t)}}(e._array,t,e._cmp)),this._queue=null,!0};var n=r;function i(e,t){return e===t?0:e!=e?t!=t?0:-1:t<e||t!=t?1:-1}function u(e,t,r){var n=t._index,i=r._index;e[n]=r,e[i]=t,t._index=i,r._index=n}e.SortedQueue=t,e.SortedQueueItem=n}); | ||
//# sourceMappingURL=index.umd.js.map |
15
index.ts
@@ -100,3 +100,3 @@ type Cmp<T> = (a: T, b: T) => number; | ||
// "Every Array object has a length property whose value is always a nonnegative integer less than 2**32." | ||
const parent = array[(item._index / 2) >>> 0]; | ||
const parent = array[(item._index - 1) >>> 1]; | ||
if (cmp(parent.value, item.value) <= 0) { | ||
@@ -113,6 +113,11 @@ return; | ||
const right = left + 1; | ||
if (right < array.length && cmp(array[right].value, item.value) <= 0) { | ||
swap(array, array[right], item); | ||
} else if (left < array.length && cmp(array[left].value, item.value) <= 0) { | ||
swap(array, array[left], item); | ||
if (left >= array.length) { | ||
return; | ||
} | ||
let child = array[left]; | ||
if (right < array.length && cmp(array[right].value, child.value) < 0) { | ||
child = array[right]; | ||
} | ||
if (cmp(child.value, item.value) <= 0) { | ||
swap(array, child, item); | ||
} else { | ||
@@ -119,0 +124,0 @@ return; |
{ | ||
"name": "sorted-queue", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A sorted queue, based on an array-backed binary heap", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24988
146