Support 'windowed' intermediate call
This commit is contained in:
committed by
Yan Zhulanow
parent
cc549d3ae5
commit
449fa8f3ec
+2
@@ -3,6 +3,7 @@ package com.intellij.debugger.streams.kotlin.lib.sequence
|
||||
|
||||
import com.intellij.debugger.streams.kotlin.resolve.ChunkedResolver
|
||||
import com.intellij.debugger.streams.kotlin.resolve.MapNotNullResolver
|
||||
import com.intellij.debugger.streams.kotlin.resolve.WindowedResolver
|
||||
import com.intellij.debugger.streams.lib.IntermediateOperation
|
||||
import com.intellij.debugger.streams.lib.impl.*
|
||||
import com.intellij.debugger.streams.resolve.AppendResolver
|
||||
@@ -35,6 +36,7 @@ class KotlinSequencesSupport : LibrarySupportBase() {
|
||||
|
||||
addIntermediateOperationsSupport(OrderBasedOperation("mapNotNull", MapNotNullResolver()))
|
||||
addIntermediateOperationsSupport(OrderBasedOperation("chunked", ChunkedResolver()))
|
||||
addIntermediateOperationsSupport(OrderBasedOperation("windowed", WindowedResolver()))
|
||||
}
|
||||
|
||||
private fun filterOperations(vararg names: String): Array<IntermediateOperation> =
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.debugger.streams.kotlin.resolve
|
||||
|
||||
import com.intellij.debugger.streams.resolve.ValuesOrderResolver
|
||||
import com.intellij.debugger.streams.trace.TraceElement
|
||||
import com.intellij.debugger.streams.trace.TraceInfo
|
||||
|
||||
/**
|
||||
* @author Vitaliy.Bibaev
|
||||
*/
|
||||
class WindowedResolver : ValuesOrderResolver {
|
||||
override fun resolve(info: TraceInfo): ValuesOrderResolver.Result {
|
||||
val indexBefore = info.valuesOrderBefore
|
||||
val indexAfter = info.valuesOrderAfter
|
||||
|
||||
val timesBefore = indexBefore.keys.sorted().toIntArray()
|
||||
val timesAfter = indexAfter.keys.sorted().toIntArray()
|
||||
|
||||
if (timesAfter.isEmpty()) return emptyTransitions(indexBefore)
|
||||
|
||||
val direct = mutableMapOf<TraceElement, MutableList<TraceElement>>()
|
||||
val reverse = mutableMapOf<TraceElement, List<TraceElement>>()
|
||||
|
||||
var windowStartIndex = 0
|
||||
var windowEndIndex = calcWindowSize(timesBefore, timesAfter)
|
||||
for (timeAfter in timesAfter) {
|
||||
if (windowEndIndex == timesAfter.size) {
|
||||
windowStartIndex += 1
|
||||
} else {
|
||||
while (windowEndIndex < timesBefore.size && timesBefore[windowEndIndex] < timeAfter) {
|
||||
windowStartIndex += 1
|
||||
windowEndIndex += 1
|
||||
}
|
||||
}
|
||||
|
||||
val window = (windowStartIndex until windowEndIndex).asSequence()
|
||||
.map { indexBefore[timesBefore[it]]!! }
|
||||
.toList()
|
||||
val mappedElement = indexAfter[timeAfter]!!
|
||||
window.forEach { direct.computeIfAbsent(it, { mutableListOf() }).add(mappedElement) }
|
||||
reverse[mappedElement] = window
|
||||
}
|
||||
|
||||
return ValuesOrderResolver.Result.of(direct, reverse)
|
||||
}
|
||||
|
||||
private fun calcWindowSize(before: IntArray, after: IntArray): Int {
|
||||
var size = 0
|
||||
while (size < before.size && before[size] < after[0]) size += 1
|
||||
return size
|
||||
}
|
||||
|
||||
private fun emptyTransitions(indexBefore: MutableMap<Int, TraceElement>): ValuesOrderResolver.Result {
|
||||
val direct = indexBefore.asSequence().sortedBy { it.key }.associate { it.value to emptyList<TraceElement>() }
|
||||
return ValuesOrderResolver.Result.of(direct, emptyMap())
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,39 @@ LineBreakpoint created at Windowed.kt:5
|
||||
Run Java
|
||||
Connected to the target VM
|
||||
Windowed.kt:5
|
||||
Exception caught: junit.framework.AssertionFailedError: Unresolved reference: it, Unresolved reference: it
|
||||
intArrayOf(1, 1, 1, 1, 1, 1, 1).asSequence()
|
||||
.windowed(3, transform = { it.sum() })
|
||||
.count()
|
||||
windowed
|
||||
before: 1,2,3,5,7,9,11
|
||||
after: 4,6,8,10,12
|
||||
count
|
||||
before: 4,6,8,10,12
|
||||
after: nothing
|
||||
mappings for windowed
|
||||
direct:
|
||||
1 -> 4
|
||||
2 -> 4,6
|
||||
3 -> 4,6,8
|
||||
5 -> 6,8,10
|
||||
7 -> 8,10,12
|
||||
9 -> nothing
|
||||
11 -> nothing
|
||||
reverse:
|
||||
1,2,3 <- 4
|
||||
2,3,5 <- 6
|
||||
3,5,7 <- 8
|
||||
5,7 <- 10
|
||||
7 <- 12
|
||||
mappings for count
|
||||
direct:
|
||||
4 -> nothing
|
||||
6 -> nothing
|
||||
8 -> nothing
|
||||
10 -> nothing
|
||||
12 -> nothing
|
||||
reverse:
|
||||
empty
|
||||
Disconnected from the target VM
|
||||
|
||||
WRONG!
|
||||
|
||||
Process finished with exit code 0
|
||||
|
||||
+31
-3
@@ -2,9 +2,37 @@ LineBreakpoint created at WindowedWithPartial.kt:5
|
||||
Run Java
|
||||
Connected to the target VM
|
||||
WindowedWithPartial.kt:5
|
||||
Exception caught: junit.framework.AssertionFailedError: Expected: <null> but was: CHAIN_CONSTRUCTION, Expected: <null> but was: CHAIN_CONSTRUCTION
|
||||
listOf(1, 1, 1, 1, 1).asSequence()
|
||||
.windowed(3, partialWindows = true, transform = { it.size })
|
||||
.count()
|
||||
windowed
|
||||
before: 1,2,3,5,7
|
||||
after: 4,6,8,9,10
|
||||
count
|
||||
before: 4,6,8,9,10
|
||||
after: nothing
|
||||
mappings for windowed
|
||||
direct:
|
||||
1 -> 4
|
||||
2 -> 4,6
|
||||
3 -> 4,6,8
|
||||
5 -> 6,8,9
|
||||
7 -> 8,9,10
|
||||
reverse:
|
||||
1,2,3 <- 4
|
||||
2,3,5 <- 6
|
||||
3,5,7 <- 8
|
||||
5,7 <- 9
|
||||
7 <- 10
|
||||
mappings for count
|
||||
direct:
|
||||
4 -> nothing
|
||||
6 -> nothing
|
||||
8 -> nothing
|
||||
9 -> nothing
|
||||
10 -> nothing
|
||||
reverse:
|
||||
empty
|
||||
Disconnected from the target VM
|
||||
|
||||
WRONG!
|
||||
|
||||
Process finished with exit code 0
|
||||
|
||||
@@ -2,5 +2,5 @@ package misc
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// Breakpoint!
|
||||
intArrayOf(1, 1, 1, 1, 1, 1, 1).asSequence().windowed(3) { it.sum() }.count()
|
||||
intArrayOf(1, 1, 1, 1, 1, 1, 1).asSequence().windowed(3, transform = { it.sum() }) .count()
|
||||
}
|
||||
+1
-1
@@ -2,5 +2,5 @@ package misc
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// Breakpoint!
|
||||
listOf(1, 1, 1, 1, 1).windowed(3, partialWindows = true) { it.size }.count()
|
||||
listOf(1, 1, 1, 1, 1).asSequence().windowed(3, partialWindows = true, transform = { it.size }).count()
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package sort
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
@@ -6,4 +5,4 @@ fun main(args: Array<String>) {
|
||||
arrayOf(Person("Bob", 42), Person("Alice", 27)).asSequence().sortedBy { it.age }.count()
|
||||
}
|
||||
|
||||
internal data class Person(val name: String, val age: Int)
|
||||
data class Person(val name: String, val age: Int)
|
||||
Reference in New Issue
Block a user