Skip to content

Commit cec77cc

Browse files
committed
Replacing the '#' to enforce private with '_' to suggest private
1 parent 96e5d45 commit cec77cc

File tree

3 files changed

+147
-147
lines changed

3 files changed

+147
-147
lines changed

lib/data/priority-queue.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
* have its priority decreased in O(log n) time.
77
*/
88
class PriorityQueue {
9-
#arr = [];
10-
#keyIndices = {};
9+
_arr = [];
10+
_keyIndices = {};
1111

1212
/**
1313
* Returns the number of elements in the queue. Takes `O(1)` time.
1414
*/
1515
size() {
16-
return this.#arr.length;
16+
return this._arr.length;
1717
}
1818

1919
/**
2020
* Returns the keys that are in the queue. Takes `O(n)` time.
2121
*/
2222
keys() {
23-
return this.#arr.map(function(x) { return x.key; });
23+
return this._arr.map(function(x) { return x.key; });
2424
}
2525

2626
/**
2727
* Returns `true` if **key** is in the queue and `false` if not.
2828
*/
2929
has(key) {
30-
return this.#keyIndices.hasOwnProperty(key);
30+
return this._keyIndices.hasOwnProperty(key);
3131
}
3232

3333
/**
@@ -37,9 +37,9 @@ class PriorityQueue {
3737
* @param {Object} key
3838
*/
3939
priority(key) {
40-
var index = this.#keyIndices[key];
40+
var index = this._keyIndices[key];
4141
if (index !== undefined) {
42-
return this.#arr[index].priority;
42+
return this._arr[index].priority;
4343
}
4444
}
4545

@@ -51,7 +51,7 @@ class PriorityQueue {
5151
if (this.size() === 0) {
5252
throw new Error("Queue underflow");
5353
}
54-
return this.#arr[0].key;
54+
return this._arr[0].key;
5555
}
5656

5757
/**
@@ -63,14 +63,14 @@ class PriorityQueue {
6363
* @param {Number} priority the initial priority for the key
6464
*/
6565
add(key, priority) {
66-
var keyIndices = this.#keyIndices;
66+
var keyIndices = this._keyIndices;
6767
key = String(key);
6868
if (!keyIndices.hasOwnProperty(key)) {
69-
var arr = this.#arr;
69+
var arr = this._arr;
7070
var index = arr.length;
7171
keyIndices[key] = index;
7272
arr.push({key: key, priority: priority});
73-
this.#decrease(index);
73+
this._decrease(index);
7474
return true;
7575
}
7676
return false;
@@ -80,10 +80,10 @@ class PriorityQueue {
8080
* Removes and returns the smallest key in the queue. Takes `O(log n)` time.
8181
*/
8282
removeMin() {
83-
this.#swap(0, this.#arr.length - 1);
84-
var min = this.#arr.pop();
85-
delete this.#keyIndices[min.key];
86-
this.#heapify(0);
83+
this._swap(0, this._arr.length - 1);
84+
var min = this._arr.pop();
85+
delete this._keyIndices[min.key];
86+
this._heapify(0);
8787
return min.key;
8888
}
8989

@@ -95,17 +95,17 @@ class PriorityQueue {
9595
* @param {Number} priority the new priority for the key
9696
*/
9797
decrease(key, priority) {
98-
var index = this.#keyIndices[key];
99-
if (priority > this.#arr[index].priority) {
98+
var index = this._keyIndices[key];
99+
if (priority > this._arr[index].priority) {
100100
throw new Error("New priority is greater than current priority. " +
101-
"Key: " + key + " Old: " + this.#arr[index].priority + " New: " + priority);
101+
"Key: " + key + " Old: " + this._arr[index].priority + " New: " + priority);
102102
}
103-
this.#arr[index].priority = priority;
104-
this.#decrease(index);
103+
this._arr[index].priority = priority;
104+
this._decrease(index);
105105
}
106106

107-
#heapify(i) {
108-
var arr = this.#arr;
107+
_heapify(i) {
108+
var arr = this._arr;
109109
var l = 2 * i;
110110
var r = l + 1;
111111
var largest = i;
@@ -115,29 +115,29 @@ class PriorityQueue {
115115
largest = arr[r].priority < arr[largest].priority ? r : largest;
116116
}
117117
if (largest !== i) {
118-
this.#swap(i, largest);
119-
this.#heapify(largest);
118+
this._swap(i, largest);
119+
this._heapify(largest);
120120
}
121121
}
122122
}
123123

124-
#decrease(index) {
125-
var arr = this.#arr;
124+
_decrease(index) {
125+
var arr = this._arr;
126126
var priority = arr[index].priority;
127127
var parent;
128128
while (index !== 0) {
129129
parent = index >> 1;
130130
if (arr[parent].priority < priority) {
131131
break;
132132
}
133-
this.#swap(index, parent);
133+
this._swap(index, parent);
134134
index = parent;
135135
}
136136
}
137137

138-
#swap(i, j) {
139-
var arr = this.#arr;
140-
var keyIndices = this.#keyIndices;
138+
_swap(i, j) {
139+
var arr = this._arr;
140+
var keyIndices = this._keyIndices;
141141
var origArrI = arr[i];
142142
var origArrJ = arr[j];
143143
arr[i] = origArrJ;

0 commit comments

Comments
 (0)