Optimizations in listOf and arrayListOf constructor functions.
Remove linkedListOf() from JS. #KT-5703 Fixed #KT-8519 Fixed
This commit is contained in:
@@ -16,34 +16,13 @@
|
||||
|
||||
package java.util
|
||||
|
||||
public fun HashSet<E>(c: Collection<E>): HashSet<E> {
|
||||
val set: HashSet<E> = HashSet(c.size())
|
||||
set.addAll(c)
|
||||
return set
|
||||
}
|
||||
public fun HashSet<E>(c: Collection<E>): HashSet<E> = HashSet<E>(c.size()).apply { addAll(c) }
|
||||
|
||||
public fun LinkedHashSet<E>(c: Collection<E>): HashSet<E> {
|
||||
val set: LinkedHashSet<E> = LinkedHashSet(c.size())
|
||||
set.addAll(c)
|
||||
return set
|
||||
}
|
||||
public fun LinkedHashSet<E>(c: Collection<E>): HashSet<E> = LinkedHashSet<E>(c.size()).apply { addAll(c) }
|
||||
|
||||
public fun HashMap<K, V>(m: Map<K, V>): HashMap<K, V> {
|
||||
val map: HashMap<K, V> = HashMap(m.size())
|
||||
map.putAll(m)
|
||||
return map
|
||||
}
|
||||
public fun HashMap<K, V>(m: Map<K, V>): HashMap<K, V> = HashMap<K, V>(m.size()).apply { putAll(m) }
|
||||
|
||||
public fun LinkedHashMap<K, V>(m: Map<K, V>): LinkedHashMap<K, V> {
|
||||
val map: LinkedHashMap<K, V> = LinkedHashMap(m.size())
|
||||
map.putAll(m)
|
||||
return map
|
||||
}
|
||||
public fun LinkedHashMap<K, V>(m: Map<K, V>): LinkedHashMap<K, V> = LinkedHashMap<K, V>(m.size()).apply { putAll(m) }
|
||||
|
||||
public fun ArrayList<E>(c: Collection<E>): ArrayList<E> = ArrayList<E>().apply { asDynamic().array = c.toTypedArray<Any?>() } // black dynamic magic
|
||||
|
||||
public fun ArrayList<E>(c: Collection<E>): ArrayList<E> {
|
||||
val result = ArrayList<E>()
|
||||
for (element in c) {
|
||||
result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
+2
@@ -1018,6 +1018,8 @@
|
||||
};
|
||||
|
||||
Kotlin.copyToArray = function (collection) {
|
||||
if (typeof collection.toArray !== "undefined") return collection.toArray();
|
||||
|
||||
var array = [];
|
||||
var it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
|
||||
@@ -44,29 +44,38 @@ internal object EmptyList : List<Nothing>, Serializable {
|
||||
private fun readResolve(): Any = EmptyList
|
||||
}
|
||||
|
||||
|
||||
private class ArrayAsCollection<T>(val values: Array<out T>): Collection<T> {
|
||||
override val size: Int get() = values.size()
|
||||
override fun isEmpty(): Boolean = values.isEmpty()
|
||||
override fun contains(o: T): Boolean = values.contains(o)
|
||||
override fun containsAll(c: Collection<T>): Boolean = c.all { contains(it) }
|
||||
override fun iterator(): Iterator<T> = values.iterator()
|
||||
// override of hidden toArray method implementation to prevent copying of values array
|
||||
public fun toArray(): Array<out Any?> = values
|
||||
}
|
||||
|
||||
/** Returns an empty read-only list. The returned list is serializable (JVM). */
|
||||
public fun emptyList<T>(): List<T> = EmptyList
|
||||
public fun <T> emptyList(): List<T> = EmptyList
|
||||
|
||||
/** Returns a new read-only list of given elements. The returned list is serializable (JVM). */
|
||||
public fun listOf<T>(vararg values: T): List<T> = if (values.size() > 0) arrayListOf(*values) else emptyList()
|
||||
public fun <T> listOf(vararg values: T): List<T> = if (values.size() > 0) values.asList() else emptyList()
|
||||
|
||||
/** Returns an empty read-only list. The returned list is serializable (JVM). */
|
||||
public fun listOf<T>(): List<T> = emptyList()
|
||||
public fun <T> listOf(): List<T> = emptyList()
|
||||
|
||||
/**
|
||||
* Returns an immutable list containing only the specified object [value].
|
||||
* The returned list is serializable.
|
||||
*/
|
||||
@JvmVersion
|
||||
public fun listOf<T>(value: T): List<T> = Collections.singletonList(value)
|
||||
public fun <T> listOf(value: T): List<T> = Collections.singletonList(value)
|
||||
|
||||
/** Returns a new [LinkedList] with the given elements. */
|
||||
public fun linkedListOf<T>(vararg values: T): LinkedList<T> = values.toCollection(LinkedList<T>())
|
||||
@JvmVersion
|
||||
public fun <T> linkedListOf(vararg values: T): LinkedList<T> = LinkedList(ArrayAsCollection(values))
|
||||
|
||||
/** Returns a new [ArrayList] with the given elements. */
|
||||
public fun arrayListOf<T>(vararg values: T): ArrayList<T> = values.toCollection(ArrayList(values.size()))
|
||||
public fun <T> arrayListOf(vararg values: T): ArrayList<T> = ArrayList(ArrayAsCollection(values))
|
||||
|
||||
/** Returns a new read-only list either of single given element, if it is not null, or empty list it the element is null. The returned list is serializable (JVM). */
|
||||
public fun <T : Any> listOfNotNull(value: T?): List<T> = if (value != null) listOf(value) else emptyList()
|
||||
|
||||
Reference in New Issue
Block a user