From e09c0ae2ffe6a915ac4376a0ed5acc93cca965f3 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 8 Oct 2015 00:39:47 +0300 Subject: [PATCH] Rename toMap { selector } to toMapBy. #KT-6657 --- libraries/stdlib/src/generated/_Arrays.kt | 225 +++++++++++------- .../stdlib/src/generated/_Collections.kt | 25 +- libraries/stdlib/src/generated/_Sequences.kt | 23 +- libraries/stdlib/src/generated/_Strings.kt | 25 +- libraries/stdlib/test/collections/MapTest.kt | 4 +- .../src/templates/Snapshots.kt | 10 + 6 files changed, 191 insertions(+), 121 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 24734087b76..f56c43e0cda 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -5514,121 +5514,49 @@ public fun ShortArray.toList(): List { return this.toArrayList() } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun Array.toMap(selector: (T) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun ByteArray.toMap(selector: (Byte) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun CharArray.toMap(selector: (Char) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun DoubleArray.toMap(selector: (Double) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun FloatArray.toMap(selector: (Float) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun IntArray.toMap(selector: (Int) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun LongArray.toMap(selector: (Long) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun ShortArray.toMap(selector: (Short) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } /** @@ -5748,6 +5676,123 @@ public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Sh return result } +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Array.toMapBy(selector: (T) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun BooleanArray.toMapBy(selector: (Boolean) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun ByteArray.toMapBy(selector: (Byte) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun CharArray.toMapBy(selector: (Char) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun DoubleArray.toMapBy(selector: (Double) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun FloatArray.toMapBy(selector: (Float) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun IntArray.toMapBy(selector: (Int) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun LongArray.toMapBy(selector: (Long) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun ShortArray.toMapBy(selector: (Short) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 15c75593125..22ec5a15a18 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -894,17 +894,9 @@ public fun Iterable.toList(): List { return this.toArrayList() } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun Iterable.toMap(selector: (T) -> K): Map { - val capacity = (collectionSizeOrDefault(10)/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } /** @@ -920,6 +912,19 @@ public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) return result } +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Iterable.toMapBy(selector: (T) -> K): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 2e357944996..30f70b7826a 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -441,16 +441,9 @@ public fun Sequence.toList(): List { return this.toArrayList() } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun Sequence.toMap(selector: (T) -> K): Map { - val result = LinkedHashMap() - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } /** @@ -465,6 +458,18 @@ public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) return result } +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Sequence.toMapBy(selector: (T) -> K): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), element) + } + return result +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 6ef7a9df5df..0337fba57e6 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -386,17 +386,9 @@ public fun String.toList(): List { return this.toArrayList() } -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) public inline fun String.toMap(selector: (Char) -> K): Map { - val capacity = (length()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result + return toMapBy(selector) } /** @@ -412,6 +404,19 @@ public inline fun String.toMap(selector: (Char) -> K, transform: (Char) - return result } +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun String.toMapBy(selector: (Char) -> K): Map { + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 49c02b6a9ef..bc8c71250af 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -160,7 +160,7 @@ class MapTest { } @test fun createWithSelector() { - val map = listOf("a", "bb", "ccc").toMap { it.length() } + val map = listOf("a", "bb", "ccc").toMapBy { it.length() } assertEquals(3, map.size()) assertEquals("a", map.get(1)) assertEquals("bb", map.get(2)) @@ -168,7 +168,7 @@ class MapTest { } @test fun createWithSelectorAndOverwrite() { - val map = listOf("aa", "bb", "ccc").toMap { it.length() } + val map = listOf("aa", "bb", "ccc").toMapBy { it.length() } assertEquals(2, map.size()) assertEquals("bb", map.get(2)) assertEquals("ccc", map.get(3)) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 96ebc5cc602..71f0e0a5189 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -94,6 +94,16 @@ fun snapshots(): List { } templates add f("toMap(selector: (T) -> K)") { + inline(true) + include(Strings) + typeParam("K") + returns("Map") + deprecate("Use toMapBy instead.") + deprecateReplacement("toMapBy(selector)") + body("return ${deprecateReplacement.default}") + } + + templates add f("toMapBy(selector: (T) -> K)") { inline(true) typeParam("K") doc {