Skip to content

Commit 244be1b

Browse files
authored
feat(stdlib): Implement mutable/immutable priority queues (#1397)
1 parent ebd87e4 commit 244be1b

File tree

7 files changed

+1272
-0
lines changed

7 files changed

+1272
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import ImmutablePriorityQueue from "immutablepriorityqueue"
2+
import List from "list"
3+
import Array from "array"
4+
5+
// formatter-ignore
6+
let lotsOfVals = [>
7+
3, 2, 1, 5, 3, 2, 2, 10, 6, 5,
8+
6, 4, 9, 10, 10, 5, 9, 6, 1, 3,
9+
3, 6, 6, 1, 3, 9, 6, 7, 4, 6,
10+
5, 4, 4, 8, 6, 4, 1, 2, 3, 8,
11+
8, 8, 10, 9, 6, 8, 4, 1, 1, 2,
12+
10, 2, 8, 10, 8, 7, 4, 8, 10, 1,
13+
3, 10, 4, 1, 6, 9, 6, 7, 8, 7,
14+
10, 4, 5, 10, 3, 2, 4, 5, 9, 5,
15+
3, 5, 10, 8, 7, 1, 6, 1, 4, 8,
16+
3, 5, 7, 2, 4, 2, 4, 5, 10, 6,
17+
7, 7, 7, 1, 10, 4, 9, 9, 10, 10,
18+
2, 5, 2, 4, 3, 10, 9, 8, 3, 5,
19+
7, 10, 9, 7, 8, 9, 3, 7, 7, 1,
20+
7, 7, 6, 3, 3, 5, 1, 8, 9, 10,
21+
7, 5, 9, 2, 9, 1, 6, 3, 10, 6,
22+
10, 2, 6, 2, 9, 3, 3, 2, 4, 1,
23+
9, 4, 9, 2, 9, 4, 8, 5, 6, 7,
24+
4, 6, 5, 4, 3, 4, 2, 9, 10, 1,
25+
10, 4, 4, 6, 3, 1, 9, 10, 2, 2,
26+
5, 5, 3, 4, 5, 8, 3, 8, 4, 10
27+
]
28+
let numVals = Array.length(lotsOfVals)
29+
30+
let sortedVals = Array.copy(lotsOfVals)
31+
Array.sort(compare, sortedVals)
32+
33+
let mut pq = ImmutablePriorityQueue.make((a, b) => a - b)
34+
let mut maxPq = ImmutablePriorityQueue.make((a, b) => b - a)
35+
36+
assert ImmutablePriorityQueue.size(pq) == 0
37+
assert ImmutablePriorityQueue.isEmpty(pq)
38+
assert ImmutablePriorityQueue.peek(pq) == None
39+
assert ImmutablePriorityQueue.drain(pq) == []
40+
41+
pq = ImmutablePriorityQueue.pop(pq)
42+
assert ImmutablePriorityQueue.size(pq) == 0
43+
44+
for (let mut i = 0; i < numVals; i += 1) {
45+
let newPq = ImmutablePriorityQueue.push(lotsOfVals[i], pq)
46+
let newMaxPq = ImmutablePriorityQueue.push(lotsOfVals[i], maxPq)
47+
assert ImmutablePriorityQueue.size(pq) == i
48+
assert ImmutablePriorityQueue.size(maxPq) == i
49+
assert ImmutablePriorityQueue.size(newPq) == i + 1
50+
assert ImmutablePriorityQueue.size(newMaxPq) == i + 1
51+
pq = newPq
52+
maxPq = newMaxPq
53+
}
54+
55+
let pqWithAll = pq
56+
let maxPqWithAll = maxPq
57+
58+
for (let mut i = 0; i < numVals; i += 1) {
59+
let val = ImmutablePriorityQueue.peek(pq)
60+
assert val == Some(sortedVals[i])
61+
let maxVal = ImmutablePriorityQueue.peek(maxPq)
62+
assert maxVal == Some(sortedVals[numVals - i - 1])
63+
64+
let newPq = ImmutablePriorityQueue.pop(pq)
65+
let newMaxPq = ImmutablePriorityQueue.pop(maxPq)
66+
let expectedSize = numVals - i
67+
assert ImmutablePriorityQueue.size(pq) == expectedSize
68+
assert ImmutablePriorityQueue.size(maxPq) == expectedSize
69+
assert ImmutablePriorityQueue.size(newPq) == expectedSize - 1
70+
assert ImmutablePriorityQueue.size(newMaxPq) == expectedSize - 1
71+
pq = newPq
72+
maxPq = newMaxPq
73+
}
74+
75+
assert ImmutablePriorityQueue.size(pq) == 0
76+
assert ImmutablePriorityQueue.size(pqWithAll) == numVals
77+
assert !ImmutablePriorityQueue.isEmpty(pqWithAll)
78+
assert ImmutablePriorityQueue.peek(pq) == None
79+
80+
let sortedList = Array.toList(sortedVals)
81+
assert ImmutablePriorityQueue.drain(pqWithAll) == sortedList
82+
assert ImmutablePriorityQueue.drain(maxPqWithAll) == List.reverse(sortedList)
83+
assert ImmutablePriorityQueue.drain(
84+
ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
85+
) ==
86+
sortedList
87+
assert ImmutablePriorityQueue.drain(
88+
ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
89+
) ==
90+
List.reverse(sortedList)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import PriorityQueue from "priorityqueue"
2+
import List from "list"
3+
import Array from "array"
4+
5+
// formatter-ignore
6+
let lotsOfVals = [>
7+
3, 2, 1, 5, 3, 2, 2, 10, 6, 5,
8+
6, 4, 9, 10, 10, 5, 9, 6, 1, 3,
9+
3, 6, 6, 1, 3, 9, 6, 7, 4, 6,
10+
5, 4, 4, 8, 6, 4, 1, 2, 3, 8,
11+
8, 8, 10, 9, 6, 8, 4, 1, 1, 2,
12+
10, 2, 8, 10, 8, 7, 4, 8, 10, 1,
13+
3, 10, 4, 1, 6, 9, 6, 7, 8, 7,
14+
10, 4, 5, 10, 3, 2, 4, 5, 9, 5,
15+
3, 5, 10, 8, 7, 1, 6, 1, 4, 8,
16+
3, 5, 7, 2, 4, 2, 4, 5, 10, 6,
17+
7, 7, 7, 1, 10, 4, 9, 9, 10, 10,
18+
2, 5, 2, 4, 3, 10, 9, 8, 3, 5,
19+
7, 10, 9, 7, 8, 9, 3, 7, 7, 1,
20+
7, 7, 6, 3, 3, 5, 1, 8, 9, 10,
21+
7, 5, 9, 2, 9, 1, 6, 3, 10, 6,
22+
10, 2, 6, 2, 9, 3, 3, 2, 4, 1,
23+
9, 4, 9, 2, 9, 4, 8, 5, 6, 7,
24+
4, 6, 5, 4, 3, 4, 2, 9, 10, 1,
25+
10, 4, 4, 6, 3, 1, 9, 10, 2, 2,
26+
5, 5, 3, 4, 5, 8, 3, 8, 4, 10
27+
]
28+
let numVals = Array.length(lotsOfVals)
29+
30+
let sortedVals = Array.copy(lotsOfVals)
31+
Array.sort(compare, sortedVals)
32+
33+
let mut pq = PriorityQueue.make((a, b) => a - b)
34+
let mut maxPq = PriorityQueue.make((a, b) => b - a)
35+
36+
assert PriorityQueue.size(pq) == 0
37+
assert PriorityQueue.isEmpty(pq)
38+
assert PriorityQueue.peek(pq) == None
39+
assert PriorityQueue.drain(pq) == []
40+
41+
assert PriorityQueue.pop(pq) == None
42+
assert PriorityQueue.size(pq) == 0
43+
44+
for (let mut i = 0; i < numVals; i += 1) {
45+
PriorityQueue.push(lotsOfVals[i], pq)
46+
PriorityQueue.push(lotsOfVals[i], maxPq)
47+
assert PriorityQueue.size(pq) == i + 1
48+
assert PriorityQueue.size(maxPq) == i + 1
49+
}
50+
51+
assert PriorityQueue.size(pq) == numVals
52+
assert PriorityQueue.size(maxPq) == numVals
53+
54+
for (let mut i = 0; i < numVals; i += 1) {
55+
let val = PriorityQueue.peek(pq)
56+
assert val == Some(sortedVals[i])
57+
let maxVal = PriorityQueue.peek(maxPq)
58+
assert maxVal == Some(sortedVals[numVals - i - 1])
59+
60+
let val = PriorityQueue.pop(pq)
61+
assert val == Some(sortedVals[i])
62+
let maxVal = PriorityQueue.pop(maxPq)
63+
assert maxVal == Some(sortedVals[numVals - i - 1])
64+
65+
assert PriorityQueue.size(pq) == numVals - i - 1
66+
assert PriorityQueue.size(maxPq) == numVals - i - 1
67+
}
68+
69+
assert PriorityQueue.size(pq) == 0
70+
assert PriorityQueue.peek(pq) == None
71+
72+
let sortedList = Array.toList(sortedVals)
73+
assert PriorityQueue.drain(
74+
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
75+
) ==
76+
sortedList
77+
assert PriorityQueue.drain(
78+
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
79+
) ==
80+
List.reverse(sortedList)

compiler/test/suites/stdlib.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ describe("stdlib", ({test, testSkip}) => {
110110
assertStdlib("set.test");
111111
assertStdlib("regex.test");
112112
assertStdlib("stack.test");
113+
assertStdlib("priorityqueue.test");
114+
assertStdlib("immutablepriorityqueue.test");
113115
assertStdlib("string.test");
114116
assertStdlib("sys.file.test");
115117
assertStdlib(~code=5, "sys.process.test");

0 commit comments

Comments
 (0)