mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-11 15:53:46 +00:00
51 lines
1.1 KiB
Kotlin
Vendored
51 lines
1.1 KiB
Kotlin
Vendored
// IGNORE_BACKEND_FIR: JVM_IR
|
|
// KJS_WITH_FULL_RUNTIME
|
|
// WITH_RUNTIME
|
|
|
|
inline fun <T> put(
|
|
x: T,
|
|
maxExclusive: Int,
|
|
isEmpty: (Int) -> Boolean,
|
|
equals: (T, T) -> Boolean,
|
|
fetch: (Int) -> T,
|
|
store: (Int, T) -> Unit
|
|
): Boolean {
|
|
var i = 0
|
|
do {
|
|
if (isEmpty(i)) {
|
|
store(i, x)
|
|
return true
|
|
}
|
|
|
|
val y = fetch(i)
|
|
if (equals(x, y)) {
|
|
return false
|
|
}
|
|
|
|
i++
|
|
if (i >= maxExclusive) return false
|
|
} while (true)
|
|
}
|
|
|
|
const val SIZE = 16
|
|
val arr = IntArray(SIZE) { -1 }
|
|
|
|
fun putNonNegInt(x: Int) =
|
|
put(x, SIZE,
|
|
isEmpty = { arr[it] < 0 },
|
|
equals = { x, y -> x == y },
|
|
fetch = { arr[it] },
|
|
store = { i, x -> arr[i] = x }
|
|
)
|
|
|
|
fun box(): String {
|
|
putNonNegInt(1)
|
|
putNonNegInt(2)
|
|
putNonNegInt(3)
|
|
|
|
if (arr[0] != 1) return "Fail, ${arr.toList().toString()}"
|
|
if (arr[1] != 2) return "Fail, ${arr.toList().toString()}"
|
|
if (arr[2] != 3) return "Fail, ${arr.toList().toString()}"
|
|
|
|
return "OK"
|
|
} |