Provide Array.fill in common stdlib

KT-32359
This commit is contained in:
Jake Wharton
2019-07-19 10:09:48 -04:00
committed by Ilya Gorbunov
parent 83e422fc3b
commit 1a6069382e
14 changed files with 677 additions and 84 deletions
@@ -1379,63 +1379,72 @@ internal fun CharArray.copyOfRangeImpl(fromIndex: Int, toIndex: Int): CharArray
/**
* Fills original array with the provided value.
*/
public fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
/**
* Fills original array with the provided value.
*/
public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
@@ -262,7 +262,8 @@ public fun UShortArray.binarySearch(element: UShort, fromIndex: Int = 0, toIndex
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.fill(element: UInt, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun UIntArray.fill(element: UInt, fromIndex: Int = 0, toIndex: Int = size): Unit {
storage.fill(element.toInt(), fromIndex, toIndex)
}
@@ -271,7 +272,8 @@ public fun UIntArray.fill(element: UInt, fromIndex: Int = 0, toIndex: Int = size
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.fill(element: ULong, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ULongArray.fill(element: ULong, fromIndex: Int = 0, toIndex: Int = size): Unit {
storage.fill(element.toLong(), fromIndex, toIndex)
}
@@ -280,7 +282,8 @@ public fun ULongArray.fill(element: ULong, fromIndex: Int = 0, toIndex: Int = si
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.fill(element: UByte, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun UByteArray.fill(element: UByte, fromIndex: Int = 0, toIndex: Int = size): Unit {
storage.fill(element.toByte(), fromIndex, toIndex)
}
@@ -289,7 +292,8 @@ public fun UByteArray.fill(element: UByte, fromIndex: Int = 0, toIndex: Int = si
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.fill(element: UShort, fromIndex: Int = 0, toIndex: Int = size): Unit {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun UShortArray.fill(element: UShort, fromIndex: Int = 0, toIndex: Int = size): Unit {
storage.fill(element.toShort(), fromIndex, toIndex)
}