Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers: fix priority queue removeAt #24322

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions lib/internal/priority_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,13 @@ module.exports = class PriorityQueue {

insert(value) {
const heap = this[kHeap];
let pos = ++this[kSize];
const pos = ++this[kSize];
heap[pos] = value;

if (heap.length === pos)
heap.length *= 2;

const compare = this[kCompare];
const setPosition = this[kSetPosition];
while (pos > 1) {
const parent = heap[pos / 2 | 0];
if (compare(parent, value) <= 0)
break;
heap[pos] = parent;
if (setPosition !== undefined)
setPosition(parent, pos);
pos = pos / 2 | 0;
}
heap[pos] = value;
if (setPosition !== undefined)
setPosition(value, pos);
this.percolateUp(pos);
}

peek() {
Expand Down Expand Up @@ -77,18 +65,37 @@ module.exports = class PriorityQueue {
setPosition(item, pos);
}

percolateUp(pos) {
const heap = this[kHeap];
const compare = this[kCompare];
const setPosition = this[kSetPosition];
const item = heap[pos];

while (pos > 1) {
const parent = heap[pos / 2 | 0];
if (compare(parent, item) <= 0)
break;
heap[pos] = parent;
if (setPosition !== undefined)
setPosition(parent, pos);
pos = pos / 2 | 0;
}
heap[pos] = item;
if (setPosition !== undefined)
setPosition(item, pos);
}

removeAt(pos) {
const heap = this[kHeap];
const size = --this[kSize];
heap[pos] = heap[size + 1];
heap[size + 1] = undefined;

if (size > 0) {
// If not removing the last item, update the shifted item's position.
if (pos <= size && this[kSetPosition] !== undefined)
this[kSetPosition](heap[pos], pos);

this.percolateDown(1);
if (size > 0 && pos <= size) {
if (pos > 1 && this[kCompare](heap[pos / 2 | 0], heap[pos]) > 0)
this.percolateUp(pos);
else
this.percolateDown(pos);
}
}

Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-priority-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,33 @@ const PriorityQueue = require('internal/priority_queue');

assert.strictEqual(queue.shift(), undefined);
}


{
// Checks that removeAt respects binary heap properties
const queue = new PriorityQueue((a, b) => {
return a.value - b.value;
}, (node, pos) => (node.position = pos));

const i3 = { value: 3, position: null };
const i7 = { value: 7, position: null };
const i8 = { value: 8, position: null };

queue.insert({ value: 1, position: null });
queue.insert({ value: 6, position: null });
queue.insert({ value: 2, position: null });
queue.insert(i7);
queue.insert(i8);
queue.insert(i3);

assert.strictEqual(i7.position, 4);
queue.removeAt(4);

// 3 should percolate up to swap with 6 (up)
assert.strictEqual(i3.position, 2);

queue.removeAt(2);

// 8 should swap places with 6 (down)
assert.strictEqual(i8.position, 4);
}