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

lib: refactor PriorityQueue to use private field #43889

Merged
merged 2 commits into from
Jul 23, 2022
Merged
Changes from 1 commit
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
54 changes: 22 additions & 32 deletions lib/internal/priority_queue.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use strict';

const {
Array,
Symbol,
} = primordials;

const kCompare = Symbol('compare');
const kHeap = Symbol('heap');
const kSetPosition = Symbol('setPosition');
const kSize = Symbol('size');
const { Array } = primordials;
F3n67u marked this conversation as resolved.
Show resolved Hide resolved

// The PriorityQueue is a basic implementation of a binary heap that accepts
// a custom sorting function via its constructor. This function is passed
Expand All @@ -17,23 +9,21 @@ const kSize = Symbol('size');
// 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 +33,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 +61,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 +81,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