@@ -20,72 +20,68 @@ export function push(heap: Heap, node: Node): void {
2020}
2121
2222export function peek ( heap : Heap ) : Node | null {
23- const first = heap [ 0 ] ;
24- return first === undefined ? null : first ;
23+ return heap . length === 0 ? null : heap [ 0 ] ;
2524}
2625
2726export function pop ( heap : Heap ) : Node | null {
28- const first = heap [ 0 ] ;
29- if ( first !== undefined ) {
30- const last = heap . pop ( ) ;
31- if ( last !== first ) {
32- heap [ 0 ] = last ;
33- siftDown ( heap , last , 0 ) ;
34- }
35- return first ;
36- } else {
27+ if ( heap . length === 0 ) {
3728 return null ;
3829 }
30+ const first = heap [ 0 ] ;
31+
32+ const last = heap . pop ( ) ;
33+
34+ if ( last !== first ) {
35+ heap [ 0 ] = last ;
36+ siftDown ( heap , last , 0 ) ;
37+ }
38+
39+ return first ;
3940}
4041
4142function siftUp ( heap , node , i ) {
4243 let index = i ;
43- while ( true ) {
44- const parentIndex = ( index - 1 ) >>> 1 ;
44+
45+ while ( index > 0 ) {
46+ const parentIndex = Math . floor ( ( index - 1 ) / 2 ) ;
4547 const parent = heap [ parentIndex ] ;
46- if ( parent !== undefined && compare ( parent , node ) > 0 ) {
47- // The parent is larger. Swap positions.
48- heap [ parentIndex ] = node ;
49- heap [ index ] = parent ;
50- index = parentIndex ;
51- } else {
52- // The parent is smaller. Exit.
53- return ;
48+
49+ if ( compare ( parent , node ) < 0 ) {
50+ break
5451 }
52+ swap ( heap , parentIndex , index ) ;
53+ index = parentIndex ;
5554 }
5655}
5756
5857function siftDown ( heap , node , i ) {
5958 let index = i ;
60- const length = heap . length ;
61- while ( index < length ) {
62- const leftIndex = ( index + 1 ) * 2 - 1 ;
63- const left = heap [ leftIndex ] ;
64- const rightIndex = leftIndex + 1 ;
65- const right = heap [ rightIndex ] ;
66-
67- // If the left or right node is smaller, swap with the smaller of those.
68- if ( left !== undefined && compare ( left , node ) < 0 ) {
69- if ( right !== undefined && compare ( right , left ) < 0 ) {
70- heap [ index ] = right ;
71- heap [ rightIndex ] = node ;
72- index = rightIndex ;
73- } else {
74- heap [ index ] = left ;
75- heap [ leftIndex ] = node ;
76- index = leftIndex ;
77- }
78- } else if ( right !== undefined && compare ( right , node ) < 0 ) {
79- heap [ index ] = right ;
80- heap [ rightIndex ] = node ;
81- index = rightIndex ;
82- } else {
83- // Neither child is smaller. Exit.
84- return ;
59+ const halfLength = Math . floor ( heap . length / 2 )
60+
61+ while ( index < halfLength ) {
62+ let bestIndex = index * 2 + 1 ;
63+ const rightIndex = index * 2 + 2 ;
64+
65+ // If the right node is smaller, swap with the smaller of those.
66+ if ( heap . length > rightIndex && compare ( heap [ rightIndex ] , heap [ bestIndex ] ) < 0 ) {
67+ bestIndex = rightIndex
68+ }
69+
70+ if ( compare ( node , heap [ bestIndex ] ) < 0 ) {
71+ break ;
8572 }
73+
74+ swap ( heap , bestIndex , index ) ;
75+ index = bestIndex ;
8676 }
8777}
8878
79+ function swap ( heap , left , right ) {
80+ const item = heap [ left ] ;
81+ heap [ left ] = heap [ right ] ;
82+ heap [ right ] = item ;
83+ }
84+
8985function compare ( a , b ) {
9086 // Compare sort index first, then task id.
9187 const diff = a . sortIndex - b . sortIndex ;
0 commit comments