diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 069e93ac246..edd9ad1e77a 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -8686,7 +8686,7 @@ public inline fun > CharArray.associateTo(desti @SinceKotlin("1.4") @ExperimentalStdlibApi public inline fun Array.associateWith(valueSelector: (K) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8704,7 +8704,7 @@ public inline fun Array.associateWith(valueSelector: (K) -> V): Ma @ExperimentalStdlibApi @kotlin.internal.InlineOnly public inline fun ByteArray.associateWith(valueSelector: (Byte) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8722,7 +8722,7 @@ public inline fun ByteArray.associateWith(valueSelector: (Byte) -> V): Map ShortArray.associateWith(valueSelector: (Short) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8740,7 +8740,7 @@ public inline fun ShortArray.associateWith(valueSelector: (Short) -> V): Map @ExperimentalStdlibApi @kotlin.internal.InlineOnly public inline fun IntArray.associateWith(valueSelector: (Int) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8758,7 +8758,7 @@ public inline fun IntArray.associateWith(valueSelector: (Int) -> V): Map LongArray.associateWith(valueSelector: (Long) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8776,7 +8776,7 @@ public inline fun LongArray.associateWith(valueSelector: (Long) -> V): Map FloatArray.associateWith(valueSelector: (Float) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8794,7 +8794,7 @@ public inline fun FloatArray.associateWith(valueSelector: (Float) -> V): Map @ExperimentalStdlibApi @kotlin.internal.InlineOnly public inline fun DoubleArray.associateWith(valueSelector: (Double) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8812,7 +8812,7 @@ public inline fun DoubleArray.associateWith(valueSelector: (Double) -> V): M @ExperimentalStdlibApi @kotlin.internal.InlineOnly public inline fun BooleanArray.associateWith(valueSelector: (Boolean) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -8830,7 +8830,7 @@ public inline fun BooleanArray.associateWith(valueSelector: (Boolean) -> V): @ExperimentalStdlibApi @kotlin.internal.InlineOnly public inline fun CharArray.associateWith(valueSelector: (Char) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -9145,7 +9145,7 @@ public fun BooleanArray.toHashSet(): HashSet { * Returns a new [HashSet] of all elements. */ public fun CharArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) + return toCollection(HashSet(mapCapacity(size.coerceAtMost(128)))) } /** @@ -9439,7 +9439,7 @@ public fun CharArray.toSet(): Set { return when (size) { 0 -> emptySet() 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) + else -> toCollection(LinkedHashSet(mapCapacity(size.coerceAtMost(128)))) } } @@ -11168,9 +11168,7 @@ public infix fun CharArray.subtract(other: Iterable): Set { * The returned set preserves the element iteration order of the original array. */ public fun Array.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11179,9 +11177,7 @@ public fun Array.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun ByteArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11190,9 +11186,7 @@ public fun ByteArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun ShortArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11201,9 +11195,7 @@ public fun ShortArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun IntArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11212,9 +11204,7 @@ public fun IntArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun LongArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11223,9 +11213,7 @@ public fun LongArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun FloatArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11234,9 +11222,7 @@ public fun FloatArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun DoubleArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11245,9 +11231,7 @@ public fun DoubleArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun BooleanArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size))) } /** @@ -11256,9 +11240,7 @@ public fun BooleanArray.toMutableSet(): MutableSet { * The returned set preserves the element iteration order of the original array. */ public fun CharArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set + return toCollection(LinkedHashSet(mapCapacity(size.coerceAtMost(128)))) } /** diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index ad59489c882..c6e06bd9274 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -708,7 +708,7 @@ public inline fun > CharSequence.associateTo(de */ @SinceKotlin("1.3") public inline fun CharSequence.associateWith(valueSelector: (Char) -> V): Map { - val result = LinkedHashMap(mapCapacity(length).coerceAtLeast(16)) + val result = LinkedHashMap(mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -742,7 +742,7 @@ public fun > CharSequence.toCollection(destinatio * Returns a new [HashSet] of all characters. */ public fun CharSequence.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(length))) + return toCollection(HashSet(mapCapacity(length.coerceAtMost(128)))) } /** @@ -772,7 +772,7 @@ public fun CharSequence.toSet(): Set { return when (length) { 0 -> emptySet() 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(length))) + else -> toCollection(LinkedHashSet(mapCapacity(length.coerceAtMost(128)))) } } diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 8056fa1d662..024f2205d98 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -4101,7 +4101,7 @@ public inline fun ShortArray.toUShortArray(): UShortArray { @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly public inline fun UIntArray.associateWith(valueSelector: (UInt) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -4120,7 +4120,7 @@ public inline fun UIntArray.associateWith(valueSelector: (UInt) -> V): Map ULongArray.associateWith(valueSelector: (ULong) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -4139,7 +4139,7 @@ public inline fun ULongArray.associateWith(valueSelector: (ULong) -> V): Map @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly public inline fun UByteArray.associateWith(valueSelector: (UByte) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } @@ -4158,7 +4158,7 @@ public inline fun UByteArray.associateWith(valueSelector: (UByte) -> V): Map @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly public inline fun UShortArray.associateWith(valueSelector: (UShort) -> V): Map { - val result = LinkedHashMap() + val result = LinkedHashMap(mapCapacity(size).coerceAtLeast(16)) return associateWithTo(result, valueSelector) } diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 4d4fa7f3322..4afbaf1f2b5 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -8,6 +8,7 @@ package test.text import kotlin.test.* import test.* import test.collections.behaviors.iteratorBehavior +import test.collections.behaviors.setBehavior import test.collections.compare import kotlin.math.sign import kotlin.random.Random @@ -1651,4 +1652,19 @@ ${" "} assertFailsWith { "".elementAt(0) } assertFailsWith { "a c".elementAt(-1) } } + + @Test + fun toHashSet() { + compare(hashSetOf('A', 'B', 'C'), "ACAABBAC".toHashSet()) { setBehavior() } + + buildString { + repeat(100) { append('1') } + append('2') + repeat(100) { append('3') } + append('4') + repeat(100) { append('5') } + }.let { + compare(hashSetOf('1', '2', '3', '4', '5'), it.toHashSet()) { setBehavior() } + } + } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt index 9c95ac83b68..97a1cb0343a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt @@ -5,10 +5,6 @@ package templates -import templates.DocExtensions.collection -import templates.DocExtensions.element -import templates.DocExtensions.mapResult -import templates.DocExtensions.pluralize import templates.Family.* import templates.SequenceClass.* @@ -35,11 +31,8 @@ object SetOps : TemplateGroupBase() { """ } body(ArraysOfObjects, ArraysOfPrimitives) { - """ - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set - """ + val capacity = "size" + if (primitive == PrimitiveType.Char) ".coerceAtMost(128)" else "" + "return toCollection(LinkedHashSet(mapCapacity($capacity)))" } body(Sequences) { """ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 953cf83e49e..b444584825e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -55,7 +55,6 @@ object Snapshots : TemplateGroupBase() { 1 -> setOf(if (this is List) this[0] else iterator().next()) else -> toCollection(LinkedHashSet(mapCapacity(size))) } - } return toCollection(LinkedHashSet()).optimizeReadOnlySet() """ @@ -63,12 +62,13 @@ object Snapshots : TemplateGroupBase() { body(Sequences) { "return toCollection(LinkedHashSet()).optimizeReadOnlySet()" } body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { - val size = if (f == CharSequences) "length" else "size" + val size = f.code.size + val capacity = if (f == CharSequences || primitive == PrimitiveType.Char) "$size.coerceAtMost(128)" else size """ return when ($size) { 0 -> emptySet() 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity($size))) + else -> toCollection(LinkedHashSet(mapCapacity($capacity))) } """ } @@ -82,8 +82,11 @@ object Snapshots : TemplateGroupBase() { returns("HashSet") body { "return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12))))" } body(Sequences) { "return toCollection(HashSet())" } - body(CharSequences) { "return toCollection(HashSet(mapCapacity(length)))" } - body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet(mapCapacity(size)))" } + body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { + val size = f.code.size + val capacity = if (f == CharSequences || primitive == PrimitiveType.Char) "$size.coerceAtMost(128)" else size + "return toCollection(HashSet(mapCapacity($capacity)))" + } } val f_toSortedSet = fn("toSortedSet()") { @@ -467,13 +470,18 @@ object Snapshots : TemplateGroupBase() { else -> "samples.collections.Collections.Transformations.associateWith" }) body { - val resultMap = when (family) { - Iterables -> "LinkedHashMap(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))" - CharSequences -> "LinkedHashMap(mapCapacity(length).coerceAtLeast(16))" - else -> "LinkedHashMap()" + val capacity = when (family) { + Iterables -> "mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)" + CharSequences -> "mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)" + ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned -> if (primitive == PrimitiveType.Char) { + "mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)" + } else { + "mapCapacity(size).coerceAtLeast(16)" + } + else -> "" } """ - val result = $resultMap + val result = LinkedHashMap($capacity) return associateWithTo(result, valueSelector) """ }