MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / pop

Method pop

publication/code/chapter08/binary_heap.rs:91–105  ·  view source on GitHub ↗

弹出最小值

(&mut self)

Source from the content-addressed store, hash-verified

89
90 // 弹出最小值
91 fn pop(&mut self) -> Option<i32> {
92 if 0 == self.size { // 没数据,返回 None
93 None
94 } else if 1 == self.size {
95 self.size -= 1; // 一个数据,比较好处理
96 self.data.pop()
97 } else { // 多个数据,先交换并弹出数据,再调整堆
98 self.data.swap(1, self.size);
99 let val = self.data.pop();
100 self.size -= 1;
101 self.move_down(1);
102
103 val
104 }
105 }
106
107 // 小数据上冒 c(child), p(parent)
108 fn move_up(&mut self, mut c: usize) {

Callers 6

dequeueMethod · 0.45
dequeueMethod · 0.45
dequeueMethod · 0.45
build_newMethod · 0.45
dequeueMethod · 0.45
nextMethod · 0.45

Calls 1

move_downMethod · 0.45

Tested by

no test coverage detected