All Downloads are FREE. Search and download functionalities are using the official Maven repository.

godot.util.IndexedIterator.kt Maven / Gradle / Ivy

There is a newer version: 0.2.0-3.3.2
Show newest version
package godot.util

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
    }
}

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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy