X-Git-Url: https://i11git.iti.kit.edu/anon-gitweb/?p=Mitarbeiter%2FTim-Zeitz%2Fstud-rust-base.git;a=blobdiff_plain;f=src%2Findex_heap.rs;h=2c6f61397984a7719e9d3156440d3ea28adbee7a;hp=477dc879cf610d95dd8c2658bde1a3227a257cf1;hb=HEAD;hpb=b98ead7d67d4779f4e70b47bdc08a72eb666cf9f diff --git a/src/index_heap.rs b/src/index_heap.rs index 477dc87..2c6f613 100644 --- a/src/index_heap.rs +++ b/src/index_heap.rs @@ -123,19 +123,19 @@ impl IndexdMinHeap { } /// Updates the key of an element if the new key is smaller than the old key. - /// Does nothing if the new key is larger. - /// Panics if the element is not part of the queue. + /// Panics if the element is not part of the queue or if the new key is larger. pub fn decrease_key(&mut self, element: T) { let position = self.positions[element.as_index()]; + assert!(element <= self.data[position]); self.data[position] = element; self.move_up_in_tree(position); } /// Updates the key of an element if the new key is larger than the old key. - /// Does nothing if the new key is smaller. - /// Panics if the element is not part of the queue. + /// Panics if the element is not part of the queue or if the new key is smaller. pub fn increase_key(&mut self, element: T) { let position = self.positions[element.as_index()]; + assert!(element >= self.data[position]); self.data[position] = element; self.move_down_in_tree(position); } @@ -168,10 +168,7 @@ impl IndexdMinHeap { let mut hole = Hole::new(&mut self.data, position); loop { - if let Some(smallest_child) = - IndexdMinHeap::::children_index_range(position, heap_size) - .min_by_key(|&child_index| hole.get(child_index)) - { + if let Some(smallest_child) = IndexdMinHeap::::children_index_range(position, heap_size).min_by_key(|&child_index| hole.get(child_index)) { if hole.get(smallest_child) >= hole.element() { self.positions[hole.element().as_index()] = position; return; // no child is smaller @@ -218,11 +215,7 @@ impl<'a, T> Hole<'a, T> { unsafe fn new(data: &'a mut [T], pos: usize) -> Self { debug_assert!(pos < data.len()); let elt = ptr::read(&data[pos]); - Hole { - data, - elt: Some(elt), - pos, - } + Hole { data, elt: Some(elt), pos } } /// Returns a reference to the element removed.