From 85d83309ae61defa5da811d44641923608d33d1f Mon Sep 17 00:00:00 2001 From: "Vitaliy.Bibaev" Date: Thu, 21 Dec 2017 16:10:13 +0300 Subject: [PATCH] Support mapNotNull intermediate operation --- .../lib/sequence/KotlinSequencesSupport.kt | 3 ++ .../kotlin/resolve/MapNotNullResolver.kt | 40 +++++++++++++++++++ .../exec/sequence/outs/mapNotNull.out | 10 ++--- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/MapNotNullResolver.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 d1f555398ad..0328141a44a 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 @@ -1,6 +1,7 @@ // 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.lib.sequence +import com.intellij.debugger.streams.kotlin.resolve.MapNotNullResolver import com.intellij.debugger.streams.lib.IntermediateOperation import com.intellij.debugger.streams.lib.impl.* import com.intellij.debugger.streams.resolve.AppendResolver @@ -30,6 +31,8 @@ class KotlinSequencesSupport : LibrarySupportBase() { addIntermediateOperationsSupport(ConcatOperation("plusElement", AppendResolver())) addIntermediateOperationsSupport(OrderBasedOperation("zipWithNext", PairMapResolver())) + + addIntermediateOperationsSupport(OrderBasedOperation("mapNotNull", MapNotNullResolver())) } private fun filterOperations(vararg names: String): Array = diff --git a/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/MapNotNullResolver.kt b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/MapNotNullResolver.kt new file mode 100644 index 00000000000..e22d26ced72 --- /dev/null +++ b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/resolve/MapNotNullResolver.kt @@ -0,0 +1,40 @@ +// 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 MapNotNullResolver : ValuesOrderResolver { + override fun resolve(info: TraceInfo): ValuesOrderResolver.Result { + val before = info.valuesOrderBefore + val after = info.valuesOrderAfter + + val invertedOrder = mutableMapOf() + val beforeTimes = before.keys.sorted().toIntArray() + val afterTimes = after.keys.sorted().toIntArray() + var beforeIndex = 0 + for (afterTime in afterTimes) { + while (beforeIndex < beforeTimes.size && afterTime > beforeTimes[beforeIndex]) beforeIndex += 1 + val beforeTime = beforeTimes[beforeIndex - 1] + if (beforeTime < afterTime) { + invertedOrder[afterTime] = beforeTime + } + } + + val direct = mutableMapOf>() + val reverse = mutableMapOf>() + + for ((afterTime, beforeTime) in invertedOrder) { + val beforeElement = before.getValue(beforeTime) + val afterElement = after.getValue(afterTime) + direct[beforeElement] = listOf(afterElement) + reverse[afterElement] = listOf(beforeElement) + } + + return ValuesOrderResolver.Result.of(direct, reverse) + } +} \ No newline at end of file diff --git a/idea/testData/debugger/sequence/exec/sequence/outs/mapNotNull.out b/idea/testData/debugger/sequence/exec/sequence/outs/mapNotNull.out index 992121ab9cb..05a8e9c345e 100644 --- a/idea/testData/debugger/sequence/exec/sequence/outs/mapNotNull.out +++ b/idea/testData/debugger/sequence/exec/sequence/outs/mapNotNull.out @@ -13,21 +13,19 @@ toList after: nothing mappings for mapNotNull direct: - 1 -> nothing + 1 -> 2 3 -> nothing 4 -> nothing - 5 -> nothing + 5 -> 6 reverse: - nothing <- 2 - nothing <- 6 + 1 <- 2 + 5 <- 6 mappings for toList direct: 2 -> nothing 6 -> nothing reverse: empty - -WRONG! Support will be implemented later Disconnected from the target VM Process finished with exit code 0