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

org.pkl.thirdparty.kotlin.collections.Iterators.kt Maven / Gradle / Ivy

Go to download

Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.

There is a newer version: 0.27.1
Show newest version
/*
 * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

@file:org.pkl.thirdparty.kotlin.jvm.JvmMultifileClass
@file:org.pkl.thirdparty.kotlin.jvm.JvmName("CollectionsKt")

package org.pkl.thirdparty.kotlin.collections


/**
 * Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop.
 * @sample samples.collections.Iterators.iterator
 */
@org.pkl.thirdparty.kotlin.internal.InlineOnly
public inline operator fun  Iterator.iterator(): Iterator = this

/**
 * Returns an [Iterator] that wraps each element produced by the original iterator
 * into an [IndexedValue] containing the index of that element and the element itself.
 *
 * @sample samples.collections.Iterators.withIndexIterator
 */
public fun  Iterator.withIndex(): Iterator> = IndexingIterator(this)

/**
 * Performs the given [operation] on each element of this [Iterator].
 * @sample samples.collections.Iterators.forEachIterator
 */
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(checkIndexOverflow(index++), iterator.next())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy