MCPcopy Create free account
hub / github.com/douchuan/algorithm / decrease_key

Method decrease_key

src/common/priority_queue.rs:208–223  ·  view source on GitHub ↗
(&mut self, i: usize, key: T)

Source from the content-addressed store, hash-verified

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) {

Callers 2

scanMethod · 0.80
relaxMethod · 0.80

Calls 4

ErrEnum · 0.85
swimMethod · 0.80
containsMethod · 0.45
partial_cmpMethod · 0.45

Tested by

no test coverage detected