Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions java/src/name/fraser/neil/plaintext/diff_match_patch.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Stack;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -763,8 +756,8 @@ public void diff_cleanupSemantic(LinkedList<Diff> diffs) {
return;
}
boolean changes = false;
Stack<Diff> equalities = new Stack<Diff>(); // Stack of qualities.
String lastequality = null; // Always equal to equalities.lastElement().text
Deque<Diff> equalities = new ArrayDeque<Diff>(); // Double-ended queue of qualities.
String lastEquality = null; // Always equal to equalities.peek().text
ListIterator<Diff> pointer = diffs.listIterator();
// Number of characters that changed prior to the equality.
int length_insertions1 = 0;
Expand All @@ -781,7 +774,7 @@ public void diff_cleanupSemantic(LinkedList<Diff> diffs) {
length_deletions1 = length_deletions2;
length_insertions2 = 0;
length_deletions2 = 0;
lastequality = thisDiff.text;
lastEquality = thisDiff.text;
} else {
// An insertion or deletion.
if (thisDiff.operation == Operation.INSERT) {
Expand All @@ -791,35 +784,35 @@ public void diff_cleanupSemantic(LinkedList<Diff> diffs) {
}
// Eliminate an equality that is smaller or equal to the edits on both
// sides of it.
if (lastequality != null && (lastequality.length()
if (lastEquality != null && (lastEquality.length()
<= Math.max(length_insertions1, length_deletions1))
&& (lastequality.length()
&& (lastEquality.length()
<= Math.max(length_insertions2, length_deletions2))) {
//System.out.println("Splitting: '" + lastequality + "'");
//System.out.println("Splitting: '" + lastEquality + "'");
// Walk back to offending equality.
while (thisDiff != equalities.lastElement()) {
while (thisDiff != equalities.peek()) {
thisDiff = pointer.previous();
}
pointer.next();

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

equalities.pop(); // Throw away the equality we just deleted.
if (!equalities.empty()) {
if (!equalities.isEmpty()) {
// Throw away the previous equality (it needs to be reevaluated).
equalities.pop();
}
if (equalities.empty()) {
if (equalities.isEmpty()) {
// 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();
thisDiff = equalities.peek();
while (thisDiff != pointer.previous()) {
// Intentionally empty loop.
}
Expand All @@ -829,7 +822,7 @@ public void diff_cleanupSemantic(LinkedList<Diff> diffs) {
length_insertions2 = 0;
length_deletions1 = 0;
length_deletions2 = 0;
lastequality = null;
lastEquality = null;
changes = true;
}
}
Expand Down Expand Up @@ -1052,8 +1045,8 @@ public void diff_cleanupEfficiency(LinkedList<Diff> diffs) {
return;
}
boolean changes = false;
Stack<Diff> equalities = new Stack<Diff>(); // Stack of equalities.
String lastequality = null; // Always equal to equalities.lastElement().text
Deque<Diff> equalities = new ArrayDeque<Diff>(); // Double-ended queue of equalities.
String lastEquality = null; // Always equal to equalities.peek().text
ListIterator<Diff> pointer = diffs.listIterator();
// Is there an insertion operation before the last equality.
boolean pre_ins = false;
Expand All @@ -1064,7 +1057,7 @@ public void diff_cleanupEfficiency(LinkedList<Diff> diffs) {
// Is there a deletion operation after the last equality.
boolean post_del = false;
Diff thisDiff = pointer.next();
Diff safeDiff = thisDiff; // The last Diff that is known to be unsplitable.
Diff safeDiff = thisDiff; // The last Diff that is known to be unsplittable.
while (thisDiff != null) {
if (thisDiff.operation == Operation.EQUAL) {
// Equality found.
Expand All @@ -1073,11 +1066,11 @@ public void diff_cleanupEfficiency(LinkedList<Diff> diffs) {
equalities.push(thisDiff);
pre_ins = post_ins;
pre_del = post_del;
lastequality = thisDiff.text;
lastEquality = thisDiff.text;
} else {
// Not a candidate, and can never become one.
equalities.clear();
lastequality = null;
lastEquality = null;
safeDiff = thisDiff;
}
post_ins = post_del = false;
Expand All @@ -1096,42 +1089,42 @@ public void diff_cleanupEfficiency(LinkedList<Diff> diffs) {
* <ins>A</del>X<ins>C</ins><del>D</del>
* <ins>A</ins><del>B</del>X<del>C</del>
*/
if (lastequality != null
if (lastEquality != null
&& ((pre_ins && pre_del && post_ins && post_del)
|| ((lastequality.length() < Diff_EditCost / 2)
|| ((lastEquality.length() < Diff_EditCost / 2)
&& ((pre_ins ? 1 : 0) + (pre_del ? 1 : 0)
+ (post_ins ? 1 : 0) + (post_del ? 1 : 0)) == 3))) {
//System.out.println("Splitting: '" + lastequality + "'");
//System.out.println("Splitting: '" + lastEquality + "'");
// Walk back to offending equality.
while (thisDiff != equalities.lastElement()) {
while (thisDiff != equalities.peek()) {
thisDiff = pointer.previous();
}
pointer.next();

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

equalities.pop(); // Throw away the equality we just deleted.
lastequality = null;
lastEquality = null;
if (pre_ins && pre_del) {
// No changes made which could affect previous entry, keep going.
post_ins = post_del = true;
equalities.clear();
safeDiff = thisDiff;
} else {
if (!equalities.empty()) {
if (!equalities.isEmpty()) {
// Throw away the previous equality (it needs to be reevaluated).
equalities.pop();
}
if (equalities.empty()) {
if (equalities.isEmpty()) {
// There are no previous questionable equalities,
// walk back to the last known safe diff.
thisDiff = safeDiff;
} else {
// There is an equality we can fall back to.
thisDiff = equalities.lastElement();
thisDiff = equalities.peek();
}
while (thisDiff != pointer.previous()) {
// Intentionally empty loop.
Expand Down Expand Up @@ -1868,19 +1861,17 @@ public LinkedList<Patch> patch_make(String text1, LinkedList<Diff> diffs) {
patch.length2 += aDiff.text.length();
}

if (aDiff.text.length() >= 2 * Patch_Margin) {
if (aDiff.text.length() >= 2 * Patch_Margin && !patch.diffs.isEmpty()) {
// Time for a new patch.
if (!patch.diffs.isEmpty()) {
patch_addContext(patch, prepatch_text);
patches.add(patch);
patch = new Patch();
// Unlike Unidiff, our patch lists have a rolling context.
// http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
// Update prepatch text & pos to reflect the application of the
// just completed patch.
prepatch_text = postpatch_text;
char_count1 = char_count2;
}
patch_addContext(patch, prepatch_text);
patches.add(patch);
patch = new Patch();
// Unlike Unidiff, our patch lists have a rolling context.
// http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
// Update prepatch text & pos to reflect the application of the
// just completed patch.
prepatch_text = postpatch_text;
char_count1 = char_count2;
}
break;
}
Expand Down
30 changes: 12 additions & 18 deletions java/tests/name/fraser/neil/plaintext/Speedtest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@

package name.fraser.neil.plaintext;

import name.fraser.neil.plaintext.diff_match_patch;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Speedtest {

public static void main(String args[]) {
public static void main(String args[]) throws IOException {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use Java Microbenchmark Harness (JMH) instead. It is used for building, running, and analyzing nano/micro/milli/macro benchmarks and produces reliable results.

String text1 = readFile("tests/name/fraser/neil/plaintext/Speedtest1.txt");
String text2 = readFile("tests/name/fraser/neil/plaintext/Speedtest2.txt");

Expand All @@ -30,32 +28,28 @@ public static void main(String args[]) {

// Execute one reverse diff as a warmup.
dmp.diff_main(text2, text1, false);
System.gc();

long start_time = System.currentTimeMillis();
long start_time = System.nanoTime();
dmp.diff_main(text1, text2, false);
long end_time = System.currentTimeMillis();
System.out.printf("Elapsed time: %f\n", ((end_time - start_time) / 1000.0));
long end_time = System.nanoTime();
System.out.printf("Elapsed time: %f\n", ((end_time - start_time) / 1000000000.0));
}

private static String readFile(String filename) {
private static String readFile(String filename) throws IOException {
// Read a file from disk and return the text contents.
StringBuffer strbuf = new StringBuffer();
StringBuilder sb = new StringBuilder();
FileReader input = new FileReader(filename);
BufferedReader bufRead = new BufferedReader(input);
try {
FileReader input = new FileReader(filename);
BufferedReader bufRead = new BufferedReader(input);
String line = bufRead.readLine();
while (line != null) {
strbuf.append(line);
strbuf.append('\n');
sb.append(line).append('\n');
line = bufRead.readLine();
}

} finally {
bufRead.close();

} catch (IOException e) {
e.printStackTrace();
input.close();
}
return strbuf.toString();
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,7 @@ private static String[] diff_rebuildtexts(LinkedList<Diff> diffs) {

// Private function for quickly building lists of diffs.
private static LinkedList<Diff> diffList(Diff... diffs) {
LinkedList<Diff> myDiffList = new LinkedList<Diff>();
for (Diff myDiff : diffs) {
myDiffList.add(myDiff);
}
return myDiffList;
return new LinkedList<Diff>(Arrays.asList(diffs));
}

public static void main(String args[]) {
Expand Down