Isolate change clusters by eliminating ranges with no changes. Return a generator of groups with up to n lines of context. Each group is in the same format as returned by GetOpCodes().
(n int)
| 417 | // Return a generator of groups with up to n lines of context. |
| 418 | // Each group is in the same format as returned by GetOpCodes(). |
| 419 | func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode { |
| 420 | if n < 0 { |
| 421 | n = 3 |
| 422 | } |
| 423 | codes := m.GetOpCodes() |
| 424 | if len(codes) == 0 { |
| 425 | codes = []OpCode{{'e', 0, 1, 0, 1}} |
| 426 | } |
| 427 | // Fixup leading and trailing groups if they show no changes. |
| 428 | if codes[0].Tag == 'e' { |
| 429 | c := codes[0] |
| 430 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 |
| 431 | codes[0] = OpCode{c.Tag, maxInt(i1, i2-n), i2, maxInt(j1, j2-n), j2} |
| 432 | } |
| 433 | if codes[len(codes)-1].Tag == 'e' { |
| 434 | c := codes[len(codes)-1] |
| 435 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 |
| 436 | codes[len(codes)-1] = OpCode{c.Tag, i1, minInt(i2, i1+n), j1, minInt(j2, j1+n)} |
| 437 | } |
| 438 | nn := n + n |
| 439 | groups := [][]OpCode{} |
| 440 | group := []OpCode{} |
| 441 | for _, c := range codes { |
| 442 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 |
| 443 | // End the current group and start a new one whenever |
| 444 | // there is a large range with no changes. |
| 445 | if c.Tag == 'e' && i2-i1 > nn { |
| 446 | group = append(group, OpCode{ |
| 447 | c.Tag, i1, minInt(i2, i1+n), |
| 448 | j1, minInt(j2, j1+n), |
| 449 | }) |
| 450 | groups = append(groups, group) |
| 451 | group = []OpCode{} |
| 452 | i1, j1 = maxInt(i1, i2-n), maxInt(j1, j2-n) |
| 453 | } |
| 454 | group = append(group, OpCode{c.Tag, i1, i2, j1, j2}) |
| 455 | } |
| 456 | if len(group) > 0 && (len(group) != 1 || group[0].Tag != 'e') { |
| 457 | groups = append(groups, group) |
| 458 | } |
| 459 | return groups |
| 460 | } |
| 461 | |
| 462 | // Return a measure of the sequences' similarity (float in [0,1]). |
| 463 | // |