From e35a214eaf583d195b43308af7152612f5c84cf0 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 9 Aug 2016 01:42:41 +0300 Subject: [PATCH] Review fixes: special case of toMap for singleton map. #KT-9108 --- js/js.libraries/src/core/kotlin.kt | 4 +++- libraries/stdlib/src/kotlin/collections/Maps.kt | 14 ++++++++++---- libraries/stdlib/test/collections/MapTest.kt | 5 +++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/js/js.libraries/src/core/kotlin.kt b/js/js.libraries/src/core/kotlin.kt index e2e9f293918..58f591d5d70 100644 --- a/js/js.libraries/src/core/kotlin.kt +++ b/js/js.libraries/src/core/kotlin.kt @@ -92,7 +92,9 @@ internal fun arrayPlusCollection(array: dynamic, collection: Collection): } // no singleton map implementation in js, return map as is -internal inline fun Map.toSingletonMap(): Map = this +internal inline fun Map.toSingletonMapOrSelf(): Map = this + +internal inline fun Map.toSingletonMap(): Map = this.toMutableMap() internal inline fun Array.copyToArrayOfAny(isVarargs: Boolean): Array = if (isVarargs) diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 350b39ff6ca..f095cb72a22 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -445,6 +445,7 @@ public fun > Sequence>.toMap(destina */ public fun Map.toMap(): Map = when (size) { 0 -> emptyMap() + 1 -> toSingletonMap() else -> toMutableMap() } @@ -551,11 +552,16 @@ public inline operator fun MutableMap.plusAssign(map: Map Map.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 Map.toSingletonMap(): Map - = with (entries.iterator().next()) { Collections.singletonMap(key, value) } +@kotlin.internal.InlineOnly +internal inline fun Map.toSingletonMapOrSelf(): Map = toSingletonMap() + +// creates a singleton copy of map +@kotlin.jvm.JvmVersion +internal fun Map.toSingletonMap(): Map + = with (entries.iterator().next()) { Collections.singletonMap(key, value) } diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index ea1c3a4707e..0ebde237b62 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -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()) + assertEquals>(expected, mutableMap3) } @test fun createWithSelector() {