-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.js
375 lines (308 loc) · 9.6 KB
/
Heap.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
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
// Generated by CoffeeScript 1.8.0
(function() {
var Heap, defaultCmp, floor, heapify, heappop, heappush, heappushpop, heapreplace, insort, min, nlargest, nsmallest, updateItem, _siftdown, _siftup;
floor = Math.floor, min = Math.min;
/*
Default comparison function to be used
*/
defaultCmp = function(x, y) {
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
};
/*
Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the right of the rightmost x.
Optional args lo (default 0) and hi (default a.length) bound the slice
of a to be searched.
*/
insort = function(a, x, lo, hi, cmp) {
var mid;
if (lo == null) {
lo = 0;
}
if (cmp == null) {
cmp = defaultCmp;
}
if (lo < 0) {
throw new Error('lo must be non-negative');
}
if (hi == null) {
hi = a.length;
}
while (lo < hi) {
mid = floor((lo + hi) / 2);
if (cmp(x, a[mid]) < 0) {
hi = mid;
} else {
lo = mid + 1;
}
}
return ([].splice.apply(a, [lo, lo - lo].concat(x)), x);
};
/*
Push item onto heap, maintaining the heap invariant.
*/
heappush = function(array, item, cmp) {
if (cmp == null) {
cmp = defaultCmp;
}
array.push(item);
return _siftdown(array, 0, array.length - 1, cmp);
};
/*
Pop the smallest item off the heap, maintaining the heap invariant.
*/
heappop = function(array, cmp) {
var lastelt, returnitem;
if (cmp == null) {
cmp = defaultCmp;
}
lastelt = array.pop();
if (array.length) {
returnitem = array[0];
array[0] = lastelt;
_siftup(array, 0, cmp);
} else {
returnitem = lastelt;
}
return returnitem;
};
/*
Pop and return the current smallest value, and add the new item.
This is more efficient than heappop() followed by heappush(), and can be
more appropriate when using a fixed size heap. Note that the value
returned may be larger than item! That constrains reasonable use of
this routine unless written as part of a conditional replacement:
if item > array[0]
item = heapreplace(array, item)
*/
heapreplace = function(array, item, cmp) {
var returnitem;
if (cmp == null) {
cmp = defaultCmp;
}
returnitem = array[0];
array[0] = item;
_siftup(array, 0, cmp);
return returnitem;
};
/*
Fast version of a heappush followed by a heappop.
*/
heappushpop = function(array, item, cmp) {
var _ref;
if (cmp == null) {
cmp = defaultCmp;
}
if (array.length && cmp(array[0], item) < 0) {
_ref = [array[0], item], item = _ref[0], array[0] = _ref[1];
_siftup(array, 0, cmp);
}
return item;
};
/*
Transform list into a heap, in-place, in O(array.length) time.
*/
heapify = function(array, cmp) {
var i, _i, _j, _len, _ref, _ref1, _results, _results1;
if (cmp == null) {
cmp = defaultCmp;
}
_ref1 = (function() {
_results1 = [];
for (var _j = 0, _ref = floor(array.length / 2); 0 <= _ref ? _j < _ref : _j > _ref; 0 <= _ref ? _j++ : _j--){ _results1.push(_j); }
return _results1;
}).apply(this).reverse();
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
i = _ref1[_i];
_results.push(_siftup(array, i, cmp));
}
return _results;
};
/*
Update the position of the given item in the heap.
This function should be called every time the item is being modified.
*/
updateItem = function(array, item, cmp) {
var pos;
if (cmp == null) {
cmp = defaultCmp;
}
pos = array.indexOf(item);
if (pos === -1) {
return;
}
_siftdown(array, 0, pos, cmp);
return _siftup(array, pos, cmp);
};
/*
Find the n largest elements in a dataset.
*/
nlargest = function(array, n, cmp) {
var elem, result, _i, _len, _ref;
if (cmp == null) {
cmp = defaultCmp;
}
result = array.slice(0, n);
if (!result.length) {
return result;
}
heapify(result, cmp);
_ref = array.slice(n);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
elem = _ref[_i];
heappushpop(result, elem, cmp);
}
return result.sort(cmp).reverse();
};
/*
Find the n smallest elements in a dataset.
*/
nsmallest = function(array, n, cmp) {
var elem, i, los, result, _i, _j, _len, _ref, _ref1, _results;
if (cmp == null) {
cmp = defaultCmp;
}
if (n * 10 <= array.length) {
result = array.slice(0, n).sort(cmp);
if (!result.length) {
return result;
}
los = result[result.length - 1];
_ref = array.slice(n);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
elem = _ref[_i];
if (cmp(elem, los) < 0) {
insort(result, elem, 0, null, cmp);
result.pop();
los = result[result.length - 1];
}
}
return result;
}
heapify(array, cmp);
_results = [];
for (i = _j = 0, _ref1 = min(n, array.length); 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
_results.push(heappop(array, cmp));
}
return _results;
};
_siftdown = function(array, startpos, pos, cmp) {
var newitem, parent, parentpos;
if (cmp == null) {
cmp = defaultCmp;
}
newitem = array[pos];
while (pos > startpos) {
parentpos = (pos - 1) >> 1;
parent = array[parentpos];
if (cmp(newitem, parent) < 0) {
array[pos] = parent;
pos = parentpos;
continue;
}
break;
}
return array[pos] = newitem;
};
_siftup = function(array, pos, cmp) {
var childpos, endpos, newitem, rightpos, startpos;
if (cmp == null) {
cmp = defaultCmp;
}
endpos = array.length;
startpos = pos;
newitem = array[pos];
childpos = 2 * pos + 1;
while (childpos < endpos) {
rightpos = childpos + 1;
if (rightpos < endpos && !(cmp(array[childpos], array[rightpos]) < 0)) {
childpos = rightpos;
}
array[pos] = array[childpos];
pos = childpos;
childpos = 2 * pos + 1;
}
array[pos] = newitem;
return _siftdown(array, startpos, pos, cmp);
};
Heap = (function() {
Heap.push = heappush;
Heap.pop = heappop;
Heap.replace = heapreplace;
Heap.pushpop = heappushpop;
Heap.heapify = heapify;
Heap.updateItem = updateItem;
Heap.nlargest = nlargest;
Heap.nsmallest = nsmallest;
function Heap(cmp) {
this.cmp = cmp != null ? cmp : defaultCmp;
this.nodes = [];
}
Heap.prototype.push = function(x) {
return heappush(this.nodes, x, this.cmp);
};
Heap.prototype.pop = function() {
return heappop(this.nodes, this.cmp);
};
Heap.prototype.peek = function() {
return this.nodes[0];
};
Heap.prototype.contains = function(x) {
return this.nodes.indexOf(x) !== -1;
};
Heap.prototype.replace = function(x) {
return heapreplace(this.nodes, x, this.cmp);
};
Heap.prototype.pushpop = function(x) {
return heappushpop(this.nodes, x, this.cmp);
};
Heap.prototype.heapify = function() {
return heapify(this.nodes, this.cmp);
};
Heap.prototype.updateItem = function(x) {
return updateItem(this.nodes, x, this.cmp);
};
Heap.prototype.clear = function() {
return this.nodes = [];
};
Heap.prototype.empty = function() {
return this.nodes.length === 0;
};
Heap.prototype.size = function() {
return this.nodes.length;
};
Heap.prototype.clone = function() {
var heap;
heap = new Heap();
heap.nodes = this.nodes.slice(0);
return heap;
};
Heap.prototype.toArray = function() {
return this.nodes.slice(0);
};
Heap.prototype.insert = Heap.prototype.push;
Heap.prototype.top = Heap.prototype.peek;
Heap.prototype.front = Heap.prototype.peek;
Heap.prototype.has = Heap.prototype.contains;
Heap.prototype.copy = Heap.prototype.clone;
return Heap;
})();
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
return define([], factory);
} else if (typeof exports === 'object') {
return module.exports = factory();
} else {
return root.Heap = factory();
}
})(this, function() {
return Heap;
});
}).call(this);