Reduce the number of edits by eliminating operationally trivial equalities. Args: diffs: Array of diff tuples.
(diffs, editcost=4)
| 485 | |
| 486 | |
| 487 | def cleanup_efficiency(diffs, editcost=4): |
| 488 | """ |
| 489 | Reduce the number of edits by eliminating operationally trivial |
| 490 | equalities. |
| 491 | |
| 492 | Args: |
| 493 | diffs: Array of diff tuples. |
| 494 | """ |
| 495 | changes = False |
| 496 | # Stack of indices where equalities are found. |
| 497 | equalities = [] |
| 498 | # Always equal to diffs[equalities[-1]][1] |
| 499 | last_equality = None |
| 500 | # Index of current position. |
| 501 | pointer = 0 |
| 502 | # Is there an insertion operation before the last equality. |
| 503 | pre_ins = False |
| 504 | # Is there a deletion operation before the last equality. |
| 505 | pre_del = False |
| 506 | # Is there an insertion operation after the last equality. |
| 507 | post_ins = False |
| 508 | # Is there a deletion operation after the last equality. |
| 509 | post_del = False |
| 510 | |
| 511 | while pointer < len(diffs): |
| 512 | if diffs[pointer][0] == DIFF_EQUAL: # Equality found. |
| 513 | if (len(diffs[pointer][1]) < editcost and (post_ins or post_del)): |
| 514 | # Candidate found. |
| 515 | equalities.append(pointer) |
| 516 | pre_ins = post_ins |
| 517 | pre_del = post_del |
| 518 | last_equality = diffs[pointer][1] |
| 519 | else: |
| 520 | # Not a candidate, and can never become one. |
| 521 | equalities = [] |
| 522 | last_equality = None |
| 523 | |
| 524 | post_ins = post_del = False |
| 525 | else: # An insertion or deletion. |
| 526 | if diffs[pointer][0] == DIFF_DELETE: |
| 527 | post_del = True |
| 528 | else: |
| 529 | post_ins = True |
| 530 | |
| 531 | # Five types to be split: |
| 532 | # <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del> |
| 533 | # <ins>A</ins>X<ins>C</ins><del>D</del> |
| 534 | # <ins>A</ins><del>B</del>X<ins>C</ins> |
| 535 | # <ins>A</del>X<ins>C</ins><del>D</del> |
| 536 | # <ins>A</ins><del>B</del>X<del>C</del> |
| 537 | |
| 538 | if last_equality and ( |
| 539 | (pre_ins and pre_del and post_ins and post_del) |
| 540 | or |
| 541 | ((len(last_equality) < editcost / 2) |
| 542 | and (pre_ins + pre_del + post_ins + post_del) == 3)): |
| 543 | |
| 544 | # Duplicate record. |