Skip to content

k-libs/k-heap

Repository files navigation

K-Heap

k heap docs dokka ff69b4 heap

A pure Kotlin heap implementation that allows for custom type items.

Import

  implementation("io.k-libs:heap:0.1.0")

Usage

Basic

Min Heap
val heap = minHeapOf(1, 5, 2, 4, 3)

require(heap.next() == 1)
require(heap.next() == 2)
require(heap.next() == 3)
require(heap.next() == 4)
require(heap.next() == 5)
Max Heap
val heap = maxHeapOf(1, 5, 2, 4, 3)

require(heap.next() == 5)
require(heap.next() == 4)
require(heap.next() == 3)
require(heap.next() == 2)
require(heap.next() == 1)

Custom Types

data class Dog(val name: String)

val dogs = listOf(
  Dog("Dexter"),
  Dog("Luna"),
  Dog("Daisy"),
  Dog("Max"),
)

val heap = heapOf(dogs) { a, b -> a.name < b.name}

Full Control

val heap = Heap<Burger>(
  initialCapacity = 32,
  scaleFactory    = 2F,
  maxSize         = 256,
) { a, b -> a.price > b.price }