Skip to content

Commit

Permalink
Check that PeekMaxMut has a parent before reading it
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Apr 22, 2021
1 parent 9aba91a commit 0812b36
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ impl<'a, T: Ord> Drop for PeekMaxMut<'a, T> {
if !self.removed {
let mut hole = Hole::new(&mut self.heap.0, self.max_index);

if hole.element() < hole.get_parent() {
if hole.has_parent() && hole.element() < hole.get_parent() {
hole.swap_with_parent();
}

Expand Down Expand Up @@ -941,6 +941,10 @@ mod tests {
assert_eq!(1, h.peek_max_mut().unwrap().pop());
assert_eq!(Some(&0), h.peek_min());
assert_eq!(Some(&0), h.peek_max());

*h.peek_max_mut().unwrap() = 1;
assert_eq!(Some(&1), h.peek_min());
assert_eq!(Some(&1), h.peek_max());
}

#[test]
Expand Down

0 comments on commit 0812b36

Please sign in to comment.