A heap is a special kind of binary tree in which every node is greater than all of its children

The implementation is based on an arrayed binary tree.

Example:

class Element implements polygonal.ds.Heapable<Element> {
    public var id:Int;
    public var position:Int;
    public function new(id:Int) {
        this.id = id;
    }
    public function compare(other:Element):Int {
        return other.id - id;
    }
    public function toString():String {
        return Std.string(id);
    }
}

...

var o = new polygonal.ds.Heap<Element>();
o.add(new Element(64));
o.add(new Element(13));
o.add(new Element(1));
o.add(new Element(37));
trace(o); //outputs:

[ Heap size=4
  front
  0 -> 1
  1 -> 13
  2 -> 37
  3 -> 64
]

Constructor

@:value({ initalCapacity : 1 })new (initalCapacity:Int = 1, ?source:Array<T>)

Parameters:

initialCapacity

the initial physical space for storing values. Useful before inserting a large number of elements as this reduces the amount of incremental reallocation.

source

copies all values from source in the range [0, source.length - 1] to this collection.

Variables

read onlycapacity:Int

The size of the allocated storage space for the elements. If more space is required to accommodate new elements, capacity grows according to this.growthRate. The capacity never falls below the initial size defined in the constructor and is usually a bit larger than this.size (mild overallocation).

@:value(GrowthRate.NORMAL)growthRate:Int = GrowthRate.NORMAL

The growth rate of the container.

See:

@:value(HashKey.next())read onlykey:Int = HashKey.next()

A unique identifier for this object.

A hash table transforms this key into an index of an array element by using a hash function.

@:value(false)reuseIterator:Bool = false

If true, reuses the iterator object instead of allocating a new one when calling this.iterator().

The default is false.

If this value is true, nested iterations will fail as only one iteration is allowed at a time.

read onlysize:Int

The total number of elements.

Methods

add (val:T):Heap<T>

Adds val.

bottom ():T

Returns the item on the bottom of the heap without removing it from the heap.

This is the largest element (assuming ascending order).

change (val:T, hint:Int):Heap<T>

Rebuilds the heap in case an existing element was modified.

This is faster than removing and re-adding an element.

Parameters:

hint

a value >= 0 indicates that val is now smaller (ascending order) or bigger (descending order) and should be moved towards the root of the tree to rebuild the heap property. Likewise, a value < 0 indicates that val is now bigger (ascending order) or smaller (descending order) and should be moved towards the leaf nodes of the tree.

@:value({ gc : false })clear (gc:Bool = false):Void

Removes all elements.

Parameters:

gc

if true, elements are nullified upon removal so the garbage collector can reclaim used memory.

@:value({ copier : null, byRef : true })clone (byRef:Bool = true, ?copier:T ‑> T):Collection<T>

Creates and returns a shallow copy (structure only - default) or deep copy (structure & elements) of this heap.

If byRef is true, primitive elements are copied by value whereas objects are copied by reference.

If byRef is false, the copier function is used for copying elements. If omitted, clone() is called on each element assuming all elements implement Cloneable.

inlinecontains (val:T):Bool

Returns true if this heap contains val.

free ():Void

Destroys this object by explicitly nullifying all elements for GC'ing used resources.

Improves GC efficiency/performance (optional).

height ():Int

Computes the height of the heap tree.

inlineisEmpty ():Bool

Returns true only if this.size is 0.

inlineiter (f:T ‑> Void):Heap<T>

Calls 'f` on all elements in unsorted order.

iterator ():Itr<T>

Returns a new HeapIterator object to iterate over all elements contained in this heap.

The values are visited in an unsorted order.

See:

pack ():Heap<T>

Reduces the capacity of the internal container to the initial capacity.

May cause a reallocation, but has no effect on this.size and its elements. An application can use this operation to free up memory by unlocking resources for the garbage collector.

pop ():T

Removes the element on top of the heap.

This is the smallest element (assuming ascending order).

remove (val:T):Bool

Removes val.

Returns:

true if val was removed.

repair ():Heap<T>

Uses the Floyd algorithm (bottom-up) to repair the heap tree by restoring the heap property.

replace (val:T):Heap<T>

Replaces the item at the top of the heap with val.

reserve (n:Int):Heap<T>

Preallocates storage for n elements.

May cause a reallocation, but has no effect on this.size and its elements. Useful before inserting a large number of elements as this reduces the amount of incremental reallocation.

sort ():Array<T>

Returns a sorted array of all elements.

toArray ():Array<T>

Returns an unordered array containing all elements in this heap.

toString ():String

Prints out all elements.

inlinetop ():T

Returns the item on top of the heap without removing it from the heap.

This is the smallest element (assuming ascending order).