Skip to content

Commit

Permalink
Merge pull request #194 from dagrejs/underscore
Browse files Browse the repository at this point in the history
Replacing the '#' to enforce private with '_' to suggest private
  • Loading branch information
rustedgrail committed Apr 11, 2024
2 parents 96e5d45 + cec77cc commit c7a5c62
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 147 deletions.
60 changes: 30 additions & 30 deletions lib/data/priority-queue.js
Expand Up @@ -6,28 +6,28 @@
* have its priority decreased in O(log n) time.
*/
class PriorityQueue {
#arr = [];
#keyIndices = {};
_arr = [];
_keyIndices = {};

/**
* Returns the number of elements in the queue. Takes `O(1)` time.
*/
size() {
return this.#arr.length;
return this._arr.length;
}

/**
* Returns the keys that are in the queue. Takes `O(n)` time.
*/
keys() {
return this.#arr.map(function(x) { return x.key; });
return this._arr.map(function(x) { return x.key; });
}

/**
* Returns `true` if **key** is in the queue and `false` if not.
*/
has(key) {
return this.#keyIndices.hasOwnProperty(key);
return this._keyIndices.hasOwnProperty(key);
}

/**
Expand All @@ -37,9 +37,9 @@ class PriorityQueue {
* @param {Object} key
*/
priority(key) {
var index = this.#keyIndices[key];
var index = this._keyIndices[key];
if (index !== undefined) {
return this.#arr[index].priority;
return this._arr[index].priority;
}
}

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

/**
Expand All @@ -63,14 +63,14 @@ class PriorityQueue {
* @param {Number} priority the initial priority for the key
*/
add(key, priority) {
var keyIndices = this.#keyIndices;
var keyIndices = this._keyIndices;
key = String(key);
if (!keyIndices.hasOwnProperty(key)) {
var arr = this.#arr;
var arr = this._arr;
var index = arr.length;
keyIndices[key] = index;
arr.push({key: key, priority: priority});
this.#decrease(index);
this._decrease(index);
return true;
}
return false;
Expand All @@ -80,10 +80,10 @@ class PriorityQueue {
* Removes and returns the smallest key in the queue. Takes `O(log n)` time.
*/
removeMin() {
this.#swap(0, this.#arr.length - 1);
var min = this.#arr.pop();
delete this.#keyIndices[min.key];
this.#heapify(0);
this._swap(0, this._arr.length - 1);
var min = this._arr.pop();
delete this._keyIndices[min.key];
this._heapify(0);
return min.key;
}

Expand All @@ -95,17 +95,17 @@ class PriorityQueue {
* @param {Number} priority the new priority for the key
*/
decrease(key, priority) {
var index = this.#keyIndices[key];
if (priority > this.#arr[index].priority) {
var index = this._keyIndices[key];
if (priority > this._arr[index].priority) {
throw new Error("New priority is greater than current priority. " +
"Key: " + key + " Old: " + this.#arr[index].priority + " New: " + priority);
"Key: " + key + " Old: " + this._arr[index].priority + " New: " + priority);
}
this.#arr[index].priority = priority;
this.#decrease(index);
this._arr[index].priority = priority;
this._decrease(index);
}

#heapify(i) {
var arr = this.#arr;
_heapify(i) {
var arr = this._arr;
var l = 2 * i;
var r = l + 1;
var largest = i;
Expand All @@ -115,29 +115,29 @@ class PriorityQueue {
largest = arr[r].priority < arr[largest].priority ? r : largest;
}
if (largest !== i) {
this.#swap(i, largest);
this.#heapify(largest);
this._swap(i, largest);
this._heapify(largest);
}
}
}

#decrease(index) {
var arr = this.#arr;
_decrease(index) {
var arr = this._arr;
var priority = arr[index].priority;
var parent;
while (index !== 0) {
parent = index >> 1;
if (arr[parent].priority < priority) {
break;
}
this.#swap(index, parent);
this._swap(index, parent);
index = parent;
}
}

#swap(i, j) {
var arr = this.#arr;
var keyIndices = this.#keyIndices;
_swap(i, j) {
var arr = this._arr;
var keyIndices = this._keyIndices;
var origArrI = arr[i];
var origArrJ = arr[j];
arr[i] = origArrJ;
Expand Down

0 comments on commit c7a5c62

Please sign in to comment.