diff: faster data structure

This commit is contained in:
mappu 2017-07-11 17:57:05 +12:00
parent 0fe5b74319
commit 7c0a7af05f

View File

@ -53,7 +53,7 @@ func Diff(o, n []string) []Instruction {
maxl := 0 maxl := 0
omax := 0 omax := 0
nmax := 0 nmax := 0
matrix := make(map[int]map[int]int, len(o)+1) matrix := make(map[twoints]int, len(o)+1)
for oindex, ovalue := range o { for oindex, ovalue := range o {
@ -66,14 +66,11 @@ func Diff(o, n []string) []Instruction {
} }
// Build diff matrix // Build diff matrix
for nindex, _ := range nkeys { for _, nindex := range nkeys {
if _, ok := matrix[oindex]; !ok { matrix[twoints{oindex, nindex}] = matrix[twoints{oindex - 1, nindex - 1}] + 1
matrix[oindex] = make(map[int]int, len(n)+1)
}
matrix[oindex][nindex] = matrix[oindex-1][nindex-1] + 1
if matrix[oindex][nindex] > maxl { if matrix[twoints{oindex, nindex}] > maxl {
maxl = matrix[oindex][nindex] maxl = matrix[twoints{oindex, nindex}]
omax = oindex + 1 - maxl omax = oindex + 1 - maxl
nmax = nindex + 1 - maxl nmax = nindex + 1 - maxl
} }
@ -87,7 +84,7 @@ func Diff(o, n []string) []Instruction {
} }
} }
ret := Diff(o[0:omax], n[0:nmax]) ret := Diff(o[:omax], n[:nmax])
if maxl != nmax { if maxl != nmax {
ret = append(ret, Instruction{OP_UNCHANGED, n[nmax : maxl-nmax]}) ret = append(ret, Instruction{OP_UNCHANGED, n[nmax : maxl-nmax]})
} }