Unify Array.plus and plusElements function templates between JVM and JS.

This commit is contained in:
Ilya Gorbunov
2016-12-08 05:10:13 +03:00
parent 202cd81134
commit 75992173b6
4 changed files with 521 additions and 566 deletions
+128 -128
View File
@@ -13057,134 +13057,6 @@ public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray
return this.asDynamic().slice(fromIndex, toIndex)
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun IntArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
public fun LongArray.sort(): Unit {
if (size > 1)
sort { a: Long, b: Long -> a.compareTo(b) }
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun ByteArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun ShortArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun DoubleArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun FloatArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun CharArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place according to the natural order of its elements.
*/
public fun <T: Comparable<T>> Array<out T>.sort(): Unit {
if (size > 1)
sort { a: T, b: T -> a.compareTo(b) }
}
/**
* Sorts the array in-place according to the order specified by the given [comparator].
*/
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1)
sort { a, b -> comparator.compare(a, b) }
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ByteArray.toTypedArray(): Array<Byte> {
return copyOf().unsafeCast<Array<Byte>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ShortArray.toTypedArray(): Array<Short> {
return copyOf().unsafeCast<Array<Short>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun IntArray.toTypedArray(): Array<Int> {
return copyOf().unsafeCast<Array<Int>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun LongArray.toTypedArray(): Array<Long> {
return copyOf().unsafeCast<Array<Long>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun FloatArray.toTypedArray(): Array<Float> {
return copyOf().unsafeCast<Array<Float>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun DoubleArray.toTypedArray(): Array<Double> {
return copyOf().unsafeCast<Array<Double>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun BooleanArray.toTypedArray(): Array<Boolean> {
return copyOf().unsafeCast<Array<Boolean>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun CharArray.toTypedArray(): Array<Char> {
return copyOf().unsafeCast<Array<Char>>()
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@@ -13400,6 +13272,134 @@ public inline fun <T> Array<out T>.plusElement(element: T): Array<T> {
return this.asDynamic().concat(arrayOf(element))
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun IntArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
public fun LongArray.sort(): Unit {
if (size > 1)
sort { a: Long, b: Long -> a.compareTo(b) }
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun ByteArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun ShortArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun DoubleArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun FloatArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place.
*/
@library("primitiveArraySort")
public fun CharArray.sort(): Unit {
noImpl
}
/**
* Sorts the array in-place according to the natural order of its elements.
*/
public fun <T: Comparable<T>> Array<out T>.sort(): Unit {
if (size > 1)
sort { a: T, b: T -> a.compareTo(b) }
}
/**
* Sorts the array in-place according to the order specified by the given [comparator].
*/
public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1)
sort { a, b -> comparator.compare(a, b) }
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ByteArray.toTypedArray(): Array<Byte> {
return copyOf().unsafeCast<Array<Byte>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ShortArray.toTypedArray(): Array<Short> {
return copyOf().unsafeCast<Array<Short>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun IntArray.toTypedArray(): Array<Int> {
return copyOf().unsafeCast<Array<Int>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun LongArray.toTypedArray(): Array<Long> {
return copyOf().unsafeCast<Array<Long>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun FloatArray.toTypedArray(): Array<Float> {
return copyOf().unsafeCast<Array<Float>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun DoubleArray.toTypedArray(): Array<Double> {
return copyOf().unsafeCast<Array<Double>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun BooleanArray.toTypedArray(): Array<Boolean> {
return copyOf().unsafeCast<Array<Boolean>>()
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun CharArray.toTypedArray(): Array<Char> {
return copyOf().unsafeCast<Array<Char>>()
}
/**
* Sorts the array in-place according to the order specified by the given [comparison] function.
*/
+287 -315
View File
@@ -13205,6 +13205,293 @@ public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray
return java.util.Arrays.copyOfRange(this, fromIndex, toIndex)
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun <T> Array<T>.plus(element: T): Array<T> {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun ByteArray.plus(element: Byte): ByteArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun ShortArray.plus(element: Short): ShortArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun IntArray.plus(element: Int): IntArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun LongArray.plus(element: Long): LongArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun FloatArray.plus(element: Float): FloatArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun DoubleArray.plus(element: Double): DoubleArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun BooleanArray.plus(element: Boolean): BooleanArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public operator fun CharArray.plus(element: Char): CharArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun <T> Array<T>.plus(elements: Collection<T>): Array<T> {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun ByteArray.plus(elements: Collection<Byte>): ByteArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun ShortArray.plus(elements: Collection<Short>): ShortArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun IntArray.plus(elements: Collection<Int>): IntArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun LongArray.plus(elements: Collection<Long>): LongArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun FloatArray.plus(elements: Collection<Float>): FloatArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun DoubleArray.plus(elements: Collection<Double>): DoubleArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun BooleanArray.plus(elements: Collection<Boolean>): BooleanArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
public operator fun CharArray.plus(elements: Collection<Char>): CharArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun <T> Array<T>.plus(elements: Array<out T>): Array<T> {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun ByteArray.plus(elements: ByteArray): ByteArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun ShortArray.plus(elements: ShortArray): ShortArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun IntArray.plus(elements: IntArray): IntArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun LongArray.plus(elements: LongArray): LongArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun FloatArray.plus(elements: FloatArray): FloatArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
public operator fun CharArray.plus(elements: CharArray): CharArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<T>.plusElement(element: T): Array<T> {
return plus(element)
}
/**
* Sorts the array in-place.
*/
@@ -13591,321 +13878,6 @@ public fun <C : MutableCollection<in R>, R> Array<*>.filterIsInstanceTo(destinat
return destination
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun <T> Array<T>.plus(element: T): Array<T> {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun ByteArray.plus(element: Byte): ByteArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun ShortArray.plus(element: Short): ShortArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun IntArray.plus(element: Int): IntArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun LongArray.plus(element: Long): LongArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun FloatArray.plus(element: Float): FloatArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun DoubleArray.plus(element: Double): DoubleArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun BooleanArray.plus(element: Boolean): BooleanArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
public operator fun CharArray.plus(element: Char): CharArray {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun <T> Array<T>.plus(elements: Collection<T>): Array<T> {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun ByteArray.plus(elements: Collection<Byte>): ByteArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun ShortArray.plus(elements: Collection<Short>): ShortArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun IntArray.plus(elements: Collection<Int>): IntArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun LongArray.plus(elements: Collection<Long>): LongArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun FloatArray.plus(elements: Collection<Float>): FloatArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun DoubleArray.plus(elements: Collection<Double>): DoubleArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun BooleanArray.plus(elements: Collection<Boolean>): BooleanArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@kotlin.jvm.JvmVersion
public operator fun CharArray.plus(elements: Collection<Char>): CharArray {
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun <T> Array<T>.plus(elements: Array<out T>): Array<T> {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun ByteArray.plus(elements: ByteArray): ByteArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun ShortArray.plus(elements: ShortArray): ShortArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun IntArray.plus(elements: IntArray): IntArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun LongArray.plus(elements: LongArray): LongArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun FloatArray.plus(elements: FloatArray): FloatArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@kotlin.jvm.JvmVersion
public operator fun CharArray.plus(elements: CharArray): CharArray {
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> Array<T>.plusElement(element: T): Array<T> {
return plus(element)
}
/**
* Sorts the array in-place according to the natural order of its elements.
*
@@ -21,69 +21,6 @@ import templates.Family.*
fun specialJS(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("plusElement(element: T)") {
only(ArraysOfObjects)
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
inline(true)
annotations("""@Suppress("NOTHING_TO_INLINE")""")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body() {
"""
return this.asDynamic().concat(arrayOf(element))
"""
}
}
templates add f("plus(element: T)") {
operator(true)
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
inline(true)
annotations("""@Suppress("NOTHING_TO_INLINE")""")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body() {
"""
return this.asDynamic().concat(arrayOf(element))
"""
}
}
templates add f("plus(elements: Collection<T>)") {
operator(true)
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
body {
"""
return arrayPlusCollection(this, elements)
"""
}
}
// This overload can cause nulls if array size is expanding, hence different return overload
templates add f("plus(elements: SELF)") {
operator(true)
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
inline(true)
annotations("""@Suppress("NOTHING_TO_INLINE")""")
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
body {
"""
return this.asDynamic().concat(elements)
"""
}
}
templates add f("sort(noinline comparison: (T, T) -> Int)") {
only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
@@ -21,65 +21,6 @@ import templates.Family.*
fun specialJVM(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("plusElement(element: T)") {
inline(Inline.Only)
only(InvariantArraysOfObjects)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body { "return plus(element)" }
}
templates add f("plus(element: T)") {
operator(true)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body() {
"""
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
"""
}
}
templates add f("plus(elements: Collection<T>)") {
operator(true)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
body {
"""
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
"""
}
}
templates add f("plus(elements: SELF)") {
operator(true)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
customSignature(InvariantArraysOfObjects) { "plus(elements: Array<out T>)" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
returns("SELF")
body {
"""
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
"""
}
}
templates add f("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
only(InvariantArraysOfObjects, ArraysOfPrimitives)
doc { "Fills original array with the provided value." }
@@ -214,7 +155,111 @@ fun specialJVM(): List<GenericFunction> {
}
object CommonArrays {
// TODO: plus
fun f_plusElement() = f("plusElement(element: T)") {
inline(Platform.JVM, Inline.Only)
inline(Platform.JS, Inline.Yes)
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
only(InvariantArraysOfObjects)
only(Platform.JS, ArraysOfObjects)
returns("SELF")
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body(Platform.JVM) { "return plus(element)" }
body(Platform.JS) {
"""
return this.asDynamic().concat(arrayOf(element))
"""
}
}
fun f_plusElementOperator() = f("plus(element: T)") {
operator(true)
inline(Platform.JS, Inline.Yes)
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
only(InvariantArraysOfObjects, ArraysOfPrimitives)
only(Platform.JS, ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body(Platform.JVM) {
"""
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
"""
}
body(Platform.JS) {
"""
return this.asDynamic().concat(arrayOf(element))
"""
}
}
fun f_plusCollection() = f("plus(elements: Collection<T>)") {
operator(true)
// TODO: inline arrayPlusCollection when @PublishedAPI is available
// inline(Platform.JS, Inline.Yes)
// annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
only(InvariantArraysOfObjects, ArraysOfPrimitives)
only(Platform.JS, ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
body(Platform.JVM) {
"""
var index = size
val result = java.util.Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
"""
}
body(Platform.JS) {
"""
return arrayPlusCollection(this, elements)
"""
}
}
fun f_plusArray() = f("plus(elements: SELF)") {
operator(true)
inline(Platform.JS, Inline.Yes)
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
only(InvariantArraysOfObjects, ArraysOfPrimitives)
only(Platform.JS, ArraysOfObjects, ArraysOfPrimitives)
customSignature(InvariantArraysOfObjects) { "plus(elements: Array<out T>)" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
returns("SELF")
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
body(Platform.JVM) {
"""
val thisSize = size
val arraySize = elements.size
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
"""
}
body(Platform.JS) {
"""
return this.asDynamic().concat(elements)
"""
}
}
fun f_copyOfRange() = f("copyOfRange(fromIndex: Int, toIndex: Int)") {
inline(Platform.JVM, Inline.Only)
inline(Platform.JS, Inline.Yes)
@@ -414,6 +459,7 @@ object CommonArrays {
// TODO: use reflection later to get all functions of matching type
fun templates() =
listOf(f_plusElement(), f_plusElementOperator(), f_plusCollection(), f_plusArray()) +
listOf(f_copyOf(), f_copyOfRange()) +
f_copyOfResized() +
f_sortPrimitives() +