Make EqualityInference deterministic
@@ -34,8 +34,7 @@ import com.google.common.collect.SetMultimap;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -170,14 +169,14 @@ public class EqualityInference
*/
public EqualityPartition generateEqualitiesPartitionedBy(Predicate<Symbol> symbolScope)
{
- Set<Expression> scopeEqualities = new HashSet<>();
- Set<Expression> scopeComplementEqualities = new HashSet<>();
- Set<Expression> scopeStraddlingEqualities = new HashSet<>();
+ ImmutableSet.Builder<Expression> scopeEqualities = ImmutableSet.builder();
+ ImmutableSet.Builder<Expression> scopeComplementEqualities = ImmutableSet.builder();
+ ImmutableSet.Builder<Expression> scopeStraddlingEqualities = ImmutableSet.builder();
for (Collection<Expression> equalitySet : equalitySets.asMap().values()) {
- Set<Expression> scopeExpressions = new HashSet<>();
- Set<Expression> scopeComplementExpressions = new HashSet<>();
- Set<Expression> scopeStraddlingExpressions = new HashSet<>();
+ Set<Expression> scopeExpressions = new LinkedHashSet<>();
+ Set<Expression> scopeComplementExpressions = new LinkedHashSet<>();
+ Set<Expression> scopeStraddlingExpressions = new LinkedHashSet<>();
// Try to push each non-derived expression into one side of the scope
for (Expression expression : filter(equalitySet, not(derivedExpressions::contains))) {
@@ -221,7 +220,7 @@ public class EqualityInference
}
}
- return new EqualityPartition(scopeEqualities, scopeComplementEqualities, scopeStraddlingEqualities);
+ return new EqualityPartition(scopeEqualities.build(), scopeComplementEqualities.build(), scopeStraddlingEqualities.build());
}
/**
@@ -340,7 +339,7 @@ public class EqualityInference
public static class Builder
{
private final DisjointSet<Expression> equalities = new DisjointSet<>();
- private final Set<Expression> derivedExpressions = new HashSet<>();
+ private final Set<Expression> derivedExpressions = new LinkedHashSet<>();
public Builder extractInferenceCandidates(Expression expression)
{
@@ -382,13 +381,14 @@ public class EqualityInference
Collection<Set<Expression>> equivalentClasses = equalities.getEquivalentClasses();
// Map every expression to the set of equivalent expressions
- Map<Expression, Set<Expression>> map = new HashMap<>();
+ ImmutableMap.Builder<Expression, Set<Expression>> mapBuilder = ImmutableMap.builder();
for (Set<Expression> expressions : equivalentClasses) {
- expressions.forEach(expression -> map.put(expression, expressions));
+ expressions.forEach(expression -> mapBuilder.put(expression, expressions));
}
// For every non-derived expression, extract the sub-expressions and see if they can be rewritten as other expressions. If so,
// use this new information to update the known equalities.
+ Map<Expression, Set<Expression>> map = mapBuilder.build();
for (Expression expression : map.keySet()) {
if (!derivedExpressions.contains(expression)) {
for (Expression subExpression : filter(SubExpressionExtractor.extract(expression), not(equalTo(expression)))) {
