/
pairing.ts
169 lines (151 loc) · 3.75 KB
/
pairing.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
import type {
Comparator,
Fn,
IClear,
ICopy,
IEmpty,
ILength,
IStack,
} from "@thi.ng/api";
import { compare } from "@thi.ng/compare";
import type { HeapOpts } from "./api";
interface Node<T> {
v?: T;
c: Node<T>[];
p?: Node<T>;
}
export class PairingHeap<T>
implements
Iterable<T>,
IClear,
ICopy<PairingHeap<T>>,
IEmpty<PairingHeap<T>>,
ILength,
IStack<T, T, PairingHeap<T>> {
protected compare: Comparator<T>;
protected root!: Node<T>;
protected _size!: number;
constructor(vals?: Iterable<T>, opts?: HeapOpts<T>) {
opts = Object.assign({ compare: compare }, opts);
this.compare = opts.compare;
this.clear();
vals && this.into(vals);
}
get length() {
return this._size;
}
*[Symbol.iterator]() {
const acc: T[] = [];
this.visit((x) => (acc.push(x), true));
yield* acc;
}
clear() {
this.root = { v: undefined, p: undefined, c: [] };
this._size = 0;
}
empty() {
return new PairingHeap<T>(undefined, { compare });
}
copy() {
const heap = this.empty();
return heap;
}
push(v: T) {
this.root = this.merge(this.root, {
v,
p: undefined,
c: [],
});
this._size++;
return this;
}
pop() {
const res = this.root.v;
if (res !== undefined) {
const children = this.root.c;
if (children.length) {
this.root = this.mergePairs(children);
} else {
this.root.v = undefined;
}
this._size--;
}
return res;
}
pushPop(x: T) {
if (!this._size || this.compare(x, this.root.v!) < 0) {
return x;
}
const res = this.pop();
this.push(x);
return res;
}
peek() {
return this._size ? this.root.v : undefined;
}
into(vals: Iterable<T>) {
for (let i of vals) {
this.push(i);
}
return this;
}
/**
* Computes union with given heap and clears `heap`, i.e. this heap
* will take ownership of `heap`'s items (if any).
*
* @param heap
*/
meld(heap: PairingHeap<T>) {
if (!heap._size) return this;
if (!this._size) {
this.root = heap.root;
this._size = heap._size;
} else {
this._size += heap._size;
this.root =
this.compare(this.peek()!, heap.peek()!) <= 0
? this.merge(this.root, heap.root)
: this.merge(heap.root, this.root);
}
heap.clear();
return this;
}
visit(fn: Fn<T, boolean>) {
this._size && this.doVisit(fn, this.root);
}
protected doVisit(fn: Fn<T, boolean>, root: Node<T>): boolean {
if (fn(root.v!)) {
const children = root.c;
let ok = true;
for (let i = children.length; ok && --i >= 0; ) {
ok = this.doVisit(fn, children[i]);
}
return ok;
}
return false;
}
protected merge(a: Node<T>, b: Node<T>) {
if (a.v === undefined) {
return b;
}
if (this.compare(a.v, b.v!) < 0) {
a.c.push(b);
b.p = a;
return a;
}
b.c.push(a);
a.p = b;
return b;
}
protected mergePairs(heaps: Node<T>[]) {
const n = heaps.length - 1;
let root = heaps[n];
if (n > 0) {
for (let i = n; --i >= 0; ) {
root = this.merge(root, heaps[i]);
}
}
root.p = undefined;
return root;
}
}