@file:kotlin.jvm.JvmMultifileClass @file:kotlin.jvm.JvmName("MapsKt") package kotlin.collections import kotlin.sequences.* private object EmptyMap : Map, java.io.Serializable { private const val serialVersionUID: Long = 8246714829545688274 override fun equals(other: Any?): Boolean = other is Map<*, *> && other.isEmpty() override fun hashCode(): Int = 0 override fun toString(): String = "{}" override val size: Int get() = 0 override fun isEmpty(): Boolean = true override fun containsKey(key: Any?): Boolean = false override fun containsValue(value: Nothing): Boolean = false override fun get(key: Any?): Nothing? = null override val entries: Set> get() = EmptySet override val keys: Set get() = EmptySet override val values: Collection get() = EmptyList private fun readResolve(): Any = EmptyMap } public fun emptyMap(): Map = @Suppress("UNCHECKED_CAST") (EmptyMap as Map) public fun mapOf(vararg pairs: Pair): Map = if (pairs.size > 0) pairs.toMap(LinkedHashMap(mapCapacity(pairs.size))) else emptyMap() public inline fun mapOf(): Map = emptyMap() public inline fun mutableMapOf(): MutableMap = LinkedHashMap() public fun mutableMapOf(vararg pairs: Pair): MutableMap = LinkedHashMap(mapCapacity(pairs.size)).apply { putAll(pairs) } public inline fun hashMapOf(): HashMap = kotlin.UnsupportedOperationException("This is intrinsic") public fun hashMapOf(vararg pairs: Pair): HashMap = kotlin.UnsupportedOperationException("This is intrinsic") public inline fun linkedMapOf(): LinkedHashMap = LinkedHashMap() internal fun mapCapacity(expectedSize: Int): Int = when { // We are not coercing the value to a valid one and not throwing an exception. It is up to the caller to // properly handle negative values. expectedSize < 0 -> expectedSize expectedSize < 3 -> expectedSize + 1 expectedSize < 1 shl (Int.SIZE_BITS - 2) -> ((expectedSize / 0.75F) + 1.0F).toInt() // any large value else -> Int.MAX_VALUE } public inline operator fun Map.contains(key: K): Boolean = containsKey(key) public inline operator fun Map.get(key: K): V? = (this as Map).get(key) public inline operator fun MutableMap.set(key: K, value: V): Unit { put(key, value) } public inline fun Map.containsKey(key: K): Boolean = (this as Map).containsKey(key) public inline fun Map.containsValue(value: V): Boolean = this.containsValue(value) public inline fun MutableMap.remove(key: K): V? = (this as MutableMap).remove(key) public inline operator fun Map.Entry.component1(): K = key public inline operator fun Map.Entry.component2(): V = value public inline fun Map.Entry.toPair(): Pair = Pair(key, value) public inline operator fun Map.iterator(): Iterator> = entries.iterator() public fun MutableMap.putAll(pairs: Array>): Unit { for ((key, value) in pairs) { put(key, value) } } public fun MutableMap.putAll(pairs: Iterable>): Unit { for ((key, value) in pairs) { put(key, value) } } public fun MutableMap.putAll(pairs: Sequence>): Unit { for ((key, value) in pairs) { put(key, value) } } public fun Iterable>.toMap(): Map { if (this is Collection) { return when (size) { 0 -> emptyMap() 1 -> mapOf(if (this is List) this[0] else iterator().next()) else -> toMap(LinkedHashMap(mapCapacity(size))) } } return toMap(LinkedHashMap()).optimizeReadOnlyMap() } public fun > Iterable>.toMap(destination: M): M = destination.apply { putAll(this@toMap) } public fun Array>.toMap(): Map = when (size) { 0 -> emptyMap() 1 -> mapOf(this[0]) else -> toMap(LinkedHashMap(mapCapacity(size))) } public fun > Array>.toMap(destination: M): M = destination.apply { putAll(this@toMap) } public fun Sequence>.toMap(): Map = toMap(LinkedHashMap()).optimizeReadOnlyMap() public fun > Sequence>.toMap(destination: M): M = destination.apply { putAll(this@toMap) } internal fun Map.optimizeReadOnlyMap() = when (size) { 0 -> emptyMap() 1 -> this // toSingletonMapOrSelf() else -> this }