Commit e5b5459
fix(diff): correct overlap detection and bounds in linear space Myers (#46)
* fix(diff): correct overlap detection and bounds in linear space Myers
This commit resolves two critical pathfinding bugs in the linear space
Myers algorithm (`findMiddleSnake`) that caused it to produce suboptimal
diffs or silently fail to explore valid search spaces.
1. Fix forward search overlap detection:
During the forward search overlap check (when `delta` is odd), the
algorithm incorrectly checked the reverse vector `vr` at `offset + (delta - k)`.
However, the reverse search already maps its furthest-reaching `x` bounds
to the same forward diagonal `k`. Checking the wrong index caused the
algorithm to miss the true optimal middle snake, leading to longer,
suboptimal edit scripts. This is fixed by correctly checking
`reverseIdx := offset + k`.
2. Fix vector sizing and offset for negative delta:
When sequence A is shorter than sequence B (`delta < 0`), the forward-diagonal
`k` in the reverse search can reach down to `-maxDiff + delta`. Because
`offset` was previously only adjusted for positive `delta`, calculating
`offset + k` would result in negative indices. This caused the reverse search
to silently trigger bounds checks and skip exploring half of its required
search grid. The `vectorSize` and `offset` are now correctly adjusted when
`delta < 0` to accommodate the full bounds.
* chore: add comments
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
---------
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>1 parent e4c9d81 commit e5b5459
1 file changed
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
| 342 | + | |
342 | 343 | | |
343 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
344 | 348 | | |
345 | | - | |
346 | | - | |
347 | 349 | | |
348 | 350 | | |
349 | 351 | | |
| |||
448 | 450 | | |
449 | 451 | | |
450 | 452 | | |
451 | | - | |
| 453 | + | |
452 | 454 | | |
453 | 455 | | |
454 | 456 | | |
| |||
0 commit comments