Skip to content

Commit

Permalink
Check index rather than item for short-circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
ejmount committed Oct 23, 2022
1 parent b195cbc commit ccdadef
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/k_smallest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ where
}

let (left_idx, right_idx) = children_of(origin);
let (left_item, right_item) = (self.get(left_idx), self.get(right_idx));

if left_item.is_none() { // the left is the earlier child, so if it doesn't exist there's nothing to swap with
if left_idx >= self.len {
// the left is the earlier child, so if it doesn't exist there's nothing to swap with
return;
}

let (left_item, right_item) = (self.get(left_idx), self.get(right_idx));


let cmp = self
.compare(left_item, right_item)
.unwrap_or(Ordering::Greater); // The right item may not exist, so default to picking the left, i.e. lower
Expand Down

0 comments on commit ccdadef

Please sign in to comment.