Optimize snapshot operations to return special collection implementations when result is empty or has single element.
#KT-9990 Fixed
This commit is contained in:
@@ -6308,63 +6308,99 @@ public fun CharArray.toHashSet(): HashSet<Char> {
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun <T> Array<out T>.toList(): List<T> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun ByteArray.toList(): List<Byte> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun ShortArray.toList(): List<Short> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun IntArray.toList(): List<Int> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun LongArray.toList(): List<Long> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun FloatArray.toList(): List<Float> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun DoubleArray.toList(): List<Double> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun BooleanArray.toList(): List<Boolean> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun CharArray.toList(): List<Char> {
|
||||
return this.toMutableList()
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -6450,63 +6486,99 @@ public fun CharArray.toMutableList(): MutableList<Char> {
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun <T> Array<out T>.toSet(): Set<T> {
|
||||
return toCollection(LinkedHashSet<T>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<T>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun ByteArray.toSet(): Set<Byte> {
|
||||
return toCollection(LinkedHashSet<Byte>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Byte>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun ShortArray.toSet(): Set<Short> {
|
||||
return toCollection(LinkedHashSet<Short>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Short>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun IntArray.toSet(): Set<Int> {
|
||||
return toCollection(LinkedHashSet<Int>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Int>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun LongArray.toSet(): Set<Long> {
|
||||
return toCollection(LinkedHashSet<Long>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Long>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun FloatArray.toSet(): Set<Float> {
|
||||
return toCollection(LinkedHashSet<Float>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Float>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun DoubleArray.toSet(): Set<Double> {
|
||||
return toCollection(LinkedHashSet<Double>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Double>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun BooleanArray.toSet(): Set<Boolean> {
|
||||
return toCollection(LinkedHashSet<Boolean>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Boolean>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun CharArray.toSet(): Set<Char> {
|
||||
return toCollection(LinkedHashSet<Char>(mapCapacity(size)))
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Char>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1003,7 +1003,14 @@ public fun <T> Iterable<T>.toHashSet(): HashSet<T> {
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun <T> Iterable<T>.toList(): List<T> {
|
||||
return this.toMutableList()
|
||||
if (this is Collection) {
|
||||
return when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(if (this is List) get(0) else iterator().next())
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
return this.toMutableList().optimizeReadOnlyList()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1026,7 +1033,14 @@ public fun <T> Collection<T>.toMutableList(): MutableList<T> {
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun <T> Iterable<T>.toSet(): Set<T> {
|
||||
return toCollection(LinkedHashSet<T>(mapCapacity(collectionSizeOrDefault(12))))
|
||||
if (this is Collection) {
|
||||
return when (size) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(if (this is List) this[0] else iterator().next())
|
||||
else -> toCollection(LinkedHashSet<T>(mapCapacity(size)))
|
||||
}
|
||||
}
|
||||
return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,9 +17,19 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col
|
||||
* Returns a [List] containing all key-value pairs.
|
||||
*/
|
||||
public fun <K, V> Map<K, V>.toList(): List<Pair<K, V>> {
|
||||
if (size == 0)
|
||||
return emptyList()
|
||||
val iterator = entries.iterator()
|
||||
if (!iterator.hasNext())
|
||||
return emptyList()
|
||||
val first = iterator.next()
|
||||
if (!iterator.hasNext())
|
||||
return listOf(first.toPair())
|
||||
val result = ArrayList<Pair<K, V>>(size)
|
||||
for (item in this)
|
||||
result.add(item.key to item.value)
|
||||
result.add(first.toPair())
|
||||
do {
|
||||
result.add(iterator.next().toPair())
|
||||
} while (iterator.hasNext())
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
@@ -522,7 +522,7 @@ public fun <T> Sequence<T>.toHashSet(): HashSet<T> {
|
||||
* Returns a [List] containing all elements.
|
||||
*/
|
||||
public fun <T> Sequence<T>.toList(): List<T> {
|
||||
return this.toMutableList()
|
||||
return this.toMutableList().optimizeReadOnlyList()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -536,7 +536,7 @@ public fun <T> Sequence<T>.toMutableList(): MutableList<T> {
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
public fun <T> Sequence<T>.toSet(): Set<T> {
|
||||
return toCollection(LinkedHashSet<T>())
|
||||
return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -590,7 +590,11 @@ public fun CharSequence.toHashSet(): HashSet<Char> {
|
||||
* Returns a [List] containing all characters.
|
||||
*/
|
||||
public fun CharSequence.toList(): List<Char> {
|
||||
return this.toMutableList()
|
||||
return when (length) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(this[0])
|
||||
else -> this.toMutableList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -604,7 +608,11 @@ public fun CharSequence.toMutableList(): MutableList<Char> {
|
||||
* Returns a [Set] of all characters.
|
||||
*/
|
||||
public fun CharSequence.toSet(): Set<Char> {
|
||||
return toCollection(LinkedHashSet<Char>(mapCapacity(length)))
|
||||
return when (length) {
|
||||
0 -> emptySet()
|
||||
1 -> setOf(this[0])
|
||||
else -> toCollection(LinkedHashSet<Char>(mapCapacity(length)))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user