Skip to content

Commit

Permalink
feat(stdlib): Implement mutable/immutable priority queues (#1397)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko authored Aug 5, 2022
1 parent ebd87e4 commit 244be1b
Show file tree
Hide file tree
Showing 7 changed files with 1,272 additions and 0 deletions.
90 changes: 90 additions & 0 deletions compiler/test/stdlib/immutablepriorityqueue.test.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import ImmutablePriorityQueue from "immutablepriorityqueue"
import List from "list"
import Array from "array"

// formatter-ignore
let lotsOfVals = [>
3, 2, 1, 5, 3, 2, 2, 10, 6, 5,
6, 4, 9, 10, 10, 5, 9, 6, 1, 3,
3, 6, 6, 1, 3, 9, 6, 7, 4, 6,
5, 4, 4, 8, 6, 4, 1, 2, 3, 8,
8, 8, 10, 9, 6, 8, 4, 1, 1, 2,
10, 2, 8, 10, 8, 7, 4, 8, 10, 1,
3, 10, 4, 1, 6, 9, 6, 7, 8, 7,
10, 4, 5, 10, 3, 2, 4, 5, 9, 5,
3, 5, 10, 8, 7, 1, 6, 1, 4, 8,
3, 5, 7, 2, 4, 2, 4, 5, 10, 6,
7, 7, 7, 1, 10, 4, 9, 9, 10, 10,
2, 5, 2, 4, 3, 10, 9, 8, 3, 5,
7, 10, 9, 7, 8, 9, 3, 7, 7, 1,
7, 7, 6, 3, 3, 5, 1, 8, 9, 10,
7, 5, 9, 2, 9, 1, 6, 3, 10, 6,
10, 2, 6, 2, 9, 3, 3, 2, 4, 1,
9, 4, 9, 2, 9, 4, 8, 5, 6, 7,
4, 6, 5, 4, 3, 4, 2, 9, 10, 1,
10, 4, 4, 6, 3, 1, 9, 10, 2, 2,
5, 5, 3, 4, 5, 8, 3, 8, 4, 10
]
let numVals = Array.length(lotsOfVals)

let sortedVals = Array.copy(lotsOfVals)
Array.sort(compare, sortedVals)

let mut pq = ImmutablePriorityQueue.make((a, b) => a - b)
let mut maxPq = ImmutablePriorityQueue.make((a, b) => b - a)

assert ImmutablePriorityQueue.size(pq) == 0
assert ImmutablePriorityQueue.isEmpty(pq)
assert ImmutablePriorityQueue.peek(pq) == None
assert ImmutablePriorityQueue.drain(pq) == []

pq = ImmutablePriorityQueue.pop(pq)
assert ImmutablePriorityQueue.size(pq) == 0

for (let mut i = 0; i < numVals; i += 1) {
let newPq = ImmutablePriorityQueue.push(lotsOfVals[i], pq)
let newMaxPq = ImmutablePriorityQueue.push(lotsOfVals[i], maxPq)
assert ImmutablePriorityQueue.size(pq) == i
assert ImmutablePriorityQueue.size(maxPq) == i
assert ImmutablePriorityQueue.size(newPq) == i + 1
assert ImmutablePriorityQueue.size(newMaxPq) == i + 1
pq = newPq
maxPq = newMaxPq
}

let pqWithAll = pq
let maxPqWithAll = maxPq

for (let mut i = 0; i < numVals; i += 1) {
let val = ImmutablePriorityQueue.peek(pq)
assert val == Some(sortedVals[i])
let maxVal = ImmutablePriorityQueue.peek(maxPq)
assert maxVal == Some(sortedVals[numVals - i - 1])

let newPq = ImmutablePriorityQueue.pop(pq)
let newMaxPq = ImmutablePriorityQueue.pop(maxPq)
let expectedSize = numVals - i
assert ImmutablePriorityQueue.size(pq) == expectedSize
assert ImmutablePriorityQueue.size(maxPq) == expectedSize
assert ImmutablePriorityQueue.size(newPq) == expectedSize - 1
assert ImmutablePriorityQueue.size(newMaxPq) == expectedSize - 1
pq = newPq
maxPq = newMaxPq
}

assert ImmutablePriorityQueue.size(pq) == 0
assert ImmutablePriorityQueue.size(pqWithAll) == numVals
assert !ImmutablePriorityQueue.isEmpty(pqWithAll)
assert ImmutablePriorityQueue.peek(pq) == None

let sortedList = Array.toList(sortedVals)
assert ImmutablePriorityQueue.drain(pqWithAll) == sortedList
assert ImmutablePriorityQueue.drain(maxPqWithAll) == List.reverse(sortedList)
assert ImmutablePriorityQueue.drain(
ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
) ==
sortedList
assert ImmutablePriorityQueue.drain(
ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
) ==
List.reverse(sortedList)
80 changes: 80 additions & 0 deletions compiler/test/stdlib/priorityqueue.test.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import PriorityQueue from "priorityqueue"
import List from "list"
import Array from "array"

// formatter-ignore
let lotsOfVals = [>
3, 2, 1, 5, 3, 2, 2, 10, 6, 5,
6, 4, 9, 10, 10, 5, 9, 6, 1, 3,
3, 6, 6, 1, 3, 9, 6, 7, 4, 6,
5, 4, 4, 8, 6, 4, 1, 2, 3, 8,
8, 8, 10, 9, 6, 8, 4, 1, 1, 2,
10, 2, 8, 10, 8, 7, 4, 8, 10, 1,
3, 10, 4, 1, 6, 9, 6, 7, 8, 7,
10, 4, 5, 10, 3, 2, 4, 5, 9, 5,
3, 5, 10, 8, 7, 1, 6, 1, 4, 8,
3, 5, 7, 2, 4, 2, 4, 5, 10, 6,
7, 7, 7, 1, 10, 4, 9, 9, 10, 10,
2, 5, 2, 4, 3, 10, 9, 8, 3, 5,
7, 10, 9, 7, 8, 9, 3, 7, 7, 1,
7, 7, 6, 3, 3, 5, 1, 8, 9, 10,
7, 5, 9, 2, 9, 1, 6, 3, 10, 6,
10, 2, 6, 2, 9, 3, 3, 2, 4, 1,
9, 4, 9, 2, 9, 4, 8, 5, 6, 7,
4, 6, 5, 4, 3, 4, 2, 9, 10, 1,
10, 4, 4, 6, 3, 1, 9, 10, 2, 2,
5, 5, 3, 4, 5, 8, 3, 8, 4, 10
]
let numVals = Array.length(lotsOfVals)

let sortedVals = Array.copy(lotsOfVals)
Array.sort(compare, sortedVals)

let mut pq = PriorityQueue.make((a, b) => a - b)
let mut maxPq = PriorityQueue.make((a, b) => b - a)

assert PriorityQueue.size(pq) == 0
assert PriorityQueue.isEmpty(pq)
assert PriorityQueue.peek(pq) == None
assert PriorityQueue.drain(pq) == []

assert PriorityQueue.pop(pq) == None
assert PriorityQueue.size(pq) == 0

for (let mut i = 0; i < numVals; i += 1) {
PriorityQueue.push(lotsOfVals[i], pq)
PriorityQueue.push(lotsOfVals[i], maxPq)
assert PriorityQueue.size(pq) == i + 1
assert PriorityQueue.size(maxPq) == i + 1
}

assert PriorityQueue.size(pq) == numVals
assert PriorityQueue.size(maxPq) == numVals

for (let mut i = 0; i < numVals; i += 1) {
let val = PriorityQueue.peek(pq)
assert val == Some(sortedVals[i])
let maxVal = PriorityQueue.peek(maxPq)
assert maxVal == Some(sortedVals[numVals - i - 1])

let val = PriorityQueue.pop(pq)
assert val == Some(sortedVals[i])
let maxVal = PriorityQueue.pop(maxPq)
assert maxVal == Some(sortedVals[numVals - i - 1])

assert PriorityQueue.size(pq) == numVals - i - 1
assert PriorityQueue.size(maxPq) == numVals - i - 1
}

assert PriorityQueue.size(pq) == 0
assert PriorityQueue.peek(pq) == None

let sortedList = Array.toList(sortedVals)
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
) ==
sortedList
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
) ==
List.reverse(sortedList)
2 changes: 2 additions & 0 deletions compiler/test/suites/stdlib.re
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ describe("stdlib", ({test, testSkip}) => {
assertStdlib("set.test");
assertStdlib("regex.test");
assertStdlib("stack.test");
assertStdlib("priorityqueue.test");
assertStdlib("immutablepriorityqueue.test");
assertStdlib("string.test");
assertStdlib("sys.file.test");
assertStdlib(~code=5, "sys.process.test");
Expand Down
Loading

0 comments on commit 244be1b

Please sign in to comment.