-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
186 lines (159 loc) · 3.93 KB
/
index.js
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
class List {
constructor (head, tail) {
if (typeof head === 'undefined' && typeof tail === 'undefined') {
this.isEmpty = true
} else {
this.head = head
this.tail = tail
}
}
prepend (head) {
return new List(head, this)
}
}
const EmptyList = new List()
// This class is a bare bones implementation of an immutable pairs heap with
// external mergepairs and comparator operations.
class Heap {
constructor (item, children) {
if (item || typeof item !== 'undefined') {
this.item = item
this.children = children || EmptyList
}
}
get isEmpty () {
return !('item' in this)
}
merge (heap, comparitor) {
if (this.isEmpty) {
return heap
}
/* istanbul ignore next because the containing object never merges an empty */
if (heap.isEmpty) {
return this
}
if (comparitor(this.item, heap.item) <= 0) {
return new Heap(this.item, this.children.prepend(heap))
} else {
return new Heap(heap.item, heap.children.prepend(this))
}
}
getMin () {
return this.item
}
pop (comparitor) {
return mergePairs(this.children, comparitor)
}
}
const EmptyHeap = new Heap()
// mergePairs returns a promise to the heap resulting from
// doing mergePairs on the list. The merged result is gathered
// recursively, but each recursive step goes through setImmediate
// to keep the overall process from smashing the stack or stealing
// large amounts of time from the event loop.
const WORK_UNIT_SIZE = 32
function mergePairs (list, comparitor, previousResult) {
return new Promise((resolve, reject) => setImmediate(() => {
let a = EmptyHeap
let b = EmptyHeap
let l = list
let n = WORK_UNIT_SIZE
// Loop throug up to WORK_UNIT_SIZE pairs of entries in the list
while (!l.isEmpty && n > 0) {
a = l.head
l = l.tail
if (!l.isEmpty) {
a = a.merge(l.head, comparitor)
l = l.tail
}
b = b.merge(a, comparitor)
n -= 1
}
if (previousResult) {
b = previousResult.merge(b, comparitor)
}
if (l.isEmpty) {
resolve(b)
} else {
resolve(mergePairs(l, comparitor, b))
}
}))
}
class PairingHeap {
constructor (comparitor) {
this.comparitor = comparitor || ((a, b) => (a > b) - (b > a))
this.size = 0
this.heap = EmptyHeap
this.busy = false
this.notifier = null
}
get length () {
return this.size
}
get isEmpty () {
return this.size === 0
}
waitForNotBusy (cb) {
if (this.busy) {
if (!this.notifier) {
this.notifier = new Promise((resolve, reject) => {
const checkBusy = () => {
if (this.busy) {
setImmediate(checkBusy)
} else {
this.notifier = null
cb()
resolve()
}
}
checkBusy()
})
}
return this.notifier.then(cb)
} else {
return Promise.resolve(this).then(cb)
}
}
insert (item) {
if (typeof item === 'undefined') {
throw Error('empty insert')
}
const insertItem = (item) => {
this.size += 1
this.heap = this.heap.merge(new Heap(item), this.comparitor)
return this
}
if (this.busy) {
return this.waitForNotBusy(() => insertItem(item))
} else {
return Promise.resolve(insertItem(item))
}
}
peek () {
return this.waitForNotBusy(() => {
if (this.isEmpty) {
throw Error('heap empty')
}
return this.heap.getMin()
})
}
pop () {
const popFunc = async () => {
if (this.isEmpty) {
throw Error('heap empty')
}
this.busy = true
const result = this.heap.getMin()
this.heap = await this.heap.pop(this.comparitor)
this.size -= 1
this.busy = false
return result
}
if (!this.busy) {
return popFunc()
} else {
return this.waitForNotBusy(popFunc)
}
}
}
module.exports = PairingHeap