-
Notifications
You must be signed in to change notification settings - Fork 1
/
priority-queue.dua
71 lines (56 loc) · 1.2 KB
/
priority-queue.dua
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
import "../lib/priority-queue.dua"
// A quick hack to replace the extern variable
// to avoid both compilation and linking errors
int __ = { untrack(__IS_RANDOM_SEED_SET); 0 };
bool __IS_RANDOM_SEED_SET = false;
nomangle int printf(str message, ...);
// Case Sorting numbers in an ascending order
// Outputs "12345"
int main()
{
MinPriorityQueue<int> q;
q.insert(5);
q.insert(2);
q.insert(4);
q.insert(1);
q.insert(3);
while (!q.is_empty()) {
printf("%d", q.pop());
}
}
// Case Sorting numbers in a descending order
// Outputs "54321"
int main()
{
MaxPriorityQueue<int> q;
q.insert(5);
q.insert(2);
q.insert(4);
q.insert(1);
q.insert(3);
while (!q.is_empty()) {
printf("%d", q.pop());
}
}
// Case Using a custom comparator
// Outputs "123"
class Integer
{
int num;
constructor(int num) : num(num) { }
constructor() { }
}
class Comparator
{
int compare(Integer& a, Integer& b) {
return a.num - b.num;
}
}
int main()
{
PriorityQueue<Integer, Comparator> p;
p.insert((3)Integer);
p.insert((2)Integer);
p.insert((1)Integer);
printf("%d%d%d", p.pop().num, p.pop().num, p.pop().num);
}