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
@@ -6,8 +6,8 @@
package templates
import templates.Family.*
import templates.Ordering.stableSortNote
import templates.Ordering.appendStableSortNote
import templates.Ordering.stableSortNote
object ArrayOps : TemplateGroupBase() {
@@ -1273,15 +1273,41 @@ object ArrayOps : TemplateGroupBase() {
}
val f_fill = fn("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
platforms(Platform.JVM)
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
} builder {
doc { "Fills original array with the provided value." }
returns("Unit")
body {
"""
java.util.Arrays.fill(this, fromIndex, toIndex, element)
"""
on(Platform.JVM) {
suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
body {
"""
java.util.Arrays.fill(this, fromIndex, toIndex, element)
"""
}
}
on(Platform.JS) {
suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
body {
"""
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);
"""
}
}
on(Platform.Native) {
suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
body {
"""
arrayFill(this, element, fromIndex, toIndex)
"""
}
}
specialFor(ArraysOfUnsigned) {