diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 475aeb9f9cf..10dad439daa 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -5891,117 +5891,81 @@ public inline fun ShortArray.toMap(selector: (Short) -> K): Map { * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun Array.toMap(selector: (T) -> K, transform: (T) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. * 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, transform)")) public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map { - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** @@ -6256,6 +6220,123 @@ public inline fun ShortArray.toMapBy(selector: (Short) -> K): Map return result } +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (T) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Boolean) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Byte) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Char) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Double) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Float) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Int) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Long) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given array. + * 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, transform: (Short) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(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 10babb31f38..1cf651dcea1 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1073,13 +1073,9 @@ public inline fun Iterable.toMap(selector: (T) -> K): Map { * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given collection. * 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, transform)")) public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) -> V): Map { - val capacity = (collectionSizeOrDefault(10)/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** @@ -1110,6 +1106,19 @@ public inline fun Iterable.toMapBy(selector: (T) -> K): Map { return result } +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given collection. + * 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, transform: (T) -> V): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(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 9e4c0361d5a..b14ed63cdf0 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -531,12 +531,9 @@ public inline fun Sequence.toMap(selector: (T) -> K): Map { * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given sequence. * 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, transform)")) public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) -> V): Map { - val result = LinkedHashMap() - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** @@ -565,6 +562,18 @@ public inline fun Sequence.toMapBy(selector: (T) -> K): Map { return result } +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given sequence. + * 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, transform: (T) -> V): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), transform(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 faa88886618..0531b8cd8b5 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -819,13 +819,9 @@ public inline fun String.toMap(selector: (Char) -> K): Map { * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to characters of the given char sequence. * If any two characters would have the same key returned by [selector] the last one gets added to the map. */ +@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector, transform)")) public inline fun CharSequence.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - val capacity = (length/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** @@ -834,12 +830,7 @@ public inline fun CharSequence.toMap(selector: (Char) -> K, transform: (C */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - val capacity = (length/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result + return toMapBy(selector, transform) } /** @@ -885,6 +876,19 @@ public inline fun String.toMapBy(selector: (Char) -> K): Map { return result } +/** + * Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to characters of the given char sequence. + * If any two characters would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun CharSequence.toMapBy(selector: (Char) -> K, transform: (Char) -> V): Map { + val capacity = (length/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + /** * Returns a [Set] of all characters. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 452c999c559..4d6f62f909a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -241,6 +241,24 @@ fun snapshots(): List { } templates add f("toMap(selector: (T) -> K, transform: (T) -> V)") { + inline(true) + include(CharSequences) + typeParam("K") + typeParam("V") + doc { f -> + """ + Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to ${f.element.pluralize()} of the given ${f.collection}. + If any two ${f.element.pluralize()} would have the same key returned by [selector] the last one gets added to the map. + """ + } + returns("Map") + deprecate(Deprecation("Use toMapBy instead.", "toMapBy(selector, transform)")) + + deprecate(Strings) { forBinaryCompatibility } + body(Strings) { "return toMapBy(selector, transform)"} + } + + templates add f("toMapBy(selector: (T) -> K, transform: (T) -> V)") { inline(true) typeParam("K") typeParam("V") @@ -276,8 +294,7 @@ fun snapshots(): List { return result """ } - deprecate(Strings) { forBinaryCompatibility } - body(CharSequences, Strings) { + body(CharSequences) { """ val capacity = (length/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16))