Specialize plus operator for UArrays (KT-28397)

This commit is contained in:
Abduqodiri Qurbonzoda
2019-01-23 00:15:39 +03:00
committed by Ilya Gorbunov
parent 8977d0e26d
commit 6cd9858147
5 changed files with 292 additions and 117 deletions
@@ -575,6 +575,134 @@ public inline fun UShortArray.copyOfRange(fromIndex: Int, toIndex: Int): UShortA
return UShortArray(storage.copyOfRange(fromIndex, toIndex))
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun UIntArray.plus(element: UInt): UIntArray {
return UIntArray(storage + element.toInt())
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun ULongArray.plus(element: ULong): ULongArray {
return ULongArray(storage + element.toLong())
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun UByteArray.plus(element: UByte): UByteArray {
return UByteArray(storage + element.toByte())
}
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun UShortArray.plus(element: UShort): UShortArray {
return UShortArray(storage + element.toShort())
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public operator fun UIntArray.plus(elements: Collection<UInt>): UIntArray {
var index = size
val result = storage.copyOf(size + elements.size)
for (element in elements) result[index++] = element.toInt()
return UIntArray(result)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public operator fun ULongArray.plus(elements: Collection<ULong>): ULongArray {
var index = size
val result = storage.copyOf(size + elements.size)
for (element in elements) result[index++] = element.toLong()
return ULongArray(result)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public operator fun UByteArray.plus(elements: Collection<UByte>): UByteArray {
var index = size
val result = storage.copyOf(size + elements.size)
for (element in elements) result[index++] = element.toByte()
return UByteArray(result)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] collection.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public operator fun UShortArray.plus(elements: Collection<UShort>): UShortArray {
var index = size
val result = storage.copyOf(size + elements.size)
for (element in elements) result[index++] = element.toShort()
return UShortArray(result)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun UIntArray.plus(elements: UIntArray): UIntArray {
return UIntArray(storage + elements.storage)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun ULongArray.plus(elements: ULongArray): ULongArray {
return ULongArray(storage + elements.storage)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun UByteArray.plus(elements: UByteArray): UByteArray {
return UByteArray(storage + elements.storage)
}
/**
* Returns an array containing all elements of the original array and then all elements of the given [elements] array.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline operator fun UShortArray.plus(elements: UShortArray): UShortArray {
return UShortArray(storage + elements.storage)
}
/**
* Returns an array of type [ByteArray], which is a copy of this array where each element is a signed reinterpretation
* of the corresponding element of this array.
@@ -99,7 +99,7 @@ class ArraysTest {
assertEquals(0.toShort(), arr[0])
assertEquals(1.toShort(), arr[1])
}
@Test fun intArray() {
val arr = IntArray(2)
@@ -107,7 +107,7 @@ class ArraysTest {
assertEquals(0, arr[0])
assertEquals(0, arr[1])
}
@Test fun intArrayInit() {
val arr = IntArray(2) { it.toInt() }
@@ -115,7 +115,7 @@ class ArraysTest {
assertEquals(0.toInt(), arr[0])
assertEquals(1.toInt(), arr[1])
}
@Test fun longArray() {
val arr = LongArray(2)
@@ -141,7 +141,7 @@ class ArraysTest {
assertEquals(expected, arr[0])
assertEquals(expected, arr[1])
}
@Test fun floatArrayInit() {
val arr = FloatArray(2) { it.toFloat() }
@@ -916,7 +916,7 @@ class ArraysTest {
byteArrayOf(1, 2, 3), byteArrayOf(4, 5, 6),
byteArrayOf(5, 6, 3), byteArrayOf(6, 3, 3), byteArrayOf(6, 6, 3)
)
doTest(
CharArray::copyInto, ::assertArrayNotSameButEquals, CharArray::contentToString,
charArrayOf('a', 'b', 'c'), charArrayOf('e', 'f', 'g'),
@@ -9,6 +9,9 @@ package test.collections
import test.collections.behaviors.collectionBehavior
import kotlin.test.*
fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String = "") { assertTrue(expected contentEquals actual, message) }
class UnsignedArraysTest {
@Test
@@ -228,4 +231,11 @@ class UnsignedArraysTest {
}
}
@Test
fun plus() {
assertArrayContentEquals(uintArrayOf(1u, 2u, 3u), uintArrayOf(1u, 2u) + 3u)
assertArrayContentEquals(uintArrayOf(1u, 2u, 3u, 4u), uintArrayOf(1u, 2u) + listOf(3u, 4u))
assertArrayContentEquals(uintArrayOf(1u, 2u, 3u, 4u), uintArrayOf(1u, 2u) + uintArrayOf(3u, 4u))
}
}