Skip to content

Commit

Permalink
improve count statements performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Oct 1, 2024
1 parent eb54981 commit 75b15ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static boolean isAnyStatementIn(DuplicateCodeFinder.DuplicateCode duplic
public static boolean isConsideredDuplicateCode(List<CtStatement> left, List<CtStatement> right) {
var duplicate = new DuplicateCodeFinder.DuplicateCode(left, right);

if (duplicate.size() < MINIMUM_DUPLICATE_STATEMENT_SIZE) {
if (!duplicate.isMoreThanOrEqualTo(MINIMUM_DUPLICATE_STATEMENT_SIZE)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ private static int countStatements(CtStatement ctStatement) {
return Math.max(count, 1);
}

public boolean isMoreThanOrEqualTo(int threshold) {
int size = 0;
// TODO: this could be optimized in the future by stopping getElements when the threshold is reached
for (var statement : this.left) {
size += countStatements(statement);
if (size >= threshold) {
return true;
}
}

return Math.max(size, 1) >= threshold;
}

public List<StructuralEqualsVisitor.Difference> differences() {
return StreamSupport.stream(zip(this.left, this.right).spliterator(), false)
.flatMap(entry -> StructuralEqualsVisitor.findDifferences(entry.getKey(), entry.getValue()).stream())
Expand Down

0 comments on commit 75b15ba

Please sign in to comment.