Move IndexedValue, IndexingIterable and IndexingIterator back to kotlin package
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
package kotlin
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data class representing a value from a collection or sequence, along with its index in that collection or sequence.
|
||||||
|
*
|
||||||
|
* @property value the underlying value.
|
||||||
|
* @property index the index of the value in the collection or sequence.
|
||||||
|
*/
|
||||||
|
public data class IndexedValue<out T>(public val index: Int, public val value: T)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper over another [Iterable] (or any other object that can produce an [Iterator]) that returns
|
||||||
|
* an indexing iterator.
|
||||||
|
*/
|
||||||
|
public class IndexingIterable<out T>(private val iteratorFactory: () -> Iterator<T>) : Iterable<IndexedValue<T>> {
|
||||||
|
override fun iterator(): Iterator<IndexedValue<T>> = IndexingIterator(iteratorFactory())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterator transforming original `iterator` into iterator of [IndexedValue], counting index from zero.
|
||||||
|
*/
|
||||||
|
public class IndexingIterator<out T>(private val iterator: Iterator<T>) : Iterator<IndexedValue<T>> {
|
||||||
|
private var index = 0
|
||||||
|
final override fun hasNext(): Boolean = iterator.hasNext()
|
||||||
|
final override fun next(): IndexedValue<T> = IndexedValue(index++, iterator.next())
|
||||||
|
}
|
||||||
@@ -26,27 +26,3 @@ public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
|
|||||||
for (element in this) operation(element)
|
for (element in this) operation(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Data class representing a value from a collection or sequence, along with its index in that collection or sequence.
|
|
||||||
*
|
|
||||||
* @property value the underlying value.
|
|
||||||
* @property index the index of the value in the collection or sequence.
|
|
||||||
*/
|
|
||||||
public data class IndexedValue<out T>(public val index: Int, public val value: T)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A wrapper over another [Iterable] (or any other object that can produce an [Iterator]) that returns
|
|
||||||
* an indexing iterator.
|
|
||||||
*/
|
|
||||||
public class IndexingIterable<out T>(private val iteratorFactory: () -> Iterator<T>) : Iterable<IndexedValue<T>> {
|
|
||||||
override fun iterator(): Iterator<IndexedValue<T>> = IndexingIterator(iteratorFactory())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Iterator transforming original `iterator` into iterator of [IndexedValue], counting index from zero.
|
|
||||||
*/
|
|
||||||
public class IndexingIterator<out T>(private val iterator: Iterator<T>) : Iterator<IndexedValue<T>> {
|
|
||||||
private var index = 0
|
|
||||||
final override fun hasNext(): Boolean = iterator.hasNext()
|
|
||||||
final override fun next(): IndexedValue<T> = IndexedValue(index++, iterator.next())
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user