当前位置: 首页>>代码示例>>Java>>正文


Java Stack.lastElement方法代码示例

本文整理汇总了Java中java.util.Stack.lastElement方法的典型用法代码示例。如果您正苦于以下问题:Java Stack.lastElement方法的具体用法?Java Stack.lastElement怎么用?Java Stack.lastElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.Stack的用法示例。


在下文中一共展示了Stack.lastElement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: diff_cleanupSemantic

import java.util.Stack; //导入方法依赖的package包/类
/**
 * Reduce the number of edits by eliminating semantically trivial equalities.
 * @param diffs LinkedList of Diff objects.
 */
public void diff_cleanupSemantic(LinkedList<Diff> diffs) {
  if (diffs.isEmpty()) {
    return;
  }
  boolean changes = false;
  Stack<Diff> equalities = new Stack<Diff>();  // Stack of qualities.
  String lastequality = null; // Always equal to equalities.lastElement().text
  ListIterator<Diff> pointer = diffs.listIterator();
  // Number of characters that changed prior to the equality.
  int length_changes1 = 0;
  // Number of characters that changed after the equality.
  int length_changes2 = 0;
  Diff thisDiff = pointer.next();
  while (thisDiff != null) {
    if (thisDiff.operation == Operation.EQUAL) {
      // equality found
      equalities.push(thisDiff);
      length_changes1 = length_changes2;
      length_changes2 = 0;
      lastequality = thisDiff.text;
    } else {
      // an insertion or deletion
      length_changes2 += thisDiff.text.length();
      if (lastequality != null && (lastequality.length() <= length_changes1)
          && (lastequality.length() <= length_changes2)) {
        //System.out.println("Splitting: '" + lastequality + "'");
        // Walk back to offending equality.
        while (thisDiff != equalities.lastElement()) {
          thisDiff = pointer.previous();
        }
        pointer.next();

        // Replace equality with a delete.
        pointer.set(new Diff(Operation.DELETE, lastequality));
        // Insert a corresponding an insert.
        pointer.add(new Diff(Operation.INSERT, lastequality));

        equalities.pop();  // Throw away the equality we just deleted.
        if (!equalities.empty()) {
          // Throw away the previous equality (it needs to be reevaluated).
          equalities.pop();
        }
        if (equalities.empty()) {
          // There are no previous equalities, walk back to the start.
          while (pointer.hasPrevious()) {
            pointer.previous();
          }
        } else {
          // There is a safe equality we can fall back to.
          thisDiff = equalities.lastElement();
          while (thisDiff != pointer.previous()) {
            // Intentionally empty loop.
          }
        }

        length_changes1 = 0;  // Reset the counters.
        length_changes2 = 0;
        lastequality = null;
        changes = true;
      }
    }
    thisDiff = pointer.hasNext() ? pointer.next() : null;
  }

  if (changes) {
    diff_cleanupMerge(diffs);
  }
  diff_cleanupSemanticLossless(diffs);
}
 
开发者ID:zouzhirong,项目名称:configx,代码行数:74,代码来源:diff_match_patch.java


注:本文中的java.util.Stack.lastElement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。