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

commonMain.implementations.persistentOrderedMap.PersistentOrderedMapBuilderContentIterators.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.persistentOrderedMap

import kotlinx.collections.immutable.implementations.immutableMap.MapEntry
import kotlinx.collections.immutable.internal.EndOfChain

internal open class PersistentOrderedMapBuilderLinksIterator(
        private var nextKey: Any?,
        internal val builder: PersistentOrderedMapBuilder
) : MutableIterator> {

    internal var lastIteratedKey: Any? = EndOfChain
    private var nextWasInvoked = false
    private var expectedModCount = builder.hashMapBuilder.modCount
    internal var index = 0

    override fun hasNext(): Boolean {
        return index < builder.size
    }

    override fun next(): LinkedValue {
        checkForComodification()
        checkHasNext()
        lastIteratedKey = nextKey
        nextWasInvoked = true
        index++
        @Suppress("UNCHECKED_CAST")
        val result = builder.hashMapBuilder.getOrElse(nextKey as K) {
            throw ConcurrentModificationException("Hash code of a key ($nextKey) has changed after it was added to the persistent map.")
        }
        nextKey = result.next
        return result
    }

    override fun remove() {
        checkNextWasInvoked()
        builder.remove(lastIteratedKey)
        lastIteratedKey = null
        nextWasInvoked = false
        expectedModCount = builder.hashMapBuilder.modCount
        index--
    }

    private fun checkHasNext() {
        if (!hasNext())
            throw NoSuchElementException()
    }

    private fun checkNextWasInvoked() {
        if (!nextWasInvoked)
            throw IllegalStateException()
    }

    private fun checkForComodification() {
        if (builder.hashMapBuilder.modCount != expectedModCount)
            throw ConcurrentModificationException()
    }
}

internal class PersistentOrderedMapBuilderEntriesIterator(map: PersistentOrderedMapBuilder): MutableIterator> {
    private val internal = PersistentOrderedMapBuilderLinksIterator(map.firstKey, map)

    override fun hasNext(): Boolean {
        return internal.hasNext()
    }

    override fun next(): MutableMap.MutableEntry {
        val links = internal.next()
        @Suppress("UNCHECKED_CAST")
        return MutableMapEntry(internal.builder.hashMapBuilder, internal.lastIteratedKey as K, links)
    }

    override fun remove() {
        internal.remove()
    }
}

private class MutableMapEntry(private val mutableMap: MutableMap>,
                                    key: K,
                                    private var links: LinkedValue) : MapEntry(key, links.value), MutableMap.MutableEntry {
    override val value: V
        get() = links.value

    override fun setValue(newValue: V): V {
        val result = links.value
        links = links.withValue(newValue)
        mutableMap[key] = links
        return result
    }
}

internal class PersistentOrderedMapBuilderKeysIterator(map: PersistentOrderedMapBuilder): MutableIterator {
    private val internal = PersistentOrderedMapBuilderLinksIterator(map.firstKey, map)

    override fun hasNext(): Boolean {
        return internal.hasNext()
    }

    override fun next(): K {
        internal.next()
        @Suppress("UNCHECKED_CAST")
        return internal.lastIteratedKey as K
    }

    override fun remove() {
        internal.remove()
    }
}

internal class PersistentOrderedMapBuilderValuesIterator(map: PersistentOrderedMapBuilder): MutableIterator {
    private val internal = PersistentOrderedMapBuilderLinksIterator(map.firstKey, map)

    override fun hasNext(): Boolean {
        return internal.hasNext()
    }

    override fun next(): V {
        return internal.next().value
    }

    override fun remove() {
        internal.remove()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy