Skip to content

Commit

Permalink
Merge pull request #1351 from jplag/feature/greedyStringTiling-determ…
Browse files Browse the repository at this point in the history
…inistic

Made the GreedyStringTiling class deterministic in case both submissi…
  • Loading branch information
tsaglam authored Oct 31, 2023
2 parents 5183486 + fe76d8e commit 366e47b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 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,15 @@ 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> submissionComparator = Comparator.comparing((Submission it) -> it.getTokenList().size())
.thenComparing(Submission::getName);

if (submissionComparator.compare(firstSubmission, secondSubmission) <= 0) {
smallerSubmission = firstSubmission;
largerSubmission = secondSubmission;
} else {
smallerSubmission = secondSubmission;
largerSubmission = firstSubmission;
}
return compareInternal(smallerSubmission, largerSubmission);
}
Expand Down

0 comments on commit 366e47b

Please sign in to comment.