/
HeapAsync.ts
892 lines (824 loc) · 23.7 KB
/
HeapAsync.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
export type AsyncComparator<T> = (a: T, b: T) => Promise<number>;
export type AsyncIsEqual<T> = (e: T, o: T) => Promise<boolean>;
/**
* Heap
* @type {Class}
*/
export class HeapAsync<T> implements Iterable<Promise<T>> {
heapArray: Array<T> = [];
_limit = 0;
/**
* Alias of add
*/
offer = this.add;
/**
* Alias of peek
*/
element = this.peek;
/**
* Alias of pop
*/
poll = this.pop;
/**
* Heap instance constructor.
* @param {Function} compare Optional comparison function, defaults to Heap.minComparator<number>
*/
constructor(public compare: AsyncComparator<T> = HeapAsync.minComparator) {}
/*
Static methods
*/
/**
* Gets children indices for given index.
* @param {Number} idx Parent index
* @return {Array(Number)} Array of children indices
*/
static getChildrenIndexOf(idx: number): Array<number> {
return [idx * 2 + 1, idx * 2 + 2];
}
/**
* Gets parent index for given index.
* @param {Number} idx Children index
* @return {Number | undefined} Parent index, -1 if idx is 0
*/
static getParentIndexOf(idx: number): number {
if (idx <= 0) {
return -1;
}
const whichChildren = idx % 2 ? 1 : 2;
return Math.floor((idx - whichChildren) / 2);
}
/**
* Gets sibling index for given index.
* @param {Number} idx Children index
* @return {Number | undefined} Sibling index, -1 if idx is 0
*/
static getSiblingIndexOf(idx: number): number {
if (idx <= 0) {
return -1;
}
const whichChildren = idx % 2 ? 1 : -1;
return idx + whichChildren;
}
/**
* Min heap comparison function, default.
* @param {any} a First element
* @param {any} b Second element
* @return {Number} 0 if they're equal, positive if `a` goes up, negative if `b` goes up
*/
static async minComparator<N>(a: N, b: N): Promise<number> {
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
}
/**
* Max heap comparison function.
* @param {any} a First element
* @param {any} b Second element
* @return {Number} 0 if they're equal, positive if `a` goes up, negative if `b` goes up
*/
static async maxComparator<N>(a: N, b: N): Promise<number> {
if (b > a) {
return 1;
} else if (b < a) {
return -1;
} else {
return 0;
}
}
/**
* Min number heap comparison function, default.
* @param {Number} a First element
* @param {Number} b Second element
* @return {Number} 0 if they're equal, positive if `a` goes up, negative if `b` goes up
*/
static async minComparatorNumber(a: number, b: number): Promise<number> {
return a - b;
}
/**
* Max number heap comparison function.
* @param {Number} a First element
* @param {Number} b Second element
* @return {Number} 0 if they're equal, positive if `a` goes up, negative if `b` goes up
*/
static async maxComparatorNumber(a: number, b: number): Promise<number> {
return b - a;
}
/**
* Default equality function.
* @param {any} a First element
* @param {any} b Second element
* @return {Boolean} True if equal, false otherwise
*/
static async defaultIsEqual<N>(a: N, b: N): Promise<boolean> {
return a === b;
}
/**
* Prints a heap.
* @param {HeapAsync} heap Heap to be printed
* @returns {String}
*/
static print<N>(heap: HeapAsync<N>): string {
function deep(i: number) {
const pi = HeapAsync.getParentIndexOf(i);
return Math.floor(Math.log2(pi + 1));
}
function repeat(str: string, times: number) {
let out = '';
for (; times > 0; --times) {
out += str;
}
return out;
}
let node = 0;
const lines: Array<Array<string>> = [];
const maxLines = deep(heap.length - 1) + 2;
let maxLength = 0;
while (node < heap.length) {
let i = deep(node) + 1;
if (node === 0) {
i = 0;
}
// Text representation
const nodeText = String(heap.get(node));
if (nodeText.length > maxLength) {
maxLength = nodeText.length;
}
// Add to line
lines[i] = lines[i] || [];
lines[i].push(nodeText);
node += 1;
}
return lines
.map((line, i) => {
const times = Math.pow(2, maxLines - i) - 1;
return (
repeat(' ', Math.floor(times / 2) * maxLength) +
line
.map((el) => {
// centered
const half = (maxLength - el.length) / 2;
return repeat(' ', Math.ceil(half)) + el + repeat(' ', Math.floor(half));
})
.join(repeat(' ', times * maxLength))
);
})
.join('\n');
}
/*
Python style
*/
/**
* Converts an array into an array-heap, in place
* @param {Array} arr Array to be modified
* @param {Function} compare Optional compare function
* @return {HeapAsync} For convenience, it returns a Heap instance
*/
static async heapify<N>(arr: Array<N>, compare?: AsyncComparator<N>): Promise<HeapAsync<N>> {
const heap = new HeapAsync(compare);
heap.heapArray = arr;
await heap.init();
return heap;
}
/**
* Extract the peek of an array-heap
* @param {Array} heapArr Array to be modified, should be a heap
* @param {Function} compare Optional compare function
* @return {any} Returns the extracted peek
*/
static heappop<N>(heapArr: Array<N>, compare?: AsyncComparator<N>): Promise<N | undefined> {
const heap = new HeapAsync(compare);
heap.heapArray = heapArr;
return heap.pop();
}
/**
* Pushes a item into an array-heap
* @param {Array} heapArr Array to be modified, should be a heap
* @param {any} item Item to push
* @param {Function} compare Optional compare function
*/
static async heappush<N>(heapArr: Array<N>, item: N, compare?: AsyncComparator<N>): Promise<void> {
const heap = new HeapAsync(compare);
heap.heapArray = heapArr;
await heap.push(item);
}
/**
* Push followed by pop, faster
* @param {Array} heapArr Array to be modified, should be a heap
* @param {any} item Item to push
* @param {Function} compare Optional compare function
* @return {any} Returns the extracted peek
*/
static heappushpop<N>(heapArr: Array<N>, item: N, compare?: AsyncComparator<N>): Promise<N> {
const heap = new HeapAsync(compare);
heap.heapArray = heapArr;
return heap.pushpop(item);
}
/**
* Replace peek with item
* @param {Array} heapArr Array to be modified, should be a heap
* @param {any} item Item as replacement
* @param {Function} compare Optional compare function
* @return {any} Returns the extracted peek
*/
static heapreplace<N>(heapArr: Array<N>, item: N, compare?: AsyncComparator<N>): Promise<N> {
const heap = new HeapAsync(compare);
heap.heapArray = heapArr;
return heap.replace(item);
}
/**
* Return the `n` most valuable elements of a heap-like Array
* @param {Array} heapArr Array, should be an array-heap
* @param {number} n Max number of elements
* @param {Function} compare Optional compare function
* @return {any} Elements
*/
static heaptop<N>(heapArr: Array<N>, n = 1, compare?: AsyncComparator<N>): Promise<Array<N>> {
const heap = new HeapAsync(compare);
heap.heapArray = heapArr;
return heap.top(n);
}
/**
* Return the `n` least valuable elements of a heap-like Array
* @param {Array} heapArr Array, should be an array-heap
* @param {number} n Max number of elements
* @param {Function} compare Optional compare function
* @return {any} Elements
*/
static heapbottom<N>(heapArr: Array<N>, n = 1, compare?: AsyncComparator<N>): Promise<Array<N>> {
const heap = new HeapAsync(compare);
heap.heapArray = heapArr;
return heap.bottom(n);
}
/**
* Return the `n` most valuable elements of an iterable
* @param {number} n Max number of elements
* @param {Iterable} Iterable Iterable list of elements
* @param {Function} compare Optional compare function
* @return {any} Elements
*/
static async nlargest<N>(
n: number,
iterable: Iterable<N>,
compare?: AsyncComparator<N>
): Promise<Array<N>> {
const heap = new HeapAsync(compare);
heap.heapArray = [...iterable];
await heap.init();
return heap.top(n);
}
/**
* Return the `n` least valuable elements of an iterable
* @param {number} n Max number of elements
* @param {Iterable} Iterable Iterable list of elements
* @param {Function} compare Optional compare function
* @return {any} Elements
*/
static async nsmallest<N>(
n: number,
iterable: Iterable<N>,
compare?: AsyncComparator<N>
): Promise<Array<N>> {
const heap = new HeapAsync(compare);
heap.heapArray = [...iterable];
await heap.init();
return heap.bottom(n);
}
/*
Instance methods
*/
/**
* Adds an element to the heap. Aliases: `offer`.
* Same as: push(element)
* @param {any} element Element to be added
* @return {Boolean} true
*/
async add(element: T): Promise<boolean> {
await this._sortNodeUp(this.heapArray.push(element) - 1);
this._applyLimit();
return true;
}
/**
* Adds an array of elements to the heap.
* Similar as: push(element, element, ...).
* @param {Array} elements Elements to be added
* @return {Boolean} true
*/
async addAll(elements: Array<T>): Promise<boolean> {
let i = this.length;
this.heapArray.push(...elements);
for (const l = this.length; i < l; ++i) {
await this._sortNodeUp(i);
}
this._applyLimit();
return true;
}
/**
* Return the bottom (lowest value) N elements of the heap.
*
* @param {Number} n Number of elements.
* @return {Array} Array of length <= N.
*/
async bottom(n = 1): Promise<Array<T>> {
if (this.heapArray.length === 0 || n <= 0) {
// Nothing to do
return [];
} else if (this.heapArray.length === 1) {
// Just the peek
return [this.heapArray[0]];
} else if (n >= this.heapArray.length) {
// The whole heap
return [...this.heapArray];
} else {
// Some elements
return this._bottomN_push(~~n);
}
}
/**
* Check if the heap is sorted, useful for testing purposes.
* @return {Undefined | Element} Returns an element if something wrong is found, otherwise it's undefined
*/
async check(): Promise<T | undefined> {
for (let j = 0; j < this.heapArray.length; ++j) {
const el = this.heapArray[j];
const children = this.getChildrenOf(j);
for (const ch of children) {
if ((await this.compare(el, ch)) > 0) {
return el;
}
}
}
}
/**
* Remove all of the elements from this heap.
*/
clear(): void {
this.heapArray = [];
}
/**
* Clone this heap
* @return {HeapAsync}
*/
clone(): HeapAsync<T> {
const cloned = new HeapAsync<T>(this.comparator());
cloned.heapArray = this.toArray();
cloned._limit = this._limit;
return cloned;
}
/**
* Returns the comparison function.
* @return {Function}
*/
comparator(): AsyncComparator<T> {
return this.compare;
}
/**
* Returns true if this queue contains the specified element.
* @param {any} o Element to be found
* @param {Function} fn Optional comparison function, receives (element, needle)
* @return {Boolean}
*/
async contains(o: T, fn: AsyncIsEqual<T> = HeapAsync.defaultIsEqual): Promise<boolean> {
for (const el of this.heapArray) {
if (await fn(el, o)) {
return true;
}
}
return false;
}
/**
* Initialise a heap, sorting nodes
* @param {Array} array Optional initial state array
*/
async init(array?: Array<T>): Promise<void> {
if (array) {
this.heapArray = [...array];
}
for (let i = Math.floor(this.heapArray.length); i >= 0; --i) {
await this._sortNodeDown(i);
}
this._applyLimit();
}
/**
* Test if the heap has no elements.
* @return {Boolean} True if no elements on the heap
*/
isEmpty(): boolean {
return this.length === 0;
}
/**
* Get the leafs of the tree (no children nodes)
*/
leafs(): Array<T> {
if (this.heapArray.length === 0) {
return [];
}
const pi = HeapAsync.getParentIndexOf(this.heapArray.length - 1);
return this.heapArray.slice(pi + 1);
}
/**
* Length of the heap.
* @return {Number}
*/
get length(): number {
return this.heapArray.length;
}
/**
* Get length limit of the heap.
* @return {Number}
*/
get limit(): number {
return this._limit;
}
/**
* Set length limit of the heap.
* @return {Number}
*/
set limit(_l: number) {
this._limit = ~~_l;
this._applyLimit();
}
/**
* Top node. Aliases: `element`.
* Same as: `top(1)[0]`
* @return {any} Top node
*/
peek(): T | undefined {
return this.heapArray[0];
}
/**
* Extract the top node (root). Aliases: `poll`.
* @return {any} Extracted top node, undefined if empty
*/
async pop(): Promise<T | undefined> {
const last = this.heapArray.pop();
if (this.length > 0 && last !== undefined) {
return this.replace(last);
}
return last;
}
/**
* Pushes element(s) to the heap.
* @param {...any} elements Elements to insert
* @return {Boolean} True if elements are present
*/
async push(...elements: Array<T>): Promise<boolean> {
if (elements.length < 1) {
return false;
} else if (elements.length === 1) {
return this.add(elements[0]);
} else {
return this.addAll(elements);
}
}
/**
* Same as push & pop in sequence, but faster
* @param {any} element Element to insert
* @return {any} Extracted top node
*/
async pushpop(element: T): Promise<T> {
if ((await this.compare(this.heapArray[0], element)) < 0) {
[element, this.heapArray[0]] = [this.heapArray[0], element];
await this._sortNodeDown(0);
}
return element;
}
/**
* Remove an element from the heap.
* @param {any} o Element to be found
* @param {Function} fn Optional function to compare
* @return {Boolean} True if the heap was modified
*/
async remove(o?: T, fn: AsyncIsEqual<T> = HeapAsync.defaultIsEqual): Promise<boolean> {
if (this.length > 0) {
if (o === undefined) {
await this.pop();
return true;
} else {
let idx = -1;
for (let i = 0; i < this.heapArray.length; ++i) {
if (await fn(this.heapArray[i], o)) {
idx = i;
break;
}
}
if (idx >= 0) {
if (idx === 0) {
await this.pop();
} else if (idx === this.length - 1) {
this.heapArray.pop();
} else {
this.heapArray.splice(idx, 1, this.heapArray.pop() as T);
await this._sortNodeUp(idx);
await this._sortNodeDown(idx);
}
return true;
}
}
}
return false;
}
/**
* Pop the current peek value, and add the new item.
* @param {any} element Element to replace peek
* @return {any} Old peek
*/
async replace(element: T): Promise<T> {
const peek = this.heapArray[0];
this.heapArray[0] = element;
await this._sortNodeDown(0);
return peek;
}
/**
* Size of the heap
* @return {Number}
*/
size(): number {
return this.length;
}
/**
* Return the top (highest value) N elements of the heap.
*
* @param {Number} n Number of elements.
* @return {Array} Array of length <= N.
*/
async top(n = 1): Promise<Array<T>> {
if (this.heapArray.length === 0 || n <= 0) {
// Nothing to do
return [];
} else if (this.heapArray.length === 1 || n === 1) {
// Just the peek
return [this.heapArray[0]];
} else if (n >= this.heapArray.length) {
// The whole peek
return [...this.heapArray];
} else {
// Some elements
return this._topN_push(~~n);
}
}
/**
* Clone the heap's internal array
* @return {Array}
*/
toArray(): Array<T> {
return [...this.heapArray];
}
/**
* String output, call to Array.prototype.toString()
* @return {String}
*/
toString(): string {
return this.heapArray.toString();
}
/**
* Get the element at the given index.
* @param {Number} i Index to get
* @return {any} Element at that index
*/
get(i: number): T {
return this.heapArray[i];
}
/**
* Get the elements of these node's children
* @param {Number} idx Node index
* @return {Array(any)} Children elements
*/
getChildrenOf(idx: number): Array<T> {
return HeapAsync.getChildrenIndexOf(idx)
.map((i) => this.heapArray[i])
.filter((e) => e !== undefined);
}
/**
* Get the element of this node's parent
* @param {Number} idx Node index
* @return {any} Parent element
*/
getParentOf(idx: number): T | undefined {
const pi = HeapAsync.getParentIndexOf(idx);
return this.heapArray[pi];
}
/**
* Iterator interface
*/
*[Symbol.iterator](): Iterator<Promise<T>> {
while (this.length) {
yield this.pop() as Promise<T>;
}
}
/**
* Returns an iterator. To comply with Java interface.
*/
iterator(): Iterable<Promise<T>> {
return this;
}
/**
* Limit heap size if needed
*/
_applyLimit(): void {
if (this._limit && this._limit < this.heapArray.length) {
let rm = this.heapArray.length - this._limit;
// It's much faster than splice
while (rm) {
this.heapArray.pop();
--rm;
}
}
}
/**
* Return the bottom (lowest value) N elements of the heap, without corner cases, unsorted
*
* @param {Number} n Number of elements.
* @return {Array} Array of length <= N.
*/
async _bottomN_push(n: number): Promise<Array<T>> {
// Use an inverted heap
const bottomHeap = new HeapAsync(this.compare);
bottomHeap.limit = n;
bottomHeap.heapArray = this.heapArray.slice(-n);
await bottomHeap.init();
const startAt = this.heapArray.length - 1 - n;
const parentStartAt = HeapAsync.getParentIndexOf(startAt);
const indices = [];
for (let i = startAt; i > parentStartAt; --i) {
indices.push(i);
}
const arr = this.heapArray;
while (indices.length) {
const i = indices.shift() as number;
if ((await this.compare(arr[i], bottomHeap.peek() as T)) > 0) {
await bottomHeap.replace(arr[i]);
if (i % 2) {
indices.push(HeapAsync.getParentIndexOf(i));
}
}
}
return bottomHeap.toArray();
}
/**
* Returns the inverse to the comparison function.
* @return {Number}
*/
_invertedCompare = (a: T, b: T): Promise<number> => {
return this.compare(a, b).then((res) => -1 * res);
};
/**
* Move a node to a new index, switching places
* @param {Number} j First node index
* @param {Number} k Another node index
*/
_moveNode(j: number, k: number): void {
[this.heapArray[j], this.heapArray[k]] = [this.heapArray[k], this.heapArray[j]];
}
/**
* Move a node down the tree (to the leaves) to find a place where the heap is sorted.
* @param {Number} i Index of the node
*/
async _sortNodeDown(i: number): Promise<void> {
let moveIt = i < this.heapArray.length - 1;
const self = this.heapArray[i];
const getPotentialParent = async (best: number, j: number) => {
if (this.heapArray.length > j && (await this.compare(this.heapArray[j], this.heapArray[best])) < 0) {
best = j;
}
return best;
};
while (moveIt) {
const childrenIdx = HeapAsync.getChildrenIndexOf(i);
let bestChildIndex = childrenIdx[0];
for (let j = 1; j < childrenIdx.length; ++j) {
bestChildIndex = await getPotentialParent(bestChildIndex, childrenIdx[j]);
}
const bestChild = this.heapArray[bestChildIndex];
if (typeof bestChild !== 'undefined' && (await this.compare(self, bestChild)) > 0) {
this._moveNode(i, bestChildIndex);
i = bestChildIndex;
} else {
moveIt = false;
}
}
}
/**
* Move a node up the tree (to the root) to find a place where the heap is sorted.
* @param {Number} i Index of the node
*/
async _sortNodeUp(i: number): Promise<void> {
let moveIt = i > 0;
while (moveIt) {
const pi = HeapAsync.getParentIndexOf(i);
if (pi >= 0 && (await this.compare(this.heapArray[pi], this.heapArray[i])) > 0) {
this._moveNode(i, pi);
i = pi;
} else {
moveIt = false;
}
}
}
/**
* Return the top (highest value) N elements of the heap, without corner cases, unsorted
* Implementation: push.
*
* @param {Number} n Number of elements.
* @return {Array} Array of length <= N.
*/
async _topN_push(n: number): Promise<Array<T>> {
// Use an inverted heap
const topHeap = new HeapAsync(this._invertedCompare);
topHeap.limit = n;
const indices = [0];
const arr = this.heapArray;
while (indices.length) {
const i = indices.shift() as number;
if (i < arr.length) {
if (topHeap.length < n) {
await topHeap.push(arr[i]);
indices.push(...HeapAsync.getChildrenIndexOf(i));
} else if ((await this.compare(arr[i], topHeap.peek() as T)) < 0) {
await topHeap.replace(arr[i]);
indices.push(...HeapAsync.getChildrenIndexOf(i));
}
}
}
return topHeap.toArray();
}
/**
* Return the top (highest value) N elements of the heap, without corner cases, unsorted
* Implementation: init + push.
*
* @param {Number} n Number of elements.
* @return {Array} Array of length <= N.
*/
async _topN_fill(n: number): Promise<Array<T>> {
// Use an inverted heap
const { heapArray } = this;
const topHeap = new HeapAsync(this._invertedCompare);
topHeap.limit = n;
topHeap.heapArray = heapArray.slice(0, n);
await topHeap.init();
const branch = HeapAsync.getParentIndexOf(n - 1) + 1;
const indices = [];
for (let i = branch; i < n; ++i) {
indices.push(...HeapAsync.getChildrenIndexOf(i).filter((l) => l < heapArray.length));
}
if ((n - 1) % 2) {
indices.push(n);
}
while (indices.length) {
const i = indices.shift() as number;
if (i < heapArray.length) {
if ((await this.compare(heapArray[i], topHeap.peek() as T)) < 0) {
await topHeap.replace(heapArray[i]);
indices.push(...HeapAsync.getChildrenIndexOf(i));
}
}
}
return topHeap.toArray();
}
/**
* Return the top (highest value) N elements of the heap, without corner cases, unsorted
* Implementation: heap.
*
* @param {Number} n Number of elements.
* @return {Array} Array of length <= N.
*/
async _topN_heap(n: number): Promise<Array<T>> {
const topHeap = this.clone();
const result: Array<T> = [];
for (let i = 0; i < n; ++i) {
result.push((await topHeap.pop()) as T);
}
return result;
}
/**
* Return index of the top element
* @param list
*/
async _topIdxOf(list: Array<T>): Promise<number> {
if (!list.length) {
return -1;
}
let idx = 0;
let top = list[idx];
for (let i = 1; i < list.length; ++i) {
const comp = await this.compare(list[i], top);
if (comp < 0) {
idx = i;
top = list[i];
}
}
return idx;
}
/**
* Return the top element
* @param list
*/
async _topOf(...list: Array<T>): Promise<T | undefined> {
const heap = new HeapAsync(this.compare);
await heap.init(list);
return heap.peek();
}
}
export default HeapAsync;