Generic binary heap
OperationsCode example:
heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(3));
heap->Insert(IntRef->New(1));
heap->Insert(IntRef->New(9));
heap->Insert(IntRef->New(8));
heap->Insert(IntRef->New(6));
heap->Insert(IntRef->New(2));
while(<>heap->IsEmpty()) {
heap->Pop()->PrintLine();
};
Constructor
New(array:H[], order:Heap->Order)
Name | Type | Description |
---|---|---|
array | H | array of values to insert |
order | Heap->Order | sort order |
Constructor
New(order:Heap->Order)
Name | Type | Description |
---|---|---|
order | Heap->Order | sort order |
Returns the heap capacity
method : public : Capacity() ~ Int
Type | Description |
---|---|
Int | heap capacity |
Inserts a value into the heap and extends the heap size if necessary
method : public : Insert(value:H) ~ Nil
Name | Type | Description |
---|---|---|
value | H | value to insert |
Checks to see if the heap is empty
method : public : IsEmpty() ~ Bool
Type | Description |
---|---|
Bool | true if empty, false otherwise |
Pop either the smallest or largest value depending upon the sort order
method : public : Pop() ~ H
Type | Description |
---|---|
H | smallest or largest value |
Searches and pops the given value
method : public : Pop(value:H) ~ Bool
Name | Type | Description |
---|---|---|
value | H | value to remove |
Type | Description |
---|---|
Bool | true if successful, false otherwise |
Size of heap, including Nil values
method : public : Size() ~ Int
Type | Description |
---|---|
Int | size of heap |
Returns a list of non-Nil values
method : public : ToArray() ~ H[]
Type | Description |
---|---|
H | list of values |
Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.
method : public : ToString() ~ String
Type | Description |
---|---|
String | string representation |
Check the top of the stack
method : public : Top() ~ H
Type | Description |
---|---|
H | value on the top of stack, Nil if stack is empty |