Review fixes: special case of toMap for singleton map.
#KT-9108
This commit is contained in:
@@ -92,7 +92,9 @@ internal fun <T> arrayPlusCollection(array: dynamic, collection: Collection<T>):
|
||||
}
|
||||
|
||||
// no singleton map implementation in js, return map as is
|
||||
internal inline fun <K, V> Map<K, V>.toSingletonMap(): Map<K, V> = this
|
||||
internal inline fun <K, V> Map<K, V>.toSingletonMapOrSelf(): Map<K, V> = this
|
||||
|
||||
internal inline fun <K, V> Map<out K, V>.toSingletonMap(): Map<K, V> = this.toMutableMap()
|
||||
|
||||
internal inline fun <T> Array<out T>.copyToArrayOfAny(isVarargs: Boolean): Array<out Any?> =
|
||||
if (isVarargs)
|
||||
|
||||
@@ -445,6 +445,7 @@ public fun <K, V, M : MutableMap<in K, in V>> Sequence<Pair<K, V>>.toMap(destina
|
||||
*/
|
||||
public fun <K, V> Map<out K, V>.toMap(): Map<K, V> = when (size) {
|
||||
0 -> emptyMap()
|
||||
1 -> toSingletonMap()
|
||||
else -> toMutableMap()
|
||||
}
|
||||
|
||||
@@ -551,11 +552,16 @@ public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(map: Map<K,
|
||||
// do not expose for now @kotlin.internal.InlineExposed
|
||||
internal fun <K, V> Map<K, V>.optimizeReadOnlyMap() = when (size) {
|
||||
0 -> emptyMap()
|
||||
1 -> toSingletonMap()
|
||||
1 -> toSingletonMapOrSelf()
|
||||
else -> this
|
||||
}
|
||||
|
||||
// creates a singleton copy of map, if it there is specialization available in target platform
|
||||
// creates a singleton copy of map, if there is specialization available in target platform, otherwise returns itself
|
||||
@kotlin.jvm.JvmVersion
|
||||
internal fun <K, V> Map<K, V>.toSingletonMap(): Map<K, V>
|
||||
= with (entries.iterator().next()) { Collections.singletonMap(key, value) }
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun <K, V> Map<K, V>.toSingletonMapOrSelf(): Map<K, V> = toSingletonMap()
|
||||
|
||||
// creates a singleton copy of map
|
||||
@kotlin.jvm.JvmVersion
|
||||
internal fun <K, V> Map<out K, V>.toSingletonMap(): Map<K, V>
|
||||
= with (entries.iterator().next()) { Collections.singletonMap(key, value) }
|
||||
|
||||
@@ -170,6 +170,8 @@ class MapTest {
|
||||
assertEquals(expected, pairs.asIterable().toMap())
|
||||
assertEquals(expected, pairs.asSequence().toMap())
|
||||
assertEquals(expected, expected.toMap())
|
||||
assertEquals(mapOf("a" to 1), expected.filterKeys { it == "a" }.toMap())
|
||||
assertEquals(emptyMap(), expected.filter { false }.toMap())
|
||||
|
||||
val mutableMap = expected.toMutableMap()
|
||||
assertEquals(expected, mutableMap)
|
||||
@@ -192,6 +194,9 @@ class MapTest {
|
||||
|
||||
val mutableMap2 = mutableMap.toMap(mutableMapOf())
|
||||
assertEquals(expected, mutableMap2)
|
||||
|
||||
val mutableMap3 = mutableMap.toMap(hashMapOf<CharSequence, Any>())
|
||||
assertEquals<Map<*, *>>(expected, mutableMap3)
|
||||
}
|
||||
|
||||
@test fun createWithSelector() {
|
||||
|
||||
Reference in New Issue
Block a user