From 7c0a7af05fbfebfcfb5234b028ebf947623d9f8a Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 11 Jul 2017 17:57:05 +1200 Subject: [PATCH] diff: faster data structure --- diff/diff.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index 534f454..9fcb02b 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -53,7 +53,7 @@ func Diff(o, n []string) []Instruction { maxl := 0 omax := 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 { @@ -66,14 +66,11 @@ func Diff(o, n []string) []Instruction { } // Build diff matrix - for nindex, _ := range nkeys { - if _, ok := matrix[oindex]; !ok { - matrix[oindex] = make(map[int]int, len(n)+1) - } - matrix[oindex][nindex] = matrix[oindex-1][nindex-1] + 1 + for _, nindex := range nkeys { + matrix[twoints{oindex, nindex}] = matrix[twoints{oindex - 1, nindex - 1}] + 1 - if matrix[oindex][nindex] > maxl { - maxl = matrix[oindex][nindex] + if matrix[twoints{oindex, nindex}] > maxl { + maxl = matrix[twoints{oindex, nindex}] omax = oindex + 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 { ret = append(ret, Instruction{OP_UNCHANGED, n[nmax : maxl-nmax]}) }