diff options
| author | gingerBill <bill@gingerbill.org> | 2022-01-01 13:44:37 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2022-01-01 13:44:37 +0000 |
| commit | 43763ddfda018925ef55fc0f22f2e9ab363a848f (patch) | |
| tree | 3edc373684ca90a17cd86780b4fc99937d103a47 /core/container/priority_queue | |
| parent | 70ed280c5ac47bd96d07878044867ec72ab94391 (diff) | |
Correct `_shift_down` logic
Diffstat (limited to 'core/container/priority_queue')
| -rw-r--r-- | core/container/priority_queue/priority_queue.odin | 15 |
1 files changed, 7 insertions, 8 deletions
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 |