-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathheap.mjs
145 lines (145 loc) · 2.97 KB
/
heap.mjs
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
// src/heap.ts
var Heap = class {
constructor(data = [], compare = (lhs, rhs) => lhs < rhs ? -1 : lhs > rhs ? 1 : 0) {
if (typeof data === "function") {
compare = data;
data = [];
}
this.data = [null, ...data];
this.lt = (i, j) => compare(this.data[i], this.data[j]) < 0;
for (let i = this.size(); i > 0; i--)
this.heapify(i);
}
size() {
return this.data.length - 1;
}
push(v) {
this.data.push(v);
let i = this.size();
while (i >> 1 !== 0 && this.lt(i, i >> 1))
this.swap(i, i >>= 1);
}
pop() {
this.swap(1, this.size());
const top = this.data.pop();
this.heapify(1);
return top;
}
top() {
return this.data[1];
}
heapify(i) {
while (true) {
let min = i;
const [l, r, n] = [i * 2, i * 2 + 1, this.data.length];
if (l < n && this.lt(l, min))
min = l;
if (r < n && this.lt(r, min))
min = r;
if (min !== i) {
this.swap(i, min);
i = min;
} else
break;
}
}
clear() {
this.data = [null];
}
swap(i, j) {
const d = this.data;
[d[i], d[j]] = [d[j], d[i]];
}
};
var RemovableHeap = class {
constructor(data = [], cmp) {
this.heap = new Heap(data, cmp);
this.counts = new Map();
this._size = 0;
for (let i = 1; i < this.heap.data.length; i++) {
this.count(this.heap.data[i], 1);
}
}
size() {
return this._size;
}
top() {
this._normalize();
return this.heap.top();
}
pop() {
this._normalize();
if (this.heap.size() < 1)
return void 0;
const top = this.heap.pop();
this.count(top, -1);
return top;
}
push(num) {
this.count(num, 1);
this.heap.push(num);
}
remove(num) {
if (Number(this.counts.get(num)) > 0) {
this.count(num, -1);
}
}
has(value) {
return this.counts.get(value) > 0;
}
count(num, diff = 1) {
var _a;
const count = (_a = this.counts.get(num)) != null ? _a : 0;
this.counts.set(num, count + diff);
this._size += diff;
}
_normalize() {
while (this.heap.size() && !this.counts.get(this.heap.top())) {
this.heap.pop();
}
}
};
var RemovableDoubleHeap = class {
constructor(data = [], cmp = (lhs, rhs) => lhs < rhs ? -1 : lhs > rhs ? 1 : 0) {
this.min = new RemovableHeap(data, cmp);
this.max = new RemovableHeap(data, (lhs, rhs) => -cmp(lhs, rhs));
}
popMin() {
const min = this.min.pop();
this.max.remove(min);
return min;
}
popMax() {
const max = this.max.pop();
this.min.remove(max);
return max;
}
remove(num) {
this.min.remove(num);
this.max.remove(num);
}
size() {
return this.min.size();
}
push(num) {
this.min.push(num);
this.max.push(num);
}
};
var PriorityQueue = class extends RemovableHeap {
offer(value) {
return this.push(value);
}
poll() {
return this.pop();
}
peek() {
return this.top();
}
};
export {
Heap,
PriorityQueue,
RemovableDoubleHeap,
RemovableHeap
};