diff --git a/README.md b/README.md new file mode 100644 index 0000000..bf8692e --- /dev/null +++ b/README.md @@ -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 + diff --git a/src/main/kotlin/ru/spbstu/wheels/MDMaps.kt b/src/main/kotlin/ru/spbstu/wheels/MDMaps.kt index d0b4433..e4c0df5 100644 --- a/src/main/kotlin/ru/spbstu/wheels/MDMaps.kt +++ b/src/main/kotlin/ru/spbstu/wheels/MDMaps.kt @@ -8,6 +8,8 @@ abstract class MDMap(val inner: MutableMap = 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 withDefault(crossinline default: () -> V): MDMap = object : MDMap() { override fun defaultValue(): V = default() @@ -51,6 +53,32 @@ inline operator fun MutableMap2D.get(key1: K1, key2: K2 inline operator fun MutableMap2D.set(key1: K1, key2: K2, value: VS) = this[key1].set(key2, value) +@Suppress(Warnings.NOTHING_TO_INLINE) +inline fun MutableMap2D.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 MutableMap2D.getOrPut(key1: K1, key2: K2, defaultValue: () -> VS) = + this[key1].getOrPut(key2, defaultValue) + +@Suppress(Warnings.NOTHING_TO_INLINE) +inline operator fun MutableMap2D.plusAssign(triple: Triple) { + this[triple.first, triple.second] = triple.third +} + +@Suppress(Warnings.NOTHING_TO_INLINE) +inline operator fun MutableMap2D.plusAssign(triples: Iterable>) { + for(triple in triples) plusAssign(triple) +} + +@Suppress(Warnings.NOTHING_TO_INLINE) +inline operator fun MutableMap2D.plusAssign(triples: Sequence>) { + for(triple in triples) plusAssign(triple) +} + typealias MutableMap3D = MDMap> fun MutableMap3D(): MutableMap3D = MDMap.withDefault { MutableMap2D() } @@ -66,3 +94,16 @@ inline operator fun MutableMap3D.get(key1: K1, @Suppress(Warnings.NOTHING_TO_INLINE) inline operator fun MutableMap3D.set(key1: K1, key2: K2, key3: K3, value: VS) = this[key1][key2].set(key3, value) + +@Suppress(Warnings.NOTHING_TO_INLINE) +inline fun MutableMap3D.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 MutableMap3D.getOrPut(key1: K1, key2: K2, key3: K3, defaultValue: () -> VS) = + this[key1][key2].getOrPut(key3, defaultValue)