heap |
module HeapMixin |
Public Methods |
Find item with the smallest key, in O(1) time.
Returns: an array containing the key and value of the smallest item, or nil if the heap is empty.
Find item with the smallest key, in O(1) time.
Returns: the value of the smallest item, or nil if the heap is empty.
Find the item with the largest key, in O(n) time.
Returns: an array containing the key and value of the largest item, or nil if the heap is empty.
Find the item with the largest key, in O(n) time.
Returns: an array containing the key and value of the largest item, or nil if the heap is empty.
class Heap < Object |
TODO: This is a binary heap; should we name it that way?
Public Methods |
Create a new Heap
capacity | the initial size of the heap. |
---|---|
impl_class | usually array, but can be any class that is compatible with Array. |
Determine whether a Heap is empty.
Public Methods |
Add a new item to the heap.
key | the priority of the item to add (lower values have higher priority). |
---|---|
value | the item to add. |
Returns: The value of the item that was added.
[]= is an alias for store.
Given a key, find a value, in O(n) time.
key | the key to search for. |
---|
Returns: the first value found with the given keym or @default if no such value is found.
Remove the smallest item from the heap, in O(log n) time.
Returns: an array containing the key and value of item that was deleted.
Remove the largest item from the heap, in O(n) time.
Returns: an array containing the key and value of the item that was deleted, or nil if the heap is empty.
Clear a heap. Has the side-effect of changing the capacity to 0.
Returns: self.
Iterate through all the elements of the heap, in order. This method allocates a new heap to do the iteration, so it may not be very fast.
Returns: the value from the last call to yield.
each_pair is an alias for each.
length is an alias for size.
Mixins |
Enumerable | |
---|---|
HeapMixin | |
EnumerableContainer | |
PairEnumerableContainer | |
IndexableContainer |
Protected Methods |
Percolate the item at position i up to its appropriate position.
Percolate the item at position i down to its approriate position
Constants |
NIL |
---|