mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
refactoring and started to reimplement histogram diff
This commit is contained in:
@@ -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 <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
|
||||
|
||||
* @see MyersDiff
|
||||
* @see difflib.DiffAlgorithm
|
||||
*/
|
||||
@@ -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;
|
||||
|
||||
25
src/main/java/difflib/algorithm/xdiff/XDFEnv.java
Normal file
25
src/main/java/difflib/algorithm/xdiff/XDFEnv.java
Normal file
@@ -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<T> {
|
||||
public final XDFile<T> xdf1 = new XDFile<>();
|
||||
public final XDFile<T> xdf2 = new XDFile<>();
|
||||
}
|
||||
31
src/main/java/difflib/algorithm/xdiff/XDFile.java
Normal file
31
src/main/java/difflib/algorithm/xdiff/XDFile.java
Normal file
@@ -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<T> {
|
||||
|
||||
int dend;
|
||||
|
||||
int dstart;
|
||||
public int ha[];
|
||||
public List<T> data;
|
||||
}
|
||||
24
src/main/java/difflib/algorithm/xdiff/XDLClassifier.java
Normal file
24
src/main/java/difflib/algorithm/xdiff/XDLClassifier.java
Normal file
@@ -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 {
|
||||
|
||||
}
|
||||
45
src/main/java/difflib/algorithm/xdiff/XHistogram.java
Normal file
45
src/main/java/difflib/algorithm/xdiff/XHistogram.java
Normal file
@@ -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<T> implements DiffAlgorithm<T> {
|
||||
|
||||
XPParam xpp;
|
||||
final XDFEnv env = new XDFEnv();
|
||||
|
||||
@Override
|
||||
public Patch<T> diff(List<T> original, List<T> 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.
|
||||
}
|
||||
}
|
||||
29
src/main/java/difflib/algorithm/xdiff/XPParam.java
Normal file
29
src/main/java/difflib/algorithm/xdiff/XPParam.java
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
46
src/main/java/difflib/algorithm/xdiff/XPrepare.java
Normal file
46
src/main/java/difflib/algorithm/xdiff/XPrepare.java
Normal file
@@ -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 <T> void prepareContext(List<T> data, XPParam xpp, XDLClassifier cf, XDFile xdf) {
|
||||
xdf.data = data;
|
||||
xdf.ha = new int[data.size()];
|
||||
for (int i=0;i<data.size();i++) {
|
||||
xdf.ha[i] = data.get(i).hashCode();
|
||||
}
|
||||
xdf.dstart = 0;
|
||||
xdf.dend = data.size()-1;
|
||||
}
|
||||
|
||||
|
||||
public static <T> void prepareEnvironment(List<T> original, List<T> 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);
|
||||
}
|
||||
}
|
||||
83
src/test/java/difflib/algorithm/xdiff/XHistogramTest.java
Normal file
83
src/test/java/difflib/algorithm/xdiff/XHistogramTest.java
Normal file
@@ -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<String> patch = new XHistogram<String>().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<String> patch = new XHistogram<String>().diff(
|
||||
readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
|
||||
readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));
|
||||
|
||||
System.out.println("done");
|
||||
}
|
||||
}
|
||||
66
src/test/java/difflib/algorithm/xdiff/XPrepareTest.java
Normal file
66
src/test/java/difflib/algorithm/xdiff/XPrepareTest.java
Normal file
@@ -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<String> 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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user