Add PeekMut::pop
A fairly common workflow is to put a bunch of stuff into a binary heap and then mutate the top value until its empty. This both makes that a bit more convenient (no need to save a boolean off and pop after to avoid borrowck issues), and a bit more efficient since you only shift once.
This commit is contained in:
parent
6c9bb42ecc
commit
f2cb47adf9
3 changed files with 34 additions and 5 deletions
|
@ -9,7 +9,7 @@
|
|||
// except according to those terms.
|
||||
|
||||
use std::collections::BinaryHeap;
|
||||
use std::collections::binary_heap::Drain;
|
||||
use std::collections::binary_heap::{Drain, PeekMut};
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
|
@ -94,6 +94,19 @@ fn test_peek_mut() {
|
|||
assert_eq!(heap.peek(), Some(&9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_peek_mut_pop() {
|
||||
let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
|
||||
let mut heap = BinaryHeap::from(data);
|
||||
assert_eq!(heap.peek(), Some(&10));
|
||||
{
|
||||
let mut top = heap.peek_mut().unwrap();
|
||||
*top -= 2;
|
||||
assert_eq!(PeekMut::pop(top), 8);
|
||||
}
|
||||
assert_eq!(heap.peek(), Some(&9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push() {
|
||||
let mut heap = BinaryHeap::from(vec![2, 4, 9]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue