Skip to content

Commit

Permalink
fix merges of pairing heap
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh authored and bobzhang committed Mar 7, 2025
1 parent 05735f0 commit 0d24ea2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions immut/priority_queue/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"moonbitlang/core/immut/list",
"moonbitlang/core/quickcheck"
],
"test-import": ["moonbitlang/core/random"],
"targets": {
"panic_test.mbt": ["not", "native"]
}
Expand Down
3 changes: 2 additions & 1 deletion immut/priority_queue/priority_queue.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ fn meld[A : Compare](l : Cont[A], r : Cont[A]) -> Cont[A] {
fn merges[A : Compare](x : @list.T[Cont[A]]) -> Cont[A] {
loop x, Empty {
Nil, acc => acc
Cons(t, rest), acc => continue rest, meld(t, acc)
Cons(t, Nil), acc => meld(t, acc)
Cons(t1, Cons(t2, rest)), acc => continue rest, meld(meld(t1, t2), acc)
}
}

Expand Down
22 changes: 22 additions & 0 deletions immut/priority_queue/priority_queue_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,25 @@ test "equal" {
let pq5 = @priority_queue.of([1, 2, 3])
inspect!(pq1 == pq5, content="false")
}

///|
test "complex" {
fn rand(upper : Int) -> Int {
let rng = @random.new()
rng.int(limit=upper)
}

let arr = (0).until(1000, inclusive=true).collect()
arr.shuffle_in_place(rand~)
let mut pq = new()
for i in 0..=1000 {
pq = pq.push(arr[i])
}
assert_eq!(pq.length(), 1001)
for i in 0..=1000 {
assert_eq!(pq.peek(), Some(1000 - i))
pq = pq.pop().unwrap()
}
inspect!(pq.iter(), content="[]")
assert_eq!(pq.length(), 0)
}

0 comments on commit 0d24ea2

Please sign in to comment.