Skip to content

Commit 0b5dbb2

Browse files
F3n67udanielleadams
authored andcommitted
lib: refactor PriorityQueue to use private field
PR-URL: #43889 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
1 parent 1256c4d commit 0b5dbb2

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

lib/internal/priority_queue.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,30 @@
22

33
const {
44
Array,
5-
Symbol,
65
} = primordials;
76

8-
const kCompare = Symbol('compare');
9-
const kHeap = Symbol('heap');
10-
const kSetPosition = Symbol('setPosition');
11-
const kSize = Symbol('size');
12-
137
// The PriorityQueue is a basic implementation of a binary heap that accepts
148
// a custom sorting function via its constructor. This function is passed
159
// the two nodes to compare, similar to the native Array#sort. Crucially
1610
// this enables priority queues that are based on a comparison of more than
1711
// just a single criteria.
1812

1913
module.exports = class PriorityQueue {
14+
#compare = (a, b) => a - b;
15+
#heap = new Array(64);
16+
#setPosition;
17+
#size = 0;
18+
2019
constructor(comparator, setPosition) {
2120
if (comparator !== undefined)
22-
this[kCompare] = comparator;
21+
this.#compare = comparator;
2322
if (setPosition !== undefined)
24-
this[kSetPosition] = setPosition;
25-
26-
this[kHeap] = new Array(64);
27-
this[kSize] = 0;
28-
}
29-
30-
[kCompare](a, b) {
31-
return a - b;
23+
this.#setPosition = setPosition;
3224
}
3325

3426
insert(value) {
35-
const heap = this[kHeap];
36-
const pos = ++this[kSize];
27+
const heap = this.#heap;
28+
const pos = ++this.#size;
3729
heap[pos] = value;
3830

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

4537
peek() {
46-
return this[kHeap][1];
38+
return this.#heap[1];
4739
}
4840

4941
percolateDown(pos) {
50-
const compare = this[kCompare];
51-
const setPosition = this[kSetPosition];
52-
const heap = this[kHeap];
53-
const size = this[kSize];
42+
const compare = this.#compare;
43+
const setPosition = this.#setPosition;
44+
const heap = this.#heap;
45+
const size = this.#size;
5446
const item = heap[pos];
5547

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

7365
percolateUp(pos) {
74-
const heap = this[kHeap];
75-
const compare = this[kCompare];
76-
const setPosition = this[kSetPosition];
66+
const heap = this.#heap;
67+
const compare = this.#compare;
68+
const setPosition = this.#setPosition;
7769
const item = heap[pos];
7870

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

9385
removeAt(pos) {
94-
const heap = this[kHeap];
95-
const size = --this[kSize];
86+
const heap = this.#heap;
87+
const size = --this.#size;
9688
heap[pos] = heap[size + 1];
9789
heap[size + 1] = undefined;
9890

9991
if (size > 0 && pos <= size) {
100-
if (pos > 1 && this[kCompare](heap[pos / 2 | 0], heap[pos]) > 0)
92+
if (pos > 1 && this.#compare(heap[pos / 2 | 0], heap[pos]) > 0)
10193
this.percolateUp(pos);
10294
else
10395
this.percolateDown(pos);
10496
}
10597
}
10698

10799
shift() {
108-
const heap = this[kHeap];
100+
const heap = this.#heap;
109101
const value = heap[1];
110102
if (value === undefined)
111103
return;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy