-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add LCS (Longest Common Subsequences) based array comparison
The default comparison behavior doesn't change. Use the option LCS() to enable the LCS-based behavior.
- Loading branch information
Showing
11 changed files
with
265 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/wI2L/jsondiff | ||
|
||
go 1.18 | ||
go 1.21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package jsondiff | ||
|
||
// lcs computes the longest common subsequence of two | ||
// slices and returns the index pairs of the LCS. | ||
func lcs(src, tgt []interface{}) [][2]int { | ||
t := make([][]int, len(src)+1) | ||
|
||
for i := 0; i <= len(src); i++ { | ||
t[i] = make([]int, len(tgt)+1) | ||
} | ||
for i := 1; i < len(t); i++ { | ||
for j := 1; j < len(t[i]); j++ { | ||
if deepEqual(src[i-1], tgt[j-1]) { | ||
t[i][j] = t[i-1][j-1] + 1 | ||
} else { | ||
t[i][j] = max(t[i-1][j], t[i][j-1]) | ||
} | ||
} | ||
} | ||
i, j := len(src), len(tgt) | ||
s := make([][2]int, 0, t[i][j]) | ||
|
||
for i > 0 && j > 0 { | ||
switch { | ||
case deepEqual(src[i-1], tgt[j-1]): | ||
s = append(s, [2]int{i - 1, j - 1}) | ||
i-- | ||
j-- | ||
case t[i-1][j] > t[i][j-1]: | ||
i-- | ||
default: | ||
j-- | ||
} | ||
} | ||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
return s | ||
} |
Oops, something went wrong.