From c73e75282b1cc5b801b6f683f0d81219b88c3fc0 Mon Sep 17 00:00:00 2001 From: wumpz Date: Thu, 13 Apr 2017 16:04:48 +0200 Subject: [PATCH] refactoring and started to reimplement histogram diff --- .../DifferentiationFailedException.java | 6 +- .../difflib/algorithm/myers/MyersDiff.java | 1 + .../java/difflib/algorithm/xdiff/XDFEnv.java | 25 ++++++ .../java/difflib/algorithm/xdiff/XDFile.java | 31 +++++++ .../algorithm/xdiff/XDLClassifier.java | 24 ++++++ .../difflib/algorithm/xdiff/XHistogram.java | 45 ++++++++++ .../java/difflib/algorithm/xdiff/XPParam.java | 29 +++++++ .../difflib/algorithm/xdiff/XPrepare.java | 46 ++++++++++ .../algorithm/xdiff/XHistogramTest.java | 83 +++++++++++++++++++ .../difflib/algorithm/xdiff/XPrepareTest.java | 66 +++++++++++++++ 10 files changed, 352 insertions(+), 4 deletions(-) rename src/main/java/difflib/algorithm/{myers => }/DifferentiationFailedException.java (91%) create mode 100644 src/main/java/difflib/algorithm/xdiff/XDFEnv.java create mode 100644 src/main/java/difflib/algorithm/xdiff/XDFile.java create mode 100644 src/main/java/difflib/algorithm/xdiff/XDLClassifier.java create mode 100644 src/main/java/difflib/algorithm/xdiff/XHistogram.java create mode 100644 src/main/java/difflib/algorithm/xdiff/XPParam.java create mode 100644 src/main/java/difflib/algorithm/xdiff/XPrepare.java create mode 100644 src/test/java/difflib/algorithm/xdiff/XHistogramTest.java create mode 100644 src/test/java/difflib/algorithm/xdiff/XPrepareTest.java diff --git a/src/main/java/difflib/algorithm/myers/DifferentiationFailedException.java b/src/main/java/difflib/algorithm/DifferentiationFailedException.java similarity index 91% rename from src/main/java/difflib/algorithm/myers/DifferentiationFailedException.java rename to src/main/java/difflib/algorithm/DifferentiationFailedException.java index 9b959e3..58280ae 100644 --- a/src/main/java/difflib/algorithm/myers/DifferentiationFailedException.java +++ b/src/main/java/difflib/algorithm/DifferentiationFailedException.java @@ -17,16 +17,14 @@ See the License for the specific language governing permissions and limitations under the License. * #L% */ -package difflib.algorithm.myers; +package difflib.algorithm; import difflib.algorithm.DiffException; /** * Thrown whenever the differencing engine cannot produce the differences between two revisions of * ta text. - * - * - * @author Juanco Anez + * @see MyersDiff * @see difflib.DiffAlgorithm */ diff --git a/src/main/java/difflib/algorithm/myers/MyersDiff.java b/src/main/java/difflib/algorithm/myers/MyersDiff.java index e5b39e6..5c3a900 100644 --- a/src/main/java/difflib/algorithm/myers/MyersDiff.java +++ b/src/main/java/difflib/algorithm/myers/MyersDiff.java @@ -19,6 +19,7 @@ limitations under the License. */ package difflib.algorithm.myers; +import difflib.algorithm.DifferentiationFailedException; import difflib.algorithm.DiffAlgorithm; import difflib.algorithm.DiffException; import difflib.patch.ChangeDelta; diff --git a/src/main/java/difflib/algorithm/xdiff/XDFEnv.java b/src/main/java/difflib/algorithm/xdiff/XDFEnv.java new file mode 100644 index 0000000..a50d81f --- /dev/null +++ b/src/main/java/difflib/algorithm/xdiff/XDFEnv.java @@ -0,0 +1,25 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +/** + * + * @author tw + */ +public class XDFEnv { + public final XDFile xdf1 = new XDFile<>(); + public final XDFile xdf2 = new XDFile<>(); +} diff --git a/src/main/java/difflib/algorithm/xdiff/XDFile.java b/src/main/java/difflib/algorithm/xdiff/XDFile.java new file mode 100644 index 0000000..c307dc1 --- /dev/null +++ b/src/main/java/difflib/algorithm/xdiff/XDFile.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +import java.util.List; + +/** + * + * @author tw + */ +public class XDFile { + + int dend; + + int dstart; + public int ha[]; + public List data; +} diff --git a/src/main/java/difflib/algorithm/xdiff/XDLClassifier.java b/src/main/java/difflib/algorithm/xdiff/XDLClassifier.java new file mode 100644 index 0000000..2b9673c --- /dev/null +++ b/src/main/java/difflib/algorithm/xdiff/XDLClassifier.java @@ -0,0 +1,24 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +/** + * + * @author tw + */ +public class XDLClassifier { + +} diff --git a/src/main/java/difflib/algorithm/xdiff/XHistogram.java b/src/main/java/difflib/algorithm/xdiff/XHistogram.java new file mode 100644 index 0000000..b3501a7 --- /dev/null +++ b/src/main/java/difflib/algorithm/xdiff/XHistogram.java @@ -0,0 +1,45 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +import difflib.algorithm.DiffAlgorithm; +import difflib.algorithm.DiffException; +import difflib.patch.Patch; +import java.util.List; + +/** + * Histogram diff implementation. + * @author tw + */ +public class XHistogram implements DiffAlgorithm { + + XPParam xpp; + final XDFEnv env = new XDFEnv(); + + @Override + public Patch diff(List original, List revised) throws DiffException { + XPrepare.prepareEnvironment(original, revised, xpp, env); + + histogramDiff(xpp, env, + env.xdf1.dstart + 1, env.xdf1.dend - env.xdf1.dstart + 1, + env.xdf2.dstart + 1, env.xdf2.dend - env.xdf2.dstart + 1); + return null; + } + + private void histogramDiff(XPParam xpp, XDFEnv env, int line1, int count1, int line2, int count2) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } +} diff --git a/src/main/java/difflib/algorithm/xdiff/XPParam.java b/src/main/java/difflib/algorithm/xdiff/XPParam.java new file mode 100644 index 0000000..494bcc9 --- /dev/null +++ b/src/main/java/difflib/algorithm/xdiff/XPParam.java @@ -0,0 +1,29 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +/** + * + * @author tw + */ +public class XPParam { + public DiffAlgorithmType type; + public long flags; + + public static enum DiffAlgorithmType { + HISTOGRAM_DIFF + } +} diff --git a/src/main/java/difflib/algorithm/xdiff/XPrepare.java b/src/main/java/difflib/algorithm/xdiff/XPrepare.java new file mode 100644 index 0000000..5aabd49 --- /dev/null +++ b/src/main/java/difflib/algorithm/xdiff/XPrepare.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +import java.util.List; + +/** + * + * @author tw + */ +public final class XPrepare { + + private static void prepareContext(List data, XPParam xpp, XDLClassifier cf, XDFile xdf) { + xdf.data = data; + xdf.ha = new int[data.size()]; + for (int i=0;i void prepareEnvironment(List original, List revised, XPParam xpp, XDFEnv env) { + //sample calculation not needed, due to already List + long enl1 = original.size() + 1; + long enl2 = revised.size() + 1; + XDLClassifier cf = new XDLClassifier(); + + prepareContext(original, xpp, cf, env.xdf1); + prepareContext(revised, xpp, cf, env.xdf2); + } +} diff --git a/src/test/java/difflib/algorithm/xdiff/XHistogramTest.java b/src/test/java/difflib/algorithm/xdiff/XHistogramTest.java new file mode 100644 index 0000000..84a5656 --- /dev/null +++ b/src/test/java/difflib/algorithm/xdiff/XHistogramTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +import difflib.DiffUtils; +import static difflib.DiffUtilsTest.readStringListFromInputStream; +import difflib.TestConstants; +import difflib.algorithm.DiffException; +import difflib.algorithm.myers.MyersDiff; +import difflib.patch.Patch; +import java.io.IOException; +import java.util.Arrays; +import java.util.zip.ZipFile; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import org.junit.Ignore; + +/** + * + * @author tw + */ +public class XHistogramTest { + + public XHistogramTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of diff method, of class XHistogram. + */ + @Test + public void testDiff() throws Exception { + final Patch patch = new XHistogram().diff( + Arrays.asList("A","B","C","A","B","B","A"), + Arrays.asList("C","B","A","B","A","C")); + assertNotNull(patch); + assertEquals(4, patch.getDeltas().size()); + assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString()); + } + + @Test + public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException { + ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip"); + + Patch patch = new XHistogram().diff( + readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))), + readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")))); + + System.out.println("done"); + } +} diff --git a/src/test/java/difflib/algorithm/xdiff/XPrepareTest.java b/src/test/java/difflib/algorithm/xdiff/XPrepareTest.java new file mode 100644 index 0000000..45d07f7 --- /dev/null +++ b/src/test/java/difflib/algorithm/xdiff/XPrepareTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.algorithm.xdiff; + +import java.util.Arrays; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author tw + */ +public class XPrepareTest { + + public XPrepareTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of prepareEnvironment method, of class XPrepare. + */ + @Test + public void testPrepareEnvironment() { + XDFEnv env = new XDFEnv<>(); + XPrepare.prepareEnvironment( + Arrays.asList("A","B","C","A","B","B","A"), + Arrays.asList("C","B","A","B","A","C"), + new XPParam(), + env); + System.out.println(Arrays.toString(env.xdf1.ha)); + System.out.println(Arrays.toString(env.xdf2.ha)); + } + +}