Skip to content

Commit c73e752

Browse files
committed
refactoring and started to reimplement histogram diff
1 parent c56423e commit c73e752

File tree

10 files changed

+352
-4
lines changed

10 files changed

+352
-4
lines changed

src/main/java/difflib/algorithm/myers/DifferentiationFailedException.java renamed to src/main/java/difflib/algorithm/DifferentiationFailedException.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
limitations under the License.
1818
* #L%
1919
*/
20-
package difflib.algorithm.myers;
20+
package difflib.algorithm;
2121

2222
import difflib.algorithm.DiffException;
2323

2424
/**
2525
* Thrown whenever the differencing engine cannot produce the differences between two revisions of
2626
* ta text.
27-
*
28-
*
29-
* @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
27+
3028
* @see MyersDiff
3129
* @see difflib.DiffAlgorithm
3230
*/

src/main/java/difflib/algorithm/myers/MyersDiff.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package difflib.algorithm.myers;
2121

22+
import difflib.algorithm.DifferentiationFailedException;
2223
import difflib.algorithm.DiffAlgorithm;
2324
import difflib.algorithm.DiffException;
2425
import difflib.patch.ChangeDelta;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
/**
19+
*
20+
* @author tw
21+
*/
22+
public class XDFEnv<T> {
23+
public final XDFile<T> xdf1 = new XDFile<>();
24+
public final XDFile<T> xdf2 = new XDFile<>();
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
import java.util.List;
19+
20+
/**
21+
*
22+
* @author tw
23+
*/
24+
public class XDFile<T> {
25+
26+
int dend;
27+
28+
int dstart;
29+
public int ha[];
30+
public List<T> data;
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
/**
19+
*
20+
* @author tw
21+
*/
22+
public class XDLClassifier {
23+
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
import difflib.algorithm.DiffAlgorithm;
19+
import difflib.algorithm.DiffException;
20+
import difflib.patch.Patch;
21+
import java.util.List;
22+
23+
/**
24+
* Histogram diff implementation.
25+
* @author tw
26+
*/
27+
public class XHistogram<T> implements DiffAlgorithm<T> {
28+
29+
XPParam xpp;
30+
final XDFEnv env = new XDFEnv();
31+
32+
@Override
33+
public Patch<T> diff(List<T> original, List<T> revised) throws DiffException {
34+
XPrepare.prepareEnvironment(original, revised, xpp, env);
35+
36+
histogramDiff(xpp, env,
37+
env.xdf1.dstart + 1, env.xdf1.dend - env.xdf1.dstart + 1,
38+
env.xdf2.dstart + 1, env.xdf2.dend - env.xdf2.dstart + 1);
39+
return null;
40+
}
41+
42+
private void histogramDiff(XPParam xpp, XDFEnv env, int line1, int count1, int line2, int count2) {
43+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
44+
}
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
/**
19+
*
20+
* @author tw
21+
*/
22+
public class XPParam {
23+
public DiffAlgorithmType type;
24+
public long flags;
25+
26+
public static enum DiffAlgorithmType {
27+
HISTOGRAM_DIFF
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
import java.util.List;
19+
20+
/**
21+
*
22+
* @author tw
23+
*/
24+
public final class XPrepare {
25+
26+
private static <T> void prepareContext(List<T> data, XPParam xpp, XDLClassifier cf, XDFile xdf) {
27+
xdf.data = data;
28+
xdf.ha = new int[data.size()];
29+
for (int i=0;i<data.size();i++) {
30+
xdf.ha[i] = data.get(i).hashCode();
31+
}
32+
xdf.dstart = 0;
33+
xdf.dend = data.size()-1;
34+
}
35+
36+
37+
public static <T> void prepareEnvironment(List<T> original, List<T> revised, XPParam xpp, XDFEnv env) {
38+
//sample calculation not needed, due to already List
39+
long enl1 = original.size() + 1;
40+
long enl2 = revised.size() + 1;
41+
XDLClassifier cf = new XDLClassifier();
42+
43+
prepareContext(original, xpp, cf, env.xdf1);
44+
prepareContext(revised, xpp, cf, env.xdf2);
45+
}
46+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
import difflib.DiffUtils;
19+
import static difflib.DiffUtilsTest.readStringListFromInputStream;
20+
import difflib.TestConstants;
21+
import difflib.algorithm.DiffException;
22+
import difflib.algorithm.myers.MyersDiff;
23+
import difflib.patch.Patch;
24+
import java.io.IOException;
25+
import java.util.Arrays;
26+
import java.util.zip.ZipFile;
27+
import org.junit.After;
28+
import org.junit.AfterClass;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import static org.junit.Assert.*;
33+
import org.junit.Ignore;
34+
35+
/**
36+
*
37+
* @author tw
38+
*/
39+
public class XHistogramTest {
40+
41+
public XHistogramTest() {
42+
}
43+
44+
@BeforeClass
45+
public static void setUpClass() {
46+
}
47+
48+
@AfterClass
49+
public static void tearDownClass() {
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
}
55+
56+
@After
57+
public void tearDown() {
58+
}
59+
60+
/**
61+
* Test of diff method, of class XHistogram.
62+
*/
63+
@Test
64+
public void testDiff() throws Exception {
65+
final Patch<String> patch = new XHistogram<String>().diff(
66+
Arrays.asList("A","B","C","A","B","B","A"),
67+
Arrays.asList("C","B","A","B","A","C"));
68+
assertNotNull(patch);
69+
assertEquals(4, patch.getDeltas().size());
70+
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());
71+
}
72+
73+
@Test
74+
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException {
75+
ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip");
76+
77+
Patch<String> patch = new XHistogram<String>().diff(
78+
readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
79+
readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));
80+
81+
System.out.println("done");
82+
}
83+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm.xdiff;
17+
18+
import java.util.Arrays;
19+
import org.junit.After;
20+
import org.junit.AfterClass;
21+
import org.junit.Before;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
*
28+
* @author tw
29+
*/
30+
public class XPrepareTest {
31+
32+
public XPrepareTest() {
33+
}
34+
35+
@BeforeClass
36+
public static void setUpClass() {
37+
}
38+
39+
@AfterClass
40+
public static void tearDownClass() {
41+
}
42+
43+
@Before
44+
public void setUp() {
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
}
50+
51+
/**
52+
* Test of prepareEnvironment method, of class XPrepare.
53+
*/
54+
@Test
55+
public void testPrepareEnvironment() {
56+
XDFEnv<String> env = new XDFEnv<>();
57+
XPrepare.prepareEnvironment(
58+
Arrays.asList("A","B","C","A","B","B","A"),
59+
Arrays.asList("C","B","A","B","A","C"),
60+
new XPParam(),
61+
env);
62+
System.out.println(Arrays.toString(env.xdf1.ha));
63+
System.out.println(Arrays.toString(env.xdf2.ha));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)