diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 9bc85a82d71..d9f5497395b 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -5413,6 +5413,366 @@ public fun Array.toShortArray(): ShortArray { 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. + */ +public inline fun Array.associate(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. + */ +public inline fun BooleanArray.associate(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. + */ +public inline fun ByteArray.associate(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. + */ +public inline fun CharArray.associate(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. + */ +public inline fun DoubleArray.associate(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. + */ +public inline fun FloatArray.associate(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. + */ +public inline fun IntArray.associate(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. + */ +public inline fun LongArray.associate(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. + */ +public inline fun ShortArray.associate(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 [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun Array.associateBy(keySelector: (T) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun IntArray.associateBy(keySelector: (Int) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the elements from the given array indexed by the key + * returned from [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun Array.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map { + val capacity = (size/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + /** * Returns an [ArrayList] of all elements. */ @@ -5712,450 +6072,225 @@ public fun ShortArray.toList(): List { * 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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun Array.toMap(selector: (T) -> K, transform: (T) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map { - return toMapBy(selector, transform) + return associateBy(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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map { - return toMapBy(selector, transform) + return associateBy(selector, transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * 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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(selector, transform) } /** diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index f34cf90a1ee..f148279e9f1 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -913,6 +913,46 @@ public fun Collection.toShortArray(): ShortArray { 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. + */ +public inline fun Iterable.associate(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 [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun Iterable.associateBy(keySelector: (T) -> K): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given collection. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + /** * Returns an [ArrayList] of all elements. */ @@ -957,50 +997,25 @@ public fun Iterable.toList(): List { * 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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) -> V): Map { - return toMapBy(selector, transform) + return associateBy(selector, transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * Returns a [Map] containing the elements from the given collection indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) 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 + return associateBy(selector) } -/** - * 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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(selector, transform) } /** diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index dd2586af528..847f51b6f6f 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -422,6 +422,43 @@ public fun Sequence.sortedWith(comparator: Comparator): Sequence } } +/** + * 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. + */ +public inline fun Sequence.associate(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 [keySelector] function applied to each element. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun Sequence.associateBy(keySelector: (T) -> K): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given sequence. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + /** * Returns an [ArrayList] of all elements. */ @@ -457,47 +494,25 @@ public fun Sequence.toList(): List { * 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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) -> V): Map { - return toMapBy(selector, transform) + return associateBy(selector, transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * Returns a [Map] containing the elements from the given sequence indexed by the key - * returned from [selector] function applied to each element. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) public inline fun Sequence.toMapBy(selector: (T) -> K): Map { - val result = LinkedHashMap() - for (element in this) { - result.put(selector(element), element) - } - return result + return associateBy(selector) } -/** - * 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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(selector, transform) } /** diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 04391b16923..99a62cfbdaa 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -491,6 +491,46 @@ public fun String.reversed(): String { return StringBuilder(this).reverse().toString() } +/** + * 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. + */ +public inline fun CharSequence.associate(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 [keySelector] function applied to each character. + * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map { + val capacity = (length/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), element) + } + return result +} + +/** + * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to characters of the given char sequence. + * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { + val capacity = (length/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(keySelector(element), valueTransform(element)) + } + return result +} + /** * Returns an [ArrayList] of all characters. */ @@ -526,50 +566,25 @@ public fun CharSequence.toList(): List { * 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)")) +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector, transform)")) public inline fun CharSequence.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - return toMapBy(selector, transform) + return associateBy(selector, transform) } -/** - * 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. - */ +@Deprecated("Use associate instead.", ReplaceWith("associate(transform)")) @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 + return associate(transform) } -/** - * Returns a [Map] containing the characters from the given char sequence indexed by the key - * returned from [selector] function applied to each character. - * If any two characters would have the same key returned by [selector] the last one gets added to the map. - */ +@Deprecated("Use associateBy instead.", ReplaceWith("associateBy(selector)")) public inline fun CharSequence.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 + return associateBy(selector) } -/** - * 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 associateBy instead.", ReplaceWith("associateBy(selector, transform)")) 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 + return associateBy(selector, transform) } /** diff --git a/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt b/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt index eb85e23d456..969112004a9 100644 --- a/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt +++ b/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt @@ -161,7 +161,7 @@ public enum class CharCategory(public val value: Int, public val code: String) { public companion object { - private val categoryMap by lazy { CharCategory.values().toMapBy { it.value } } + private val categoryMap by lazy { CharCategory.values().associateBy { it.value } } public fun valueOf(category: Int): CharCategory = categoryMap[category] ?: throw IllegalArgumentException("Category #$category is not defined.") } diff --git a/libraries/stdlib/src/kotlin/text/CharDirectionalityJVM.kt b/libraries/stdlib/src/kotlin/text/CharDirectionalityJVM.kt index 8ea0c8cc9ec..e824b3baca9 100644 --- a/libraries/stdlib/src/kotlin/text/CharDirectionalityJVM.kt +++ b/libraries/stdlib/src/kotlin/text/CharDirectionalityJVM.kt @@ -110,7 +110,7 @@ public enum class CharDirectionality(public val value: Int) { public companion object { - private val directionalityMap by lazy { CharDirectionality.values().toMapBy { it.value } } + private val directionalityMap by lazy { CharDirectionality.values().associateBy { it.value } } public fun valueOf(directionality: Int): CharDirectionality = directionalityMap[directionality] ?: throw IllegalArgumentException("Directionality #$directionality is not defined.") } diff --git a/libraries/stdlib/test/collections/MapJVMTest.kt b/libraries/stdlib/test/collections/MapJVMTest.kt index d921ecfe575..1b59e9fb120 100644 --- a/libraries/stdlib/test/collections/MapJVMTest.kt +++ b/libraries/stdlib/test/collections/MapJVMTest.kt @@ -44,7 +44,7 @@ class MapJVMTest { } @test fun iterateAndRemove() { - val map = (1..5).toMapBy({ it }, { 'a' + it }).toLinkedMap() + val map = (1..5).associateBy({ it }, { 'a' + it }).toLinkedMap() val iterator = map.iterator() while (iterator.hasNext()) { if (iterator.next().key % 2 == 0) diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 25e5ea796f4..13fce7fdf7e 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -172,7 +172,7 @@ class MapTest { } @test fun createWithSelector() { - val map = listOf("a", "bb", "ccc").toMapBy { it.length } + val map = listOf("a", "bb", "ccc").associateBy { it.length } assertEquals(3, map.size) assertEquals("a", map.get(1)) assertEquals("bb", map.get(2)) @@ -180,14 +180,14 @@ class MapTest { } @test fun createWithSelectorAndOverwrite() { - val map = listOf("aa", "bb", "ccc").toMapBy { it.length } + val map = listOf("aa", "bb", "ccc").associateBy { it.length } assertEquals(2, map.size) assertEquals("bb", map.get(2)) assertEquals("ccc", map.get(3)) } @test fun createWithSelectorForKeyAndValue() { - val map = listOf("a", "bb", "ccc").toMapBy({ it.length }, { it.toUpperCase() }) + val map = listOf("a", "bb", "ccc").associateBy({ it.length }, { it.toUpperCase() }) assertEquals(3, map.size) assertEquals("A", map[1]) assertEquals("BB", map[2]) @@ -195,7 +195,7 @@ class MapTest { } @test fun createWithPairSelector() { - val map = listOf("a", "bb", "ccc").toMap { it.length to it.toUpperCase() } + val map = listOf("a", "bb", "ccc").associate { it.length to it.toUpperCase() } assertEquals(3, map.size) assertEquals("A", map[1]) assertEquals("BB", map[2]) diff --git a/libraries/tools/idl2k/src/main/kotlin/gen.kt b/libraries/tools/idl2k/src/main/kotlin/gen.kt index 85b46916866..ad683832d76 100644 --- a/libraries/tools/idl2k/src/main/kotlin/gen.kt +++ b/libraries/tools/idl2k/src/main/kotlin/gen.kt @@ -170,11 +170,11 @@ fun mapDefinitions(repository: Repository, definitions: Iterable, typedefs: Iterable): GenerateUnionTypes { - val declaredTypes = ifaces.toMapBy { it.name } + val declaredTypes = ifaces.associateBy { it.name } val anonymousUnionTypes = collectUnionTypes(declaredTypes) val anonymousUnionTypeTraits = generateUnionTypeTraits(anonymousUnionTypes) - val anonymousUnionsMap = anonymousUnionTypeTraits.toMapBy { it.name } + val anonymousUnionsMap = anonymousUnionTypeTraits.associateBy { it.name } val typedefsToBeGenerated = typedefs.filter { it.types is UnionType } .map { NamedValue(it.name, it.types as UnionType) } diff --git a/libraries/tools/idl2k/src/main/kotlin/render.kt b/libraries/tools/idl2k/src/main/kotlin/render.kt index e42c05c6cb2..06395d425c7 100644 --- a/libraries/tools/idl2k/src/main/kotlin/render.kt +++ b/libraries/tools/idl2k/src/main/kotlin/render.kt @@ -227,7 +227,7 @@ private fun Pair.betterName() = if (((0..9).map { it.toString() fun List>.toMultiMap(): Map> = groupBy { it.first }.mapValues { it.value.map { it.second } } fun Appendable.render(namespace: String, ifaces: List, unions : GenerateUnionTypes) { - val declaredTypes = ifaces.toMapBy { it.name } + val declaredTypes = ifaces.associateBy { it.name } val allTypes = declaredTypes + unions.anonymousUnionsMap + unions.typedefsMarkersMap declaredTypes.values.filter { it.namespace == namespace }.forEach { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index a20e5fdc5fb..d1bb7b71063 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -111,6 +111,15 @@ fun snapshots(): List { typeParam("V") returns("Map") annotations("""@kotlin.jvm.JvmName("toMapOfPairs")""") + deprecate(Deprecation("Use associate instead.", replaceWith = "associate(transform)")) + } + + templates add f("associate(transform: (T) -> Pair)") { + inline(true) + include(CharSequences) + typeParam("K") + typeParam("V") + returns("Map") doc { f -> """ Returns a [Map] containing key-value pairs provided by [transform] function applied to ${f.element}s of the given ${f.collection}. @@ -159,13 +168,21 @@ fun snapshots(): List { } templates add f("toMapBy(selector: (T) -> K)") { + inline(true) + typeParam("K") + returns("Map") + include(CharSequences) + deprecate(Deprecation("Use associateBy instead.", replaceWith = "associateBy(selector)")) + } + + templates add f("associateBy(keySelector: (T) -> K)") { inline(true) typeParam("K") doc { f -> """ Returns a [Map] containing the ${f.element.pluralize()} from the given ${f.collection} indexed by the key - returned from [selector] function applied to each ${f.element}. - If any two ${f.element.pluralize()} would have the same key returned by [selector] the last one gets added to the map. + returned from [keySelector] function applied to each ${f.element}. + If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map. """ } returns("Map") @@ -180,7 +197,7 @@ fun snapshots(): List { val capacity = (collectionSizeOrDefault(10)/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - result.put(selector(element), element) + result.put(keySelector(element), element) } return result """ @@ -189,7 +206,7 @@ fun snapshots(): List { """ val result = LinkedHashMap() for (element in this) { - result.put(selector(element), element) + result.put(keySelector(element), element) } return result """ @@ -199,7 +216,7 @@ fun snapshots(): List { val capacity = (length/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - result.put(selector(element), element) + result.put(keySelector(element), element) } return result """ @@ -209,7 +226,7 @@ fun snapshots(): List { val capacity = (size/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - result.put(selector(element), element) + result.put(keySelector(element), element) } return result """ @@ -228,17 +245,26 @@ fun snapshots(): List { """ } returns("Map") - deprecate(Deprecation("Use toMapBy instead.", "toMapBy(selector, transform)")) + deprecate(Deprecation("Use associateBy instead.", "associateBy(selector, transform)")) } templates add f("toMapBy(selector: (T) -> K, transform: (T) -> V)") { + inline(true) + typeParam("K") + typeParam("V") + include(CharSequences) + returns("Map") + deprecate(Deprecation("Use associateBy instead.", replaceWith = "associateBy(selector, transform)")) + } + + templates add f("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { inline(true) 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 a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to ${f.element.pluralize()} of the given ${f.collection}. + If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map. """ } returns("Map") @@ -253,7 +279,7 @@ fun snapshots(): List { val capacity = (collectionSizeOrDefault(10)/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - result.put(selector(element), transform(element)) + result.put(keySelector(element), valueTransform(element)) } return result """ @@ -262,7 +288,7 @@ fun snapshots(): List { """ val result = LinkedHashMap() for (element in this) { - result.put(selector(element), transform(element)) + result.put(keySelector(element), valueTransform(element)) } return result """ @@ -272,7 +298,7 @@ fun snapshots(): List { val capacity = (length/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - result.put(selector(element), transform(element)) + result.put(keySelector(element), valueTransform(element)) } return result """ @@ -282,7 +308,7 @@ fun snapshots(): List { val capacity = (size/.75f) + 1 val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - result.put(selector(element), transform(element)) + result.put(keySelector(element), valueTransform(element)) } return result """