Skip to content

Commit

Permalink
readme + some mdmaps goodies
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Aug 7, 2020
1 parent 1ee0515 commit e8c45bf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[ ![Download](https://api.bintray.com/packages/vorpal-research/kotlin-maven/kotlin-wheels/images/download.svg) ](https://bintray.com/vorpal-research/kotlin-maven/kotlin-wheels/_latestVersion)

# Kotlin wheels

My personal collection of utils used across my other projects

41 changes: 41 additions & 0 deletions src/main/kotlin/ru/spbstu/wheels/MDMaps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ abstract class MDMap<K, V>(val inner: MutableMap<K, V> = mutableMapOf()) {
operator fun get(key: K): V = inner.getOrPut(key, this::defaultValue)
operator fun set(key: K, value: V) = inner.set(key, value)

fun remove(key: K) { inner.remove(key) }

companion object {
inline fun <K, V> withDefault(crossinline default: () -> V): MDMap<K, V> = object : MDMap<K, V>() {
override fun defaultValue(): V = default()
Expand Down Expand Up @@ -51,6 +53,32 @@ inline operator fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.get(key1: K1, key2: K2
inline operator fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.set(key1: K1, key2: K2, value: VS) =
this[key1].set(key2, value)

@Suppress(Warnings.NOTHING_TO_INLINE)
inline fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.remove(key1: K1, key2: K2) {
val mLevel1 = this.inner[key1] ?: return
mLevel1.remove(key2)
if(mLevel1.isEmpty()) remove(key1)
}

@Suppress(Warnings.NOTHING_TO_INLINE)
inline fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.getOrPut(key1: K1, key2: K2, defaultValue: () -> VS) =
this[key1].getOrPut(key2, defaultValue)

@Suppress(Warnings.NOTHING_TO_INLINE)
inline operator fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.plusAssign(triple: Triple<K1, K2, VS>) {
this[triple.first, triple.second] = triple.third
}

@Suppress(Warnings.NOTHING_TO_INLINE)
inline operator fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.plusAssign(triples: Iterable<Triple<K1, K2, VS>>) {
for(triple in triples) plusAssign(triple)
}

@Suppress(Warnings.NOTHING_TO_INLINE)
inline operator fun <K1, K2, VS> MutableMap2D<K1, K2, VS>.plusAssign(triples: Sequence<Triple<K1, K2, VS>>) {
for(triple in triples) plusAssign(triple)
}

typealias MutableMap3D<K1, K2, K3, VS> = MDMap<K1, MutableMap2D<K2, K3, VS>>

fun <K1, K2, K3, VS> MutableMap3D(): MutableMap3D<K1, K2, K3, VS> = MDMap.withDefault { MutableMap2D<K2, K3, VS>() }
Expand All @@ -66,3 +94,16 @@ inline operator fun <K1, K2, K3, VS> MutableMap3D<K1, K2, K3, VS>.get(key1: K1,
@Suppress(Warnings.NOTHING_TO_INLINE)
inline operator fun <K1, K2, K3, VS> MutableMap3D<K1, K2, K3, VS>.set(key1: K1, key2: K2, key3: K3, value: VS) =
this[key1][key2].set(key3, value)

@Suppress(Warnings.NOTHING_TO_INLINE)
inline fun <K1, K2, K3, VS> MutableMap3D<K1, K2, K3, VS>.remove(key1: K1, key2: K2, key3: K3) {
val mLevel1 = this.inner[key1] ?: return
val mLevel2 = mLevel1.inner[key2] ?: return
mLevel2.remove(key3)
if(mLevel2.isEmpty()) mLevel1.inner.remove(key2)
if(mLevel1.inner.isEmpty()) this.remove(key1)
}

@Suppress(Warnings.NOTHING_TO_INLINE)
inline fun <K1, K2, K3, VS> MutableMap3D<K1, K2, K3, VS>.getOrPut(key1: K1, key2: K2, key3: K3, defaultValue: () -> VS) =
this[key1][key2].getOrPut(key3, defaultValue)

0 comments on commit e8c45bf

Please sign in to comment.