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

Rename the class Table to TokenHashMap.

Timur Sağlam 5 лет назад
Родитель
Сommit
a27b52cb36

+ 6 - 6
jplag.frontend-utils/src/main/java/jplag/Table.java → jplag.frontend-utils/src/main/java/jplag/TokenHashMap.java

@@ -9,24 +9,24 @@ import java.util.Map;
 /**
  * A {@link HashMap} that maps Integer keys to multiple Integer values. Note that all keys with identical
  * <code>(key % prime)</code> are mapped to the same values. Specifically, <code>prime</code> is the next prime number
- * that is larger or equal to the specified size (see {@link Table#Table(int)}).
+ * that is larger or equal to the specified size (see {@link TokenHashMap#Table(int)}).
  */
-public class Table { // TODO TS: I think this might map token hashes to token indices for the token array in the structure class (rename?)
+public class TokenHashMap {
     private final Map<Integer, ArrayList<Integer>> mappedEntries;
     private final int primeNumber;
 
     /**
      * Creates the {@link HashMap}.
-     * @param size specifies the initial size and the key mapping (see {@link Table}).
+     * @param size specifies the initial size and the key mapping (see {@link TokenHashMap}).
      */
-    public Table(int size) {
+    public TokenHashMap(int size) {
         mappedEntries = new HashMap<>(size);
         primeNumber = nextPrimeNumber(size);
     }
 
     /**
      * Returns all stored numbers for a key. Note that all keys with identical <code>(key % prime)</code> are mapped to the
-     * same values (see {@link Table}).
+     * same values (see {@link TokenHashMap}).
      * @param key is the specific key.
      * @return the stored numbers or null if nothing is stored.
      */
@@ -40,7 +40,7 @@ public class Table { // TODO TS: I think this might map token hashes to token in
 
     /**
      * Stores a number for a key, does not replace the previous stored numbers for that key. Note that all keys with
-     * identical <code>(key % prime)</code> are mapped to the same values (see {@link Table}).
+     * identical <code>(key % prime)</code> are mapped to the same values (see {@link TokenHashMap}).
      * @param key is the specific key.
      * @param value is the number to store.
      */

+ 2 - 2
jplag.frontend-utils/src/main/java/jplag/TokenList.java

@@ -4,11 +4,11 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
- * List of tokens. Allows random access to individual tokens.
+ * List of tokens. Allows random access to individual tokens. Contains a hash map for token hashes.
  */
 public class TokenList implements TokenConstants {
     private final List<Token> tokens;
-    Table table = null;
+    TokenHashMap tokenHashes = null;
     int hash_length = -1;
 
     public TokenList() {

+ 4 - 4
jplag.frontend.java-1.9/src/main/java/jplag/java19/Parser.java

@@ -3,10 +3,10 @@ package jplag.java19;
 import java.io.File;
 
 public class Parser extends jplag.Parser {
-    private jplag.TokenList struct;
+    private jplag.TokenList tokenList;
 
     public jplag.TokenList parse(File dir, String files[]) {
-        struct = new jplag.TokenList();
+        tokenList = new jplag.TokenList();
         errors = 0;
         File pathedFiles[] = new File[files.length];
         for (int i = 0; i < files.length; i++) {
@@ -15,11 +15,11 @@ public class Parser extends jplag.Parser {
         JavacAdapter javac = new JavacAdapter();
         errors += javac.parseFiles(dir, pathedFiles, this);
         this.parseEnd();
-        return struct;
+        return tokenList;
     }
 
     public void add(int type, String filename, long line, long col, long length) {
-        struct.addToken(new JavaToken(type, filename, (int) line, (int) col, (int) length));
+        tokenList.addToken(new JavaToken(type, filename, (int) line, (int) col, (int) length));
     }
 
     public void errorsInc() {

+ 5 - 5
jplag/src/main/java/jplag/GreedyStringTiling.java

@@ -41,7 +41,7 @@ public class GreedyStringTiling implements TokenConstants {
         int modulo = ((1 << 6) - 1);   // Modulo 64!
 
         int loops = tokenList.size() - hashLength;
-        tokenList.table = (makeTable ? new Table(3 * loops) : null);
+        tokenList.tokenHashes = (makeTable ? new TokenHashMap(3 * loops) : null);
         int hash = 0;
         int hashedLength = 0;
         for (int i = 0; i < hashLength; i++) {
@@ -57,7 +57,7 @@ public class GreedyStringTiling implements TokenConstants {
             for (int i = 0; i < loops; i++) {
                 if (hashedLength >= hashLength) {
                     tokenList.getToken(i).hash = hash;
-                    tokenList.table.add(hash, i);   // add into hashtable
+                    tokenList.tokenHashes.add(hash, i);   // add into hashtable
                 } else {
                     tokenList.getToken(i).hash = -1;
                 }
@@ -102,7 +102,7 @@ public class GreedyStringTiling implements TokenConstants {
             largerSubmission = secondSubmission;
         }
         // if hashtable exists in first but not in second structure: flip around!
-        if (largerSubmission.tokenList.table == null && smallerSubmission.tokenList.table != null) {
+        if (largerSubmission.tokenList.tokenHashes == null && smallerSubmission.tokenList.tokenHashes != null) {
             Submission swap = smallerSubmission;
             smallerSubmission = largerSubmission;
             largerSubmission = swap;
@@ -142,7 +142,7 @@ public class GreedyStringTiling implements TokenConstants {
         if (first.hash_length != minimalTokenMatch) {
             createHashes(first, minimalTokenMatch, withBaseCode); // don't make table if it is not a base code comparison
         }
-        if (second.hash_length != minimalTokenMatch || second.table == null) {
+        if (second.hash_length != minimalTokenMatch || second.tokenHashes == null) {
             createHashes(second, minimalTokenMatch, true);
         }
 
@@ -152,7 +152,7 @@ public class GreedyStringTiling implements TokenConstants {
             maxmatch = minimalTokenMatch;
             matches.clear();
             for (int x = 0; x < first.size() - maxmatch; x++) {
-                List<Integer> elementsOfSecond = second.table.get(first.getToken(x).hash);
+                List<Integer> elementsOfSecond = second.tokenHashes.get(first.getToken(x).hash);
                 if (first.getToken(x).marked || first.getToken(x).hash == -1 || elementsOfSecond == null) {
                     continue;
                 }