6
6
* have its priority decreased in O(log n) time.
7
7
*/
8
8
class PriorityQueue {
9
- #arr = [ ] ;
10
- #keyIndices = { } ;
9
+ _arr = [ ] ;
10
+ _keyIndices = { } ;
11
11
12
12
/**
13
13
* Returns the number of elements in the queue. Takes `O(1)` time.
14
14
*/
15
15
size ( ) {
16
- return this . #arr . length ;
16
+ return this . _arr . length ;
17
17
}
18
18
19
19
/**
20
20
* Returns the keys that are in the queue. Takes `O(n)` time.
21
21
*/
22
22
keys ( ) {
23
- return this . #arr . map ( function ( x ) { return x . key ; } ) ;
23
+ return this . _arr . map ( function ( x ) { return x . key ; } ) ;
24
24
}
25
25
26
26
/**
27
27
* Returns `true` if **key** is in the queue and `false` if not.
28
28
*/
29
29
has ( key ) {
30
- return this . #keyIndices . hasOwnProperty ( key ) ;
30
+ return this . _keyIndices . hasOwnProperty ( key ) ;
31
31
}
32
32
33
33
/**
@@ -37,9 +37,9 @@ class PriorityQueue {
37
37
* @param {Object } key
38
38
*/
39
39
priority ( key ) {
40
- var index = this . #keyIndices [ key ] ;
40
+ var index = this . _keyIndices [ key ] ;
41
41
if ( index !== undefined ) {
42
- return this . #arr [ index ] . priority ;
42
+ return this . _arr [ index ] . priority ;
43
43
}
44
44
}
45
45
@@ -51,7 +51,7 @@ class PriorityQueue {
51
51
if ( this . size ( ) === 0 ) {
52
52
throw new Error ( "Queue underflow" ) ;
53
53
}
54
- return this . #arr [ 0 ] . key ;
54
+ return this . _arr [ 0 ] . key ;
55
55
}
56
56
57
57
/**
@@ -63,14 +63,14 @@ class PriorityQueue {
63
63
* @param {Number } priority the initial priority for the key
64
64
*/
65
65
add ( key , priority ) {
66
- var keyIndices = this . #keyIndices ;
66
+ var keyIndices = this . _keyIndices ;
67
67
key = String ( key ) ;
68
68
if ( ! keyIndices . hasOwnProperty ( key ) ) {
69
- var arr = this . #arr ;
69
+ var arr = this . _arr ;
70
70
var index = arr . length ;
71
71
keyIndices [ key ] = index ;
72
72
arr . push ( { key : key , priority : priority } ) ;
73
- this . #decrease ( index ) ;
73
+ this . _decrease ( index ) ;
74
74
return true ;
75
75
}
76
76
return false ;
@@ -80,10 +80,10 @@ class PriorityQueue {
80
80
* Removes and returns the smallest key in the queue. Takes `O(log n)` time.
81
81
*/
82
82
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 ) ;
87
87
return min . key ;
88
88
}
89
89
@@ -95,17 +95,17 @@ class PriorityQueue {
95
95
* @param {Number } priority the new priority for the key
96
96
*/
97
97
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 ) {
100
100
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 ) ;
102
102
}
103
- this . #arr [ index ] . priority = priority ;
104
- this . #decrease ( index ) ;
103
+ this . _arr [ index ] . priority = priority ;
104
+ this . _decrease ( index ) ;
105
105
}
106
106
107
- #heapify ( i ) {
108
- var arr = this . #arr ;
107
+ _heapify ( i ) {
108
+ var arr = this . _arr ;
109
109
var l = 2 * i ;
110
110
var r = l + 1 ;
111
111
var largest = i ;
@@ -115,29 +115,29 @@ class PriorityQueue {
115
115
largest = arr [ r ] . priority < arr [ largest ] . priority ? r : largest ;
116
116
}
117
117
if ( largest !== i ) {
118
- this . #swap ( i , largest ) ;
119
- this . #heapify ( largest ) ;
118
+ this . _swap ( i , largest ) ;
119
+ this . _heapify ( largest ) ;
120
120
}
121
121
}
122
122
}
123
123
124
- #decrease ( index ) {
125
- var arr = this . #arr ;
124
+ _decrease ( index ) {
125
+ var arr = this . _arr ;
126
126
var priority = arr [ index ] . priority ;
127
127
var parent ;
128
128
while ( index !== 0 ) {
129
129
parent = index >> 1 ;
130
130
if ( arr [ parent ] . priority < priority ) {
131
131
break ;
132
132
}
133
- this . #swap ( index , parent ) ;
133
+ this . _swap ( index , parent ) ;
134
134
index = parent ;
135
135
}
136
136
}
137
137
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 ;
141
141
var origArrI = arr [ i ] ;
142
142
var origArrJ = arr [ j ] ;
143
143
arr [ i ] = origArrJ ;
0 commit comments