From 5c0142f7d7d3b7045d0a78ffda27ad4bdd2ead8f Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 25 Oct 2023 10:20:58 +0200 Subject: [PATCH 1/2] Made the GreedyStringTiling class deterministic in case both submissions have the same amount of tokens. --- .../java/de/jplag/GreedyStringTiling.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/de/jplag/GreedyStringTiling.java b/core/src/main/java/de/jplag/GreedyStringTiling.java index c832d4f30..19620ab56 100644 --- a/core/src/main/java/de/jplag/GreedyStringTiling.java +++ b/core/src/main/java/de/jplag/GreedyStringTiling.java @@ -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; @@ -22,9 +23,8 @@ public class GreedyStringTiling { private final int minimumMatchLength; - private final int minimumNeighborLength; - private JPlagOptions options; - private ConcurrentMap tokenTypeValues; + private final JPlagOptions options; + private final ConcurrentMap tokenTypeValues; private final Map> baseCodeMarkings = new IdentityHashMap<>(); private final Map cachedTokenValueLists = new IdentityHashMap<>(); @@ -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); } @@ -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 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); } From fe76d8e33ca7c89494d9874412de8c4227a085eb Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Mon, 30 Oct 2023 16:02:14 +0100 Subject: [PATCH 2/2] Improved naming --- core/src/main/java/de/jplag/GreedyStringTiling.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/de/jplag/GreedyStringTiling.java b/core/src/main/java/de/jplag/GreedyStringTiling.java index 19620ab56..3cef93354 100644 --- a/core/src/main/java/de/jplag/GreedyStringTiling.java +++ b/core/src/main/java/de/jplag/GreedyStringTiling.java @@ -78,8 +78,10 @@ public final JPlagComparison generateBaseCodeMarking(Submission submission, Subm public final JPlagComparison compare(Submission firstSubmission, Submission secondSubmission) { Submission smallerSubmission; Submission largerSubmission; - Comparator comp = Comparator.comparing((Submission s) -> s.getTokenList().size()).thenComparing(Submission::getName); - if (comp.compare(firstSubmission, secondSubmission) <= 0) { + Comparator submissionComparator = Comparator.comparing((Submission it) -> it.getTokenList().size()) + .thenComparing(Submission::getName); + + if (submissionComparator.compare(firstSubmission, secondSubmission) <= 0) { smallerSubmission = firstSubmission; largerSubmission = secondSubmission; } else {