From: Tim "tim3z" Zeitz Date: Thu, 16 Dec 2021 09:46:28 +0000 (+0100) Subject: make heap api more robust X-Git-Url: https://i11git.iti.kit.edu/anon-gitweb/?p=Mitarbeiter%2FTim-Zeitz%2Fstud-rust-base.git;a=commitdiff_plain;h=06c34a2e9b80e195c51932f62a99cce4f0110000 make heap api more robust --- diff --git a/src/index_heap.rs b/src/index_heap.rs index 477dc87..5850ae8 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); }