Unify toTypedArray and asList templates between JVM and JS

This commit is contained in:
Ilya Gorbunov
2016-12-06 07:23:36 +03:00
parent 3d7e6e1996
commit f3df648f4a
5 changed files with 174 additions and 214 deletions
+56 -56
View File
@@ -12560,6 +12560,62 @@ public inline fun CharArray.asList(): List<Char> {
return this.unsafeCast<Array<Char>>().asList() return this.unsafeCast<Array<Char>>().asList()
} }
/**
* 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 `true` if the two specified arrays are *deeply* equal to one another, * Returns `true` if the two specified arrays are *deeply* equal to one another,
* i.e. contain the same number of the same elements in the same order. * i.e. contain the same number of the same elements in the same order.
@@ -13400,59 +13456,3 @@ public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
sort { a, b -> comparator.compare(a, b) } 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>>()
}
+88 -105
View File
@@ -12903,7 +12903,6 @@ public fun DoubleArray.sum(): Double {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun <T> Array<out T>.asList(): List<T> { public fun <T> Array<out T>.asList(): List<T> {
return ArraysUtilJVM.asList(this) return ArraysUtilJVM.asList(this)
} }
@@ -12911,7 +12910,6 @@ public fun <T> Array<out T>.asList(): List<T> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun ByteArray.asList(): List<Byte> { public fun ByteArray.asList(): List<Byte> {
return object : AbstractList<Byte>(), RandomAccess { return object : AbstractList<Byte>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -12926,7 +12924,6 @@ public fun ByteArray.asList(): List<Byte> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun ShortArray.asList(): List<Short> { public fun ShortArray.asList(): List<Short> {
return object : AbstractList<Short>(), RandomAccess { return object : AbstractList<Short>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -12941,7 +12938,6 @@ public fun ShortArray.asList(): List<Short> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun IntArray.asList(): List<Int> { public fun IntArray.asList(): List<Int> {
return object : AbstractList<Int>(), RandomAccess { return object : AbstractList<Int>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -12956,7 +12952,6 @@ public fun IntArray.asList(): List<Int> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun LongArray.asList(): List<Long> { public fun LongArray.asList(): List<Long> {
return object : AbstractList<Long>(), RandomAccess { return object : AbstractList<Long>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -12971,7 +12966,6 @@ public fun LongArray.asList(): List<Long> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun FloatArray.asList(): List<Float> { public fun FloatArray.asList(): List<Float> {
return object : AbstractList<Float>(), RandomAccess { return object : AbstractList<Float>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -12986,7 +12980,6 @@ public fun FloatArray.asList(): List<Float> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun DoubleArray.asList(): List<Double> { public fun DoubleArray.asList(): List<Double> {
return object : AbstractList<Double>(), RandomAccess { return object : AbstractList<Double>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -13001,7 +12994,6 @@ public fun DoubleArray.asList(): List<Double> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun BooleanArray.asList(): List<Boolean> { public fun BooleanArray.asList(): List<Boolean> {
return object : AbstractList<Boolean>(), RandomAccess { return object : AbstractList<Boolean>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -13016,7 +13008,6 @@ public fun BooleanArray.asList(): List<Boolean> {
/** /**
* Returns a [List] that wraps the original array. * Returns a [List] that wraps the original array.
*/ */
@kotlin.jvm.JvmVersion
public fun CharArray.asList(): List<Char> { public fun CharArray.asList(): List<Char> {
return object : AbstractList<Char>(), RandomAccess { return object : AbstractList<Char>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -13028,6 +13019,94 @@ public fun CharArray.asList(): List<Char> {
} }
} }
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ByteArray.toTypedArray(): Array<Byte> {
val result = arrayOfNulls<Byte>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Byte>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ShortArray.toTypedArray(): Array<Short> {
val result = arrayOfNulls<Short>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Short>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun IntArray.toTypedArray(): Array<Int> {
val result = arrayOfNulls<Int>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Int>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun LongArray.toTypedArray(): Array<Long> {
val result = arrayOfNulls<Long>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Long>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun FloatArray.toTypedArray(): Array<Float> {
val result = arrayOfNulls<Float>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Float>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun DoubleArray.toTypedArray(): Array<Double> {
val result = arrayOfNulls<Double>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Double>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun BooleanArray.toTypedArray(): Array<Boolean> {
val result = arrayOfNulls<Boolean>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Boolean>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun CharArray.toTypedArray(): Array<Char> {
val result = arrayOfNulls<Char>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Char>
}
/** /**
* Searches the array or the range of the array for the provided [element] using the binary search algorithm. * Searches the array or the range of the array for the provided [element] using the binary search algorithm.
* The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. * The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined.
@@ -13964,99 +14043,3 @@ public fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: In
java.util.Arrays.sort(this, fromIndex, toIndex, comparator) java.util.Arrays.sort(this, fromIndex, toIndex, comparator)
} }
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun ByteArray.toTypedArray(): Array<Byte> {
val result = arrayOfNulls<Byte>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Byte>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun ShortArray.toTypedArray(): Array<Short> {
val result = arrayOfNulls<Short>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Short>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun IntArray.toTypedArray(): Array<Int> {
val result = arrayOfNulls<Int>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Int>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun LongArray.toTypedArray(): Array<Long> {
val result = arrayOfNulls<Long>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Long>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun FloatArray.toTypedArray(): Array<Float> {
val result = arrayOfNulls<Float>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Float>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun DoubleArray.toTypedArray(): Array<Double> {
val result = arrayOfNulls<Double>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Double>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun BooleanArray.toTypedArray(): Array<Boolean> {
val result = arrayOfNulls<Boolean>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Boolean>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
@kotlin.jvm.JvmVersion
public fun CharArray.toTypedArray(): Array<Char> {
val result = arrayOfNulls<Char>(size)
for (index in indices)
result[index] = this[index]
@Suppress("UNCHECKED_CAST")
return result as Array<Char>
}
@@ -47,7 +47,8 @@ val commonGenerators = sequenceOf(
::sequences, ::sequences,
::ranges, ::ranges,
::numeric, ::numeric,
::comparables ::comparables,
CommonArrays::templates
) )
fun generateCollectionsAPI(outDir: File) { fun generateCollectionsAPI(outDir: File) {
@@ -21,36 +21,6 @@ import templates.Family.*
fun specialJS(): List<GenericFunction> { fun specialJS(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>() val templates = arrayListOf<GenericFunction>()
templates add f("asList()") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns a [List] that wraps the original array." }
returns("List<T>")
body(ArraysOfObjects) {
"""
return ArrayList<T>(this.unsafeCast<Array<Any?>>())
"""
}
inline(true, ArraysOfPrimitives)
body(ArraysOfPrimitives) {"""return this.unsafeCast<Array<T>>().asList()"""}
}
templates add f("toTypedArray()") {
only(ArraysOfPrimitives)
returns("Array<T>")
doc {
"""
Returns a *typed* object array containing all of the elements of this primitive array.
"""
}
body {
"""
return copyOf().unsafeCast<Array<T>>()
"""
}
}
templates add f("copyOfRange(fromIndex: Int, toIndex: Int)") { templates add f("copyOfRange(fromIndex: Int, toIndex: Int)") {
// TODO: Arguments checking as in java? // TODO: Arguments checking as in java?
only(ArraysOfObjects, ArraysOfPrimitives) only(ArraysOfObjects, ArraysOfPrimitives)
@@ -247,17 +247,29 @@ fun specialJVM(): List<GenericFunction> {
} }
} }
templates add f("asList()") { templates.forEach { it.apply { jvmOnly(true) } }
return templates
}
object CommonArrays {
fun f_asList() = f("asList()") {
only(ArraysOfObjects, ArraysOfPrimitives) only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns a [List] that wraps the original array." } doc { "Returns a [List] that wraps the original array." }
returns("List<T>") returns("List<T>")
body(ArraysOfObjects) { body(Platform.JVM, ArraysOfObjects) {
""" """
return ArraysUtilJVM.asList(this) return ArraysUtilJVM.asList(this)
""" """
} }
body(ArraysOfPrimitives) { body(Platform.JS, ArraysOfObjects) {
"""
return ArrayList<T>(this.unsafeCast<Array<Any?>>())
"""
}
body(Platform.JVM, ArraysOfPrimitives) {
""" """
return object : AbstractList<T>(), RandomAccess { return object : AbstractList<T>(), RandomAccess {
override val size: Int get() = this@asList.size override val size: Int get() = this@asList.size
@@ -269,19 +281,14 @@ fun specialJVM(): List<GenericFunction> {
} }
""" """
} }
//
// body(ArraysOfObjects) {
// """ inline(Platform.JS, Inline.Yes, ArraysOfPrimitives)
// return ArrayList<T>(this.unsafeCast<Array<Any?>>()) body(Platform.JS, ArraysOfPrimitives) {"""return this.unsafeCast<Array<T>>().asList()"""}
// """
// }
//
// inline(true, ArraysOfPrimitives)
// body(ArraysOfPrimitives) {"""return this.unsafeCast<Array<T>>().asList()"""}
} }
templates add f("toTypedArray()") { fun f_toTypedArray() = f("toTypedArray()") {
only(ArraysOfPrimitives) only(ArraysOfPrimitives)
returns("Array<T>") returns("Array<T>")
doc { doc {
@@ -289,7 +296,7 @@ fun specialJVM(): List<GenericFunction> {
Returns a *typed* object array containing all of the elements of this primitive array. Returns a *typed* object array containing all of the elements of this primitive array.
""" """
} }
body { body(Platform.JVM) {
""" """
val result = arrayOfNulls<T>(size) val result = arrayOfNulls<T>(size)
for (index in indices) for (index in indices)
@@ -298,14 +305,13 @@ fun specialJVM(): List<GenericFunction> {
return result as Array<T> return result as Array<T>
""" """
} }
// body { body(Platform.JS) {
// """ """
// return copyOf().unsafeCast<Array<T>>() return copyOf().unsafeCast<Array<T>>()
// """ """
// } }
} }
templates.forEach { it.apply { jvmOnly(true) } } // TODO: use reflection later to get all functions of matching type
fun templates() = listOf(this.f_asList(), this.f_toTypedArray())
return templates
} }