Array.plus — different implementations for JVM and JS.

Array.copyOf, copyOfRange available in JS.
This commit is contained in:
Ilya Gorbunov
2015-07-06 04:37:50 +03:00
parent c883df544c
commit 7866184eb6
8 changed files with 960 additions and 357 deletions
+19
View File
@@ -61,3 +61,22 @@ public fun lazy<T>(initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
*
* The [mode] parameter is ignored. */
public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
private fun arrayCopyResize(source: dynamic, newSize: Int, defaultValue: Any?): dynamic {
val result = source.slice(0, newSize)
var index: Int = source.length
if (newSize > index) {
result.length = newSize
while (index < newSize) result[index++] = defaultValue
}
return result
}
private fun <T> arrayPlusCollection(array: dynamic, collection: Collection<T>): dynamic {
val result = array.slice(0)
result.length += collection.size()
var index: Int = array.length
for (element in collection) result[index++] = element
return result
}
+423
View File
@@ -75,3 +75,426 @@ public inline fun ShortArray.asList(): List<Short> {
return (this as Array<Short>).asList()
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun <T> Array<out T>.copyOf(): Array<T> {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun BooleanArray.copyOf(): BooleanArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun ByteArray.copyOf(): ByteArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun CharArray.copyOf(): CharArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun DoubleArray.copyOf(): DoubleArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun FloatArray.copyOf(): FloatArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun IntArray.copyOf(): IntArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun LongArray.copyOf(): LongArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun ShortArray.copyOf(): ShortArray {
return (this: dynamic).slice(0)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun BooleanArray.copyOf(newSize: Int): BooleanArray {
return arrayCopyResize(this, newSize, false)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun ByteArray.copyOf(newSize: Int): ByteArray {
return arrayCopyResize(this, newSize, 0)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun CharArray.copyOf(newSize: Int): CharArray {
return arrayCopyResize(this, newSize, '\u0000')
}
/**
* Returns new array which is a copy of the original array.
*/
public fun ShortArray.copyOf(newSize: Int): ShortArray {
return arrayCopyResize(this, newSize, 0)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun IntArray.copyOf(newSize: Int): IntArray {
return arrayCopyResize(this, newSize, 0)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun LongArray.copyOf(newSize: Int): LongArray {
return arrayCopyResize(this, newSize, 0L)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun FloatArray.copyOf(newSize: Int): FloatArray {
return arrayCopyResize(this, newSize, 0.0f)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun DoubleArray.copyOf(newSize: Int): DoubleArray {
return arrayCopyResize(this, newSize, 0.0)
}
/**
* Returns new array which is a copy of the original array.
*/
public fun <T> Array<out T>.copyOf(newSize: Int): Array<T?> {
return arrayCopyResize(this, newSize, null)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun <T> Array<out T>.copyOfRange(from: Int, to: Int): Array<T> {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun BooleanArray.copyOfRange(from: Int, to: Int): BooleanArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun ByteArray.copyOfRange(from: Int, to: Int): ByteArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun CharArray.copyOfRange(from: Int, to: Int): CharArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun DoubleArray.copyOfRange(from: Int, to: Int): DoubleArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun FloatArray.copyOfRange(from: Int, to: Int): FloatArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun IntArray.copyOfRange(from: Int, to: Int): IntArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun LongArray.copyOfRange(from: Int, to: Int): LongArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns new array which is a copy of range of original array.
*/
suppress("NOTHING_TO_INLINE")
public inline fun ShortArray.copyOfRange(from: Int, to: Int): ShortArray {
return (this: dynamic).slice(from, to)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun <T> Array<out T>.plus(array: Array<out T>): Array<out T> {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun BooleanArray.plus(array: BooleanArray): BooleanArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun ByteArray.plus(array: ByteArray): ByteArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun CharArray.plus(array: CharArray): CharArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun DoubleArray.plus(array: DoubleArray): DoubleArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun FloatArray.plus(array: FloatArray): FloatArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun IntArray.plus(array: IntArray): IntArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun LongArray.plus(array: LongArray): LongArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
suppress("NOTHING_TO_INLINE")
public inline fun ShortArray.plus(array: ShortArray): ShortArray {
return (this: dynamic).concat(array)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun <T> Array<out T>.plus(collection: Collection<T>): Array<out T> {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun BooleanArray.plus(collection: Collection<Boolean>): BooleanArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun ByteArray.plus(collection: Collection<Byte>): ByteArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun CharArray.plus(collection: Collection<Char>): CharArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun DoubleArray.plus(collection: Collection<Double>): DoubleArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun FloatArray.plus(collection: Collection<Float>): FloatArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun IntArray.plus(collection: Collection<Int>): IntArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun LongArray.plus(collection: Collection<Long>): LongArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun ShortArray.plus(collection: Collection<Short>): ShortArray {
return arrayPlusCollection(this, collection)
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun <T> Array<out T>.plus(element: T): Array<out T> {
val result = this.copyOf()
(result: dynamic).push(element)
return result as Array<out T>
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun BooleanArray.plus(element: Boolean): BooleanArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as BooleanArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun ByteArray.plus(element: Byte): ByteArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as ByteArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun CharArray.plus(element: Char): CharArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as CharArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun DoubleArray.plus(element: Double): DoubleArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as DoubleArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun FloatArray.plus(element: Float): FloatArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as FloatArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun IntArray.plus(element: Int): IntArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as IntArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun LongArray.plus(element: Long): LongArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as LongArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun ShortArray.plus(element: Short): ShortArray {
val result = this.copyOf()
(result: dynamic).push(element)
return result as ShortArray
}
+40 -280
View File
@@ -598,35 +598,35 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, S
}
/**
* Returns a list containing all elements of original collection and then all elements of the given [array].
* Returns a list containing all elements of the original collection and then all elements of the given [array].
*/
public fun <T> Collection<T>.plus(array: Array<out T>): List<T> {
val answer = ArrayList<T>(this.size() + array.size())
answer.addAll(this)
answer.addAll(array)
return answer
val result = ArrayList<T>(this.size() + array.size())
result.addAll(this)
result.addAll(array)
return result
}
/**
* Returns a list containing all elements of original collection and then all elements of the given [array].
* Returns a list containing all elements of the original collection and then all elements of the given [array].
*/
public fun <T> Iterable<T>.plus(array: Array<out T>): List<T> {
if (this is Collection) return this.plus(array)
val answer = ArrayList<T>()
answer.addAll(this)
answer.addAll(array)
return answer
val result = ArrayList<T>()
result.addAll(this)
result.addAll(array)
return result
}
/**
* Returns a list containing all elements of original collection and then all elements of the given [array].
* Returns a sequence containing all elements of original sequence and then all elements of the given [array].
*/
public fun <T> Sequence<T>.plus(array: Array<out T>): Sequence<T> {
return this.plus(array.asList())
}
/**
* Returns a list containing all elements of original collection and then all elements of the given [array].
* Returns a set containing all elements both of the original set and the given [array].
*/
public fun <T> Set<T>.plus(array: Array<out T>): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(this.size() + array.size()))
@@ -635,92 +635,14 @@ public fun <T> Set<T>.plus(array: Array<out T>): Set<T> {
return result
}
public fun <T> Array<out T>.plus(collection: Collection<T>): Array<out T> {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as Array<out T>
}
public fun BooleanArray.plus(collection: Collection<Boolean>): BooleanArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as BooleanArray
}
public fun ByteArray.plus(collection: Collection<Byte>): ByteArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as ByteArray
}
public fun CharArray.plus(collection: Collection<Char>): CharArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as CharArray
}
public fun DoubleArray.plus(collection: Collection<Double>): DoubleArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as DoubleArray
}
public fun FloatArray.plus(collection: Collection<Float>): FloatArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as FloatArray
}
public fun IntArray.plus(collection: Collection<Int>): IntArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as IntArray
}
public fun LongArray.plus(collection: Collection<Long>): LongArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as LongArray
}
public fun ShortArray.plus(collection: Collection<Short>): ShortArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as ShortArray
}
/**
* Returns a list containing all elements of the original collection and then all elements of the given [collection].
*/
public fun <T> Collection<T>.plus(collection: Collection<T>): List<T> {
val answer = ArrayList<T>(this.size() + collection.size())
answer.addAll(this)
answer.addAll(collection)
return answer
val result = ArrayList<T>(this.size() + collection.size())
result.addAll(this)
result.addAll(collection)
return result
}
/**
@@ -728,9 +650,9 @@ public fun <T> Collection<T>.plus(collection: Collection<T>): List<T> {
*/
public fun <T> Collection<T>.plus(collection: Iterable<T>): List<T> {
if (collection is Collection) return this.plus(collection)
val answer = ArrayList<T>(this)
answer.addAll(collection)
return answer
val result = ArrayList<T>(this)
result.addAll(collection)
return result
}
/**
@@ -738,21 +660,21 @@ public fun <T> Collection<T>.plus(collection: Iterable<T>): List<T> {
*/
public fun <T> Iterable<T>.plus(collection: Iterable<T>): List<T> {
if (this is Collection) return this.plus(collection)
val answer = ArrayList<T>(0)
answer.addAll(this)
answer.addAll(collection)
return answer
val result = ArrayList<T>()
result.addAll(this)
result.addAll(collection)
return result
}
/**
* Returns a sequence containing all elements of original sequence and then all elements of the given [collection]
* Returns a sequence containing all elements of original sequence and then all elements of the given [collection].
*/
public fun <T> Sequence<T>.plus(collection: Iterable<T>): Sequence<T> {
return sequenceOf(this, collection.asSequence()).flatten()
}
/**
* Returns a list containing all elements of the original collection and then all elements of the given [collection].
* Returns a set containing all elements both of the original set and the given [collection].
*/
public fun <T> Set<T>.plus(collection: Iterable<T>): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2))
@@ -761,176 +683,14 @@ public fun <T> Set<T>.plus(collection: Iterable<T>): Set<T> {
return result
}
public fun <T> Array<out T>.plus(collection: Array<out T>): Array<out T> {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as Array<out T>
}
public fun BooleanArray.plus(collection: BooleanArray): BooleanArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as BooleanArray
}
public fun ByteArray.plus(collection: ByteArray): ByteArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as ByteArray
}
public fun CharArray.plus(collection: CharArray): CharArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as CharArray
}
public fun DoubleArray.plus(collection: DoubleArray): DoubleArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as DoubleArray
}
public fun FloatArray.plus(collection: FloatArray): FloatArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as FloatArray
}
public fun IntArray.plus(collection: IntArray): IntArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as IntArray
}
public fun LongArray.plus(collection: LongArray): LongArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as LongArray
}
public fun ShortArray.plus(collection: ShortArray): ShortArray {
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as ShortArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun <T> Array<out T>.plus(element: T): Array<out T> {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as Array<out T>
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun BooleanArray.plus(element: Boolean): BooleanArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as BooleanArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun ByteArray.plus(element: Byte): ByteArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as ByteArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun CharArray.plus(element: Char): CharArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as CharArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun DoubleArray.plus(element: Double): DoubleArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as DoubleArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun FloatArray.plus(element: Float): FloatArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as FloatArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun IntArray.plus(element: Int): IntArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as IntArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun LongArray.plus(element: Long): LongArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as LongArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun ShortArray.plus(element: Short): ShortArray {
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as ShortArray
}
/**
* Returns a list containing all elements of the original collection and then the given [element].
*/
public fun <T> Collection<T>.plus(element: T): List<T> {
val answer = ArrayList<T>(size() + 1)
answer.addAll(this)
answer.add(element)
return answer
val result = ArrayList<T>(size() + 1)
result.addAll(this)
result.add(element)
return result
}
/**
@@ -938,10 +698,10 @@ public fun <T> Collection<T>.plus(element: T): List<T> {
*/
public fun <T> Iterable<T>.plus(element: T): List<T> {
if (this is Collection) return this.plus(element)
val answer = ArrayList<T>()
answer.addAll(this)
answer.add(element)
return answer
val result = ArrayList<T>()
result.addAll(this)
result.add(element)
return result
}
/**
@@ -955,10 +715,10 @@ public fun <T> Sequence<T>.plus(element: T): Sequence<T> {
* Returns a set containing all elements of the original set and then the given [element].
*/
public fun <T> Set<T>.plus(element: T): Set<T> {
val copyOfSet = LinkedHashSet<T>(mapCapacity(size() + 1))
copyOfSet.addAll(this)
copyOfSet.add(element)
return copyOfSet
val result = LinkedHashSet<T>(mapCapacity(size() + 1))
result.addAll(this)
result.add(element)
return result
}
/**
@@ -544,6 +544,294 @@ public fun <C : MutableCollection<in R>, R> Sequence<*>.filterIsInstanceTo(desti
return destination
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun <T> Array<out T>.plus(array: Array<out T>): Array<out T> {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as Array<out T>
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun BooleanArray.plus(array: BooleanArray): BooleanArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as BooleanArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun ByteArray.plus(array: ByteArray): ByteArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as ByteArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun CharArray.plus(array: CharArray): CharArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as CharArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun DoubleArray.plus(array: DoubleArray): DoubleArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as DoubleArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun FloatArray.plus(array: FloatArray): FloatArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as FloatArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun IntArray.plus(array: IntArray): IntArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as IntArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun LongArray.plus(array: LongArray): LongArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as LongArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [array].
*/
public fun ShortArray.plus(array: ShortArray): ShortArray {
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as ShortArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun <T> Array<out T>.plus(collection: Collection<T>): Array<out T> {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as Array<out T>
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun BooleanArray.plus(collection: Collection<Boolean>): BooleanArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as BooleanArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun ByteArray.plus(collection: Collection<Byte>): ByteArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as ByteArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun CharArray.plus(collection: Collection<Char>): CharArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as CharArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun DoubleArray.plus(collection: Collection<Double>): DoubleArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as DoubleArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun FloatArray.plus(collection: Collection<Float>): FloatArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as FloatArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun IntArray.plus(collection: Collection<Int>): IntArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as IntArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun LongArray.plus(collection: Collection<Long>): LongArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as LongArray
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [collection].
*/
public fun ShortArray.plus(collection: Collection<Short>): ShortArray {
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as ShortArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun <T> Array<out T>.plus(element: T): Array<out T> {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as Array<out T>
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun BooleanArray.plus(element: Boolean): BooleanArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as BooleanArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun ByteArray.plus(element: Byte): ByteArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as ByteArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun CharArray.plus(element: Char): CharArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as CharArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun DoubleArray.plus(element: Double): DoubleArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as DoubleArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun FloatArray.plus(element: Float): FloatArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as FloatArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun IntArray.plus(element: Int): IntArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as IntArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun LongArray.plus(element: Long): LongArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as LongArray
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public fun ShortArray.plus(element: Short): ShortArray {
val result = this.copyOf(size() + 1)
result[size()] = element
return result as ShortArray
}
/**
* Sorts array or range in array inplace.
*/
@@ -210,13 +210,21 @@ class ArraysTest {
}
test fun plus() {
assertEquals(listOf("1", "2", "3", "4"), arrayOf("1", "2") + arrayOf("3", "4"))
assertEquals(listOf("1", "2", "3", "4"), listOf("1", "2") + arrayOf("3", "4"))
val arr = arrayOf("1", "2") + arrayOf("3", "4")
assertEquals(4, arr.size())
checkContent(arr.iterator(), 4) { (it + 1).toString() }
}
test fun plusVararg() {
fun onePlus(vararg a: String) = arrayOf("1") + a
assertEquals(listOf("1", "2"), onePlus("2"))
fun stringOnePlus(vararg a: String) = arrayOf("1") + a
fun numberOnePlus(vararg a: Int) = intArrayOf(1) + a
val arrS = stringOnePlus("2")
checkContent(arrS.iterator(), 2) { (it + 1).toString() }
val arrN = numberOnePlus(2)
checkContent(arrN.iterator(), 2) { it + 1 }
}
test fun first() {
@@ -6,48 +6,37 @@ fun generators(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("plus(element: T)") {
exclude(Strings)
only(Iterables, Collections, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection and then the given [element]." }
returns("List<T>")
returns("SELF", ArraysOfObjects, ArraysOfPrimitives, Sets, Sequences)
body {
"""
if (this is Collection) return this.plus(element)
val answer = ArrayList<T>()
answer.addAll(this)
answer.add(element)
return answer
val result = ArrayList<T>()
result.addAll(this)
result.add(element)
return result
"""
}
body(Collections) {
"""
val answer = ArrayList<T>(size() + 1)
answer.addAll(this)
answer.add(element)
return answer
"""
}
doc(ArraysOfObjects, ArraysOfPrimitives) { "Returns an array containing all elements of the original array and then the given [element]." }
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val answer = this.copyOf(size() + 1)
answer[size()] = element
return answer as SELF
val result = ArrayList<T>(size() + 1)
result.addAll(this)
result.add(element)
return result
"""
}
// TODO: use build scope function when available
// TODO: use immutable sets when available
// TODO: precalculate size
returns("SELF", Sets, Sequences)
doc(Sets) { "Returns a set containing all elements of the original set and then the given [element]." }
body(Sets) {
"""
val copyOfSet = LinkedHashSet<T>(mapCapacity(size() + 1))
copyOfSet.addAll(this)
copyOfSet.add(element)
return copyOfSet
val result = LinkedHashSet<T>(mapCapacity(size() + 1))
result.addAll(this)
result.add(element)
return result
"""
}
@@ -67,23 +56,24 @@ fun generators(): List<GenericFunction> {
body {
"""
if (this is Collection) return this.plus(collection)
val answer = ArrayList<T>(0)
answer.addAll(this)
answer.addAll(collection)
return answer
val result = ArrayList<T>()
result.addAll(this)
result.addAll(collection)
return result
"""
}
body(Collections) {
"""
if (collection is Collection) return this.plus(collection)
val answer = ArrayList<T>(this)
answer.addAll(collection)
return answer
val result = ArrayList<T>(this)
result.addAll(collection)
return result
"""
}
// TODO: try to precalculate size
// TODO: use immutable set builder when available
doc(Sets) { "Returns a set containing all elements both of the original set and the given [collection]." }
body(Sets) {
"""
val result = LinkedHashSet<T>(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2))
@@ -93,7 +83,7 @@ fun generators(): List<GenericFunction> {
"""
}
doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [collection]" }
doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [collection]." }
body(Sequences) {
"""
return sequenceOf(this, collection.asSequence()).flatten()
@@ -102,52 +92,42 @@ fun generators(): List<GenericFunction> {
}
templates add f("plus(collection: Collection<T>)") {
only(Collections, ArraysOfPrimitives, ArraysOfObjects)
only(Collections)
doc { "Returns a list containing all elements of the original collection and then all elements of the given [collection]." }
returns("List<T>")
returns("SELF", ArraysOfObjects, ArraysOfPrimitives)
body(Collections) {
body {
"""
val answer = ArrayList<T>(this.size() + collection.size())
answer.addAll(this)
answer.addAll(collection)
return answer
"""
}
// ?
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as SELF
val result = ArrayList<T>(this.size() + collection.size())
result.addAll(this)
result.addAll(collection)
return result
"""
}
}
templates add f("plus(array: Array<out T>)") {
only(Iterables, Collections, Sets, Sequences)
doc { "Returns a list containing all elements of original collection and then all elements of the given [array]." }
doc { "Returns a list containing all elements of the original collection and then all elements of the given [array]." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
if (this is Collection) return this.plus(array)
val answer = ArrayList<T>()
answer.addAll(this)
answer.addAll(array)
return answer
val result = ArrayList<T>()
result.addAll(this)
result.addAll(array)
return result
"""
}
body(Collections) {
"""
val answer = ArrayList<T>(this.size() + array.size())
answer.addAll(this)
answer.addAll(array)
return answer
val result = ArrayList<T>(this.size() + array.size())
result.addAll(this)
result.addAll(array)
return result
"""
}
doc(Sets) { "Returns a set containing all elements both of the original set and the given [array]." }
body(Sets) {
"""
val result = LinkedHashSet<T>(mapCapacity(this.size() + array.size()))
@@ -156,6 +136,7 @@ fun generators(): List<GenericFunction> {
return result
"""
}
doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [array]." }
body(Sequences) {
"""
return this.plus(array.asList())
@@ -163,20 +144,6 @@ fun generators(): List<GenericFunction> {
}
}
templates add f("plus(collection: SELF)") {
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
body {
"""
val thisSize = size()
val answer = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
answer[thisSize + i] = element
}
return answer as SELF
"""
}
}
templates add f("plus(sequence: Sequence<T>)") {
only(Sequences)
@@ -21,5 +21,98 @@ fun specialJS(): List<GenericFunction> {
body(ArraysOfPrimitives) {"""return (this as Array<T>).asList()"""}
}
templates add f("copyOfRange(from: Int, to: Int)") {
// TODO: Arguments checking as in java?
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of range of original array." }
inline(true)
annotations("""suppress("NOTHING_TO_INLINE")""")
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
body {
"return (this: dynamic).slice(from, to)"
}
}
templates add f("copyOf()") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of the original array." }
inline(true)
annotations("""suppress("NOTHING_TO_INLINE")""")
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
body {
"return (this: dynamic).slice(0)"
}
}
val allArrays = PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it } + (ArraysOfObjects to null)
templates addAll allArrays.map {
val (family, primitive) = it
f("copyOf(newSize: Int)") {
only(family)
val defaultValue: String
if (primitive != null) {
returns("SELF")
only(primitive)
defaultValue = when (primitive) {
PrimitiveType.Boolean -> false.toString()
PrimitiveType.Char -> "'\\u0000'"
else -> "ZERO"
}
} else {
returns(ArraysOfObjects) { "Array<T?>" }
defaultValue = "null"
}
doc { "Returns new array which is a copy of the original array." }
body {
"""
return arrayCopyResize(this, newSize, $defaultValue)
"""
}
}
}
templates add f("plus(element: T)") {
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val result = this.copyOf()
(result: dynamic).push(element)
return result as SELF
"""
}
}
templates add f("plus(collection: Collection<T>)") {
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." }
body {
"""
return arrayPlusCollection(this, collection)
"""
}
}
// This overload can cause nulls if array size is expanding, hence different return overload
templates add f("plus(array: SELF)") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." }
inline(true)
annotations("""suppress("NOTHING_TO_INLINE")""")
returns("SELF")
body {
"""
return (this: dynamic).concat(array)
"""
}
}
return templates
}
@@ -5,6 +5,51 @@ import templates.Family.*
fun specialJVM(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("plus(element: T)") {
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then the given [element]." }
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val result = this.copyOf(size() + 1)
result[size()] = element
return result as SELF
"""
}
}
templates add f("plus(collection: Collection<T>)") {
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." }
body {
"""
val thisSize = size()
val result = this.copyOf(thisSize + collection.size())
collection.forEachIndexed { i, element ->
result[thisSize + i] = element
}
return result as SELF
"""
}
}
templates add f("plus(array: SELF)") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." }
returns("SELF")
body {
"""
val thisSize = size()
val arraySize = array.size()
val result = this.copyOf(thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
return result as SELF
"""
}
}
templates add f("copyOfRange(from: Int, to: Int)") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of range of original array." }