commonMain.implementations.persistentOrderedMap.PersistentOrderedMapContentIterators.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-collections-immutable-metadata Show documentation
Show all versions of kotlinx-collections-immutable-metadata Show documentation
Kotlin Immutable Collections multiplatform library
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
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
}
}