| 206 | } |
| 207 | |
| 208 | pub fn decrease_key(&mut self, i: usize, key: T) -> Result<(), &'static str> { |
| 209 | if !self.contains(i) { |
| 210 | Err("index is not in the priority queue") |
| 211 | } else { |
| 212 | match self.keys[i].partial_cmp(&Some(key)) { |
| 213 | None => Err("Calling decreaseKey() with a key that comparison is impossible"), |
| 214 | Some(Ordering::Equal) => Err("Calling decreaseKey() with a key equal to the key in the priority queue"), |
| 215 | Some(Ordering::Less) => Err("Calling decreaseKey() with a key strictly greater than the key in the priority queue"), |
| 216 | Some(Ordering::Greater) => { |
| 217 | self.keys[i] = Some(key); |
| 218 | self.swim(self.qp[i] as usize); |
| 219 | Ok(()) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | fn swim(&mut self, mut k: usize) { |
| 226 | while k > 1 && self.compare(k / 2, k) { |