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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy