From 449fa8f3ec23d88ff86947e35e7406c89ec02232 Mon Sep 17 00:00:00 2001 From: "Vitaliy.Bibaev" Date: Fri, 22 Dec 2017 11:06:39 +0300 Subject: [PATCH] Support 'windowed' intermediate call --- .../lib/sequence/KotlinSequencesSupport.kt | 2 + .../kotlin/resolve/WindowedResolver.kt | 57 +++++++++++++++++++ .../sequence/exec/sequence/outs/windowed.out | 36 +++++++++++- .../sequence/outs/windowedWithPartial.out | 34 ++++++++++- .../exec/sequence/src/misc/Windowed.kt | 2 +- .../sequence/src/misc/WindowedWithPartial.kt | 2 +- .../exec/sequence/src/sort/SortedBy.kt | 3 +- 7 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/WindowedResolver.kt diff --git a/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/lib/sequence/KotlinSequencesSupport.kt b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/lib/sequence/KotlinSequencesSupport.kt index 1ef29da4f1c..5e48a2665d2 100644 --- a/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/lib/sequence/KotlinSequencesSupport.kt +++ b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/lib/sequence/KotlinSequencesSupport.kt @@ -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 = diff --git a/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/WindowedResolver.kt b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/WindowedResolver.kt new file mode 100644 index 00000000000..821e1e87714 --- /dev/null +++ b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/WindowedResolver.kt @@ -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>() + val reverse = mutableMapOf>() + + 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): ValuesOrderResolver.Result { + val direct = indexBefore.asSequence().sortedBy { it.key }.associate { it.value to emptyList() } + return ValuesOrderResolver.Result.of(direct, emptyMap()) + } +} \ No newline at end of file diff --git a/idea/testData/debugger/sequence/exec/sequence/outs/windowed.out b/idea/testData/debugger/sequence/exec/sequence/outs/windowed.out index fe87b8d4e0b..dd002c60cde 100644 --- a/idea/testData/debugger/sequence/exec/sequence/outs/windowed.out +++ b/idea/testData/debugger/sequence/exec/sequence/outs/windowed.out @@ -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 diff --git a/idea/testData/debugger/sequence/exec/sequence/outs/windowedWithPartial.out b/idea/testData/debugger/sequence/exec/sequence/outs/windowedWithPartial.out index 2afc5120e37..1e98f10a342 100644 --- a/idea/testData/debugger/sequence/exec/sequence/outs/windowedWithPartial.out +++ b/idea/testData/debugger/sequence/exec/sequence/outs/windowedWithPartial.out @@ -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: but was: CHAIN_CONSTRUCTION, Expected: 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 diff --git a/idea/testData/debugger/sequence/exec/sequence/src/misc/Windowed.kt b/idea/testData/debugger/sequence/exec/sequence/src/misc/Windowed.kt index 44fc63c6480..fbce74e23b0 100644 --- a/idea/testData/debugger/sequence/exec/sequence/src/misc/Windowed.kt +++ b/idea/testData/debugger/sequence/exec/sequence/src/misc/Windowed.kt @@ -2,5 +2,5 @@ package misc fun main(args: Array) { // 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() } \ No newline at end of file diff --git a/idea/testData/debugger/sequence/exec/sequence/src/misc/WindowedWithPartial.kt b/idea/testData/debugger/sequence/exec/sequence/src/misc/WindowedWithPartial.kt index 0b6e4562687..42c52ee8e64 100644 --- a/idea/testData/debugger/sequence/exec/sequence/src/misc/WindowedWithPartial.kt +++ b/idea/testData/debugger/sequence/exec/sequence/src/misc/WindowedWithPartial.kt @@ -2,5 +2,5 @@ package misc fun main(args: Array) { // 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() } \ No newline at end of file diff --git a/idea/testData/debugger/sequence/exec/sequence/src/sort/SortedBy.kt b/idea/testData/debugger/sequence/exec/sequence/src/sort/SortedBy.kt index d7531718e0c..62032c8a488 100644 --- a/idea/testData/debugger/sequence/exec/sequence/src/sort/SortedBy.kt +++ b/idea/testData/debugger/sequence/exec/sequence/src/sort/SortedBy.kt @@ -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) { @@ -6,4 +5,4 @@ fun main(args: Array) { arrayOf(Person("Bob", 42), Person("Alice", 27)).asSequence().sortedBy { it.age }.count() } -internal data class Person(val name: String, val age: Int) \ No newline at end of file +data class Person(val name: String, val age: Int) \ No newline at end of file