Skip to content

Commit

Permalink
refactor: extract to local variable to avoid multiple computations
Browse files Browse the repository at this point in the history
  • Loading branch information
lppedd committed Jun 11, 2024
1 parent ed5830c commit 3a97e16
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,29 @@ public class MyersDiffWithLinearSpace<T>(
++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
Expand Down

0 comments on commit 3a97e16

Please sign in to comment.