Module binaryheap

Binary heap implementation

A binary heap (or binary tree) is a sorting algorithm.

The 'plain binary heap' is managed by positions. Which are hard to get once an element is inserted. It can be anywhere in the list because it is re-sorted upon insertion/deletion of items. The array with values is stored in field values:

peek = heap.values[1]

A 'unique binary heap' is where the payload is unique and the payload itself also stored (as key) in the heap with the position as value, as in;

heap.reverse[payload] = [pos]

Due to this setup the reverse search, based on payload, is now a much faster operation because instead of traversing the list/heap, you can do;

pos = heap.reverse[payload]

This means that deleting elements from a 'unique binary heap' is faster than from a plain heap.

All management functions in the 'unique binary heap' take payload instead of pos as argument. Note that the value of the payload must be unique!

Fields of heap object:

  • values - array of values
  • payloads - array of payloads (unique binary heap only)
  • reverse - map from payloads to indices (unique binary heap only)

Basic heap

binaryHeap (swap, erase, lt) Creates a new binary heap.

Plain heap

heap:insert (value) Inserts an element in the heap.
heap:peek () Returns the element at the top of the heap, without removing it.
heap:pop () Removes the top of the heap and returns it.
heap:remove (pos) Removes an element from the heap.
heap:size () Returns the number of elements in the heap.
heap:update (pos, newValue) Updates the value of an element in the heap.
maxHeap (gt) Creates a new max-heap, where the largest value is at the top.
minHeap (lt) Creates a new min-heap, where the smallest value is at the top.

Unique heap

heap:size () Returns the number of elements in the heap.
maxUnique (gt) Creates a new max-heap with unique payloads.
minUnique (lt) Creates a new min-heap with unique payloads.
unique:insert (value, payload) Inserts an element in the heap.
unique:peek () Returns the element at the top of the heap, without removing it.
unique:peekValue () Returns the element at the top of the heap, without removing it.
unique:pop () Removes the top of the heap and returns it.
unique:remove (payload) Removes an element from the heap.
unique:update (payload, newValue) Updates the value of an element in the heap.
unique:valueByPayload (payload) Returns the value associated with the payload


Basic heap

This is the base implementation of the heap. Under regular circumstances this should not be used, instead use a Plain heap or Unique heap.
binaryHeap (swap, erase, lt)
Creates a new binary heap. This is the core of all heaps, the others are built upon these sorting functions.

Parameters:

  • swap (function) swap(heap, idx1, idx2) swaps values at idx1 and idx2 in the heaps heap.values and heap.payloads lists (see return value below).
  • erase (function) swap(heap, position) raw removal
  • lt (function) in lt(a, b) returns true when a < b (for a min-heap)

Returns:

    table with two methods; heap:bubbleUp(pos) and heap:sinkDown(pos) that implement the sorting algorithm and two fields; heap.values and heap.payloads being lists, holding the values and payloads respectively.

Plain heap

A plain heap carries a single piece of information per entry. This can be any type (except nil), as long as the comparison function used to create the heap can handle it.
heap:insert (value)
Inserts an element in the heap.

Parameters:

  • value the value used for sorting this element

Returns:

    nothing, or throws an error on bad input
heap:peek ()
Returns the element at the top of the heap, without removing it.

Returns:

    value at the top, or nil if there is none
heap:pop ()
Removes the top of the heap and returns it.

Returns:

    value at the top, or nil if there is none
heap:remove (pos)
Removes an element from the heap.

Parameters:

  • pos the position to remove

Returns:

    value, or nil if a bad pos value was provided
heap:size ()
Returns the number of elements in the heap.

Returns:

    number of elements
heap:update (pos, newValue)
Updates the value of an element in the heap.

Parameters:

  • pos the position which value to update
  • newValue the new value to use for this payload
maxHeap (gt)
Creates a new max-heap, where the largest value is at the top.

Parameters:

  • gt (optional) comparison function (greater-than), see binaryHeap.

Returns:

    the new heap
minHeap (lt)
Creates a new min-heap, where the smallest value is at the top.

Parameters:

  • lt (optional) comparison function (less-than), see binaryHeap.

Returns:

    the new heap

Unique heap

A unique heap carries 2 pieces of information per entry.

  1. The value, this is used for ordering the heap. It can be any type (except nil), as long as the comparison function used to create the heap can handle it.
  2. The payload, this can be any type (except nil), but it MUST be unique.

With the 'unique heap' it is easier to remove elements from the heap.

heap:size ()
Returns the number of elements in the heap.

Returns:

    number of elements
maxUnique (gt)
Creates a new max-heap with unique payloads. A max-heap is where the largest value is at the top.

NOTE: All management functions in the 'unique binary heap' take payload instead of pos as argument.

Parameters:

  • gt (optional) comparison function (greater-than), see binaryHeap.

Returns:

    the new heap
minUnique (lt)
Creates a new min-heap with unique payloads. A min-heap is where the smallest value is at the top.

NOTE: All management functions in the 'unique binary heap' take payload instead of pos as argument.

Parameters:

  • lt (optional) comparison function (less-than), see binaryHeap.

Returns:

    the new heap
unique:insert (value, payload)
Inserts an element in the heap.

Parameters:

  • value the value used for sorting this element
  • payload the payload attached to this element

Returns:

    nothing, or throws an error on bad input
unique:peek ()
Returns the element at the top of the heap, without removing it.

Returns:

    payload, value, or nil if there is none
unique:peekValue ()
Returns the element at the top of the heap, without removing it.

Returns:

    value at the top, or nil if there is none

Usage:

    -- simple timer based heap example
    while true do
      sleep(heap:peekValue() - gettime())  -- assume LuaSocket gettime function
      coroutine.resume((heap:pop()))       -- assumes payload to be a coroutine,
                                           -- double parens to drop extra return value
    end
unique:pop ()
Removes the top of the heap and returns it. When used with timers, pop will return the payload that is due.

Note: this function returns payload as the first result to prevent extra locals when retrieving the payload.

Returns:

    payload, value, or nil if there is none
unique:remove (payload)
Removes an element from the heap.

Parameters:

  • payload the payload to remove

Returns:

    value, payload or nil if not found
unique:update (payload, newValue)
Updates the value of an element in the heap.

Parameters:

  • payload the payoad whose value to update
  • newValue the new value to use for this payload

Returns:

    nothing, or throws an error on bad input
unique:valueByPayload (payload)
Returns the value associated with the payload

Parameters:

  • payload the payload to lookup

Returns:

    value or nil if no such payload exists
generated by LDoc 1.4.6 Last updated 2018-11-07 17:56:33