godot.util.IndexedIterator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of godot-library-debug Show documentation
Show all versions of godot-library-debug Show documentation
Contains godot api as kotlin classes and jvm cpp interaction code.
The newest version!
package godot.util
@Suppress("UNCHECKED_CAST")
internal class IndexedIterator(
private var size: () -> Int,
private val getter: (Int) -> T,
private val eraser: (Int) -> Unit
) : MutableIterator {
private var index = 0
private var length = size()
override fun hasNext(): Boolean {
return index < length
}
override fun next(): T {
return getter(index++)
}
override fun remove() {
eraser(--index)
length = size()
}
}
internal class Entry(
override val key: K,
private val getter: (K) -> V,
private val setter: (K, V) -> Unit
) : MutableMap.MutableEntry {
override val value: V
get() {
return getter(key)
}
override fun setValue(newValue: V): V {
val ret = value
setter(key, newValue)
return ret
}
}
@Suppress("UNCHECKED_CAST")
internal class MapIterator(
private val keyIterator: MutableIterator,
private val getter: (K) -> V,
private val setter: (K, V) -> Unit,
private val eraser: (K) -> Unit
) : MutableIterator> {
var key: Any? = null
override fun hasNext() = keyIterator.hasNext()
override fun next(): MutableMap.MutableEntry {
key = keyIterator.next()
return Entry(key as K, getter, setter)
}
override fun remove() {
keyIterator.remove()
eraser(key as K)
}
}