From 43763ddfda018925ef55fc0f22f2e9ab363a848f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 1 Jan 2022 13:44:37 +0000 Subject: Correct `_shift_down` logic --- core/container/priority_queue/priority_queue.odin | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'core/container/priority_queue') diff --git a/core/container/priority_queue/priority_queue.odin b/core/container/priority_queue/priority_queue.odin index 2bdb3c777..e324287f3 100644 --- a/core/container/priority_queue/priority_queue.odin +++ b/core/container/priority_queue/priority_queue.odin @@ -56,24 +56,23 @@ cap :: proc(pq: $Q/Priority_Queue($T)) -> int { _shift_down :: proc(pq: ^$Q/Priority_Queue($T), i0, n: int) -> bool { // O(n log n) - i := i0 - j, j1, j2: int - if 0 > i || i > n { + if 0 > i0 || i0 > n { return false } + i := i0 queue := pq.queue[:] for { j1 := 2*i + 1 - if 0 > j1 || j1 >= n { + if j1 < 0 || j1 >= n { break } - j, j2 = j1, j1+1 - if j1 < n && pq.less(queue[j2], queue[j1]) { + j := j1 + if j2 := j1+1; j2 < n && pq.less(queue[j2], queue[j1]) { j = j2 } - if !pq.less(queue[i], queue[j]) { + if !pq.less(queue[j], queue[i]) { break } @@ -87,7 +86,7 @@ _shift_up :: proc(pq: ^$Q/Priority_Queue($T), j: int) { j := j queue := pq.queue[:] n := builtin.len(queue) - for 0 <= j && j < n { + for 0 <= j { i := (j-1)/2 if i == j || !pq.less(queue[j], queue[i]) { break -- cgit v1.2.3