Skip to content

Commit

Permalink
Made the GreedyStringTiling class deterministic in case both submissi…
Browse files Browse the repository at this point in the history
…ons have the same amount of tokens.
  • Loading branch information
TwoOfTwelve committed Oct 25, 2023
1 parent 4349d4f commit 5c0142f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions core/src/main/java/de/jplag/GreedyStringTiling.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.jplag;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
Expand All @@ -22,9 +23,8 @@
public class GreedyStringTiling {

private final int minimumMatchLength;
private final int minimumNeighborLength;
private JPlagOptions options;
private ConcurrentMap<TokenType, Integer> tokenTypeValues;
private final JPlagOptions options;
private final ConcurrentMap<TokenType, Integer> tokenTypeValues;
private final Map<Submission, Set<Token>> baseCodeMarkings = new IdentityHashMap<>();

private final Map<Submission, int[]> cachedTokenValueLists = new IdentityHashMap<>();
Expand All @@ -33,8 +33,8 @@ public class GreedyStringTiling {
public GreedyStringTiling(JPlagOptions options) {
this.options = options;
// Ensures 1 <= neighborLength <= minimumTokenMatch
this.minimumNeighborLength = Math.min(Math.max(options.mergingOptions().minimumNeighborLength(), 1), options.minimumTokenMatch());
this.minimumMatchLength = options.mergingOptions().enabled() ? this.minimumNeighborLength : options.minimumTokenMatch();
int minimumNeighborLength = Math.min(Math.max(options.mergingOptions().minimumNeighborLength(), 1), options.minimumTokenMatch());
this.minimumMatchLength = options.mergingOptions().enabled() ? minimumNeighborLength : options.minimumTokenMatch();
this.tokenTypeValues = new ConcurrentHashMap<>();
this.tokenTypeValues.put(SharedTokenType.FILE_END, 0);
}
Expand Down Expand Up @@ -78,12 +78,13 @@ public final JPlagComparison generateBaseCodeMarking(Submission submission, Subm
public final JPlagComparison compare(Submission firstSubmission, Submission secondSubmission) {
Submission smallerSubmission;
Submission largerSubmission;
if (firstSubmission.getTokenList().size() > secondSubmission.getTokenList().size()) {
smallerSubmission = secondSubmission;
largerSubmission = firstSubmission;
} else {
Comparator<Submission> comp = Comparator.comparing((Submission s) -> s.getTokenList().size()).thenComparing(Submission::getName);
if (comp.compare(firstSubmission, secondSubmission) <= 0) {
smallerSubmission = firstSubmission;
largerSubmission = secondSubmission;
} else {
smallerSubmission = secondSubmission;
largerSubmission = firstSubmission;
}
return compareInternal(smallerSubmission, largerSubmission);
}
Expand Down

0 comments on commit 5c0142f

Please sign in to comment.