Struct bheap::BinaryMaxHeap[][src]

pub struct BinaryMaxHeap<T> where
    T: Ord + Uid
{ buffer: Vec<T>, index: HashMap<u64, usize>, }
Expand description

A re-prioritizable binary max heap containing a buffer for storing elements and a hashmap index for keeping track of element positions.

Fields

buffer: Vec<T>

in-memory storage for elements

index: HashMap<u64, usize>

mapping from element uids to positions in the heap buffer

Implementations

Creates a new BinaryMaxHeap from a given vector, which may or may not be empty. If the vector already contains elements, the elements are re-arranged with a build_heap() operation.

Creates an empty binary max heap with no elements.

Swaps elements at the given indices byt first swapping the elements in the buffer vector and next updating the HashMap index with new indices.

Convenience method for comparing elements at the given indices.

Restores heap property by moving the element in the given index upwards along it’s parents to the root, until it has no parents or it is <= to its parents. It operates in the following manner:

heapify_up(heap, i) {
    while i > 0 {
        let parent = (i - 1) / 2;
        if heap[i] > heap[parent] {
            swap(heap, i, parent)
        } else { break; }
    }
}

Restores heap property by moving the element at the given index, downwards along it’s children, towards the leaves, until it has no children or it is >= to its children. It operates in the following manner:

heapify_dn(heap, i) {
    while i < len(heap) / 2 {
        let max = i;
        let lc, rc = 2 * i + 1, 2 * i + 2;

        if lc < len(heap) && heap[max] < lc { max = lc; }
        if rc < len(heap) && heap[max] < rc { max = rc; }

        if i != max { swap(heap, i, max); i = max; }
        else { break; }
    }
}

Corrects the HashMap index for the given heap position, by updating the entry with the uid of the element at that position. The values stored is the index. Concisely:

index[buffer[i].uid()] = i

Returns a mutable reference to the element at the givven heap position, if present. This implementation assumes that no mutation used with respect to the returned mutable reference, modifies the uid() property for the element.

Pushes a new element in this priority queue.

Peeks at the element with highest priority, if present.

Pops the element with the highest property, if present.

Builds the HashMap index from uids to buffer positions.

Builds a heap from un-organized elements in the in-memory buffer. It operates as follows:

build_heap(heap) {
    build_index(heap);

    for i = len(heap) / 2; i >= 0; i-- {
        heapify_dn(heap, i);
    }
}

Restores heap property at the given position.

Returns the position for element with given uid in the heap buffer.

Returns the position for element in the heap buffer.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.