-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityq.gleam
193 lines (178 loc) · 4.06 KB
/
priorityq.gleam
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import gleam/list
import gleam/option.{type Option, None, Some}
import gleam/order.{type Order, Gt}
/// A priority queue implemented with a max pairing heap.
///
pub opaque type PriorityQueue(t) {
PriorityQueue(data: PairingHeap(t), cmp: Cmp(t))
}
type PairingHeap(t) {
Empty
NonEmpty(PairingTree(t))
}
type Cmp(t) =
fn(t, t) -> Order
type PairingTree(t) {
PairingTree(val: t, children: List(PairingTree(t)), size: Int)
}
/// Creates an empty priority queue.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// new(int.compare) // -> PriorityQueue(Int)
/// ```
///
pub fn new(cmp: Cmp(t)) -> PriorityQueue(t) {
PriorityQueue(Empty, cmp)
}
/// Creates a priority queue from a list.
///
/// Runs in linear time.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// from_list([1, 10, 5], int.compare) // -> PriorityQueue(Int)
/// ```
///
pub fn from_list(ls: List(t), cmp: Cmp(t)) -> PriorityQueue(t) {
new(cmp)
|> list.fold(ls, _, push)
}
fn from_pairing_tree(tree: PairingTree(t), cmp: Cmp(t)) -> PriorityQueue(t) {
PriorityQueue(NonEmpty(tree), cmp)
}
fn one(val: t, cmp: Cmp(t)) -> PriorityQueue(t) {
PriorityQueue(NonEmpty(PairingTree(val, [], size: 1)), cmp)
}
/// Returns whether the priority queue is empty.
///
/// Runs in constant time.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// new(int.compare) |> is_empty() // -> True
/// from_list([0], int.compare) |> is_empty() // -> False
/// ```
///
pub fn is_empty(pq: PriorityQueue(t)) -> Bool {
pq.data == Empty
}
/// Returns the number of elements in the priority queue.
///
/// Runs in constant time.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// from_list([1, 2, 3], int.compare) |> size() // -> 3
/// ```
///
pub fn size(pq: PriorityQueue(t)) -> Int {
case pq.data {
Empty -> 0
NonEmpty(tree) -> tree.size
}
}
/// Returns the maximum value in the priority queue.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// new(int.compare) |> peek() // -> None
/// from_list([1, 10, 5], int.compare) |> peek() // -> 10
/// ```
///
pub fn peek(pq: PriorityQueue(t)) -> Option(t) {
case pq.data {
Empty -> None
NonEmpty(tree) -> Some(tree.val)
}
}
fn merge(pq1: PriorityQueue(t), pq2: PriorityQueue(t)) -> PriorityQueue(t) {
case pq1.cmp == pq2.cmp {
False -> panic as "inconsistent cmp function"
True ->
case pq1.data, pq2.data {
Empty, _ -> pq2
_, Empty -> pq1
NonEmpty(tree1), NonEmpty(tree2) -> {
let new_size = tree1.size + tree2.size
case pq1.cmp(tree1.val, tree2.val) {
Gt ->
PriorityQueue(
NonEmpty(PairingTree(
tree1.val,
[tree2, ..tree1.children],
new_size,
)),
pq1.cmp,
)
_ ->
PriorityQueue(
NonEmpty(PairingTree(
tree2.val,
[tree1, ..tree2.children],
new_size,
)),
pq1.cmp,
)
}
}
}
}
}
/// Pushes a value into the priority queue.
///
/// Runs in constant time.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// new(int.compare) |> push(10) // -> PriorityQueue(Int)
/// ```
///
pub fn push(pq: PriorityQueue(t), val: t) -> PriorityQueue(t) {
merge(one(val, pq.cmp), pq)
}
/// Pops the maximum value from the priority queue.
///
/// Runs in amortized logarithmic time.
///
/// ## Examples
///
/// ```gleam
/// import gleam/int
///
/// from_list([0]) |> pop() // -> PriorityQueue(Int)
/// ```
///
pub fn pop(pq: PriorityQueue(t)) -> PriorityQueue(t) {
case pq.data {
Empty -> pq
NonEmpty(tree) -> merge_pairs(tree.children, pq.cmp)
}
}
fn merge_pairs(trees: List(PairingTree(t)), cmp: Cmp(t)) -> PriorityQueue(t) {
case trees {
[] -> PriorityQueue(Empty, cmp)
[tree] -> from_pairing_tree(tree, cmp)
[tree1, tree2, ..rest] ->
merge(from_pairing_tree(tree1, cmp), from_pairing_tree(tree2, cmp))
|> merge(merge_pairs(rest, cmp))
}
}