Compare commits

...

1 Commits

Author SHA1 Message Date
Abduqodiri Qurbonzoda
42e96482b2 Optimize union function #KT-36682 2020-04-22 03:31:43 +03:00
8 changed files with 106 additions and 128 deletions

View File

@@ -8187,7 +8187,7 @@ public inline fun <T, K, V> Array<out T>.associate(transform: (T) -> Pair<K, V>)
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives
*/
public inline fun <K, V> ByteArray.associate(transform: (Byte) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -8277,7 +8277,7 @@ public inline fun <K, V> DoubleArray.associate(transform: (Double) -> Pair<K, V>
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives
*/
public inline fun <K, V> BooleanArray.associate(transform: (Boolean) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -8292,7 +8292,7 @@ public inline fun <K, V> BooleanArray.associate(transform: (Boolean) -> Pair<K,
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives
*/
public inline fun <K, V> CharArray.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -8322,7 +8322,7 @@ public inline fun <T, K> Array<out T>.associateBy(keySelector: (T) -> K): Map<K,
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy
*/
public inline fun <K> ByteArray.associateBy(keySelector: (Byte) -> K): Map<K, Byte> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Byte>(capacity), keySelector)
}
@@ -8412,7 +8412,7 @@ public inline fun <K> DoubleArray.associateBy(keySelector: (Double) -> K): Map<K
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy
*/
public inline fun <K> BooleanArray.associateBy(keySelector: (Boolean) -> K): Map<K, Boolean> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Boolean>(capacity), keySelector)
}
@@ -8427,7 +8427,7 @@ public inline fun <K> BooleanArray.associateBy(keySelector: (Boolean) -> K): Map
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy
*/
public inline fun <K> CharArray.associateBy(keySelector: (Char) -> K): Map<K, Char> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Char>(capacity), keySelector)
}
@@ -8455,7 +8455,7 @@ public inline fun <T, K, V> Array<out T>.associateBy(keySelector: (T) -> K, valu
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform
*/
public inline fun <K, V> ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -8539,7 +8539,7 @@ public inline fun <K, V> DoubleArray.associateBy(keySelector: (Double) -> K, val
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform
*/
public inline fun <K, V> BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -8553,7 +8553,7 @@ public inline fun <K, V> BooleanArray.associateBy(keySelector: (Boolean) -> K, v
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform
*/
public inline fun <K, V> CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
val capacity = mapCapacity(size).coerceAtLeast(16)
val capacity = mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -9011,7 +9011,7 @@ public inline fun <K, V> Array<out K>.associateWith(valueSelector: (K) -> V): Ma
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> ByteArray.associateWith(valueSelector: (Byte) -> V): Map<Byte, V> {
val result = LinkedHashMap<Byte, V>(mapCapacity(size).coerceAtLeast(16))
val result = LinkedHashMap<Byte, V>(mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
@@ -9119,7 +9119,7 @@ public inline fun <V> DoubleArray.associateWith(valueSelector: (Double) -> V): M
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> BooleanArray.associateWith(valueSelector: (Boolean) -> V): Map<Boolean, V> {
val result = LinkedHashMap<Boolean, V>(mapCapacity(size).coerceAtLeast(16))
val result = LinkedHashMap<Boolean, V>(mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
@@ -9403,7 +9403,7 @@ public fun <T> Array<out T>.toHashSet(): HashSet<T> {
* Returns a new [HashSet] of all elements.
*/
public fun ByteArray.toHashSet(): HashSet<Byte> {
return toCollection(HashSet<Byte>(mapCapacity(size)))
return toCollection(HashSet<Byte>(mapCapacity(size.coerceAtMost(256))))
}
/**
@@ -9445,7 +9445,7 @@ public fun DoubleArray.toHashSet(): HashSet<Double> {
* Returns a new [HashSet] of all elements.
*/
public fun BooleanArray.toHashSet(): HashSet<Boolean> {
return toCollection(HashSet<Boolean>(mapCapacity(size)))
return toCollection(HashSet<Boolean>(mapCapacity(size.coerceAtMost(2))))
}
/**
@@ -9655,7 +9655,7 @@ public fun ByteArray.toSet(): Set<Byte> {
return when (size) {
0 -> emptySet()
1 -> setOf(this[0])
else -> toCollection(LinkedHashSet<Byte>(mapCapacity(size)))
else -> toCollection(LinkedHashSet<Byte>(mapCapacity(size.coerceAtMost(256))))
}
}
@@ -9733,7 +9733,7 @@ public fun BooleanArray.toSet(): Set<Boolean> {
return when (size) {
0 -> emptySet()
1 -> setOf(this[0])
else -> toCollection(LinkedHashSet<Boolean>(mapCapacity(size)))
else -> toCollection(LinkedHashSet<Boolean>(mapCapacity(size.coerceAtMost(2))))
}
}
@@ -9939,7 +9939,7 @@ public inline fun <R, C : MutableCollection<in R>> CharArray.flatMapTo(destinati
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <T, K> Array<out T>.groupBy(keySelector: (T) -> K): Map<K, List<T>> {
return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<T>>(mapCapacity(size)), keySelector)
}
/**
@@ -9951,7 +9951,7 @@ public inline fun <T, K> Array<out T>.groupBy(keySelector: (T) -> K): Map<K, Lis
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> ByteArray.groupBy(keySelector: (Byte) -> K): Map<K, List<Byte>> {
return groupByTo(LinkedHashMap<K, MutableList<Byte>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Byte>>(mapCapacity(size.coerceAtMost(256))), keySelector)
}
/**
@@ -9963,7 +9963,7 @@ public inline fun <K> ByteArray.groupBy(keySelector: (Byte) -> K): Map<K, List<B
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> ShortArray.groupBy(keySelector: (Short) -> K): Map<K, List<Short>> {
return groupByTo(LinkedHashMap<K, MutableList<Short>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Short>>(mapCapacity(size)), keySelector)
}
/**
@@ -9975,7 +9975,7 @@ public inline fun <K> ShortArray.groupBy(keySelector: (Short) -> K): Map<K, List
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> IntArray.groupBy(keySelector: (Int) -> K): Map<K, List<Int>> {
return groupByTo(LinkedHashMap<K, MutableList<Int>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Int>>(mapCapacity(size)), keySelector)
}
/**
@@ -9987,7 +9987,7 @@ public inline fun <K> IntArray.groupBy(keySelector: (Int) -> K): Map<K, List<Int
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> LongArray.groupBy(keySelector: (Long) -> K): Map<K, List<Long>> {
return groupByTo(LinkedHashMap<K, MutableList<Long>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Long>>(mapCapacity(size)), keySelector)
}
/**
@@ -9999,7 +9999,7 @@ public inline fun <K> LongArray.groupBy(keySelector: (Long) -> K): Map<K, List<L
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> FloatArray.groupBy(keySelector: (Float) -> K): Map<K, List<Float>> {
return groupByTo(LinkedHashMap<K, MutableList<Float>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Float>>(mapCapacity(size)), keySelector)
}
/**
@@ -10011,7 +10011,7 @@ public inline fun <K> FloatArray.groupBy(keySelector: (Float) -> K): Map<K, List
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> DoubleArray.groupBy(keySelector: (Double) -> K): Map<K, List<Double>> {
return groupByTo(LinkedHashMap<K, MutableList<Double>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Double>>(mapCapacity(size)), keySelector)
}
/**
@@ -10023,7 +10023,7 @@ public inline fun <K> DoubleArray.groupBy(keySelector: (Double) -> K): Map<K, Li
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> BooleanArray.groupBy(keySelector: (Boolean) -> K): Map<K, List<Boolean>> {
return groupByTo(LinkedHashMap<K, MutableList<Boolean>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Boolean>>(mapCapacity(size.coerceAtMost(2))), keySelector)
}
/**
@@ -10035,7 +10035,7 @@ public inline fun <K> BooleanArray.groupBy(keySelector: (Boolean) -> K): Map<K,
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> CharArray.groupBy(keySelector: (Char) -> K): Map<K, List<Char>> {
return groupByTo(LinkedHashMap<K, MutableList<Char>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Char>>(mapCapacity(size.coerceAtMost(128))), keySelector)
}
/**
@@ -10048,7 +10048,7 @@ public inline fun <K> CharArray.groupBy(keySelector: (Char) -> K): Map<K, List<C
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <T, K, V> Array<out T>.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10061,7 +10061,7 @@ public inline fun <T, K, V> Array<out T>.groupBy(keySelector: (T) -> K, valueTra
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(256))), keySelector, valueTransform)
}
/**
@@ -10074,7 +10074,7 @@ public inline fun <K, V> ByteArray.groupBy(keySelector: (Byte) -> K, valueTransf
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10087,7 +10087,7 @@ public inline fun <K, V> ShortArray.groupBy(keySelector: (Short) -> K, valueTran
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10100,7 +10100,7 @@ public inline fun <K, V> IntArray.groupBy(keySelector: (Int) -> K, valueTransfor
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10113,7 +10113,7 @@ public inline fun <K, V> LongArray.groupBy(keySelector: (Long) -> K, valueTransf
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10126,7 +10126,7 @@ public inline fun <K, V> FloatArray.groupBy(keySelector: (Float) -> K, valueTran
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10139,7 +10139,7 @@ public inline fun <K, V> DoubleArray.groupBy(keySelector: (Double) -> K, valueTr
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(2))), keySelector, valueTransform)
}
/**
@@ -10152,7 +10152,7 @@ public inline fun <K, V> BooleanArray.groupBy(keySelector: (Boolean) -> K, value
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(128))), keySelector, valueTransform)
}
/**
@@ -11484,7 +11484,7 @@ public fun <T> Array<out T>.toMutableSet(): MutableSet<T> {
* The returned set preserves the element iteration order of the original array.
*/
public fun ByteArray.toMutableSet(): MutableSet<Byte> {
return toCollection(LinkedHashSet<Byte>(mapCapacity(size)))
return toCollection(LinkedHashSet<Byte>(mapCapacity(size.coerceAtMost(256))))
}
/**
@@ -11538,7 +11538,7 @@ public fun DoubleArray.toMutableSet(): MutableSet<Double> {
* The returned set preserves the element iteration order of the original array.
*/
public fun BooleanArray.toMutableSet(): MutableSet<Boolean> {
return toCollection(LinkedHashSet<Boolean>(mapCapacity(size)))
return toCollection(LinkedHashSet<Boolean>(mapCapacity(size.coerceAtMost(2))))
}
/**
@@ -11560,7 +11560,8 @@ public fun CharArray.toMutableSet(): MutableSet<Char> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun <T> Array<out T>.union(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
val capacity = size + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<T>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11575,7 +11576,8 @@ public infix fun <T> Array<out T>.union(other: Iterable<T>): Set<T> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun ByteArray.union(other: Iterable<Byte>): Set<Byte> {
val set = this.toMutableSet()
val capacity = size.coerceAtMost(256) + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Byte>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11590,7 +11592,8 @@ public infix fun ByteArray.union(other: Iterable<Byte>): Set<Byte> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun ShortArray.union(other: Iterable<Short>): Set<Short> {
val set = this.toMutableSet()
val capacity = size + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Short>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11605,7 +11608,8 @@ public infix fun ShortArray.union(other: Iterable<Short>): Set<Short> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun IntArray.union(other: Iterable<Int>): Set<Int> {
val set = this.toMutableSet()
val capacity = size + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Int>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11620,7 +11624,8 @@ public infix fun IntArray.union(other: Iterable<Int>): Set<Int> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun LongArray.union(other: Iterable<Long>): Set<Long> {
val set = this.toMutableSet()
val capacity = size + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Long>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11635,7 +11640,8 @@ public infix fun LongArray.union(other: Iterable<Long>): Set<Long> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun FloatArray.union(other: Iterable<Float>): Set<Float> {
val set = this.toMutableSet()
val capacity = size + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Float>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11650,7 +11656,8 @@ public infix fun FloatArray.union(other: Iterable<Float>): Set<Float> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun DoubleArray.union(other: Iterable<Double>): Set<Double> {
val set = this.toMutableSet()
val capacity = size + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Double>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11665,7 +11672,8 @@ public infix fun DoubleArray.union(other: Iterable<Double>): Set<Double> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun BooleanArray.union(other: Iterable<Boolean>): Set<Boolean> {
val set = this.toMutableSet()
val capacity = size.coerceAtMost(2) + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Boolean>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11680,7 +11688,8 @@ public infix fun BooleanArray.union(other: Iterable<Boolean>): Set<Boolean> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun CharArray.union(other: Iterable<Char>): Set<Char> {
val set = this.toMutableSet()
val capacity = size.coerceAtMost(128) + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<Char>(mapCapacity(capacity)))
set.addAll(other)
return set
}

View File

@@ -1226,7 +1226,7 @@ public fun <T, C : MutableCollection<in T>> Iterable<T>.toCollection(destination
* Returns a new [HashSet] of all elements.
*/
public fun <T> Iterable<T>.toHashSet(): HashSet<T> {
return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))
return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(10))))
}
/**
@@ -1304,7 +1304,7 @@ public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.flatMapTo(dest
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <T, K> Iterable<T>.groupBy(keySelector: (T) -> K): Map<K, List<T>> {
return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<T>>(mapCapacity(collectionSizeOrDefault(10))), keySelector)
}
/**
@@ -1317,7 +1317,7 @@ public inline fun <T, K> Iterable<T>.groupBy(keySelector: (T) -> K): Map<K, List
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <T, K, V> Iterable<T>.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(collectionSizeOrDefault(10))), keySelector, valueTransform)
}
/**
@@ -1536,7 +1536,8 @@ public fun <T> Iterable<T>.toMutableSet(): MutableSet<T> {
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
val capacity = collectionSizeOrDefault(10) + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<T>(mapCapacity(capacity)))
set.addAll(other)
return set
}

View File

@@ -616,7 +616,7 @@ public inline fun String.reversed(): String {
* @sample samples.text.Strings.associate
*/
public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(length).coerceAtLeast(16)
val capacity = mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -631,7 +631,7 @@ public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>)
* @sample samples.text.Strings.associateBy
*/
public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K, Char> {
val capacity = mapCapacity(length).coerceAtLeast(16)
val capacity = mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Char>(capacity), keySelector)
}
@@ -645,7 +645,7 @@ public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K,
* @sample samples.text.Strings.associateByWithValueTransform
*/
public inline fun <K, V> CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
val capacity = mapCapacity(length).coerceAtLeast(16)
val capacity = mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -805,7 +805,7 @@ public inline fun <R, C : MutableCollection<in R>> CharSequence.flatMapTo(destin
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> CharSequence.groupBy(keySelector: (Char) -> K): Map<K, List<Char>> {
return groupByTo(LinkedHashMap<K, MutableList<Char>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<Char>>(mapCapacity(length.coerceAtMost(128))), keySelector)
}
/**
@@ -818,7 +818,7 @@ public inline fun <K> CharSequence.groupBy(keySelector: (Char) -> K): Map<K, Lis
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(length.coerceAtMost(128))), keySelector, valueTransform)
}
/**

View File

@@ -4263,7 +4263,7 @@ public inline fun <V> ULongArray.associateWith(valueSelector: (ULong) -> V): Map
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <V> UByteArray.associateWith(valueSelector: (UByte) -> V): Map<UByte, V> {
val result = LinkedHashMap<UByte, V>(mapCapacity(size).coerceAtLeast(16))
val result = LinkedHashMap<UByte, V>(mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
@@ -4478,7 +4478,7 @@ public inline fun <R, C : MutableCollection<in R>> UShortArray.flatMapTo(destina
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> UIntArray.groupBy(keySelector: (UInt) -> K): Map<K, List<UInt>> {
return groupByTo(LinkedHashMap<K, MutableList<UInt>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<UInt>>(mapCapacity(size)), keySelector)
}
/**
@@ -4493,7 +4493,7 @@ public inline fun <K> UIntArray.groupBy(keySelector: (UInt) -> K): Map<K, List<U
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> ULongArray.groupBy(keySelector: (ULong) -> K): Map<K, List<ULong>> {
return groupByTo(LinkedHashMap<K, MutableList<ULong>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<ULong>>(mapCapacity(size)), keySelector)
}
/**
@@ -4508,7 +4508,7 @@ public inline fun <K> ULongArray.groupBy(keySelector: (ULong) -> K): Map<K, List
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> UByteArray.groupBy(keySelector: (UByte) -> K): Map<K, List<UByte>> {
return groupByTo(LinkedHashMap<K, MutableList<UByte>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<UByte>>(mapCapacity(size.coerceAtMost(256))), keySelector)
}
/**
@@ -4523,7 +4523,7 @@ public inline fun <K> UByteArray.groupBy(keySelector: (UByte) -> K): Map<K, List
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> UShortArray.groupBy(keySelector: (UShort) -> K): Map<K, List<UShort>> {
return groupByTo(LinkedHashMap<K, MutableList<UShort>>(), keySelector)
return groupByTo(LinkedHashMap<K, MutableList<UShort>>(mapCapacity(size)), keySelector)
}
/**
@@ -4539,7 +4539,7 @@ public inline fun <K> UShortArray.groupBy(keySelector: (UShort) -> K): Map<K, Li
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> UIntArray.groupBy(keySelector: (UInt) -> K, valueTransform: (UInt) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -4555,7 +4555,7 @@ public inline fun <K, V> UIntArray.groupBy(keySelector: (UInt) -> K, valueTransf
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> ULongArray.groupBy(keySelector: (ULong) -> K, valueTransform: (ULong) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -4571,7 +4571,7 @@ public inline fun <K, V> ULongArray.groupBy(keySelector: (ULong) -> K, valueTran
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> UByteArray.groupBy(keySelector: (UByte) -> K, valueTransform: (UByte) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(256))), keySelector, valueTransform)
}
/**
@@ -4587,7 +4587,7 @@ public inline fun <K, V> UByteArray.groupBy(keySelector: (UByte) -> K, valueTran
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> UShortArray.groupBy(keySelector: (UShort) -> K, valueTransform: (UShort) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**

View File

@@ -351,7 +351,10 @@ object Mapping : TemplateGroupBase() {
sequenceClassification(terminal)
typeParam("K")
returns("Map<K, List<T>>")
body { "return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)" }
body {
val capacity = if (f == Sequences) "" else "mapCapacity(${f.code.toSetSize(primitive)})"
"return groupByTo(LinkedHashMap<K, MutableList<T>>($capacity), keySelector)"
}
}
val f_groupByTo_key = fn("groupByTo(destination: M, keySelector: (T) -> K)") {
@@ -407,7 +410,10 @@ object Mapping : TemplateGroupBase() {
typeParam("K")
typeParam("V")
returns("Map<K, List<V>>")
body { "return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)" }
body {
val capacity = if (f == Sequences) "" else "mapCapacity(${f.code.toSetSize(primitive)})"
"return groupByTo(LinkedHashMap<K, MutableList<V>>($capacity), keySelector, valueTransform)"
}
}

View File

@@ -31,8 +31,7 @@ object SetOps : TemplateGroupBase() {
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
val capacity = "size" + if (primitive == PrimitiveType.Char) ".coerceAtMost(128)" else ""
"return toCollection(LinkedHashSet<T>(mapCapacity($capacity)))"
"return toCollection(LinkedHashSet<T>(mapCapacity(${f.code.toSetSize(primitive)})))"
}
body(Sequences) {
"""
@@ -122,7 +121,8 @@ object SetOps : TemplateGroupBase() {
returns("Set<T>")
body {
"""
val set = this.toMutableSet()
val capacity = ${f.code.toSetSize(primitive)} + other.collectionSizeOrDefault(10)
val set = toCollection(LinkedHashSet<T>(mapCapacity(capacity)))
set.addAll(other)
return set
"""

View File

@@ -62,13 +62,11 @@ object Snapshots : TemplateGroupBase() {
body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
val size = f.code.size
val capacity = if (f == CharSequences || primitive == PrimitiveType.Char) "$size.coerceAtMost(128)" else size
"""
return when ($size) {
return when (${f.code.size}) {
0 -> emptySet()
1 -> setOf(this[0])
else -> toCollection(LinkedHashSet<T>(mapCapacity($capacity)))
else -> toCollection(LinkedHashSet<T>(mapCapacity(${f.code.toSetSize(primitive)})))
}
"""
}
@@ -80,12 +78,9 @@ object Snapshots : TemplateGroupBase() {
} builder {
doc { "Returns a new [HashSet] of all ${f.element.pluralize()}." }
returns("HashSet<T>")
body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(HashSet<T>())" }
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<T>(mapCapacity($capacity)))"
body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"return toCollection(HashSet<T>(mapCapacity(${f.code.toSetSize(primitive)})))"
}
}
@@ -216,9 +211,9 @@ object Snapshots : TemplateGroupBase() {
ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitives"
else -> "samples.collections.Collections.Transformations.associate"
})
body {
body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
val capacity = mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
"""
}
@@ -227,18 +222,6 @@ object Snapshots : TemplateGroupBase() {
return associateTo(LinkedHashMap<K, V>(), transform)
"""
}
body(CharSequences) {
"""
val capacity = mapCapacity(length).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val capacity = mapCapacity(size).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
"""
}
}
val f_associateTo = fn("associateTo(destination: M, transform: (T) -> Pair<K, V>)") {
@@ -298,9 +281,9 @@ object Snapshots : TemplateGroupBase() {
// Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection
// constructor.
body {
body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
val capacity = mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
"""
}
@@ -309,18 +292,6 @@ object Snapshots : TemplateGroupBase() {
return associateByTo(LinkedHashMap<K, T>(), keySelector)
"""
}
body(CharSequences) {
"""
val capacity = mapCapacity(length).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val capacity = mapCapacity(size).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
"""
}
}
val f_associateByTo_key = fn("associateByTo(destination: M, keySelector: (T) -> K)") {
@@ -383,9 +354,9 @@ object Snapshots : TemplateGroupBase() {
* constructor.
*/
body {
body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
val capacity = mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
"""
}
@@ -394,18 +365,6 @@ object Snapshots : TemplateGroupBase() {
return associateByTo(LinkedHashMap<K, V>(), keySelector, valueTransform)
"""
}
body(CharSequences) {
"""
val capacity = mapCapacity(length).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val capacity = mapCapacity(size).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
"""
}
}
val f_associateByTo_key_value = fn("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") {
@@ -470,16 +429,7 @@ object Snapshots : TemplateGroupBase() {
else -> "samples.collections.Collections.Transformations.associateWith"
})
body {
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 capacity = if (f == Sequences) "" else "mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)"
"""
val result = LinkedHashMap<K, V>($capacity)
return associateWithTo(result, valueSelector)

View File

@@ -20,6 +20,18 @@ val Family.CodeExtension.size: String
else -> error("size property isn't supported for $family")
}
fun Family.CodeExtension.toSetSize(primitive: PrimitiveType?): String = when (family) {
Iterables -> "collectionSizeOrDefault(10)"
Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects -> "size"
ArraysOfPrimitives, ArraysOfUnsigned ->
if (primitive == PrimitiveType.Boolean) "size.coerceAtMost(2)"
else if (primitive == PrimitiveType.Char) "size.coerceAtMost(128)"
else if (primitive == PrimitiveType.Byte || primitive == PrimitiveType.UByte) "size.coerceAtMost(256)"
else "size"
CharSequences, Strings -> "length.coerceAtMost(128)"
else -> error("size property isn't supported for $family")
}
object DocExtensions {
val Family.element: String