1- function defaultCompare ( a , b ) {
2- return a < b ? - 1 : a > b ? 1 : 0 ;
3- }
41
52export default class TinyQueue {
6- constructor ( data , compare ) {
7- this . data = data || [ ] ;
3+ constructor ( data = [ ] , compare = defaultCompare ) {
4+ this . data = data ;
85 this . length = this . data . length ;
9- this . compare = compare || defaultCompare ;
6+ this . compare = compare ;
107
118 if ( this . length > 0 ) {
129 for ( let i = ( this . length >> 1 ) - 1 ; i >= 0 ; i -- ) this . _down ( i ) ;
1310 }
1411 }
12+
1513 push ( item ) {
1614 this . data . push ( item ) ;
1715 this . length ++ ;
1816 this . _up ( this . length - 1 ) ;
1917 }
18+
2019 pop ( ) {
2120 if ( this . length === 0 ) return undefined ;
2221
@@ -31,12 +30,13 @@ export default class TinyQueue {
3130
3231 return top ;
3332 }
33+
3434 peek ( ) {
3535 return this . data [ 0 ] ;
3636 }
37+
3738 _up ( pos ) {
38- const data = this . data ;
39- const compare = this . compare ;
39+ const { data, compare} = this ;
4040 const item = data [ pos ] ;
4141
4242 while ( pos > 0 ) {
@@ -49,16 +49,16 @@ export default class TinyQueue {
4949
5050 data [ pos ] = item ;
5151 }
52+
5253 _down ( pos ) {
53- const data = this . data ;
54- const compare = this . compare ;
54+ const { data, compare} = this ;
5555 const halfLength = this . length >> 1 ;
5656 const item = data [ pos ] ;
5757
5858 while ( pos < halfLength ) {
5959 let left = ( pos << 1 ) + 1 ;
60- let right = left + 1 ;
6160 let best = data [ left ] ;
61+ const right = left + 1 ;
6262
6363 if ( right < this . length && compare ( data [ right ] , best ) < 0 ) {
6464 left = right ;
@@ -73,3 +73,7 @@ export default class TinyQueue {
7373 data [ pos ] = item ;
7474 }
7575}
76+
77+ function defaultCompare ( a , b ) {
78+ return a < b ? - 1 : a > b ? 1 : 0 ;
79+ }
0 commit comments