The OpenD Programming Language

heapify

Convenience function that returns a BinaryHeap!Store object initialized with s and initialSize.

BinaryHeap!(less, Store)
heapify
(
alias less = "a < b"
Store
)
(
Store s
,
size_t initialSize = size_t.max
)

Examples

import std.range.primitives;
{
    // example from "Introduction to Algorithms" Cormen et al., p 146
    int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
    auto h = heapify(a);
    h = heapify!"a < b"(a);
    assert(h.front == 16);
    assert(a == [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]);
    auto witness = [ 16, 14, 10, 9, 8, 7, 4, 3, 2, 1 ];
    for (; !h.empty; h.removeFront(), witness.popFront())
    {
        assert(!witness.empty);
        assert(witness.front == h.front);
    }
    assert(witness.empty);
}
{
    int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
    int[] b = new int[a.length];
    BinaryHeap!("a < b", int[]) h = BinaryHeap!("a < b", int[])(b, 0);
    foreach (e; a)
    {
        h.insert(e);
    }
    assert(b == [ 16, 14, 10, 8, 7, 3, 9, 1, 4, 2 ]);
}

Meta