-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprioqueue.js
77 lines (73 loc) · 1.92 KB
/
prioqueue.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
/* ---------------------------------------------------------------------
PrioQueue - priority sorted random access collection, e.g. for queuing rendering
tasks which must be done in specific order due to layer compositing
* ---------------------------------------------------------------------- */
class PrioQueue {
constructor() {
this.prios = new Map();
}
get(prio) {
if(!this.prios.has(prio)) {
this.prios.set(prio, new Array()); }
return this.prios.get(prio);
}
insertlast(prio, value) {
this.get(prio).push(value);
}
insertfirst(prio, value) {
this.get(prio).unshift(value);
}
remove(prio=null, value=null) {
if(prio==null && value==null) {
this.prios.clear();
}
else if(value==null && prio!=null) {
if(this.prios.has(prio))
this.prios.delete(prio);
}
else if(value!=null && prio==null) {
this.prios.elems().each((prio, vals)=>{
vals.remove(value);
if(vals.length <= 0) {
this.prios.delete(prio);
}
});
} else {
if(this.prios.has(prio))
{
this.prios.get(prio).delete(value);
}
}
}
minprio() {
return Array.from(this.prios.keys()).sort((l,r) => l>r?1:l<r?-1:0 )[0];
}
maxprio() {
return Array.from(this.prios.keys()).sort((l,r) => l>r?-1:l<r?1:0 )[0];
}
length() {
var total = 0;
Array.from(this.prios.values()).each((vals)=>{total=total+vals.length})
return total;
}
pop() {
if(this.prios.size <= 0)
return [null, null];
var prio = this.maxprio();
var vals = this.prios.get(prio);
var val = vals.pop();
if(vals.length <= 0)
this.prios.delete(prio);
return [prio, val];
}
shift() {
if(this.prios.size <= 0)
return [null, null];
var prio = this.minprio();
var vals = this.prios.get(prio);
var val = vals.shift();
if(vals.length <= 0)
this.prios.delete(prio);
return [prio, val];
}
}