@js-sdsl/priority-queue
Version:
javascript standard data structure library which benchmark against C++ STL
197 lines (189 loc) • 5.2 kB
JavaScript
var extendStatics = function(t, i) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function(t, i) {
t.__proto__ = i;
} || function(t, i) {
for (var r in i) if (Object.prototype.hasOwnProperty.call(i, r)) t[r] = i[r];
};
return extendStatics(t, i);
};
function __extends(t, i) {
if (typeof i !== "function" && i !== null) throw new TypeError("Class extends value " + String(i) + " is not a constructor or null");
extendStatics(t, i);
function __() {
this.constructor = t;
}
t.prototype = i === null ? Object.create(i) : (__.prototype = i.prototype, new __);
}
function __read(t, i) {
var r = typeof Symbol === "function" && t[Symbol.iterator];
if (!r) return t;
var e = r.call(t), n, u = [], s;
try {
while ((i === void 0 || i-- > 0) && !(n = e.next()).done) u.push(n.value);
} catch (t) {
s = {
error: t
};
} finally {
try {
if (n && !n.done && (r = e["return"])) r.call(e);
} finally {
if (s) throw s.error;
}
}
return u;
}
function __spreadArray(t, i, r) {
if (r || arguments.length === 2) for (var e = 0, n = i.length, u; e < n; e++) {
if (u || !(e in i)) {
if (!u) u = Array.prototype.slice.call(i, 0, e);
u[e] = i[e];
}
}
return t.concat(u || Array.prototype.slice.call(i));
}
var Base = function() {
function Base() {
this.t = 0;
}
Object.defineProperty(Base.prototype, "length", {
get: function() {
return this.t;
},
enumerable: false,
configurable: true
});
Base.prototype.size = function() {
return this.t;
};
Base.prototype.empty = function() {
return this.t === 0;
};
return Base;
}();
(function(t) {
__extends(Container, t);
function Container() {
return t !== null && t.apply(this, arguments) || this;
}
return Container;
})(Base);
var PriorityQueue = function(t) {
__extends(PriorityQueue, t);
function PriorityQueue(i, r, e) {
if (i === void 0) {
i = [];
}
if (r === void 0) {
r = function(t, i) {
if (t > i) return -1;
if (t < i) return 1;
return 0;
};
}
if (e === void 0) {
e = true;
}
var n = t.call(this) || this;
n.i = r;
if (Array.isArray(i)) {
n.u = e ? __spreadArray([], __read(i), false) : i;
} else {
n.u = [];
var u = n;
i.forEach((function(t) {
u.u.push(t);
}));
}
n.t = n.u.length;
var s = n.t >> 1;
for (var o = n.t - 1 >> 1; o >= 0; --o) {
n.o(o, s);
}
return n;
}
PriorityQueue.prototype.h = function(t) {
var i = this.u[t];
while (t > 0) {
var r = t - 1 >> 1;
var e = this.u[r];
if (this.i(e, i) <= 0) break;
this.u[t] = e;
t = r;
}
this.u[t] = i;
};
PriorityQueue.prototype.o = function(t, i) {
var r = this.u[t];
while (t < i) {
var e = t << 1 | 1;
var n = e + 1;
var u = this.u[e];
if (n < this.t && this.i(u, this.u[n]) > 0) {
e = n;
u = this.u[n];
}
if (this.i(u, r) >= 0) break;
this.u[t] = u;
t = e;
}
this.u[t] = r;
};
PriorityQueue.prototype.clear = function() {
this.t = 0;
this.u.length = 0;
};
PriorityQueue.prototype.push = function(t) {
this.u.push(t);
this.h(this.t);
this.t += 1;
};
PriorityQueue.prototype.pop = function() {
if (this.t === 0) return;
var t = this.u[0];
var i = this.u.pop();
this.t -= 1;
if (this.t) {
this.u[0] = i;
this.o(0, this.t >> 1);
}
return t;
};
PriorityQueue.prototype.top = function() {
return this.u[0];
};
PriorityQueue.prototype.find = function(t) {
return this.u.indexOf(t) >= 0;
};
PriorityQueue.prototype.remove = function(t) {
var i = this.u.indexOf(t);
if (i < 0) return false;
if (i === 0) {
this.pop();
} else if (i === this.t - 1) {
this.u.pop();
this.t -= 1;
} else {
this.u.splice(i, 1, this.u.pop());
this.t -= 1;
this.h(i);
this.o(i, this.t >> 1);
}
return true;
};
PriorityQueue.prototype.updateItem = function(t) {
var i = this.u.indexOf(t);
if (i < 0) return false;
this.h(i);
this.o(i, this.t >> 1);
return true;
};
PriorityQueue.prototype.toArray = function() {
return __spreadArray([], __read(this.u), false);
};
return PriorityQueue;
}(Base);
export { PriorityQueue };
//# sourceMappingURL=index.js.map