binary_heap
import std::collections::binary_heap; · source
Types
BinaryHeap<T, A>
type struct BinaryHeap<T, A = GlobalAllocator> where A: Allocator {
private elements: Array<T, A>;
}
A priority queue: pop hands out the greatest element first, by Ord. The elements are kept in an Array laid out as a max-heap — the element at index i is no less than those at 2i + 1 and 2i + 2 — so push and pop are O(log n) and peek is O(1).
import std::collections::binary_heap::{ BinaryHeap };
mut heap: BinaryHeap<i64> = BinaryHeap::new();
heap.push(3);
heap.push(9);
heap.push(4);
const largest: Option<i64> = heap.pop(); // Some(9)
from_array arranges an existing array into a heap in O(n), and into_sorted uses the heap up to hand back its elements in ascending order. For the smallest element first, order the elements the other way round with a wrapper whose Ord is reversed.
Constructors
static new() -> BinaryHeap<T, GlobalAllocator>;
static new() -> BinaryHeap<T, GlobalAllocator>;An empty heap backed by GlobalAllocator.
static new_in(alloc: A) -> BinaryHeap<T, A>;
static new_in(alloc: A) -> BinaryHeap<T, A>;An empty heap that allocates through alloc.
static from_array(elements: Array<T, A>) -> BinaryHeap<T, A> where T: Ord;
static from_array(elements: Array<T, A>) -> BinaryHeap<T, A> where T: Ord;A heap holding the elements of elements, arranged in O(n).
Methods
length(&this) -> i64;
length(&this) -> i64;The number of elements in the heap.
is_empty(&this) -> boolean;
is_empty(&this) -> boolean;True when the heap has no elements.
peek(&this) -> Option<T*>;
peek(&this) -> Option<T*>;The greatest element, or None when empty.
push(mut &this, value: T) -> void where T: Ord;
push(mut &this, value: T) -> void where T: Ord;Add value. Panics on allocation failure.
pop(mut &this) -> Option<T> where T: Ord;
pop(mut &this) -> Option<T> where T: Ord;Remove and return the greatest element, or None when empty.
into_sorted(mut this) -> Array<T, A> where T: Ord;
into_sorted(mut this) -> Array<T, A> where T: Ord;The elements in ascending order. The heap is used up.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct BinaryHeap<T, A = GlobalAllocator> where A: Allocator {}A priority queue: pop hands out the greatest element first, by Ord. Pushing and popping take time logarithmic in the size.
type parameter Ttype parameter Awhere A: Allocator
type struct GlobalAllocator {}Zero-sized handle to the process-wide allocator. Construct with GlobalAllocator::new(); all instances dispatch through the same underlying backend.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocationError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocationError>;
}A source of heap memory: hands out blocks described by a Layout and takes them back. Collections, Box and String take an A: Allocator type parameter, so the storage strategy is the caller's choice; GlobalAllocator is the default.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
BinaryHeap.elements: Array<T, A>type struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
type parameter Timport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::collections::binary_heap;BinaryHeap: a priority queue that always hands out its greatest element.
module pagetype struct BinaryHeap<T, A = GlobalAllocator> where A: Allocator {}A priority queue: pop hands out the greatest element first, by Ord. Pushing and popping take time logarithmic in the size.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut heap: BinaryHeap<i64>static BinaryHeap::new() -> BinaryHeap<T, GlobalAllocator>An empty heap backed by GlobalAllocator.
mut heap: BinaryHeap<i64>BinaryHeap::push(mut &this, value: T) -> void
where T: OrdAdd value. Panics on allocation failure.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const largest: Option<i64>type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
BinaryHeap::pop(mut &this) -> Option<T>
where T: OrdRemove and return the greatest element, or None when empty.
type trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
BinaryHeap::peek(&this) -> Option<T*>The greatest element, or None when empty.
static BinaryHeap::from_array(elements: Array<T, A>) -> BinaryHeap<T, A>
where T: OrdA heap holding the elements of elements, arranged in O(n).
BinaryHeap::into_sorted(mut this) -> Array<T, A>
where T: OrdThe elements in ascending order. The heap is used up.
std::collections::binary_heapstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static BinaryHeap::new_in(alloc: A) -> BinaryHeap<T, A>An empty heap that allocates through alloc.
(parameter) alloc: A(parameter) elements: Array<T, A>&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
BinaryHeap::is_empty(&this) -> booleanTrue when the heap has no elements.
std::collections::binary_heapOption::Nonemut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
(parameter) value: Tmut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language reference