Revert toArrayList for primitive arrays back to trivial implementation.
Use newly introduced Int.MAX_VALUE in JS. Inline mapCapacityForValues function. Precalculate capacity of linked hash set being created with toMutableSet function.
This commit is contained in:
@@ -264,7 +264,7 @@ public fun <T> Iterable<T>.subtract(other: Iterable<T>): Set<T> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun <T> Array<out T>.toMutableSet(): MutableSet<T> {
|
||||
val set = LinkedHashSet<T>(size())
|
||||
val set = LinkedHashSet<T>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -273,7 +273,7 @@ public fun <T> Array<out T>.toMutableSet(): MutableSet<T> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun BooleanArray.toMutableSet(): MutableSet<Boolean> {
|
||||
val set = LinkedHashSet<Boolean>(size())
|
||||
val set = LinkedHashSet<Boolean>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -282,7 +282,7 @@ public fun BooleanArray.toMutableSet(): MutableSet<Boolean> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun ByteArray.toMutableSet(): MutableSet<Byte> {
|
||||
val set = LinkedHashSet<Byte>(size())
|
||||
val set = LinkedHashSet<Byte>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -291,7 +291,7 @@ public fun ByteArray.toMutableSet(): MutableSet<Byte> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun CharArray.toMutableSet(): MutableSet<Char> {
|
||||
val set = LinkedHashSet<Char>(size())
|
||||
val set = LinkedHashSet<Char>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -300,7 +300,7 @@ public fun CharArray.toMutableSet(): MutableSet<Char> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun DoubleArray.toMutableSet(): MutableSet<Double> {
|
||||
val set = LinkedHashSet<Double>(size())
|
||||
val set = LinkedHashSet<Double>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -309,7 +309,7 @@ public fun DoubleArray.toMutableSet(): MutableSet<Double> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun FloatArray.toMutableSet(): MutableSet<Float> {
|
||||
val set = LinkedHashSet<Float>(size())
|
||||
val set = LinkedHashSet<Float>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -318,7 +318,7 @@ public fun FloatArray.toMutableSet(): MutableSet<Float> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun IntArray.toMutableSet(): MutableSet<Int> {
|
||||
val set = LinkedHashSet<Int>(size())
|
||||
val set = LinkedHashSet<Int>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -327,7 +327,7 @@ public fun IntArray.toMutableSet(): MutableSet<Int> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun LongArray.toMutableSet(): MutableSet<Long> {
|
||||
val set = LinkedHashSet<Long>(size())
|
||||
val set = LinkedHashSet<Long>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
@@ -336,7 +336,7 @@ public fun LongArray.toMutableSet(): MutableSet<Long> {
|
||||
* Returns a mutable set containing all distinct elements from the given collection.
|
||||
*/
|
||||
public fun ShortArray.toMutableSet(): MutableSet<Short> {
|
||||
val set = LinkedHashSet<Short>(size())
|
||||
val set = LinkedHashSet<Short>(mapCapacity(size()))
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
}
|
||||
|
||||
@@ -21,56 +21,72 @@ public fun <T> Array<out T>.toArrayList(): ArrayList<T> {
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun BooleanArray.toArrayList(): ArrayList<Boolean> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Boolean>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun ByteArray.toArrayList(): ArrayList<Byte> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Byte>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun CharArray.toArrayList(): ArrayList<Char> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Char>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun DoubleArray.toArrayList(): ArrayList<Double> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Double>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun FloatArray.toArrayList(): ArrayList<Float> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Float>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun IntArray.toArrayList(): ArrayList<Int> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Int>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun LongArray.toArrayList(): ArrayList<Long> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Long>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun ShortArray.toArrayList(): ArrayList<Short> {
|
||||
return this.asList().toArrayList()
|
||||
val list = ArrayList<Short>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,6 +100,8 @@ public fun <T> Collection<T>.toArrayList(): ArrayList<T> {
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun <T> Iterable<T>.toArrayList(): ArrayList<T> {
|
||||
if (this is Collection<T>)
|
||||
return this.toArrayList()
|
||||
return toCollection(ArrayList<T>())
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public fun listOf<T>(vararg values: T): List<T> = if (values.size() == 0) emptyL
|
||||
public fun listOf<T>(): List<T> = emptyList()
|
||||
|
||||
/** Returns a new read-only ordered set with the given elements. */
|
||||
public fun setOf<T>(vararg values: T): Set<T> = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet<T>(mapCapacityForValues(values)))
|
||||
public fun setOf<T>(vararg values: T): Set<T> = if (values.size() == 0) emptySet() else values.toCollection(LinkedHashSet<T>(mapCapacity(values.size())))
|
||||
|
||||
/** Returns an empty read-only set. */
|
||||
public fun setOf<T>(): Set<T> = emptySet()
|
||||
@@ -58,10 +58,10 @@ public fun linkedListOf<T>(vararg values: T): LinkedList<T> = values.toCollectio
|
||||
public fun arrayListOf<T>(vararg values: T): ArrayList<T> = values.toCollection(ArrayList(values.size()))
|
||||
|
||||
/** Returns a new [HashSet] with the given elements. */
|
||||
public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(HashSet(mapCapacityForValues(values)))
|
||||
public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(HashSet(mapCapacity(values.size())))
|
||||
|
||||
/** Returns a new [LinkedHashSet] with the given elements. */
|
||||
public fun linkedSetOf<T>(vararg values: T): LinkedHashSet<T> = values.toCollection(LinkedHashSet(mapCapacityForValues(values)))
|
||||
public fun linkedSetOf<T>(vararg values: T): LinkedHashSet<T> = values.toCollection(LinkedHashSet(mapCapacity(values.size())))
|
||||
|
||||
/**
|
||||
* Returns an [IntRange] of the valid indices for this collection.
|
||||
|
||||
@@ -38,7 +38,7 @@ public fun mapOf<K, V>(): Map<K, V> = emptyMap()
|
||||
* @sample test.collections.MapTest.createUsingPairs
|
||||
*/
|
||||
public fun <K, V> hashMapOf(vararg values: Pair<K, V>): HashMap<K, V> {
|
||||
val answer = HashMap<K, V>(mapCapacityForValues(values))
|
||||
val answer = HashMap<K, V>(mapCapacity(values.size()))
|
||||
answer.putAll(*values)
|
||||
return answer
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public fun <K, V> hashMapOf(vararg values: Pair<K, V>): HashMap<K, V> {
|
||||
* @sample test.collections.MapTest.createLinkedMap
|
||||
*/
|
||||
public fun <K, V> linkedMapOf(vararg values: Pair<K, V>): LinkedHashMap<K, V> {
|
||||
val answer = LinkedHashMap<K, V>(mapCapacityForValues(values))
|
||||
val answer = LinkedHashMap<K, V>(mapCapacity(values.size()))
|
||||
answer.putAll(*values)
|
||||
return answer
|
||||
}
|
||||
@@ -62,20 +62,16 @@ public fun <K, V> linkedMapOf(vararg values: Pair<K, V>): LinkedHashMap<K, V> {
|
||||
* very large sizes, allows support non-collection classes, and provides consistency for all map based class construction.
|
||||
*/
|
||||
|
||||
private val MAX_POWER_OF_TWO: Int = 1 shl (Integer.SIZE - 2)
|
||||
private val INT_MAX_POWER_OF_TWO: Int = Int.MAX_VALUE / 2 + 1
|
||||
|
||||
private fun mapCapacity(expectedSize: Int): Int {
|
||||
if (expectedSize < 3) {
|
||||
return expectedSize + 1
|
||||
}
|
||||
if (expectedSize < MAX_POWER_OF_TWO) {
|
||||
if (expectedSize < INT_MAX_POWER_OF_TWO) {
|
||||
return expectedSize + expectedSize / 3
|
||||
}
|
||||
return Integer.MAX_VALUE // any large value
|
||||
}
|
||||
|
||||
private fun <T> mapCapacityForValues(values: Array<T>): Int {
|
||||
return mapCapacity(values.size())
|
||||
return Int.MAX_VALUE // any large value
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user