juh juh
08/01/2022, 8:26 PMnull
val tempLines = Array<IntArray?>(width + height) { null } as Array<IntArray>
for (x in 0 until width) {
tempLines[x] = IntArray(height) { y -> x + y * width }
}
for (y in 0 until height) {
tempLines[y + width] = IntArray(width) { x -> x + y * width }
}
Creating two arrays and concatenating them via +
is not an option, because performance is very criticalKlitos Kyriacou
08/01/2022, 8:45 PMval tempLines = arrayOfNulls<IntArray>(width + height) as Array<IntArray>
But why not initialize in one go?
val tempLines = Array(width + height) {
if (it < width)
IntArray(height) { y -> it + y * width }
else
IntArray(width) { x -> x + it * width }
}
ephemient
08/01/2022, 8:59 PMjuh juh
08/01/2022, 9:00 PMif(it < width)
check called on every loop step, tho maybe performance hit is negligible enough for me to use itephemient
08/01/2022, 9:01 PMjuh juh
08/01/2022, 9:02 PMrequireNotNull
but I doubt it doesn't have performance implicationsjuh juh
08/01/2022, 9:02 PMArray(length){null}
actually have a dedicated loop upon creation, setting every element to nulljuh juh
08/01/2022, 9:03 PMarrayOfNulls
doesn't have this issue as far as I seeephemient
08/01/2022, 9:03 PMephemient
08/01/2022, 9:03 PMjuh juh
08/01/2022, 9:05 PMjuh juh
08/01/2022, 9:06 PMephemient
08/01/2022, 9:06 PMjuh juh
08/01/2022, 9:07 PMfun1
there is an extra loop, writing `null`s into array of `null`sephemient
08/01/2022, 9:09 PMArray
constructor is effectively
fun <T> Array(size: Int, init: (Int) -> T) =
arrayOfNulls<T>(size).apply {
for (i in 0 until size) {
set(i, init(i))
}
} as Array<T>
although it's hard-wired in the compilerjuh juh
08/01/2022, 9:11 PMephemient
08/01/2022, 9:11 PMjuh juh
08/01/2022, 9:13 PMjuh juh
08/01/2022, 9:14 PMjuh juh
08/01/2022, 9:16 PMephemient
08/01/2022, 9:17 PMephemient
08/01/2022, 9:17 PM