Skip to content

Commit

Permalink
refactor interval_heap
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtkana committed Apr 4, 2024
1 parent cbec663 commit d9c1553
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions libs/interval_heap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,24 @@ fn max_heapify_up<T: Ord>(values: &mut [T], mut end: usize) {
}

fn min_heapify_down<T: Ord>(values: &mut [T], mut start: usize) {
let n = values.len();
loop {
let end = start + 1;
if end >= values.len() {
if end >= n {
break;
}
if values[start] > values[end] {
values.swap(start, end);
}
let left = 2 * start + 2;
if left >= values.len() {
break;
let mut swp = 2 * start + 4;
if swp >= n || values[swp - 2] < values[swp] {
swp -= 2;
}
let right = 2 * start + 4;
let next = if right >= values.len() || values[left] < values[right] { left } else { right };
if values[start] <= values[next] {
if swp >= n || values[start] <= values[swp] {
break;
}
values.swap(start, next);
start = next;
values.swap(start, swp);
start = swp;
}
}

Expand All @@ -144,17 +143,15 @@ fn max_heapify_down<T: Ord>(values: &mut [T], mut end: usize) {
if values[start] > values[end] {
values.swap(start, end);
}
let left = 2 * end + 1;
if left >= n {
break;
let mut swp = 2 * end + 3;
if swp >= n || values[swp - 2] > values[swp] {
swp -= 2;
}
let right = 2 * end + 3;
let next = if right >= n || values[left] > values[right] { left } else { right };
if values[end] >= values[next] {
if swp >= n || values[end] >= values[swp] {
break;
}
values.swap(end, next);
end = next;
values.swap(end, swp);
end = swp;
}
}

Expand Down

0 comments on commit d9c1553

Please sign in to comment.