@@ -85,48 +85,43 @@ class KeyPriorityQueue {
85
85
const currPos = this . keys [ key ]
86
86
this . _heap [ currPos ] [ 1 ] = priority
87
87
const parentPos = getParentPosition ( currPos )
88
- const currPriority = this . _heap [ currPos ] [ 1 ]
89
- let parentPriority = Infinity
90
- if ( parentPos >= 0 ) {
91
- parentPriority = this . _heap [ parentPos ] [ 1 ]
92
- }
88
+ const currPriority = this . _getPriorityOrInfinite ( currPos )
89
+ const parentPriority = this . _getPriorityOrInfinite ( parentPos )
93
90
const [ child1Pos , child2Pos ] = getChildrenPosition ( currPos )
94
- let [ child1Priority , child2Priority ] = [ Infinity , Infinity ]
95
- if ( child1Pos < this . _heap . length ) {
96
- child1Priority = this . _heap [ child1Pos ] [ 1 ]
97
- }
98
- if ( child2Pos < this . _heap . length ) {
99
- child2Priority = this . _heap [ child2Pos ] [ 1 ]
100
- }
91
+ const child1Priority = this . _getPriorityOrInfinite ( child1Pos )
92
+ const child2Priority = this . _getPriorityOrInfinite ( child2Pos )
101
93
102
94
if ( parentPos >= 0 && parentPriority > currPriority ) {
103
95
this . _shiftUp ( currPos )
104
- } else if ( child2Pos < this . _heap . length &&
105
- ( child1Priority < currPriority || child2Priority < currPriority ) ) {
96
+ } else if ( child1Priority < currPriority || child2Priority < currPriority ) {
106
97
this . _shiftDown ( currPos )
107
98
}
108
99
}
109
100
101
+ _getPriorityOrInfinite ( position ) {
102
+ // Helper function, returns priority of the node, or Infinite if no node corresponds to this position
103
+ if ( position >= 0 && position < this . _heap . length ) {
104
+ return this . _heap [ position ] [ 1 ]
105
+ }
106
+ else {
107
+ return Infinity
108
+ }
109
+ }
110
+
110
111
_shiftUp ( position ) {
111
112
// Helper function to shift up a node to proper position (equivalent to bubbleUp)
112
113
let currPos = position
113
114
let parentPos = getParentPosition ( currPos )
114
- let currPriority = this . _heap [ currPos ] [ 1 ]
115
- let parentPriority = Infinity
116
- if ( parentPos >= 0 ) {
117
- parentPriority = this . _heap [ parentPos ] [ 1 ]
118
- }
115
+ let currPriority = this . _getPriorityOrInfinite ( currPos )
116
+ let parentPriority = this . _getPriorityOrInfinite ( parentPos )
119
117
120
118
while ( parentPos >= 0 && parentPriority > currPriority ) {
121
119
this . _swap ( currPos , parentPos )
122
120
currPos = parentPos
123
121
parentPos = getParentPosition ( currPos )
124
- currPriority = this . _heap [ currPos ] [ 1 ]
125
- try {
126
- parentPriority = this . _heap [ parentPos ] [ 1 ]
127
- } catch ( error ) {
128
- parentPriority = Infinity
129
- }
122
+ currPriority = this . _getPriorityOrInfinite ( currPos )
123
+ parentPriority = this . _getPriorityOrInfinite ( parentPos )
124
+
130
125
}
131
126
this . keys [ this . _heap [ currPos ] [ 0 ] ] = currPos
132
127
}
@@ -135,22 +130,15 @@ class KeyPriorityQueue {
135
130
// Helper function to shift down a node to proper position (equivalent to bubbleDown)
136
131
let currPos = position
137
132
let [ child1Pos , child2Pos ] = getChildrenPosition ( currPos )
138
- let [ child1Priority , child2Priority ] = [ Infinity , Infinity ]
139
- if ( child1Pos < this . _heap . length ) {
140
- child1Priority = this . _heap [ child1Pos ] [ 1 ]
141
- }
142
- if ( child2Pos < this . _heap . length ) {
143
- child2Priority = this . _heap [ child2Pos ] [ 1 ]
144
- }
145
- let currPriority
146
- try {
147
- currPriority = this . _heap [ currPos ] [ 1 ]
148
- } catch {
133
+ let child1Priority = this . _getPriorityOrInfinite ( child1Pos )
134
+ let child2Priority = this . _getPriorityOrInfinite ( child2Pos )
135
+ let currPriority = this . _getPriorityOrInfinite ( currPos )
136
+
137
+ if ( currPriority == Infinity ) {
149
138
return
150
139
}
151
140
152
- while ( child2Pos < this . _heap . length &&
153
- ( child1Priority < currPriority || child2Priority < currPriority ) ) {
141
+ while ( child1Priority < currPriority || child2Priority < currPriority ) {
154
142
if ( child1Priority < currPriority && child1Priority < child2Priority ) {
155
143
this . _swap ( child1Pos , currPos )
156
144
currPos = child1Pos
@@ -159,18 +147,9 @@ class KeyPriorityQueue {
159
147
currPos = child2Pos
160
148
}
161
149
[ child1Pos , child2Pos ] = getChildrenPosition ( currPos )
162
- try {
163
- [ child1Priority , child2Priority ] = [ this . _heap [ child1Pos ] [ 1 ] , this . _heap [ child2Pos ] [ 1 ] ]
164
- } catch ( error ) {
165
- [ child1Priority , child2Priority ] = [ Infinity , Infinity ]
166
- }
167
-
168
- currPriority = this . _heap [ currPos ] [ 1 ]
169
- }
170
- this . keys [ this . _heap [ currPos ] [ 0 ] ] = currPos
171
- if ( child1Pos < this . _heap . length && child1Priority < currPriority ) {
172
- this . _swap ( child1Pos , currPos )
173
- this . keys [ this . _heap [ child1Pos ] [ 0 ] ] = child1Pos
150
+ child1Priority = this . _getPriorityOrInfinite ( child1Pos )
151
+ child2Priority = this . _getPriorityOrInfinite ( child2Pos )
152
+ currPriority = this . _getPriorityOrInfinite ( currPos )
174
153
}
175
154
}
176
155
0 commit comments