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