Move IndexedValue, IndexingIterable and IndexingIterator back to kotlin package

This commit is contained in:
Ilya Gorbunov
2015-12-13 17:28:53 +03:00
parent 871c5c66b4
commit 91b920f18e
2 changed files with 26 additions and 24 deletions
@@ -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)
}
/**
* 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())
}