def diff_cleanupSemantic(self, diffs):
    """Reduce the number of edits by eliminating semantically trivial
    equalities.

    Args:
      diffs: Array of diff tuples.
    """
    changes = False
    equalities = []  # Stack of indices where equalities are found.
    lastequality = None  # Always equal to diffs[equalities[-1]][1]
    pointer = 0  # Index of current position.
    # Number of chars that changed prior to the equality.
    length_insertions1, length_deletions1 = 0, 0
    # Number of chars that changed after the equality.
    length_insertions2, length_deletions2 = 0, 0
    while pointer < len(diffs):
      if diffs[pointer][0] == self.DIFF_EQUAL:  # Equality found.
        equalities.append(pointer)
        length_insertions1, length_insertions2 = length_insertions2, 0
        length_deletions1, length_deletions2 = length_deletions2, 0
        lastequality = diffs[pointer][1]
      else:  # An insertion or deletion.
        if diffs[pointer][0] == self.DIFF_INSERT:
          length_insertions2 += len(diffs[pointer][1])
        else:
          length_deletions2 += len(diffs[pointer][1])
        # Eliminate an equality that is smaller or equal to the edits on both
        # sides of it.
        if (lastequality and (len(lastequality) <=
            max(length_insertions1, length_deletions1)) and
            (len(lastequality) <= max(length_insertions2, length_deletions2))):
          # Duplicate record.
          diffs.insert(equalities[-1], (self.DIFF_DELETE, lastequality))
          # Change second copy to insert.
          diffs[equalities[-1] + 1] = (self.DIFF_INSERT,
              diffs[equalities[-1] + 1][1])
          # Throw away the equality we just deleted.
          equalities.pop()
          # Throw away the previous equality (it needs to be reevaluated).
          if len(equalities):
            equalities.pop()
          if len(equalities):
            pointer = equalities[-1]
          else:
            pointer = -1
          # Reset the counters.
          length_insertions1, length_deletions1 = 0, 0
          length_insertions2, length_deletions2 = 0, 0
          lastequality = None
          changes = True
      pointer += 1

    # Normalize the diff.
    if changes:
      self.diff_cleanupMerge(diffs)
    self.diff_cleanupSemanticLossless(diffs)

    # Find any overlaps between deletions and insertions.
    # e.g: <del>abcxxx</del><ins>xxxdef</ins>
    #   -> <del>abc</del>xxx<ins>def</ins>
    # e.g: <del>xxxabc</del><ins>defxxx</ins>
    #   -> <ins>def</ins>xxx<del>abc</del>
    # Only extract an overlap if it is as big as the edit ahead or behind it.
    pointer = 1
    while pointer < len(diffs):
      if (diffs[pointer - 1][0] == self.DIFF_DELETE and
          diffs[pointer][0] == self.DIFF_INSERT):
        deletion = diffs[pointer - 1][1]
        insertion = diffs[pointer][1]
        overlap_length1 = self.diff_commonOverlap(deletion, insertion)
        overlap_length2 = self.diff_commonOverlap(insertion, deletion)
        if overlap_length1 >= overlap_length2:
          if (overlap_length1 >= len(deletion) / 2.0 or
              overlap_length1 >= len(insertion) / 2.0):
            # Overlap found.  Insert an equality and trim the surrounding edits.
            diffs.insert(pointer, (self.DIFF_EQUAL,
                                   insertion[:overlap_length1]))
            diffs[pointer - 1] = (self.DIFF_DELETE,
                                  deletion[:len(deletion) - overlap_length1])
            diffs[pointer + 1] = (self.DIFF_INSERT,
                                  insertion[overlap_length1:])
            pointer += 1
        else:
          if (overlap_length2 >= len(deletion) / 2.0 or
              overlap_length2 >= len(insertion) / 2.0):
            # Reverse overlap found.
            # Insert an equality and swap and trim the surrounding edits.
            diffs.insert(pointer, (self.DIFF_EQUAL, deletion[:overlap_length2]))
            diffs[pointer - 1] = (self.DIFF_INSERT,
                                  insertion[:len(insertion) - overlap_length2])
            diffs[pointer + 1] = (self.DIFF_DELETE, deletion[overlap_length2:])
            pointer += 1
        pointer += 1
      pointer += 1
dataset/ETHPy150Open JT5D/Alfred-Popclip-Sublime/Sublime Text 2/JsFormat/libs/diff_match_patch/python2/diff_match_patch.py/diff_match_patch.diff_cleanupSemantic/original