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

commonMain.implementations.immutableList.PersistentVectorIterator.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2019 JetBrains s.r.o.
 * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
 */

package kotlinx.collections.immutable.implementations.immutableList

internal class PersistentVectorIterator(root: Array,
                                               private val tail: Array,
                                               index: Int,
                                               size: Int,
                                               trieHeight: Int) : AbstractListIterator(index, size) {
    private val trieIterator: TrieIterator

    init {
        val trieSize = rootSize(size)
        val trieIndex = index.coerceAtMost(trieSize)
        trieIterator = TrieIterator(root, trieIndex, trieSize, trieHeight)
    }

    override fun next(): T {
        checkHasNext()
        if (trieIterator.hasNext()) {
            index++
            return trieIterator.next()
        }
        return tail[index++ - trieIterator.size]
    }

    override fun previous(): T {
        checkHasPrevious()
        if (index > trieIterator.size) {
            return tail[--index - trieIterator.size]
        }
        index--
        return trieIterator.previous()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy