From 544bc9a70ce1a89c380d0ca20f4b46af34f31e89 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 30 Nov 2015 04:19:52 +0300 Subject: [PATCH] Introduce toMap with key-value pair selector. #KT-6657 --- libraries/stdlib/src/generated/_Arrays.kt | 126 ++++++++++++++++++ .../stdlib/src/generated/_Collections.kt | 14 ++ libraries/stdlib/src/generated/_Sequences.kt | 13 ++ libraries/stdlib/src/generated/_Strings.kt | 14 ++ libraries/stdlib/test/collections/MapTest.kt | 14 +- .../src/templates/Snapshots.kt | 54 ++++++++ 6 files changed, 232 insertions(+), 3 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 2b397bf8881..475aeb9f9cf 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -6004,6 +6004,132 @@ public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Sh return result } +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun Array.toMap(transform: (T) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun BooleanArray.toMap(transform: (Boolean) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun ByteArray.toMap(transform: (Byte) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun CharArray.toMap(transform: (Char) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun DoubleArray.toMap(transform: (Double) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun FloatArray.toMap(transform: (Float) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun IntArray.toMap(transform: (Int) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun LongArray.toMap(transform: (Long) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun ShortArray.toMap(transform: (Short) -> Pair): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + /** * Returns a [Map] containing the elements from the given array indexed by the key * returned from [selector] function applied to each element. diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 9d51a97a598..10babb31f38 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1082,6 +1082,20 @@ public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) return result } +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given collection. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun Iterable.toMap(transform: (T) -> Pair): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + /** * Returns a [Map] containing the elements from the given collection indexed by the key * returned from [selector] function applied to each element. diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 31c08e9277c..9e4c0361d5a 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -539,6 +539,19 @@ public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) return result } +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given sequence. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun Sequence.toMap(transform: (T) -> Pair): Map { + val result = LinkedHashMap() + for (element in this) { + result += transform(element) + } + return result +} + /** * Returns a [Map] containing the elements from the given sequence indexed by the key * returned from [selector] function applied to each element. diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index fb4cecbc6f4..faa88886618 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -842,6 +842,20 @@ public inline fun String.toMap(selector: (Char) -> K, transform: (Char) - return result } +/** + * Returns a [Map] containing key-value pairs provided by [transform] function applied to characters of the given char sequence. + * If any of two pairs would have the same key the last one gets added to the map. + */ +@kotlin.jvm.JvmName("toMapOfPairs") +public inline fun CharSequence.toMap(transform: (Char) -> Pair): Map { + val capacity = (length/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result +} + /** * Returns a [Map] containing the characters from the given char sequence indexed by the key * returned from [selector] function applied to each character. diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 808cc4e31fe..ffc331949e0 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -184,9 +184,17 @@ class MapTest { @test fun createWithSelectorForKeyAndValue() { val map = listOf("a", "bb", "ccc").toMap({ it.length }, { it.toUpperCase() }) assertEquals(3, map.size) - assertEquals("A", map.get(1)) - assertEquals("BB", map.get(2)) - assertEquals("CCC", map.get(3)) + assertEquals("A", map[1]) + assertEquals("BB", map[2]) + assertEquals("CCC", map[3]) + } + + @test fun createWithPairSelector() { + val map = listOf("a", "bb", "ccc").toMap { it.length to it.toUpperCase() } + assertEquals(3, map.size) + assertEquals("A", map[1]) + assertEquals("BB", map[2]) + assertEquals("CCC", map[3]) } @test fun createUsingTo() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index e9edc58c30b..452c999c559 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -127,6 +127,60 @@ fun snapshots(): List { deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)", level = DeprecationLevel.HIDDEN)) } + templates add f("toMap(transform: (T) -> Pair)") { + inline(true) + include(CharSequences) + typeParam("K") + typeParam("V") + returns("Map") + annotations("""@kotlin.jvm.JvmName("toMapOfPairs")""") + doc { f -> + """ + Returns a [Map] containing key-value pairs provided by [transform] function applied to ${f.element}s of the given ${f.collection}. + If any of two pairs would have the same key the last one gets added to the map. + """ + } + body { + """ + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result + """ + } + body(Sequences) { + """ + val result = LinkedHashMap() + for (element in this) { + result += transform(element) + } + return result + """ + } + body(CharSequences) { + """ + val capacity = (length/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result += transform(element) + } + return result + """ + } + } + templates add f("toMapBy(selector: (T) -> K)") { inline(true) typeParam("K")