Provide Array.fill in common stdlib
KT-32359
This commit is contained in:
committed by
Ilya Gorbunov
parent
83e422fc3b
commit
1a6069382e
@@ -1053,6 +1053,141 @@ public actual fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray
|
||||
return withType("CharArray", this.asDynamic().slice(fromIndex, toIndex))
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
if (fromIndex !in 0..size) {
|
||||
throw IndexOutOfBoundsException("fromIndex $fromIndex out of range [0, $size]")
|
||||
}
|
||||
if (toIndex > size) {
|
||||
throw IndexOutOfBoundsException("toIndex $toIndex out of range [$fromIndex, $size]")
|
||||
}
|
||||
require(fromIndex <= toIndex) { "fromIndex($fromIndex) > toIndex($toIndex)" }
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all elements of the original array and then the given [element].
|
||||
*/
|
||||
|
||||
@@ -158,3 +158,43 @@ public actual fun UShortArray.asList(): List<UShort> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills original array with the provided value.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user