-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.go
More file actions
355 lines (304 loc) · 10.1 KB
/
histogram.go
File metadata and controls
355 lines (304 loc) · 10.1 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package diffx
// Histogram-style diff algorithm.
//
// This implements an approach similar to Git's histogram diff:
// 1. Count element frequencies in both sequences
// 2. Find the lowest-frequency element that appears in both (the best anchor)
// 3. Split sequences at that anchor point
// 4. Recursively apply to both halves
// 5. Fall back to Myers when no good anchors exist
//
// This naturally avoids matching high-frequency elements like "the", "for", "-"
// because they're never chosen as anchor points.
//
// References:
// - JGit HistogramDiff (Eclipse License)
// - raygard/hdiff (0BSD License)
// - Bram Cohen's patience diff concept
// histogramOptions configures histogram diff behavior.
type histogramOptions struct {
// maxChainLength is the maximum frequency for an element to be considered
// as an anchor. Elements appearing more than this are ignored.
// Git uses 64 by default, but for word-level diff we use a lower value.
maxChainLength int
// fallbackToMyers controls whether to use Myers when no good anchors exist.
fallbackToMyers bool
// filterStopwords prevents common words from being used as anchors.
filterStopwords bool
}
func defaultHistogramOptions() *histogramOptions {
return &histogramOptions{
maxChainLength: 64, // Match Git's default; allow higher-frequency anchors
fallbackToMyers: true,
filterStopwords: true, // Filter stopwords for histogram anchors; Myers fallback finds others
}
}
// stopwords are common words that make poor anchors even at low frequency.
// These words appear frequently in natural language but carry little semantic meaning.
// NOTE: We intentionally exclude single-character punctuation and code keywords
// because they ARE meaningful anchors in code diffs (e.g., matching "(" is important).
var stopwords = map[string]bool{
// Articles and determiners - these truly have no semantic meaning
"a": true, "an": true, "the": true,
// Very common prepositions that often appear in unrelated contexts
"in": true, "on": true, "to": true, "for": true, "of": true, "with": true,
// Common conjunctions
"and": true, "or": true,
// Very common verbs that don't carry much meaning
"is": true, "are": true, "be": true,
}
// isStopword checks if a string element is a stopword.
func isStopword(e Element) bool {
s, ok := e.(StringElement)
if !ok {
return false
}
return stopwords[string(s)]
}
// histogramDiff performs histogram-style diff on two element sequences.
func histogramDiff(a, b []Element, opts *histogramOptions) []DiffOp {
if opts == nil {
opts = defaultHistogramOptions()
}
// Handle trivial cases
if len(a) == 0 && len(b) == 0 {
return nil
}
if len(a) == 0 {
return []DiffOp{{Type: Insert, AStart: 0, AEnd: 0, BStart: 0, BEnd: len(b)}}
}
if len(b) == 0 {
return []DiffOp{{Type: Delete, AStart: 0, AEnd: len(a), BStart: 0, BEnd: 0}}
}
// Trim common prefix
prefixLen := 0
for prefixLen < len(a) && prefixLen < len(b) && a[prefixLen].Equal(b[prefixLen]) {
prefixLen++
}
// Trim common suffix
suffixLen := 0
for suffixLen < len(a)-prefixLen && suffixLen < len(b)-prefixLen &&
a[len(a)-1-suffixLen].Equal(b[len(b)-1-suffixLen]) {
suffixLen++
}
// If everything matches, return Equal
if prefixLen+suffixLen >= len(a) && prefixLen+suffixLen >= len(b) {
return []DiffOp{{Type: Equal, AStart: 0, AEnd: len(a), BStart: 0, BEnd: len(b)}}
}
// Work on the middle section
aStart, aEnd := prefixLen, len(a)-suffixLen
bStart, bEnd := prefixLen, len(b)-suffixLen
// Build result with prefix
var result []DiffOp
if prefixLen > 0 {
result = append(result, DiffOp{Type: Equal, AStart: 0, AEnd: prefixLen, BStart: 0, BEnd: prefixLen})
}
// Diff the middle section
middleOps := histogramDiffRecursive(a[aStart:aEnd], b[bStart:bEnd], aStart, bStart, opts)
result = append(result, middleOps...)
// Add suffix
if suffixLen > 0 {
result = append(result, DiffOp{
Type: Equal,
AStart: len(a) - suffixLen,
AEnd: len(a),
BStart: len(b) - suffixLen,
BEnd: len(b),
})
}
return mergeAdjacentOps(result)
}
// histogramDiffRecursive performs the core histogram algorithm on a section.
func histogramDiffRecursive(a, b []Element, aOffset, bOffset int, opts *histogramOptions) []DiffOp {
if len(a) == 0 && len(b) == 0 {
return nil
}
if len(a) == 0 {
return []DiffOp{{Type: Insert, AStart: aOffset, AEnd: aOffset, BStart: bOffset, BEnd: bOffset + len(b)}}
}
if len(b) == 0 {
return []DiffOp{{Type: Delete, AStart: aOffset, AEnd: aOffset + len(a), BStart: bOffset, BEnd: bOffset}}
}
// Build frequency histogram for sequence A
aFreq := make(map[uint64]int)
aIndices := make(map[uint64][]int) // hash -> list of indices in A
for i, e := range a {
h := e.Hash()
aFreq[h]++
aIndices[h] = append(aIndices[h], i)
}
// Find the best anchor: consider both frequency AND position balance.
// We want low-frequency tokens, but also tokens that create balanced splits.
// Score = frequency * (1 + positionImbalance), lower is better.
bestIdx := -1
bestScore := float64(opts.maxChainLength+1) * 3 // Initialize to impossible value
var bestHash uint64
for i, e := range b {
// Skip stopwords if filtering is enabled
if opts.filterStopwords && isStopword(e) {
continue
}
h := e.Hash()
freq := aFreq[h]
if freq <= 0 || freq > opts.maxChainLength {
continue
}
// Find the best matching position in A for this potential anchor
bRatio := float64(i) / float64(len(b))
bestPosImbalance := 2.0
for _, aIdx := range aIndices[h] {
if !a[aIdx].Equal(e) {
continue
}
aRatio := float64(aIdx) / float64(len(a))
imbalance := aRatio - bRatio
if imbalance < 0 {
imbalance = -imbalance
}
if imbalance < bestPosImbalance {
bestPosImbalance = imbalance
}
}
if bestPosImbalance > 1.5 {
continue // No valid match position found
}
// Score combines frequency and position imbalance
// Lower frequency is better, lower imbalance is better
score := float64(freq) * (1.0 + bestPosImbalance*2)
if score < bestScore {
bestScore = score
bestIdx = i
bestHash = h
}
}
// No good anchor found - fall back to Myers to find more anchors.
// Myers will find common subsequences that histogram missed.
// The anchor elimination post-processing will clean up bad stopword matches.
if bestIdx == -1 {
if opts.fallbackToMyers {
return myersFallback(a, b, aOffset, bOffset)
}
// Only use delete+insert if Myers fallback is disabled
return []DiffOp{
{Type: Delete, AStart: aOffset, AEnd: aOffset + len(a), BStart: bOffset, BEnd: bOffset},
{Type: Insert, AStart: aOffset + len(a), AEnd: aOffset + len(a), BStart: bOffset, BEnd: bOffset + len(b)},
}
}
// Find the best matching position in A for this anchor.
// Instead of picking the first occurrence, pick the one that creates
// the most balanced split (position ratio in A closest to position ratio in B).
bRatio := float64(bestIdx) / float64(len(b))
aMatchIdx := -1
bestRatioDiff := 2.0 // Initialize to impossible value
for _, idx := range aIndices[bestHash] {
// Verify hash collision
if !a[idx].Equal(b[bestIdx]) {
continue
}
aRatio := float64(idx) / float64(len(a))
ratioDiff := aRatio - bRatio
if ratioDiff < 0 {
ratioDiff = -ratioDiff
}
if ratioDiff < bestRatioDiff {
bestRatioDiff = ratioDiff
aMatchIdx = idx
}
}
if aMatchIdx == -1 {
// No valid match found - fall back
if opts.fallbackToMyers {
return myersFallback(a, b, aOffset, bOffset)
}
return []DiffOp{
{Type: Delete, AStart: aOffset, AEnd: aOffset + len(a), BStart: bOffset, BEnd: bOffset},
{Type: Insert, AStart: aOffset + len(a), AEnd: aOffset + len(a), BStart: bOffset, BEnd: bOffset + len(b)},
}
}
// Extend the match forward and backward to find the full matching region
matchStartA, matchStartB := aMatchIdx, bestIdx
matchEndA, matchEndB := aMatchIdx+1, bestIdx+1
// Extend backward
for matchStartA > 0 && matchStartB > 0 && a[matchStartA-1].Equal(b[matchStartB-1]) {
matchStartA--
matchStartB--
}
// Extend forward
for matchEndA < len(a) && matchEndB < len(b) && a[matchEndA].Equal(b[matchEndB]) {
matchEndA++
matchEndB++
}
// Recursively diff the sections before and after the match
var result []DiffOp
// Before the match
if matchStartA > 0 || matchStartB > 0 {
beforeOps := histogramDiffRecursive(
a[:matchStartA], b[:matchStartB],
aOffset, bOffset,
opts,
)
result = append(result, beforeOps...)
}
// The matching region
result = append(result, DiffOp{
Type: Equal,
AStart: aOffset + matchStartA,
AEnd: aOffset + matchEndA,
BStart: bOffset + matchStartB,
BEnd: bOffset + matchEndB,
})
// After the match
if matchEndA < len(a) || matchEndB < len(b) {
afterOps := histogramDiffRecursive(
a[matchEndA:], b[matchEndB:],
aOffset+matchEndA, bOffset+matchEndB,
opts,
)
result = append(result, afterOps...)
}
return result
}
// myersFallback uses the standard Myers algorithm for a section.
func myersFallback(a, b []Element, aOffset, bOffset int) []DiffOp {
// Create a temporary context for Myers diff
o := defaultOptions()
o.preprocessing = false // Already preprocessed
o.postprocessing = false // Will be done after
o.anchorElimination = false
ctx := newDiffContext(a, b, o)
ctx.compareSeq(0, len(a), 0, len(b), false)
ops := ctx.buildOps()
// Adjust offsets
for i := range ops {
ops[i].AStart += aOffset
ops[i].AEnd += aOffset
ops[i].BStart += bOffset
ops[i].BEnd += bOffset
}
return ops
}
// DiffHistogram performs histogram-style diff on string slices.
func DiffHistogram(a, b []string, opts ...Option) []DiffOp {
return DiffElementsHistogram(toElements(a), toElements(b), opts...)
}
// DiffElementsHistogram performs histogram-style diff on Element slices.
func DiffElementsHistogram(a, b []Element, opts ...Option) []DiffOp {
// Apply options
o := defaultOptions()
for _, opt := range opts {
opt(o)
}
origA, origB := a, b
histOpts := defaultHistogramOptions()
// Run histogram diff
ops := histogramDiff(a, b, histOpts)
// Apply anchor elimination if enabled
if o.anchorElimination {
ops = eliminateWeakAnchors(ops, origA, origB)
}
// Apply boundary shifting if enabled
if o.postprocessing {
ops = shiftBoundaries(ops, origA, origB)
}
return ops
}