@@ -537,30 +537,28 @@ export class HeapAsync<T> implements Iterable<Promise<T>> {
537
537
* @return {Boolean } True if the heap was modified
538
538
*/
539
539
async remove ( o ?: T , fn : AsyncIsEqual < T > = HeapAsync . defaultIsEqual ) : Promise < boolean > {
540
- if ( this . length > 0 ) {
541
- if ( o === undefined ) {
542
- await this . pop ( ) ;
540
+ if ( ! this . heapArray . length ) return false ;
541
+ if ( o === undefined ) {
542
+ await this . pop ( ) ;
543
+ return true ;
544
+ }
545
+ const queue = [ 0 ] ;
546
+ while ( queue . length ) {
547
+ const idx = queue . shift ( ) as number ;
548
+ if ( await fn ( this . heapArray [ idx ] , o ) ) {
549
+ if ( idx === 0 ) {
550
+ await this . pop ( ) ;
551
+ } else if ( idx === this . heapArray . length - 1 ) {
552
+ this . heapArray . pop ( ) ;
553
+ } else {
554
+ this . heapArray . splice ( idx , 1 , this . heapArray . pop ( ) as T ) ;
555
+ await this . _sortNodeUp ( idx ) ;
556
+ await this . _sortNodeDown ( idx ) ;
557
+ }
543
558
return true ;
544
559
} else {
545
- let idx = - 1 ;
546
- for ( let i = 0 ; i < this . heapArray . length ; ++ i ) {
547
- if ( await fn ( this . heapArray [ i ] , o ) ) {
548
- idx = i ;
549
- break ;
550
- }
551
- }
552
- if ( idx >= 0 ) {
553
- if ( idx === 0 ) {
554
- await this . pop ( ) ;
555
- } else if ( idx === this . length - 1 ) {
556
- this . heapArray . pop ( ) ;
557
- } else {
558
- this . heapArray . splice ( idx , 1 , this . heapArray . pop ( ) as T ) ;
559
- await this . _sortNodeUp ( idx ) ;
560
- await this . _sortNodeDown ( idx ) ;
561
- }
562
- return true ;
563
- }
560
+ const children = HeapAsync . getChildrenIndexOf ( idx ) . filter ( ( c ) => c < this . heapArray . length ) ;
561
+ queue . push ( ...children ) ;
564
562
}
565
563
}
566
564
return false ;
@@ -737,29 +735,20 @@ export class HeapAsync<T> implements Iterable<Promise<T>> {
737
735
* @param {Number } i Index of the node
738
736
*/
739
737
async _sortNodeDown ( i : number ) : Promise < void > {
740
- let moveIt = i < this . heapArray . length - 1 ;
741
- const self = this . heapArray [ i ] ;
742
-
743
- const getPotentialParent = async ( best : number , j : number ) => {
744
- if ( this . heapArray . length > j && ( await this . compare ( this . heapArray [ j ] , this . heapArray [ best ] ) ) < 0 ) {
745
- best = j ;
738
+ const length = this . heapArray . length ;
739
+ while ( true ) {
740
+ const left = 2 * i + 1 ;
741
+ const right = left + 1 ;
742
+ let best = i ;
743
+ if ( left < length && ( await this . compare ( this . heapArray [ left ] , this . heapArray [ best ] ) ) < 0 ) {
744
+ best = left ;
746
745
}
747
- return best ;
748
- } ;
749
-
750
- while ( moveIt ) {
751
- const childrenIdx = HeapAsync . getChildrenIndexOf ( i ) ;
752
- let bestChildIndex = childrenIdx [ 0 ] ;
753
- for ( let j = 1 ; j < childrenIdx . length ; ++ j ) {
754
- bestChildIndex = await getPotentialParent ( bestChildIndex , childrenIdx [ j ] ) ;
755
- }
756
- const bestChild = this . heapArray [ bestChildIndex ] ;
757
- if ( typeof bestChild !== 'undefined' && ( await this . compare ( self , bestChild ) ) > 0 ) {
758
- this . _moveNode ( i , bestChildIndex ) ;
759
- i = bestChildIndex ;
760
- } else {
761
- moveIt = false ;
746
+ if ( right < length && ( await this . compare ( this . heapArray [ right ] , this . heapArray [ best ] ) ) < 0 ) {
747
+ best = right ;
762
748
}
749
+ if ( best === i ) break ;
750
+ this . _moveNode ( i , best ) ;
751
+ i = best ;
763
752
}
764
753
}
765
754
@@ -768,15 +757,12 @@ export class HeapAsync<T> implements Iterable<Promise<T>> {
768
757
* @param {Number } i Index of the node
769
758
*/
770
759
async _sortNodeUp ( i : number ) : Promise < void > {
771
- let moveIt = i > 0 ;
772
- while ( moveIt ) {
760
+ while ( i > 0 ) {
773
761
const pi = HeapAsync . getParentIndexOf ( i ) ;
774
- if ( pi >= 0 && ( await this . compare ( this . heapArray [ pi ] , this . heapArray [ i ] ) ) > 0 ) {
762
+ if ( pi >= 0 && ( await this . compare ( this . heapArray [ i ] , this . heapArray [ pi ] ) ) < 0 ) {
775
763
this . _moveNode ( i , pi ) ;
776
764
i = pi ;
777
- } else {
778
- moveIt = false ;
779
- }
765
+ } else break ;
780
766
}
781
767
}
782
768
0 commit comments