-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
196 lines (169 loc) · 3.46 KB
/
diff.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// go.diff :: diff.go
//
// Copyright (c) 2014-2021 Akinori Hattori <[email protected]>
//
// SPDX-License-Identifier: MIT
//
// Package diff implements the difference algorithm, which is based upon
// S. Wu, U. Manber, G. Myers, and W. Miller,
// "An O(NP) Sequence Comparison Algorithm" August 1989.
package diff
type Interface interface {
// Equal returns whether the elements at i and j are equal.
Equal(i, j int) bool
}
// Bytes returns the differences between byte slices.
func Bytes(a, b []byte) []Change {
return Diff(len(a), len(b), &bytes{a, b})
}
type bytes struct {
A, B []byte
}
func (p *bytes) Equal(i, j int) bool { return p.A[i] == p.B[j] }
// Ints returns the differences between int slices.
func Ints(a, b []int) []Change {
return Diff(len(a), len(b), &ints{a, b})
}
type ints struct {
A, B []int
}
func (p *ints) Equal(i, j int) bool { return p.A[i] == p.B[j] }
// Runes returns the differences between rune slices.
func Runes(a, b []rune) []Change {
return Diff(len(a), len(b), &runes{a, b})
}
type runes struct {
A, B []rune
}
func (p *runes) Equal(i, j int) bool { return p.A[i] == p.B[j] }
// Strings returns the differences between string slices.
func Strings(a, b []string) []Change {
return Diff(len(a), len(b), &strings{a, b})
}
type strings struct {
A, B []string
}
func (p *strings) Equal(i, j int) bool { return p.A[i] == p.B[j] }
// Diff returns the differences between data.
// It makes O(NP) (the worst case) calls to data.Equal.
func Diff(m, n int, data Interface) []Change {
c := &context{data: data}
if n >= m {
c.M = m
c.N = n
} else {
c.M = n
c.N = m
c.xchg = true
}
c.Δ = c.N - c.M
return c.compare()
}
type Change struct {
A, B int // position in a and b
Del int // number of elements that deleted from a
Ins int // number of elements that inserted into b
}
type context struct {
data Interface
M, N int
Δ int
fp []point
xchg bool
}
func (c *context) compare() []Change {
c.fp = make([]point, (c.M+1)+(c.N+1)+1)
for i := range c.fp {
c.fp[i].y = -1
}
Δ := c.Δ + (c.M + 1)
for p := 0; c.fp[Δ].y != c.N; p++ {
for k := -p; k < c.Δ; k++ {
c.snake(k)
}
for k := c.Δ + p; k > c.Δ; k-- {
c.snake(k)
}
c.snake(c.Δ)
}
lcs, n := c.reverse(c.fp[Δ].lcs)
cl := make([]Change, 0, n+1)
var x, y int
for ; lcs != nil; lcs = lcs.next {
if x < lcs.x || y < lcs.y {
if !c.xchg {
cl = append(cl, Change{x, y, lcs.x - x, lcs.y - y})
} else {
cl = append(cl, Change{y, x, lcs.y - y, lcs.x - x})
}
}
x = lcs.x + lcs.n
y = lcs.y + lcs.n
}
if x < c.M || y < c.N {
if !c.xchg {
cl = append(cl, Change{x, y, c.M - x, c.N - y})
} else {
cl = append(cl, Change{y, x, c.N - y, c.M - x})
}
}
return cl
}
func (c *context) snake(k int) {
var y int
var prev *lcs
kk := k + (c.M + 1)
h := &c.fp[kk-1]
v := &c.fp[kk+1]
if h.y+1 >= v.y {
y = h.y + 1
prev = h.lcs
} else {
y = v.y
prev = v.lcs
}
x := y - k
n := 0
for x < c.M && y < c.N {
var eq bool
if !c.xchg {
eq = c.data.Equal(x, y)
} else {
eq = c.data.Equal(y, x)
}
if !eq {
break
}
x++
y++
n++
}
p := &c.fp[kk]
p.y = y
if n == 0 {
p.lcs = prev
} else {
p.lcs = &lcs{
x: x - n,
y: y - n,
n: n,
next: prev,
}
}
}
func (c *context) reverse(curr *lcs) (next *lcs, n int) {
for ; curr != nil; n++ {
curr.next, next, curr = next, curr, curr.next
}
return
}
type point struct {
y int
lcs *lcs
}
type lcs struct {
x, y int
n int
next *lcs
}