kotlin.collections.Iterators.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")
package kotlin.collections
import java.util.*
/**
* Creates an [Iterator] for an [Enumeration], allowing to use it in `for` loops.
*/
@kotlin.jvm.JvmVersion
public operator fun Enumeration.iterator(): Iterator = object : Iterator {
override fun hasNext(): Boolean = hasMoreElements()
public override fun next(): T = nextElement()
}
/**
* Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop.
*/
@kotlin.internal.InlineOnly
public inline operator fun Iterator.iterator(): Iterator = this
/**
* Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue],
* containing value and it's index.
*/
public fun Iterator.withIndex(): Iterator> = IndexingIterator(this)
/**
* Performs the given [operation] on each element of this [Iterator].
*/
public inline fun Iterator.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Iterator transforming original `iterator` into iterator of [IndexedValue], counting index from zero.
*/
internal class IndexingIterator(private val iterator: Iterator) : Iterator> {
private var index = 0
final override fun hasNext(): Boolean = iterator.hasNext()
final override fun next(): IndexedValue = IndexedValue(index++, iterator.next())
}