-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.js
More file actions
95 lines (82 loc) · 2.22 KB
/
priorityqueue.js
File metadata and controls
95 lines (82 loc) · 2.22 KB
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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.PriorityQueue = factory());
})(this, (function () { 'use strict';
/**
* @copyright can-dy-jack 2023
*/
/**
* @class PriorityQueue
*/
var PriorityQueue = function PriorityQueue(
data,
compare
) {
if ( data === void 0 ) data = [];
if ( compare === void 0 ) compare = function (a, b) {
return a === b ? 0 : a > b ? 1 : -1;
};
this.data = data;
this.compare = compare;
this.size = data.length;
if (this.size > 0) {
for (var i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
this._up(i);
}
}
};
PriorityQueue.prototype.push = function push (val) {
this.data.push(val);
this.size++;
if (this.size === 1) {
return;
}
var idx = this.size - 1;
while (idx > 0) {
var parent = Math.floor((idx - 1) / 2);
var cur = this.data[parent];
if (this.compare(val, cur) >= 0) {
break;
}
this.data[idx] = cur;
idx = parent;
}
this.data[idx] = val;
};
PriorityQueue.prototype.peek = function peek () {
return this.data[0];
};
PriorityQueue.prototype.pop = function pop () {
if (this.isEmpty()) { return undefined; }
var top = this.data[0];
this.size--;
if (this.isEmpty()) {
return this.data.pop();
}
this.data[0] = this.data.pop();
this._up(0);
return top;
};
PriorityQueue.prototype._up = function _up (pos) {
var half = Math.floor(this.size / 2);
var val = this.data[pos];
while (pos < half) {
var more = pos * 2 + 1;
var right = more + 2;
if (right < this.size && this.compare(this.data[right], this.data[more]) < 0) {
more = right;
}
if (this.compare(this.data[more], val) >= 0) {
break;
}
this.data[pos] = this.data[more];
pos = more;
}
this.data[pos] = val;
};
PriorityQueue.prototype.isEmpty = function isEmpty () {
return this.size === 0;
};
return PriorityQueue;
}));