Array.plus — different implementations for JVM and JS.
Array.copyOf, copyOfRange available in JS.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user