From 3a97e16bdd2b0907c34f756b93b81a29ec4aa2f0 Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Tue, 11 Jun 2024 23:37:31 +0200 Subject: [PATCH] refactor: extract to local variable to avoid multiple computations --- .../myers/MyersDiffWithLinearSpace.kt | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt index 71c8c34..77cb0b7 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt @@ -64,28 +64,29 @@ public class MyersDiffWithLinearSpace( ++i ++j } else { + // index is less than 0 here if data.script is empty + val index = data.script.size - 1 + // TODO: compress these commands if (end1 - start1 > end2 - start2) { - if (data.script.isEmpty() || - data.script[data.script.size - 1].endOriginal != i || - data.script[data.script.size - 1].deltaType != DeltaType.DELETE + if (index < 0 || + data.script[index].endOriginal != i || + data.script[index].deltaType != DeltaType.DELETE ) { data.script.add(Change(DeltaType.DELETE, i, i + 1, j, j)) } else { - data.script[data.script.size - 1] = - data.script[data.script.size - 1].copy(endOriginal = i + 1) + data.script[index] = data.script[index].copy(endOriginal = i + 1) } ++i } else { - if (data.script.isEmpty() || - data.script[data.script.size - 1].endRevised != j || - data.script[data.script.size - 1].deltaType != DeltaType.INSERT + if (index < 0 || + data.script[index].endRevised != j || + data.script[index].deltaType != DeltaType.INSERT ) { data.script.add(Change(DeltaType.INSERT, i, i, j, j + 1)) } else { - data.script[data.script.size - 1] = - data.script[data.script.size - 1].copy(endRevised = j + 1) + data.script[index] = data.script[index].copy(endRevised = j + 1) } ++j