-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merger.ts
86 lines (86 loc) · 2.73 KB
/
Merger.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
/**
* Copyright (c) 2018-present, heineiuo.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { assert } from "./DBHelper.ts";
import { Entry, InternalKeyComparator } from "./Format.ts";
export default class IteratorMerger {
constructor(icmp: InternalKeyComparator, list: AsyncIterableIterator<Entry>[], num: number) {
this._icmp = icmp;
this._list = list;
this._num = num;
this._cache = new Array(num);
}
private _icmp: InternalKeyComparator;
private _list: AsyncIterableIterator<Entry>[];
private _num: number;
private _cache: (IteratorResult<Entry> | null)[];
public async *iterator(reverse = false): AsyncIterableIterator<Entry> {
assert(this._num >= 0);
if (this._num === 0) {
return;
}
if (this._num === 1) {
yield* this._list[0];
return;
}
while (true) {
const current = reverse
? await this.findLargest()
: await this.findSmallest();
if (!current)
break;
yield current;
}
}
private async findLargest(): Promise<Entry | null> {
let largest = null;
let hit = -1;
for (let i = 0; i < this._num; i++) {
const child = this._cache[i] || (await this._list[i].next());
this._cache[i] = child;
if (!child.done) {
if (largest === null) {
largest = child.value;
hit = i;
}
else if (this._icmp.compare(child.value.key, largest.key) > 0) {
largest = child.value;
hit = i;
}
}
}
for (let i = 0; i < this._num; i++) {
if (i === hit) {
this._cache[i] = null;
}
}
return largest;
}
private async findSmallest(): Promise<Entry | null> {
let smallest = null;
let hit = -1;
for (let i = 0; i < this._num; i++) {
const child = this._cache[i] || (await this._list[i].next());
this._cache[i] = child;
if (!child.done) {
if (smallest === null) {
smallest = child.value;
hit = i;
}
else if (this._icmp.compare(child.value.key, smallest.key) < 0) {
smallest = child.value;
hit = i;
}
}
}
for (let i = 0; i < this._num; i++) {
if (i === hit) {
this._cache[i] = null;
}
}
return smallest;
}
}