XHeap

require.mx('mxjs/base/algorithms/xheap.js');

This library provides a priority queue (heap) implementation

StatusName
Heap createHeap ( Function ↓cmp_function )

Returns a Heap object, which encapsulates a priority queue.

Array heapSort ( Array ⇅array, Number ↓start, Number ↓end, Function ↓cmp_function )

Sorts an array using heapsort.


Library Functions

↑heap = Lib.createHeap ( ↓cmp_function )

Returns a Heap object, which encapsulates a priority queue.

Parameters:
  • Function ↓cmp_function - - optional, function f(a,b) which returns positive if a > b, 0 if a = b and negative if a < b. If omitted, sorts as numbers.
Returns: Heap ↑heap
⇅array = Lib.heapSort ( ⇅array, ↓start, ↓end, ↓cmp_function )

Sorts an array using heapsort.

Parameters:
  • Array ⇅array - - array to sort
  • Number ↓start - - index of array to start sorting at.
  • Number ↓end - - 1 + index of array to stop sorting at.
  • Function ↓cmp_function - - optional, function f(a,b) which returns positive if a > b, 0 if a = b and negative if a < b. If omitted, sorts as numbers.
Returns: Array ⇅array

Class: Heap

A Heap encapsulates a priority queue

StatusName
add ( Number ↓value )

Adds the specified value to the heap

Number remove ( )

Removes and returns the largest (by compare function) value in the heap.

Number size ( )

Returns the current size of this heap.

Heap.add ( ↓value )

Adds the specified value to the heap

Parameters:
  • Number ↓value - - the value to add to the heap
Heap.remove ( )

Removes and returns the largest (by compare function) value in the heap.

Parameters:
Returns: Number ↑value - - the returned value
Heap.size ( )

Returns the current size of this heap.

Parameters:
Returns: Number ↑size - - the current heap size