-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patience.go
115 lines (102 loc) · 2.73 KB
/
patience.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package patience implements the Patience Diff algorithm.
package patience
// DiffType defines the type of a diff element.
type DiffType int8
const (
// Delete represents a diff delete operation.
Delete DiffType = -1
// Insert represents a diff insert operation.
Insert DiffType = 1
// Equal represents no diff.
Equal DiffType = 0
)
// DiffLine represents a single line and its diff type.
type DiffLine struct {
Text string
Type DiffType
}
// toDiffLines is a convenience function to convert a slice of strings
// to a slice of DiffLines with the specified diff type.
func toDiffLines(a []string, t DiffType) []DiffLine {
diffs := make([]DiffLine, len(a))
for i, l := range a {
diffs[i] = DiffLine{l, t}
}
return diffs
}
// uniqueElements returns a slice of unique elements from a slice of
// strings, and a slice of the original indices of each element.
func uniqueElements(a []string) ([]string, []int) {
m := make(map[string]int)
for _, e := range a {
m[e]++
}
elements := []string{}
indices := []int{}
for i, e := range a {
if m[e] == 1 {
elements = append(elements, e)
indices = append(indices, i)
}
}
return elements, indices
}
// Diff returns the patience diff of two slices of strings.
func Diff(a, b []string) []DiffLine {
switch {
case len(a) == 0 && len(b) == 0:
return nil
case len(a) == 0:
return toDiffLines(b, Insert)
case len(b) == 0:
return toDiffLines(a, Delete)
}
// Find equal elements at the head of slices a and b.
i := 0
for i < len(a) && i < len(b) && a[i] == b[i] {
i++
}
if i > 0 {
return append(
toDiffLines(a[:i], Equal),
Diff(a[i:], b[i:])...,
)
}
// Find equal elements at the tail of slices a and b.
j := 0
for j < len(a) && j < len(b) && a[len(a)-1-j] == b[len(b)-1-j] {
j++
}
if j > 0 {
return append(
Diff(a[:len(a)-j], b[:len(b)-j]),
toDiffLines(a[len(a)-j:], Equal)...,
)
}
// Find the longest common subsequence of unique elements in a and b.
ua, idxa := uniqueElements(a)
ub, idxb := uniqueElements(b)
lcs := LCS(ua, ub)
// If the LCS is empty, the diff is all deletions and insertions.
if len(lcs) == 0 {
return append(toDiffLines(a, Delete), toDiffLines(b, Insert)...)
}
// Lookup the original indices of slices a and b.
for i, x := range lcs {
lcs[i][0] = idxa[x[0]]
lcs[i][1] = idxb[x[1]]
}
diffs := []DiffLine{}
ga, gb := 0, 0
for _, ip := range lcs {
// Diff the gaps between the lcs elements.
diffs = append(diffs, Diff(a[ga:ip[0]], b[gb:ip[1]])...)
// Append the LCS elements to the diff.
diffs = append(diffs, DiffLine{Type: Equal, Text: a[ip[0]]})
ga = ip[0] + 1
gb = ip[1] + 1
}
// Diff the remaining elements of a and b after the final LCS element.
diffs = append(diffs, Diff(a[ga:], b[gb:])...)
return diffs
}