windowed function: add default parameters, drop or keep partial windows, KEEP-11

Make step default to 1.
Add partialWindows boolean parameter defaulting to false to control
whether to keep partial windows in the end.
This commit is contained in:
Ilya Gorbunov
2017-07-27 00:57:58 +03:00
parent c04b0072af
commit 559255f38e
17 changed files with 336 additions and 142 deletions
@@ -1029,12 +1029,33 @@ public expect inline fun <T> Collection<T>.plusElement(element: T): List<T>
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public expect fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>>
public fun <T> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List<List<T>> {
checkWindowSizeStep(size, step)
if (this is RandomAccess && this is List) {
val thisSize = this.size
val result = ArrayList<List<T>>((thisSize + step - 1) / step)
var index = 0
while (index < thisSize) {
val windowSize = size.coerceAtMost(thisSize - index)
if (windowSize < size && !partialWindows) break
result.add(List(windowSize) { this[it + index] })
index += step
}
return result
}
val result = ArrayList<List<T>>()
windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = false).forEach {
result.add(it)
}
return result
}
/**
* Returns a list of results of applying the given [transform] function to
@@ -1047,12 +1068,34 @@ public expect fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>>
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public expect fun <T, R> Iterable<T>.windowed(size: Int, step: Int, transform: (List<T>) -> R): List<R>
public fun <T, R> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List<T>) -> R): List<R> {
checkWindowSizeStep(size, step)
if (this is RandomAccess && this is List) {
val thisSize = this.size
val result = ArrayList<R>((thisSize + step - 1) / step)
val window = MovingSubList(this)
var index = 0
while (index < thisSize) {
window.move(index, (index + size).coerceAtMost(thisSize))
if (!partialWindows && window.size < size) break
result.add(transform(window))
index += step
}
return result
}
val result = ArrayList<R>()
windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = true).forEach {
result.add(transform(it))
}
return result
}
/**
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
@@ -952,12 +952,16 @@ public expect inline fun <T> Sequence<T>.plusElement(element: T): Sequence<T>
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public expect fun <T> Sequence<T>.windowed(size: Int, step: Int): Sequence<List<T>>
public fun <T> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence<List<T>> {
return windowedSequence(size, step, partialWindows, reuseBuffer = false)
}
/**
* Returns a sequence of results of applying the given [transform] function to
@@ -970,12 +974,16 @@ public expect fun <T> Sequence<T>.windowed(size: Int, step: Int): Sequence<List<
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public expect fun <T, R> Sequence<T>.windowed(size: Int, step: Int, transform: (List<T>) -> R): Sequence<R>
public fun <T, R> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List<T>) -> R): Sequence<R> {
return windowedSequence(size, step, partialWindows, reuseBuffer = true).map(transform)
}
/**
* Returns a sequence of pairs built from elements of both sequences with same indexes.
@@ -727,12 +727,16 @@ public expect inline fun String.partition(predicate: (Char) -> Boolean): Pair<St
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public expect fun CharSequence.windowed(size: Int, step: Int): List<String>
public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List<String> {
return windowed(size, step, partialWindows) { it.toString() }
}
/**
* Returns a list of results of applying the given [transform] function to
@@ -745,12 +749,26 @@ public expect fun CharSequence.windowed(size: Int, step: Int): List<String>
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public expect fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSequence) -> R): List<R>
public fun <R> CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): List<R> {
checkWindowSizeStep(size, step)
val thisSize = this.length
val result = ArrayList<R>((thisSize + step - 1) / step)
var index = 0
while (index < thisSize) {
val end = index + size
val coercedEnd = if (end > thisSize) { if (partialWindows) thisSize else break } else end
result.add(transform(subSequence(index, coercedEnd)))
index += step
}
return result
}
/**
* Returns a sequence of snapshots of the window of the given [size]
@@ -761,12 +779,16 @@ public expect fun <R> CharSequence.windowed(size: Int, step: Int, transform: (Ch
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public expect fun CharSequence.windowedSequence(size: Int, step: Int): Sequence<String>
public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence<String> {
return windowedSequence(size, step, partialWindows) { it.toString() }
}
/**
* Returns a sequence of results of applying the given [transform] function to
@@ -779,12 +801,18 @@ public expect fun CharSequence.windowedSequence(size: Int, step: Int): Sequence<
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public expect fun <R> CharSequence.windowedSequence(size: Int, step: Int, transform: (CharSequence) -> R): Sequence<R>
public fun <R> CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): Sequence<R> {
checkWindowSizeStep(size, step)
val windows = (if (partialWindows) indices else 0 until length - size + 1) step step
return windows.asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) }
}
/**
* Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence.
@@ -153,7 +153,10 @@ class Sequences {
val moreSparseWindows = sequence.windowed(size = 5, step = 3)
assertPrints(moreSparseWindows.take(4).toList(), "[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [10, 11, 12, 13, 14]]")
val partialWindows = sequence.take(10).windowed(size = 5, step = 3)
val fullWindows = sequence.take(10).windowed(size = 5, step = 3)
assertPrints(fullWindows.toList(), "[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]]")
val partialWindows = sequence.take(10).windowed(size = 5, step = 3, partialWindows = true)
assertPrints(partialWindows.toList(), "[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10], [10]]")
}
@@ -161,10 +164,10 @@ class Sequences {
fun averageWindows() {
val dataPoints = sequenceOf(10, 15, 18, 25, 19, 21, 14, 8, 5)
val averaged = dataPoints.windowed(size = 4, step = 1) { window -> window.average() }
val averaged = dataPoints.windowed(size = 4, step = 1, partialWindows = true) { window -> window.average() }
assertPrints(averaged.toList(), "[17.0, 19.25, 20.75, 19.75, 15.5, 12.0, 9.0, 6.5, 5.0]")
val averagedNoPartialWindows = dataPoints.windowed(size = 4, step = 1).filter { it.size == 4 }.map { it.average() }
val averagedNoPartialWindows = dataPoints.windowed(size = 4, step = 1).map { it.average() }
assertPrints(averagedNoPartialWindows.toList(), "[17.0, 19.25, 20.75, 19.75, 15.5, 12.0]")
}
}
+16 -9
View File
@@ -1809,7 +1809,7 @@ public fun <T : Any> List<T?>.requireNoNulls(): List<T> {
*/
@SinceKotlin("1.2")
public fun <T> Iterable<T>.chunked(size: Int): List<List<T>> {
return windowed(size, size)
return windowed(size, size, partialWindows = true)
}
/**
@@ -1828,7 +1828,7 @@ public fun <T> Iterable<T>.chunked(size: Int): List<List<T>> {
*/
@SinceKotlin("1.2")
public fun <T, R> Iterable<T>.chunked(size: Int, transform: (List<T>) -> R): List<R> {
return windowed(size, size, transform)
return windowed(size, size, partialWindows = true, transform = transform)
}
/**
@@ -2021,25 +2021,29 @@ public inline fun <T> Collection<T>.plusElement(element: T): List<T> {
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>> {
public fun <T> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List<List<T>> {
checkWindowSizeStep(size, step)
if (this is RandomAccess && this is List) {
val thisSize = this.size
val result = ArrayList<List<T>>((thisSize + step - 1) / step)
var index = 0
while (index < thisSize) {
result.add(List(size.coerceAtMost(thisSize - index)) { this[it + index] })
val windowSize = size.coerceAtMost(thisSize - index)
if (windowSize < size && !partialWindows) break
result.add(List(windowSize) { this[it + index] })
index += step
}
return result
}
val result = ArrayList<List<T>>()
windowedIterator(iterator(), size, step, dropTrailing = false, reuseBuffer = false).forEach {
windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = false).forEach {
result.add(it)
}
return result
@@ -2056,12 +2060,14 @@ public fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>> {
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <T, R> Iterable<T>.windowed(size: Int, step: Int, transform: (List<T>) -> R): List<R> {
public fun <T, R> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List<T>) -> R): List<R> {
checkWindowSizeStep(size, step)
if (this is RandomAccess && this is List) {
val thisSize = this.size
@@ -2070,13 +2076,14 @@ public fun <T, R> Iterable<T>.windowed(size: Int, step: Int, transform: (List<T>
var index = 0
while (index < thisSize) {
window.move(index, (index + size).coerceAtMost(thisSize))
if (!partialWindows && window.size < size) break
result.add(transform(window))
index += step
}
return result
}
val result = ArrayList<R>()
windowedIterator(iterator(), size, step, dropTrailing = false, reuseBuffer = true).forEach {
windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = true).forEach {
result.add(transform(it))
}
return result
+12 -8
View File
@@ -1344,7 +1344,7 @@ public fun <T : Any> Sequence<T?>.requireNoNulls(): Sequence<T> {
*/
@SinceKotlin("1.2")
public fun <T> Sequence<T>.chunked(size: Int): Sequence<List<T>> {
return windowed(size, size)
return windowed(size, size, partialWindows = true)
}
/**
@@ -1365,7 +1365,7 @@ public fun <T> Sequence<T>.chunked(size: Int): Sequence<List<T>> {
*/
@SinceKotlin("1.2")
public fun <T, R> Sequence<T>.chunked(size: Int, transform: (List<T>) -> R): Sequence<R> {
return windowed(size, size, transform)
return windowed(size, size, partialWindows = true, transform = transform)
}
/**
@@ -1546,13 +1546,15 @@ public inline fun <T> Sequence<T>.plusElement(element: T): Sequence<T> {
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun <T> Sequence<T>.windowed(size: Int, step: Int): Sequence<List<T>> {
return windowedSequence(size, step, dropTrailing = false, reuseBuffer = false)
public fun <T> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence<List<T>> {
return windowedSequence(size, step, partialWindows, reuseBuffer = false)
}
/**
@@ -1566,13 +1568,15 @@ public fun <T> Sequence<T>.windowed(size: Int, step: Int): Sequence<List<T>> {
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <T, R> Sequence<T>.windowed(size: Int, step: Int, transform: (List<T>) -> R): Sequence<R> {
return windowedSequence(size, step, dropTrailing = false, reuseBuffer = true).map(transform)
public fun <T, R> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List<T>) -> R): Sequence<R> {
return windowedSequence(size, step, partialWindows, reuseBuffer = true).map(transform)
}
/**
+26 -15
View File
@@ -1121,7 +1121,7 @@ public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double {
*/
@SinceKotlin("1.2")
public fun CharSequence.chunked(size: Int): List<String> {
return windowed(size, size)
return windowed(size, size, partialWindows = true)
}
/**
@@ -1140,7 +1140,7 @@ public fun CharSequence.chunked(size: Int): List<String> {
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List<R> {
return windowed(size, size, transform)
return windowed(size, size, partialWindows = true, transform = transform)
}
/**
@@ -1173,7 +1173,7 @@ public fun CharSequence.chunkedSequence(size: Int): Sequence<String> {
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence<R> {
return windowedSequence(size, size, transform)
return windowedSequence(size, size, partialWindows = true, transform = transform)
}
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@@ -1233,13 +1233,15 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, S
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun CharSequence.windowed(size: Int, step: Int): List<String> {
return windowed(size, step) { it.toString() }
public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List<String> {
return windowed(size, step, partialWindows) { it.toString() }
}
/**
@@ -1253,18 +1255,22 @@ public fun CharSequence.windowed(size: Int, step: Int): List<String> {
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSequence) -> R): List<R> {
public fun <R> CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): List<R> {
checkWindowSizeStep(size, step)
val thisSize = this.length
val result = ArrayList<R>((thisSize + step - 1) / step)
var index = 0
while (index < thisSize) {
result.add(transform(subSequence(index, (index + size).coerceAtMost(thisSize))))
val end = index + size
val coercedEnd = if (end > thisSize) { if (partialWindows) thisSize else break } else end
result.add(transform(subSequence(index, coercedEnd)))
index += step
}
return result
@@ -1279,13 +1285,15 @@ public fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSeque
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun CharSequence.windowedSequence(size: Int, step: Int): Sequence<String> {
return windowedSequence(size, step) { it.toString() }
public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence<String> {
return windowedSequence(size, step, partialWindows) { it.toString() }
}
/**
@@ -1299,14 +1307,17 @@ public fun CharSequence.windowedSequence(size: Int, step: Int): Sequence<String>
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step
* @param step the number of elements to move the window forward by on an each step, by default 1
* @param partialWindows controls whether or not to keep partial windows in the end if any,
* by default `false` which means partial windows won't be preserved
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.windowedSequence(size: Int, step: Int, transform: (CharSequence) -> R): Sequence<R> {
public fun <R> CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): Sequence<R> {
checkWindowSizeStep(size, step)
return (indices step step).asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) }
val windows = (if (partialWindows) indices else 0 until length - size + 1) step step
return windows.asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) }
}
/**
@@ -27,12 +27,12 @@ internal fun checkWindowSizeStep(size: Int, step: Int) {
}
}
internal fun <T> Sequence<T>.windowedSequence(size: Int, step: Int, dropTrailing: Boolean, reuseBuffer: Boolean): Sequence<List<T>> {
internal fun <T> Sequence<T>.windowedSequence(size: Int, step: Int, partialWindows: Boolean, reuseBuffer: Boolean): Sequence<List<T>> {
checkWindowSizeStep(size, step)
return Sequence { windowedIterator(iterator(), size, step, dropTrailing, reuseBuffer) }
return Sequence { windowedIterator(iterator(), size, step, partialWindows, reuseBuffer) }
}
internal fun <T> windowedIterator(iterator: Iterator<T>, size: Int, step: Int, dropTrailing: Boolean, reuseBuffer: Boolean): Iterator<List<T>> {
internal fun <T> windowedIterator(iterator: Iterator<T>, size: Int, step: Int, partialWindows: Boolean, reuseBuffer: Boolean): Iterator<List<T>> {
if (!iterator.hasNext()) return EmptyIterator
return buildIterator<List<T>> {
val gap = step - size
@@ -49,7 +49,7 @@ internal fun <T> windowedIterator(iterator: Iterator<T>, size: Int, step: Int, d
}
}
if (buffer.isNotEmpty()) {
if (!dropTrailing || buffer.size == size) yield(buffer)
if (partialWindows || buffer.size == size) yield(buffer)
}
} else {
val buffer = RingBuffer<T>(size)
@@ -60,7 +60,7 @@ internal fun <T> windowedIterator(iterator: Iterator<T>, size: Int, step: Int, d
buffer.removeFirst(step)
}
}
if (!dropTrailing) {
if (partialWindows) {
while (buffer.size > step) {
yield(if (reuseBuffer) buffer else ArrayList(buffer))
buffer.removeFirst(step)
@@ -167,23 +167,36 @@ abstract class OrderedIterableTests<T : Iterable<String>>(createFrom: (Array<out
val size = 7
val data = createFrom(Array(size) { "$it" })
val result = data.windowed(4, 2)
assertEquals(listOf(
listOf("0", "1", "2", "3"),
listOf("2", "3", "4", "5")
), result)
val resultPartial = data.windowed(4, 2, partialWindows = true)
assertEquals(listOf(
listOf("0", "1", "2", "3"),
listOf("2", "3", "4", "5"),
listOf("4", "5", "6"),
listOf("6")
), result)
), resultPartial)
val result2 = data.windowed(2, 3) { it.joinToString("") }
assertEquals(listOf("01", "34", "6"), result2)
assertEquals(listOf("01", "34"), result2)
assertEquals(data.chunked(2), data.windowed(2, 2))
val result2partial = data.windowed(2, 3, partialWindows = true) { it.joinToString("") }
assertEquals(listOf("01", "34", "6"), result2partial)
assertEquals(data.chunked(2), data.windowed(2, 2, partialWindows = true))
assertEquals(data.take(2), data.windowed(2, size).single())
assertEquals(data.take(3), data.windowed(3, size + 3).single())
val result3 = data.windowed(size, 1)
result3.forEachIndexed { index, window ->
assertEquals(data.toList(), data.windowed(size, 1).single())
assertTrue(data.windowed(size + 1, 1).isEmpty())
val result3partial = data.windowed(size, 1, partialWindows = true)
result3partial.forEachIndexed { index, window ->
assertEquals(size - index, window.size, "size of window#$index")
}
@@ -259,23 +259,36 @@ public class SequenceTest {
val seq = infiniteSeq.take(7)
val result1 = seq.windowed(4, 2)
assertEquals(listOf(
listOf(0, 1, 2, 3),
listOf(2, 3, 4, 5)
), result1.toList())
val result1partial = seq.windowed(4, 2, partialWindows = true)
assertEquals(listOf(
listOf(0, 1, 2, 3),
listOf(2, 3, 4, 5),
listOf(4, 5, 6),
listOf(6)
), result1.toList())
), result1partial.toList())
val result2 = seq.windowed(2, 3) { it.joinToString("") }
assertEquals(listOf("01", "34", "6"), result2.toList())
assertEquals(listOf("01", "34"), result2.toList())
assertEquals(seq.chunked(2).toList(), seq.windowed(2, 2).toList())
val result2partial = seq.windowed(2, 3, partialWindows = true) { it.joinToString("") }
assertEquals(listOf("01", "34", "6"), result2partial.toList())
assertEquals(seq.chunked(2).toList(), seq.windowed(2, 2, partialWindows = true).toList())
assertEquals(seq.take(2).toList(), seq.windowed(2, size).single())
assertEquals(seq.take(3).toList(), seq.windowed(3, size + 3).single())
val result3 = seq.windowed(size, 1)
result3.forEachIndexed { index, window ->
assertEquals(seq.toList(), seq.windowed(size, 1).single())
assertTrue(seq.windowed(size + 1, 1).none())
val result3partial = seq.windowed(size, 1, partialWindows = true)
result3partial.forEachIndexed { index, window ->
assertEquals(size - index, window.size, "size of window#$index")
}
+19 -7
View File
@@ -884,18 +884,27 @@ class StringTest {
val size = 7
val data = arg1("abcdefg")
val result = data.windowed(4, 2)
assertEquals(listOf("abcd", "cdef", "efg", "g"), result)
assertEquals(listOf("abcd", "cdef"), result)
val resultPartial = data.windowed(4, 2, partialWindows = true)
assertEquals(listOf("abcd", "cdef", "efg", "g"), resultPartial)
val result2 = data.windowed(2, 3) { it.reversed().toString() }
assertEquals(listOf("ba", "ed", "g"), result2)
assertEquals(listOf("ba", "ed"), result2)
val result2partial = data.windowed(2, 3, partialWindows = true) { it.reversed().toString() }
assertEquals(listOf("ba", "ed", "g"), result2partial)
assertEquals(data.chunked(2), data.windowed(2, 2))
assertEquals(data.chunked(2), data.windowed(2, 2, partialWindows = true))
assertEquals(data.take(2), data.windowed(2, size).single())
assertEquals(data.take(3), data.windowed(3, size + 3).single())
val result3 = data.windowed(size, 1)
result3.forEachIndexed { index, window ->
assertEquals(data.toString(), data.windowed(size, 1).single())
assertTrue(data.windowed(size + 1, 1).isEmpty())
val result3partial = data.windowed(size, 1, partialWindows = true)
result3partial.forEachIndexed { index, window ->
assertEquals(size - index, window.length, "size of window#$index")
}
@@ -907,8 +916,11 @@ class StringTest {
}
for (window in 1..size + 1) {
for (step in 1..size + 1)
compare(data.windowed(window, step).iterator(), data.windowedSequence(window, step).iterator()) { iteratorBehavior() }
for (step in 1..size + 1) {
compare(data.windowed(window, step).iterator(), data.windowedSequence(window, step).iterator()) { iteratorBehavior() }
compare(data.windowed(window, step, partialWindows = true).iterator(),
data.windowedSequence(window, step, partialWindows = true).iterator()) { iteratorBehavior() }
}
}
}