Просмотр исходного кода

Remove JPlagComparison inheritance from Matches

Philipp Bauch 6 лет назад
Родитель
Сommit
c5330b62a1

+ 41 - 23
jplag/src/main/java/jplag/JPlagComparison.java

@@ -1,12 +1,14 @@
 package jplag;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.List;
 
 /**
  * This method represents the whole result of a comparison between two submissions.
  */
-public class JPlagComparison extends Matches implements Comparator<JPlagComparison> {
+public class JPlagComparison implements Comparator<JPlagComparison> {
 
   public Submission subA;
   public Submission subB;
@@ -14,16 +16,28 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
   public AllBasecodeMatches bcmatchesA = null;
   public AllBasecodeMatches bcmatchesB = null;
 
+  public List<Match> matches = new ArrayList<>();
+
   public JPlagComparison(Submission subA, Submission subB) {
     super();
     this.subA = subA;
     this.subB = subB;
   }
 
+  public final void addMatch(int startA, int startB, int length) {
+    for (Match match : matches) {
+      if (match.overlap(startA, startB, length)) {
+        return;
+      }
+    }
+
+    matches.add(new Match(startA, startB, length));
+  }
+
   /* s==0 uses the start indexes of subA as key for the sorting algorithm.
    * Otherwise the start indexes of subB are used. */
   public final int[] sort_permutation(int s) {   // bubblesort!!!
-    int size = size();
+    int size = matches.size();
     int[] perm = new int[size];
     int i, j, tmp;
 
@@ -35,7 +49,7 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
     if (s == 0) {     // submission A
       for (i = 1; i < size; i++) {
         for (j = 0; j < (size - i); j++) {
-          if (matches[perm[j]].startA > matches[perm[j + 1]].startA) {
+          if (matches.get(perm[j]).startA > matches.get(perm[j + 1]).startA) {
             tmp = perm[j];
             perm[j] = perm[j + 1];
             perm[j + 1] = tmp;
@@ -45,7 +59,7 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
     } else {        // submission B
       for (i = 1; i < size; i++) {
         for (j = 0; j < (size - i); j++) {
-          if (matches[perm[j]].startB > matches[perm[j + 1]].startB) {
+          if (matches.get(perm[j]).startB > matches.get(perm[j + 1]).startB) {
             tmp = perm[j];
             perm[j] = perm[j + 1];
             perm[j + 1] = tmp;
@@ -60,15 +74,15 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
    */
   public final void sort() {   // bubblesort!!!
     Match tmp;
-    int size = size();
+    int size = matches.size();
     int i, j;
 
     for (i = 1; i < size; i++) {
       for (j = 0; j < (size - i); j++) {
-        if (matches[j].startA > matches[j + 1].startA) {
-          tmp = matches[j];
-          matches[j] = matches[j + 1];
-          matches[j + 1] = tmp;
+        if (matches.get(j).startA > matches.get(j + 1).startA) {
+          tmp = matches.get(j);
+          matches.set(j, matches.get(j + 1));
+          matches.set(j + 1, tmp);
         }
       }
     }
@@ -81,20 +95,24 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
    * Get the total number of matched tokens for this comparison.
    */
   public final int getNumberOfMatchedTokens() {
-    int erg = 0;
-    for (int i = 0; i < size(); i++) {
-      erg += matches[i].length;
+    int numberOfMatchedTokens = 0;
+
+    for (Match match : matches) {
+      numberOfMatchedTokens += match.length;
     }
-    return erg;
+
+    return numberOfMatchedTokens;
   }
 
   private int biggestMatch() {
     int erg = 0;
-    for (int i = 0; i < size(); i++) {
-      if (matches[i].length > erg) {
-        erg = matches[i].length;
+
+    for (Match match : matches) {
+      if (match.length > erg) {
+        erg = match.length;
       }
     }
+
     return erg;
   }
 
@@ -197,10 +215,10 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
     Token[] tokens = (j == 0 ? subA : subB).tokenList.tokens;
     int i, h, starti, starth, count = 1;
     o1:
-    for (i = 1; i < size(); i++) {
-      starti = (j == 0 ? matches[i].startA : matches[i].startB);
+    for (i = 1; i < matches.size(); i++) {
+      starti = (j == 0 ? matches.get(i).startA : matches.get(i).startB);
       for (h = 0; h < i; h++) {
-        starth = (j == 0 ? matches[h].startA : matches[h].startB);
+        starth = (j == 0 ? matches.get(h).startA : matches.get(h).startB);
         if (tokens[starti].file.equals(tokens[starth].file)) {
           continue o1;
         }
@@ -208,13 +226,13 @@ public class JPlagComparison extends Matches implements Comparator<JPlagComparis
       count++;
     }
     String[] res = new String[count];
-    res[0] = tokens[(j == 0 ? matches[0].startA : matches[0].startB)].file;
+    res[0] = tokens[(j == 0 ? matches.get(0).startA : matches.get(0).startB)].file;
     count = 1;
     o2:
-    for (i = 1; i < size(); i++) {
-      starti = (j == 0 ? matches[i].startA : matches[i].startB);
+    for (i = 1; i < matches.size(); i++) {
+      starti = (j == 0 ? matches.get(i).startA : matches.get(i).startB);
       for (h = 0; h < i; h++) {
-        starth = (j == 0 ? matches[h].startA : matches[h].startB);
+        starth = (j == 0 ? matches.get(h).startA : matches.get(h).startB);
         if (tokens[starti].file.equals(tokens[starth].file)) {
           continue o2;
         }

+ 4 - 2
jplag/src/main/java/jplag/Matches.java

@@ -1,7 +1,9 @@
 package jplag;
 
 /**
- * Minimal class that stores "Match" objects.
+ * Minimal data structure that stores "Match" objects.
+ * <p>
+ * Note: This class is only used by GSTiling as a data structure to store matches.
  */
 public class Matches {
 
@@ -22,7 +24,7 @@ public class Matches {
     return anzahl;
   }
 
-  public final void ensureCapacity(int minCapacity) {
+  private void ensureCapacity(int minCapacity) {
     int oldCapacity = matches.length;
     if (minCapacity > oldCapacity) {
       Match[] oldMatches = matches;

+ 6 - 6
jplag/src/main/java/jplag/strategy/NormalComparisonStrategy.java

@@ -36,7 +36,7 @@ public class NormalComparisonStrategy extends AbstractComparisonStrategy {
     int i, j, numberOfComparisons = 0;
 
     Submission s1, s2;
-    JPlagComparison match;
+    JPlagComparison comparison;
 
     long timeMillis = System.currentTimeMillis();
 
@@ -54,17 +54,17 @@ public class NormalComparisonStrategy extends AbstractComparisonStrategy {
           continue;
         }
 
-        match = this.gSTiling.compare(s1, s2);
+        comparison = this.gSTiling.compare(s1, s2);
         numberOfComparisons++;
 
-        System.out.println("Comparing " + s1.name + "-" + s2.name + ": " + match.percent());
+        System.out.println("Comparing " + s1.name + "-" + s2.name + ": " + comparison.percent());
 
         if (baseCodeSubmission != null) {
-          match.bcmatchesA = baseCodeMatches.get(match.subA.name);
-          match.bcmatchesB = baseCodeMatches.get(match.subB.name);
+          comparison.bcmatchesA = baseCodeMatches.get(comparison.subA.name);
+          comparison.bcmatchesB = baseCodeMatches.get(comparison.subB.name);
         }
 
-        registerMatch(match, dist, avgMatches, maxMatches, null, i, j);
+        registerMatch(comparison, dist, avgMatches, maxMatches, null, i, j);
       }
     }