MCPcopy Create free account
hub / github.com/sergi/go-diff / diffBisect

Method diffBisect

diffmatchpatch/diff.go:261–374  ·  view source on GitHub ↗

diffBisect finds the 'middle snake' of a diff, splits the problem in two and returns the recursively constructed diff. See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations.

(runes1, runes2 []rune, deadline time.Time)

Source from the content-addressed store, hash-verified

259// diffBisect finds the 'middle snake' of a diff, splits the problem in two and returns the recursively constructed diff.
260// See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations.
261func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time) []Diff {
262 // Cache the text lengths to prevent multiple calls.
263 runes1Len, runes2Len := len(runes1), len(runes2)
264
265 maxD := (runes1Len + runes2Len + 1) / 2
266 vOffset := maxD
267 vLength := 2 * maxD
268
269 v1 := make([]int, vLength)
270 v2 := make([]int, vLength)
271 for i := range v1 {
272 v1[i] = -1
273 v2[i] = -1
274 }
275 v1[vOffset+1] = 0
276 v2[vOffset+1] = 0
277
278 delta := runes1Len - runes2Len
279 // If the total number of characters is odd, then the front path will collide with the reverse path.
280 front := (delta%2 != 0)
281 // Offsets for start and end of k loop. Prevents mapping of space beyond the grid.
282 k1start := 0
283 k1end := 0
284 k2start := 0
285 k2end := 0
286 for d := 0; d < maxD; d++ {
287 // Bail out if deadline is reached.
288 if !deadline.IsZero() && d%16 == 0 && time.Now().After(deadline) {
289 break
290 }
291
292 // Walk the front path one step.
293 for k1 := -d + k1start; k1 <= d-k1end; k1 += 2 {
294 k1Offset := vOffset + k1
295 var x1 int
296
297 if k1 == -d || (k1 != d && v1[k1Offset-1] < v1[k1Offset+1]) {
298 x1 = v1[k1Offset+1]
299 } else {
300 x1 = v1[k1Offset-1] + 1
301 }
302
303 y1 := x1 - k1
304 for x1 < runes1Len && y1 < runes2Len {
305 if runes1[x1] != runes2[y1] {
306 break
307 }
308 x1++
309 y1++
310 }
311 v1[k1Offset] = x1
312 if x1 > runes1Len {
313 // Ran off the right of the graph.
314 k1end += 2
315 } else if y1 > runes2Len {
316 // Ran off the bottom of the graph.
317 k1start += 2
318 } else if front {

Callers 2

diffComputeMethod · 0.95
DiffBisectMethod · 0.95

Calls 1

diffBisectSplitMethod · 0.95

Tested by

no test coverage detected