Struct std::collections::BinaryHeapExperimental [-]  [+] [src]

pub struct BinaryHeap<T> {
    // some fields omitted
}

A priority queue implemented with a binary heap.

This will be a max-heap.

Methods

impl<T: Ord<T>> BinaryHeap<T>

fn new() -> BinaryHeap<T>

Creates an empty BinaryHeap as a max-heap.

Example

fn main() { use std::collections::BinaryHeap; let heap: BinaryHeap<uint> = BinaryHeap::new(); }
use std::collections::BinaryHeap;
let heap: BinaryHeap<uint> = BinaryHeap::new();

fn with_capacity(capacity: uint) -> BinaryHeap<T>

Creates an empty BinaryHeap with a specific capacity. This preallocates enough memory for capacity elements, so that the BinaryHeap does not have to be reallocated until it contains at least that many values.

Example

fn main() { use std::collections::BinaryHeap; let heap: BinaryHeap<uint> = BinaryHeap::with_capacity(10u); }
use std::collections::BinaryHeap;
let heap: BinaryHeap<uint> = BinaryHeap::with_capacity(10u);

fn from_vec(xs: Vec<T>) -> BinaryHeap<T>

Creates a BinaryHeap from a vector. This is sometimes called heapifying the vector.

Example

fn main() { use std::collections::BinaryHeap; let heap = BinaryHeap::from_vec(vec![9i, 1, 2, 7, 3, 2]); }
use std::collections::BinaryHeap;
let heap = BinaryHeap::from_vec(vec![9i, 1, 2, 7, 3, 2]);

fn iter(&'a self) -> Items<'a, T>

An iterator visiting all values in underlying vector, in arbitrary order.

Example

fn main() { use std::collections::BinaryHeap; let heap = BinaryHeap::from_vec(vec![1i, 2, 3, 4]); // Print 1, 2, 3, 4 in arbitrary order for x in heap.iter() { println!("{}", x); } }
use std::collections::BinaryHeap;
let heap = BinaryHeap::from_vec(vec![1i, 2, 3, 4]);

// Print 1, 2, 3, 4 in arbitrary order
for x in heap.iter() {
    println!("{}", x);
}

fn into_iter(self) -> MoveItems<T>

Creates a consuming iterator, that is, one that moves each value out of the binary heap in arbitrary order. The binary heap cannot be used after calling this.

Example

fn main() { use std::collections::BinaryHeap; let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4]); // Print 1, 2, 3, 4 in arbitrary order for x in pq.into_iter() { // x has type int, not &int println!("{}", x); } }
use std::collections::BinaryHeap;
let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4]);

// Print 1, 2, 3, 4 in arbitrary order
for x in pq.into_iter() {
    // x has type int, not &int
    println!("{}", x);
}

fn top(&'a self) -> Option<&'a T>

Returns the greatest item in a queue, or None if it is empty.

Example

fn main() { use std::collections::BinaryHeap; let mut heap = BinaryHeap::new(); assert_eq!(heap.top(), None); heap.push(1i); heap.push(5i); heap.push(2i); assert_eq!(heap.top(), Some(&5i)); }
use std::collections::BinaryHeap;

let mut heap = BinaryHeap::new();
assert_eq!(heap.top(), None);

heap.push(1i);
heap.push(5i);
heap.push(2i);
assert_eq!(heap.top(), Some(&5i));

fn capacity(&self) -> uint

Returns the number of elements the queue can hold without reallocating.

Example

fn main() { use std::collections::BinaryHeap; let heap: BinaryHeap<uint> = BinaryHeap::with_capacity(100u); assert!(heap.capacity() >= 100u); }
use std::collections::BinaryHeap;

let heap: BinaryHeap<uint> = BinaryHeap::with_capacity(100u);
assert!(heap.capacity() >= 100u);

fn reserve_exact(&mut self, additional: uint)

Reserves the minimum capacity for exactly additional more elements to be inserted in the given BinaryHeap. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Panics

Panics if the new capacity overflows uint.

Example

fn main() { use std::collections::BinaryHeap; let mut heap: BinaryHeap<uint> = BinaryHeap::new(); heap.reserve_exact(100u); assert!(heap.capacity() >= 100u); }
use std::collections::BinaryHeap;

let mut heap: BinaryHeap<uint> = BinaryHeap::new();
heap.reserve_exact(100u);
assert!(heap.capacity() >= 100u);

fn reserve(&mut self, additional: uint)

Reserves capacity for at least additional more elements to be inserted in the BinaryHeap. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new capacity overflows uint.

Example

fn main() { use std::collections::BinaryHeap; let mut heap: BinaryHeap<uint> = BinaryHeap::new(); heap.reserve(100u); assert!(heap.capacity() >= 100u); }
use std::collections::BinaryHeap;

let mut heap: BinaryHeap<uint> = BinaryHeap::new();
heap.reserve(100u);
assert!(heap.capacity() >= 100u);

fn shrink_to_fit(&mut self)

Discards as much additional capacity as possible.

fn pop(&mut self) -> Option<T>

Removes the greatest item from a queue and returns it, or None if it is empty.

Example

fn main() { use std::collections::BinaryHeap; let mut heap = BinaryHeap::from_vec(vec![1i, 3]); assert_eq!(heap.pop(), Some(3i)); assert_eq!(heap.pop(), Some(1i)); assert_eq!(heap.pop(), None); }
use std::collections::BinaryHeap;

let mut heap = BinaryHeap::from_vec(vec![1i, 3]);

assert_eq!(heap.pop(), Some(3i));
assert_eq!(heap.pop(), Some(1i));
assert_eq!(heap.pop(), None);

fn push(&mut self, item: T)

Pushes an item onto the queue.

Example

fn main() { use std::collections::BinaryHeap; let mut heap = BinaryHeap::new(); heap.push(3i); heap.push(5i); heap.push(1i); assert_eq!(heap.len(), 3); assert_eq!(heap.top(), Some(&5i)); }
use std::collections::BinaryHeap;

let mut heap = BinaryHeap::new();
heap.push(3i);
heap.push(5i);
heap.push(1i);

assert_eq!(heap.len(), 3);
assert_eq!(heap.top(), Some(&5i));

fn push_pop(&mut self, item: T) -> T

Pushes an item onto a queue then pops the greatest item off the queue in an optimized fashion.

Example

fn main() { use std::collections::BinaryHeap; let mut heap = BinaryHeap::new(); heap.push(1i); heap.push(5i); assert_eq!(heap.push_pop(3i), 5); assert_eq!(heap.push_pop(9i), 9); assert_eq!(heap.len(), 2); assert_eq!(heap.top(), Some(&3i)); }
use std::collections::BinaryHeap;

let mut heap = BinaryHeap::new();
heap.push(1i);
heap.push(5i);

assert_eq!(heap.push_pop(3i), 5);
assert_eq!(heap.push_pop(9i), 9);
assert_eq!(heap.len(), 2);
assert_eq!(heap.top(), Some(&3i));

fn replace(&mut self, item: T) -> Option<T>

Pops the greatest item off a queue then pushes an item onto the queue in an optimized fashion. The push is done regardless of whether the queue was empty.

Example

fn main() { use std::collections::BinaryHeap; let mut heap = BinaryHeap::new(); assert_eq!(heap.replace(1i), None); assert_eq!(heap.replace(3i), Some(1i)); assert_eq!(heap.len(), 1); assert_eq!(heap.top(), Some(&3i)); }
use std::collections::BinaryHeap;

let mut heap = BinaryHeap::new();

assert_eq!(heap.replace(1i), None);
assert_eq!(heap.replace(3i), Some(1i));
assert_eq!(heap.len(), 1);
assert_eq!(heap.top(), Some(&3i));

fn into_vec(self) -> Vec<T>

Consumes the BinaryHeap and returns the underlying vector in arbitrary order.

Example

fn main() { use std::collections::BinaryHeap; let heap = BinaryHeap::from_vec(vec![1i, 2, 3, 4, 5, 6, 7]); let vec = heap.into_vec(); // Will print in some order for x in vec.iter() { println!("{}", x); } }
use std::collections::BinaryHeap;

let heap = BinaryHeap::from_vec(vec![1i, 2, 3, 4, 5, 6, 7]);
let vec = heap.into_vec();

// Will print in some order
for x in vec.iter() {
    println!("{}", x);
}

fn into_sorted_vec(self) -> Vec<T>

Consumes the BinaryHeap and returns a vector in sorted (ascending) order.

Example

fn main() { use std::collections::BinaryHeap; let mut heap = BinaryHeap::from_vec(vec![1i, 2, 4, 5, 7]); heap.push(6); heap.push(3); let vec = heap.into_sorted_vec(); assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]); }
use std::collections::BinaryHeap;

let mut heap = BinaryHeap::from_vec(vec![1i, 2, 4, 5, 7]);
heap.push(6);
heap.push(3);

let vec = heap.into_sorted_vec();
assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]);

fn len(&self) -> uint

Returns the length of the queue.

fn is_empty(&self) -> bool

Returns true if the queue contains no elements

fn clear(&mut self)

Drops all items from the queue.

Trait Implementations

impl<T: Ord<T>> Default for BinaryHeap<T>

fn default() -> BinaryHeap<T>

impl<T: Ord<T>> FromIterator<T> for BinaryHeap<T>

fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BinaryHeap<T>

impl<T: Ord<T>> Extend<T> for BinaryHeap<T>

fn extend<Iter: Iterator<T>>(&mut self, iter: Iter)

Derived Implementations

impl<T: Clone> Clone for BinaryHeap<T>

fn clone(&self) -> BinaryHeap<T>

fn clone_from(&mut self, &BinaryHeap<T>)