~bigbes/sr-ht-spec

ref: d4b6373d153470b30c9bb6882f71d9bee0a8f416 sr-ht-spec/prosediff/myers.go -rw-r--r-- 5.0 KiB
d4b6373d — Eugene Blikh graph: seed the test keyset from ecoretest 10 days ago
                                                                                
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package prosediff

// Myers O(ND) sequence diff, used at three levels: blocks, words and code
// lines. Sequences are always interned to []int first, so equality is exact
// integer equality and there is no hash-collision risk.

// Op is the role of a run in an edit script.
type Op uint8

const (
	// OpEqual marks content present, unchanged, in both revisions.
	OpEqual Op = iota
	// OpDelete marks content present only in the old revision.
	OpDelete
	// OpInsert marks content present only in the new revision.
	OpInsert
)

func (o Op) String() string {
	switch o {
	case OpEqual:
		return "equal"
	case OpDelete:
		return "delete"
	case OpInsert:
		return "insert"
	}
	return "unknown"
}

// edit is one run of an edit script: n consecutive elements sharing an Op.
type edit struct {
	op Op
	n  int
}

// interner maps arbitrary strings to dense ints so sequence comparison is
// integer equality.
type interner struct {
	ids map[string]int
}

func newInterner() *interner { return &interner{ids: make(map[string]int)} }

func (in *interner) id(s string) int {
	if v, ok := in.ids[s]; ok {
		return v
	}
	v := len(in.ids) + 1
	in.ids[s] = v
	return v
}

func (in *interner) all(ss []string) []int {
	out := make([]int, len(ss))
	for i, s := range ss {
		out[i] = in.id(s)
	}
	return out
}

// diffInts returns the edit script turning a into b, as a coalesced run list.
// The script is a minimal edit script (Myers), with common prefix and suffix
// trimmed first so the expensive part only runs over the changed middle.
func diffInts(a, b []int) []edit {
	pre := 0
	for pre < len(a) && pre < len(b) && a[pre] == b[pre] {
		pre++
	}
	suf := 0
	for suf < len(a)-pre && suf < len(b)-pre && a[len(a)-1-suf] == b[len(b)-1-suf] {
		suf++
	}
	var out []edit
	if pre > 0 {
		out = append(out, edit{OpEqual, pre})
	}
	out = append(out, myers(a[pre:len(a)-suf], b[pre:len(b)-suf])...)
	if suf > 0 {
		out = append(out, edit{OpEqual, suf})
	}
	return coalesce(out)
}

// vsnap is the V array of one Myers iteration, stored only over the diagonals
// [-d, d] that iteration can reach.
type vsnap struct {
	d    int
	vals []int32
}

func (s vsnap) get(k int) int { return int(s.vals[k+s.d]) }

func myers(a, b []int) []edit {
	n, m := len(a), len(b)
	switch {
	case n == 0 && m == 0:
		return nil
	case n == 0:
		return []edit{{OpInsert, m}}
	case m == 0:
		return []edit{{OpDelete, n}}
	}

	maxD := n + m
	v := make([]int32, 2*maxD+1)
	off := maxD
	trace := make([]vsnap, 0, 16)

	for d := 0; d <= maxD; d++ {
		done := false
		for k := -d; k <= d; k += 2 {
			var x int
			if k == -d || (k != d && v[off+k-1] < v[off+k+1]) {
				x = int(v[off+k+1]) // move down: consume one element of b
			} else {
				x = int(v[off+k-1]) + 1 // move right: consume one element of a
			}
			y := x - k
			for x < n && y < m && a[x] == b[y] {
				x++
				y++
			}
			v[off+k] = int32(x)
			if x >= n && y >= m {
				done = true
				break
			}
		}
		snap := vsnap{d: d, vals: make([]int32, 2*d+1)}
		for k := -d; k <= d; k += 2 {
			snap.vals[k+d] = v[off+k]
		}
		trace = append(trace, snap)
		if done {
			return backtrack(trace, n, m)
		}
	}
	panic("prosediff: myers did not converge")
}

// backtrack walks the saved V arrays from the end point back to the origin,
// emitting the edit script in reverse and then flipping it.
func backtrack(trace []vsnap, n, m int) []edit {
	var rev []edit
	push := func(op Op) {
		if len(rev) > 0 && rev[len(rev)-1].op == op {
			rev[len(rev)-1].n++
			return
		}
		rev = append(rev, edit{op, 1})
	}

	x, y := n, m
	for d := len(trace) - 1; d > 0; d-- {
		prev := trace[d-1]
		k := x - y
		var prevK int
		if k == -d || (k != d && prev.get(k-1) < prev.get(k+1)) {
			prevK = k + 1
		} else {
			prevK = k - 1
		}
		prevX := prev.get(prevK)
		prevY := prevX - prevK
		for x > prevX && y > prevY {
			push(OpEqual)
			x--
			y--
		}
		if x == prevX {
			push(OpInsert)
			y--
		} else {
			push(OpDelete)
			x--
		}
		x, y = prevX, prevY
	}
	for x > 0 && y > 0 {
		push(OpEqual)
		x--
		y--
	}
	// d == 0 leaves at most one of x, y non-zero only when the other
	// sequence was fully consumed on the diagonal, which the prefix trim
	// already handled; keep the guard rather than assume.
	for ; x > 0; x-- {
		push(OpDelete)
	}
	for ; y > 0; y-- {
		push(OpInsert)
	}

	out := make([]edit, 0, len(rev))
	for i := len(rev) - 1; i >= 0; i-- {
		out = append(out, rev[i])
	}
	return coalesce(out)
}

func coalesce(in []edit) []edit {
	out := in[:0:0]
	for _, e := range in {
		if e.n == 0 {
			continue
		}
		if len(out) > 0 && out[len(out)-1].op == e.op {
			out[len(out)-1].n += e.n
			continue
		}
		out = append(out, e)
	}
	return out
}

// commonCount reports how many elements the edit script keeps equal.
func commonCount(script []edit) int {
	n := 0
	for _, e := range script {
		if e.op == OpEqual {
			n += e.n
		}
	}
	return n
}

// ratio is the classic 2*common/(len(a)+len(b)) similarity in [0,1].
func ratio(a, b []int) float64 {
	if len(a) == 0 && len(b) == 0 {
		return 1
	}
	return 2 * float64(commonCount(diffInts(a, b))) / float64(len(a)+len(b))
}