diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index d9f5497395b..dc0c923cc12 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -5414,120 +5414,93 @@ public fun Array.toShortArray(): ShortArray { } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array. + * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** @@ -5536,12 +5509,8 @@ public inline fun ShortArray.associate(transform: (Short) -> Pair): * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5550,12 +5519,8 @@ public inline fun Array.associateBy(keySelector: (T) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5564,12 +5529,8 @@ public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5578,12 +5539,8 @@ public inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5592,12 +5549,8 @@ public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5606,12 +5559,8 @@ public inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5620,12 +5569,8 @@ public inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5634,12 +5579,8 @@ public inline fun IntArray.associateBy(keySelector: (Int) -> K): Map * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5648,12 +5589,8 @@ public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -5661,12 +5598,8 @@ public inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5674,12 +5607,8 @@ public inline fun Array.associateBy(keySelector: (T) -> K, valu * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5687,12 +5616,8 @@ public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, v * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5700,12 +5625,8 @@ public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTr * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5713,12 +5634,8 @@ public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTr * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5726,12 +5643,8 @@ public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, val * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5739,12 +5652,8 @@ public inline fun FloatArray.associateBy(keySelector: (Float) -> K, value * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5752,12 +5661,8 @@ public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTran * 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 + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) } /** @@ -5765,12 +5670,350 @@ public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTr * 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)) + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { - result.put(keySelector(element), valueTransform(element)) + destination.put(keySelector(element), element) } - return result + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given array + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (T) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Boolean) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Byte) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Char) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Double) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Float) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Int) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Long) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (Short) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination } /** diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index f148279e9f1..d41a3b52080 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -914,16 +914,13 @@ public fun Collection.toShortArray(): ShortArray { } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given collection. + * 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 + val capacity = ((collectionSizeOrDefault(10)/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** @@ -932,12 +929,8 @@ public inline fun Iterable.associate(transform: (T) -> Pair): * 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 + val capacity = ((collectionSizeOrDefault(10)/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -945,12 +938,46 @@ public inline fun Iterable.associateBy(keySelector: (T) -> K): Map Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - val capacity = (collectionSizeOrDefault(10)/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + val capacity = ((collectionSizeOrDefault(10)/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given collection + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { - result.put(keySelector(element), valueTransform(element)) + destination.put(keySelector(element), element) } - return result + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (T) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination } /** diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 847f51b6f6f..376afded6b2 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -423,15 +423,12 @@ 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. + * 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 + return associateTo(LinkedHashMap(), transform) } /** @@ -440,11 +437,7 @@ public inline fun Sequence.associate(transform: (T) -> Pair): * 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 + return associateByTo(LinkedHashMap(), keySelector) } /** @@ -452,11 +445,45 @@ public inline fun Sequence.associateBy(keySelector: (T) -> K): Map Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - val result = LinkedHashMap() + return associateByTo(LinkedHashMap(), keySelector, valueTransform) +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each element of the given sequence + * and value is the element itself. + * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K): M { for (element in this) { - result.put(keySelector(element), valueTransform(element)) + destination.put(keySelector(element), element) } - return result + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each element 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.associateTo(destination: M, transform: (T) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination } /** diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 99a62cfbdaa..42b680d008f 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -492,16 +492,13 @@ public fun String.reversed(): String { } /** - * Returns a [Map] containing key-value pairs provided by [transform] function applied to characters of the given char sequence. + * 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 + val capacity = ((length/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) } /** @@ -510,12 +507,8 @@ public inline fun CharSequence.associate(transform: (Char) -> Pair) * 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 + val capacity = ((length/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) } /** @@ -523,12 +516,46 @@ public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { - val capacity = (length/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + val capacity = ((length/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function applied to each character of the given char sequence + * and value is the character itself. + * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. + */ +public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M { for (element in this) { - result.put(keySelector(element), valueTransform(element)) + destination.put(keySelector(element), element) } - return result + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs, + * where key is provided by the [keySelector] function and + * and value is provided by the [valueTransform] function 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.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} + +/** + * Populates and returns the [destination] mutable map with key-value pairs + * provided by [transform] function applied to each character 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.associateTo(destination: M, transform: (Char) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination } /** diff --git a/libraries/stdlib/test/collections/MapJVMTest.kt b/libraries/stdlib/test/collections/MapJVMTest.kt index 9a4141692f2..e8c23b6079f 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).associateBy({ it }, { 'a' + it }).toLinkedMap() // TODO: use associateByTo(linkedMapOf()) + val map = (1..5).associateByTo(linkedMapOf(), { it }, { 'a' + it }) val iterator = map.iterator() while (iterator.hasNext()) { if (iterator.next().key % 2 == 0) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index d1bb7b71063..0b5cf676c8b 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -122,47 +122,56 @@ fun snapshots(): List { 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}. + Returns a [Map] containing key-value pairs provided by [transform] function + applied to ${f.element.pluralize()} of the given ${f.collection}. If any of two pairs would have the same key the last one gets added to the map. """ } body { """ - val capacity = (collectionSizeOrDefault(10)/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result += transform(element) - } - return result + val capacity = ((collectionSizeOrDefault(10)/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) """ } body(Sequences) { """ - val result = LinkedHashMap() - for (element in this) { - result += transform(element) - } - return result + return associateTo(LinkedHashMap(), transform) """ } body(CharSequences) { """ - val capacity = (length/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result += transform(element) - } - return result + val capacity = ((length/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) """ } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) + """ + } + } + + templates add f("associateTo(destination: M, transform: (T) -> Pair)") { + inline(true) + include(CharSequences) + typeParam("K") + typeParam("V") + typeParam("M : MutableMap") + returns("M") + doc { f -> + """ + Populates and returns the [destination] mutable map with key-value pairs + provided by [transform] function applied to each ${f.element} of the given ${f.collection}. + If any of two pairs would have the same key the last one gets added to the map. + """ + } + body { + """ for (element in this) { - result += transform(element) + destination += transform(element) } - return result + return destination """ } } @@ -177,6 +186,7 @@ fun snapshots(): List { templates add f("associateBy(keySelector: (T) -> K)") { inline(true) + include(CharSequences) typeParam("K") doc { f -> """ @@ -194,41 +204,49 @@ fun snapshots(): List { body { """ - 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 + val capacity = ((collectionSizeOrDefault(10)/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) """ } body(Sequences) { """ - val result = LinkedHashMap() - for (element in this) { - result.put(keySelector(element), element) - } - return result + return associateByTo(LinkedHashMap(), keySelector) """ } body(CharSequences) { """ - val capacity = (length/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(keySelector(element), element) - } - return result + val capacity = ((length/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) """ } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) + """ + } + } + + templates add f("associateByTo(destination: M, keySelector: (T) -> K)") { + inline(true) + include(CharSequences) + typeParam("K") + typeParam("M : MutableMap") + returns("M") + doc { f -> + """ + Populates and returns the [destination] mutable map with key-value pairs, + where key is provided by the [keySelector] function applied to each ${f.element} of the given ${f.collection} + and value is the ${f.element} itself. + If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map. + """ + } + body { + """ for (element in this) { - result.put(keySelector(element), element) + destination.put(keySelector(element), element) } - return result + return destination """ } } @@ -259,6 +277,7 @@ fun snapshots(): List { templates add f("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { inline(true) + include(CharSequences) typeParam("K") typeParam("V") doc { f -> @@ -276,41 +295,51 @@ fun snapshots(): List { body { """ - 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 + val capacity = ((collectionSizeOrDefault(10)/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) """ } body(Sequences) { """ - val result = LinkedHashMap() - for (element in this) { - result.put(keySelector(element), valueTransform(element)) - } - return result + return associateByTo(LinkedHashMap(), keySelector, valueTransform) """ } body(CharSequences) { """ - 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 + val capacity = ((length/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) """ } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val capacity = (size/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + val capacity = ((size/.75f) + 1).toInt().coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) + """ + } + } + + templates add f("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { + inline(true) + include(CharSequences) + typeParam("K") + typeParam("V") + typeParam("M : MutableMap") + returns("M") + + doc { f -> + """ + Populates and returns the [destination] mutable map with key-value pairs, + where key is provided by the [keySelector] function and + and value is provided by the [valueTransform] function 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. + """ + } + body { + """ for (element in this) { - result.put(keySelector(element), valueTransform(element)) + destination.put(keySelector(element), valueTransform(element)) } - return result + return destination """ } }