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

commonMain.implementations.persistentOrderedMap.PersistentOrderedMapContentIterators.kt Maven / Gradle / Ivy

There is a newer version: 0.3.8
Show 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

internal open class PersistentOrderedMapLinksIterator(
        internal var nextKey: Any?,
        private val hashMap: Map>
) : Iterator> {
    internal var index = 0

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

    override fun next(): LinkedValue {
        if (!hasNext()) {
            throw NoSuchElementException()
        }
        @Suppress("UNCHECKED_CAST")
        val result = hashMap.getOrElse(nextKey as K) {
            throw ConcurrentModificationException("Hash code of a key ($nextKey) has changed after it was added to the persistent map.")
        }
        index++
        nextKey = result.next
        return result
    }

}

internal class PersistentOrderedMapEntriesIterator(map: PersistentOrderedMap) : Iterator> {
    private val internal = PersistentOrderedMapLinksIterator(map.firstKey, map.hashMap)

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

    override fun next(): Map.Entry {
        @Suppress("UNCHECKED_CAST")
        val nextKey = internal.nextKey as K
        val nextValue = internal.next().value
        return MapEntry(nextKey, nextValue)
    }
}

internal class PersistentOrderedMapKeysIterator(map: PersistentOrderedMap) : Iterator {
    private val internal = PersistentOrderedMapLinksIterator(map.firstKey, map.hashMap)

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

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

internal class PersistentOrderedMapValuesIterator(map: PersistentOrderedMap) : Iterator {
    private val internal = PersistentOrderedMapLinksIterator(map.firstKey, map.hashMap)

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy