-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpriority-queue.js
49 lines (49 loc) · 1.24 KB
/
priority-queue.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
"use strict";
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PriorityQueue = void 0;
/** Naive priority queue; not intended for large datasets */
class PriorityQueue {
_comparator;
_items = new Array();
constructor(_comparator) {
this._comparator = _comparator;
}
clear() {
this._items = new Array();
}
push(item) {
const index = this._items.findIndex((existing) => this._comparator(item, existing) <= 0);
if (index === -1) {
this._items.push(item);
}
else {
this._items.splice(index, 0, item);
}
}
pop() {
if (this._items.length === 0) {
return undefined;
}
return this._items.splice(0, 1)[0];
}
peek() {
if (this._items.length === 0) {
return undefined;
}
return this._items[0];
}
get size() {
return this._items.length;
}
toArray() {
return this._items.slice();
}
}
exports.PriorityQueue = PriorityQueue;