forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.mpl
executable file
·70 lines (61 loc) · 1.08 KB
/
PriorityQueue.mpl
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
"PriorityQueue" module
"Array" includeModule
PriorityQueue: [{
data: Array;
swap: [
copy i1:; copy i2:;
i1ref: i1 @data.at;
i2ref: i2 @data.at;
tmp: @i1ref move copy;
@i2ref move @i1ref set
@tmp move @i2ref set
];
parent: [1 - 2 /];
lchild: [2 * 1 +];
rchild: [2 * 2 +];
lift: [
copy i:;
[
i 0 > [
p: i parent;
p data.at i data.at < [
i p swap
p @i set
TRUE
] &&
] &&
] loop
];
push: [
@data.pushBack
data.getSize 1 - lift
];
top: [0 @data.at];
pop: [
i: 0 dynamic;
[
l: i lchild;
r: i rchild;
l data.getSize < [
r data.getSize < [
c: r data.at l data.at < [l copy] [r copy] if;
i c swap
c @i set
TRUE
] [
i l swap
FALSE
] if
] [
i data.getSize 1 - < [
i data.getSize 1 - swap
i lift
] when
FALSE
] if
] loop
@data.popBack
];
getSize: [data.getSize];
empty: [data.getSize 0 =];
}];