1- class PriorityQueue < T extends Object > {
1+ class PriorityQueue < T extends object > {
22 private _queue : Array < T > ;
33 private _size : number = 0 ;
4- private _comparator : Function | null ;
4+ private _comparator : ( ( val : T , parent : T ) => number ) | null ;
55
6- constructor ( initialCapacity ?: number , comparator ?: Function ) {
6+ constructor ( initialCapacity ?: number , comparator ?: ( val : T , parent : T ) => number ) {
77 const cap = initialCapacity ?? 11 ;
88 const com = comparator ?? null ;
99 if ( cap < 1 ) {
@@ -19,7 +19,7 @@ class PriorityQueue<T extends Object> {
1919 const newCapacity =
2020 oldCapacity + ( oldCapacity < 64 ? oldCapacity + 2 : oldCapacity >> 1 ) ;
2121 if ( ! Number . isSafeInteger ( newCapacity ) ) {
22- throw new Error ( 'capacity out of range ' ) ;
22+ throw new Error ( 'OOM: new capacity not a safe integer ' ) ;
2323 }
2424 this . _queue . length = newCapacity ;
2525 }
@@ -38,8 +38,8 @@ class PriorityQueue<T extends Object> {
3838 private siftupUsingComparator ( k : number , item : T ) : void {
3939 while ( k > 0 ) {
4040 // find the parent
41- let parent = ( k - 1 ) >>> 1 ;
42- let e = this . _queue [ parent ] as T ;
41+ const parent = ( k - 1 ) >>> 1 ;
42+ const e = this . _queue [ parent ] as T ;
4343 // compare item with it parent, if item's priority less, break siftup and insert
4444 if ( this . _comparator ! ( item , e ) >= 0 ) {
4545 break ;
@@ -54,8 +54,8 @@ class PriorityQueue<T extends Object> {
5454
5555 private siftupComparable ( k : number , item : T ) : void {
5656 while ( k > 0 ) {
57- let parent = ( k - 1 ) >>> 1 ;
58- let e = this . _queue [ parent ] as T ;
57+ const parent = ( k - 1 ) >>> 1 ;
58+ const e = this . _queue [ parent ] as T ;
5959 if ( item . toString ( ) . localeCompare ( e . toString ( ) ) >= 0 ) {
6060 break ;
6161 }
@@ -74,19 +74,19 @@ class PriorityQueue<T extends Object> {
7474 }
7575
7676 private sinkUsingComparator ( k : number , item : T ) : void {
77- let half = this . _size >>> 1 ;
77+ const half = this . _size >>> 1 ;
7878 while ( k < half ) {
7979 let child = ( k << 1 ) + 1 ;
8080 let object = this . _queue [ child ] ;
81- let right = child + 1 ;
82- // compare left right child, assgn child the bigger one
81+ const right = child + 1 ;
82+ // compare left right child, assign child the bigger one
8383 if (
8484 right < this . _size &&
8585 this . _comparator ! ( object , this . _queue [ right ] ) > 0
8686 ) {
8787 object = this . _queue [ ( child = right ) ] ;
8888 }
89- //compare item and child if bigger is item, break
89+ // compare item and child if bigger is item, break
9090 if ( this . _comparator ! ( item , object ) <= 0 ) {
9191 break ;
9292 }
@@ -97,11 +97,11 @@ class PriorityQueue<T extends Object> {
9797 }
9898
9999 private sinkComparable ( k : number , item : T ) : void {
100- let half = this . _size >>> 1 ;
100+ const half = this . _size >>> 1 ;
101101 while ( k < half ) {
102102 let child = ( k << 1 ) + 1 ;
103103 let object = this . _queue [ child ] ;
104- let right = child + 1 ;
104+ const right = child + 1 ;
105105
106106 if (
107107 right < this . _size &&
@@ -128,7 +128,7 @@ class PriorityQueue<T extends Object> {
128128 }
129129
130130 public add ( item : T ) : boolean {
131- let i = this . _size ;
131+ const i = this . _size ;
132132 if ( i >= this . _queue . length ) {
133133 this . grow ( ) ;
134134 }
@@ -145,9 +145,9 @@ class PriorityQueue<T extends Object> {
145145 if ( this . _size === 0 ) {
146146 return null ;
147147 }
148- let s = -- this . _size ;
149- let result = < T > this . _queue [ 0 ] ;
150- let x = < T > this . _queue [ s ] ;
148+ const s = -- this . _size ;
149+ const result = < T > this . _queue [ 0 ] ;
150+ const x = < T > this . _queue [ s ] ;
151151 this . _queue . slice ( s , 1 ) ;
152152 if ( s !== 0 ) {
153153 this . sink ( 0 , x ) ;
@@ -164,9 +164,7 @@ class PriorityQueue<T extends Object> {
164164 }
165165
166166 public clear ( ) : void {
167- for ( let item of this . _queue ) {
168- ( item as any ) = null ;
169- }
167+ this . _queue . fill ( null as unknown as T ) ;
170168 this . _size = 0 ;
171169 }
172170
@@ -179,7 +177,7 @@ class PriorityQueue<T extends Object> {
179177 }
180178
181179 public toArray ( ) : Array < T > {
182- return this . _queue . filter ( item => item ) ;
180+ return this . _queue ;
183181 }
184182
185183 public toString ( ) : string {
0 commit comments