Heap

class Heap<T>

Heap

A simple, generic heap implementation that uses a caller provided function to enable heaps of custom types.

The basic heap types, min-heap and max-heap, can be implemented by using a simple lambda such as:

// min heap
{ a, b -> a < b }

// max heap
{ a, b -> a b }

Author

Elizabeth Paige Harper - https://github.com/foxcapades

Since

0.1.0

Parameters

T

Type of values contained in this heap.

Constructors

Link copied to clipboard
fun <T> Heap(initialCapacity: Int = 8, scaleFactor: Float = 1.5f, maxSize: Int = Int.MAX_VALUE, sortTest: SortTest<T>)

Constructs a new heap instance.

Functions

Link copied to clipboard
inline fun forEach(fn: (T) -> Unit)

Destructively iterates over the elements popped from this heap and calls the given function on each value.

Link copied to clipboard
inline fun forEachIndexed(fn: (i: Int, it: T) -> Unit)

Destructively iterates over the elements popped from this heap and calls the given function on each value.

Link copied to clipboard
operator fun hasNext(): Boolean

Tests whether this heap contains more items.

Link copied to clipboard
fun insert(value: T)

Inserts a new value into the heap.

Link copied to clipboard

Tests whether this heap contains zero items.

Link copied to clipboard

Tests whether this heap contains one or more items.

Link copied to clipboard
operator fun iterator(): Heap<T>

Returns a destructive iterator over the contents of this heap.

Link copied to clipboard
operator fun next(): T

Removes and returns the value at the top of this heap.

Link copied to clipboard
fun peekNext(): T

Returns the value at the top of this heap without removing it.

Link copied to clipboard
inline operator fun plusAssign(value: T)

Properties

Link copied to clipboard

The current capacity of the heap.

Link copied to clipboard

The maximum allowed number of items in this heap.

Link copied to clipboard
var size: Int

The current number of items in this heap.