KDS

KDS: Data Structures

Kds is a Data Structure library for Multiplatform Kotlin. It includes a set of optimized data structures written in Kotlin Common so they are available in JVM, JS and future multiplatform targets. Those structures are designed to be allocation-efficient and fast, so Kds include specialized versions for primitives like Int or Double.

https://github.com/korlibs/kds

Star

Build Status Maven Version

Table of contents:

Pages

  •  Lists - ArrayList, FastArrayList, Array2, Deque, Pool, PriorityQueue, Queue, Stack, ListReader...
  •  Sets - IntSet, BitSet
  •  Maps - FastMap, IntMap, IntIntMap, IntFloatMap, CacheMap, CaseInsensitiveStringMap, WeakMap, MapList extensions...
  •  Algorithms - genericBinarySearch, genericSort, mapWhile, getCyclic
  •  Delegates - Extra, Computed, WeakProperty

Some samples:

// Case Insensitive Map
val map = mapOf("hELLo" to 1, "World" to 2).toCaseInsensitiveMap()
println(map["hello"])

// BitSet
val array = BitSet(100) // Stores 100 bits
array[99] = true

// TypedArrayList
val v20 = intArrayListOf(10, 20).getCyclic(-1)

// Deque
val deque = IntDeque().apply {
    addFirst(n)
    removeFirst()
    addLast(n)
}

// CacheMap
val cache = CacheMap<String, Int>(maxSize = 2).apply {
    this["a"] = 1
    this["b"] = 2
    this["c"] = 3
    assertEquals("{b=2, c=3}", this.toString())
}

// IntIntMap
val m = IntIntMap().apply {
    this[0] = 98
}

// Pool
val pool = Pool { Demo() }
pool.alloc { demo ->
    println("Temporarilly allocated $demo")
}

// Priority Queue
val pq = IntPriorityQueue()
pq.add(10)
pq.add(5)
pq.add(15)
assertEquals(5, pq.removeHead())

// Extra Properties
class Demo : Extra by Extra.Mixin() { val default = 9 }
var Demo.demo by Extra.Property { 0 }
var Demo.demo2 by Extra.PropertyThis<Demo, Int> { default }
val demo = Demo()
assertEquals(0, demo.demo)
assertEquals(9, demo.demo2)
demo.demo = 7
assertEquals(7, demo.demo)
assertEquals("{demo=7, demo2=9}", demo.extra.toString())

// mapWhile
val iterator = listOf(1, 2, 3).iterator()
assertEquals(listOf(1, 2, 3), mapWhile({ iterator.hasNext() }) { iterator.next()})

// And much more!

Using with gradle

Requires Gradle 7.1.1 (JVM 11~17) for building and Kotlin >=1.7.21 for running:

build.gradle.kts

val kdsVersion = "4.0.2"

// For multiplatform projects
kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation("com.soywiz.korlibs.kds:kds:$kdsVersion") 
            }
        }
    }
}

dependencies {
    // For JVM only
    implementation("com.soywiz.korlibs.kds:kds-jvm:$kdsVersion") 
    // For Android only
    implementation("com.soywiz.korlibs.kds:kds-android:$kdsVersion") 
    // For JS only
    implementation("com.soywiz.korlibs.kds:kds-js:$kdsVersion") 
}