@@ -283,34 +283,37 @@ function BinaryHeap(scoreFunction) {
283
283
284
284
BinaryHeap . prototype = {
285
285
push : function ( element ) {
286
+ var content = this . content ;
286
287
// Add the new element to the end of the array.
287
- this . content . push ( element ) ;
288
+ content . push ( element ) ;
288
289
289
290
// Allow it to sink down.
290
- this . sinkDown ( this . content . length - 1 ) ;
291
+ this . sinkDown ( content . length - 1 ) ;
291
292
} ,
292
293
pop : function ( ) {
294
+ var content = this . content ;
293
295
// Store the first element so we can return it later.
294
- var result = this . content [ 0 ] ;
296
+ var result = content [ 0 ] ;
295
297
// Get the element at the end of the array.
296
- var end = this . content . pop ( ) ;
298
+ var end = content . pop ( ) ;
297
299
// If there are any elements left, put the end element at the
298
300
// start, and let it bubble up.
299
- if ( this . content . length > 0 ) {
300
- this . content [ 0 ] = end ;
301
+ if ( content . length !== 0 ) {
302
+ content [ 0 ] = end ;
301
303
this . bubbleUp ( 0 ) ;
302
304
}
303
305
return result ;
304
306
} ,
305
307
remove : function ( node ) {
306
- var i = this . content . indexOf ( node ) ;
308
+ var content = this . content ;
309
+ var i = content . indexOf ( node ) ;
307
310
308
311
// When it is found, the process seen in 'pop' is repeated
309
312
// to fill up the hole.
310
- var end = this . content . pop ( ) ;
313
+ var end = content . pop ( ) ;
311
314
312
- if ( i !== this . content . length - 1 ) {
313
- this . content [ i ] = end ;
315
+ if ( i !== content . length - 1 ) {
316
+ content [ i ] = end ;
314
317
315
318
if ( this . scoreFunction ( end ) < this . scoreFunction ( node ) ) {
316
319
this . sinkDown ( i ) ;
@@ -326,19 +329,22 @@ BinaryHeap.prototype = {
326
329
this . sinkDown ( this . content . indexOf ( node ) ) ;
327
330
} ,
328
331
sinkDown : function ( n ) {
332
+ var content = this . content ;
333
+ var scoreFunction = this . scoreFunction ;
329
334
// Fetch the element that has to be sunk.
330
- var element = this . content [ n ] ;
331
-
335
+ var element = content [ n ] ;
336
+ //
337
+ var elemScore = scoreFunction ( element ) ;
338
+ var parentN = 0 , parent = 0 ;
332
339
// When at 0, an element can not sink any further.
333
340
while ( n > 0 ) {
334
-
335
341
// Compute the parent element's index, and fetch it.
336
- var parentN = ( ( n + 1 ) >> 1 ) - 1 ;
337
- var parent = this . content [ parentN ] ;
342
+ parentN = ( ( n + 1 ) >> 1 ) - 1 ;
343
+ parent = content [ parentN ] ;
338
344
// Swap the elements if the parent is greater.
339
- if ( this . scoreFunction ( element ) < this . scoreFunction ( parent ) ) {
340
- this . content [ parentN ] = element ;
341
- this . content [ n ] = parent ;
345
+ if ( elemScore < scoreFunction ( parent ) ) {
346
+ content [ parentN ] = element ;
347
+ content [ n ] = parent ;
342
348
// Update 'n' to continue at the new position.
343
349
n = parentN ;
344
350
}
@@ -349,23 +355,29 @@ BinaryHeap.prototype = {
349
355
}
350
356
} ,
351
357
bubbleUp : function ( n ) {
358
+ var content = this . content ;
359
+ var scoreFunction = this . scoreFunction ;
352
360
// Look up the target element and its score.
353
- var length = this . content . length ;
354
- var element = this . content [ n ] ;
355
- var elemScore = this . scoreFunction ( element ) ;
361
+ var length = content . length ;
362
+ var element = content [ n ] ;
363
+ var elemScore = scoreFunction ( element ) ;
364
+ // early declarations with type hints
365
+ var child2N = 0 , child1N = 0 ;
366
+ var child1Score = 0 ;
367
+ var swap = - 1 ; // no type change !! -1 stands for no swap. X2 speed increase !!!
368
+ var child1 = null , child2 = null ;
356
369
357
370
while ( true ) {
358
371
// Compute the indices of the child elements.
359
- var child2N = ( n + 1 ) << 1 ;
360
- var child1N = child2N - 1 ;
372
+ child2N = ( n + 1 ) << 1 ;
373
+ child1N = child2N - 1 ;
361
374
// This is used to store the new position of the element, if any.
362
- var swap = null ;
363
- var child1Score ;
375
+ swap = - 1 ;
364
376
// If the first child exists (is inside the array)...
365
377
if ( child1N < length ) {
366
378
// Look it up and compute its score.
367
- var child1 = this . content [ child1N ] ;
368
- child1Score = this . scoreFunction ( child1 ) ;
379
+ child1 = content [ child1N ] ;
380
+ child1Score = scoreFunction ( child1 ) ;
369
381
370
382
// If the score is less than our element's, we need to swap.
371
383
if ( child1Score < elemScore ) {
@@ -375,17 +387,16 @@ BinaryHeap.prototype = {
375
387
376
388
// Do the same checks for the other child.
377
389
if ( child2N < length ) {
378
- var child2 = this . content [ child2N ] ;
379
- var child2Score = this . scoreFunction ( child2 ) ;
380
- if ( child2Score < ( swap === null ? elemScore : child1Score ) ) {
390
+ child2 = content [ child2N ] ;
391
+ if ( scoreFunction ( child2 ) < ( swap >= 0 ? elemScore : child1Score ) ) {
381
392
swap = child2N ;
382
393
}
383
394
}
384
395
385
396
// If the element needs to be moved, swap it, and continue.
386
- if ( swap !== null ) {
387
- this . content [ n ] = this . content [ swap ] ;
388
- this . content [ swap ] = element ;
397
+ if ( swap >= 0 ) {
398
+ content [ n ] = content [ swap ] ;
399
+ content [ swap ] = element ;
389
400
n = swap ;
390
401
}
391
402
// Otherwise, we are done.
@@ -401,4 +412,4 @@ return {
401
412
Graph : Graph
402
413
} ;
403
414
404
- } ) ;
415
+ } ) ;
0 commit comments