Skip to content

Commit

Permalink
lib: refactor PriorityQueue to use private field
Browse files Browse the repository at this point in the history
PR-URL: #43889
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Mestery <[email protected]>
  • Loading branch information
F3n67u authored Jul 23, 2022
1 parent 069cec8 commit df7c49c
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions lib/internal/priority_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,30 @@

const {
Array,
Symbol,
} = primordials;

const kCompare = Symbol('compare');
const kHeap = Symbol('heap');
const kSetPosition = Symbol('setPosition');
const kSize = Symbol('size');

// The PriorityQueue is a basic implementation of a binary heap that accepts
// a custom sorting function via its constructor. This function is passed
// the two nodes to compare, similar to the native Array#sort. Crucially
// this enables priority queues that are based on a comparison of more than
// just a single criteria.

module.exports = class PriorityQueue {
#compare = (a, b) => a - b;
#heap = new Array(64);
#setPosition;
#size = 0;

constructor(comparator, setPosition) {
if (comparator !== undefined)
this[kCompare] = comparator;
this.#compare = comparator;
if (setPosition !== undefined)
this[kSetPosition] = setPosition;

this[kHeap] = new Array(64);
this[kSize] = 0;
}

[kCompare](a, b) {
return a - b;
this.#setPosition = setPosition;
}

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

if (heap.length === pos)
Expand All @@ -43,14 +35,14 @@ module.exports = class PriorityQueue {
}

peek() {
return this[kHeap][1];
return this.#heap[1];
}

percolateDown(pos) {
const compare = this[kCompare];
const setPosition = this[kSetPosition];
const heap = this[kHeap];
const size = this[kSize];
const compare = this.#compare;
const setPosition = this.#setPosition;
const heap = this.#heap;
const size = this.#size;
const item = heap[pos];

while (pos * 2 <= size) {
Expand All @@ -71,9 +63,9 @@ module.exports = class PriorityQueue {
}

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

while (pos > 1) {
Expand All @@ -91,21 +83,21 @@ module.exports = class PriorityQueue {
}

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

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

shift() {
const heap = this[kHeap];
const heap = this.#heap;
const value = heap[1];
if (value === undefined)
return;
Expand Down

0 comments on commit df7c49c

Please sign in to comment.