Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the GreedyStringTiling class deterministic in case both submissi… #1351

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading