Fix/improve hashCode for Functions

Some types of Function had hashCode methods which didn't match the
behavior of equals, while others could simply be rewritten in a simpler
way by using Objects.hash.
This commit is contained in:
Riccardo Azzolini 2019-10-25 22:48:49 +02:00
parent 0c316226e6
commit a74f047655
4 changed files with 11 additions and 5 deletions

View File

@ -136,7 +136,7 @@ public abstract class FunctionDynamic implements Function {
@Override @Override
public int hashCode() { public int hashCode() {
return functions.hashCode() + 883 * super.hashCode(); return Arrays.hashCode(functions);
} }
@Override @Override

View File

@ -6,6 +6,8 @@ import it.cavallium.warppi.util.Errors;
import it.cavallium.warppi.util.Utils; import it.cavallium.warppi.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.Objects;
public abstract class FunctionOperator implements Function { public abstract class FunctionOperator implements Function {
/** /**
@ -175,7 +177,7 @@ public abstract class FunctionOperator implements Function {
@Override @Override
public int hashCode() { public int hashCode() {
return parameter1.hashCode() + 7 * parameter2.hashCode() + 883 * super.hashCode(); return Objects.hash(parameter1, parameter2);
} }
@Override @Override

View File

@ -4,6 +4,8 @@ import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error; import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.Objects;
public abstract class FunctionSingle implements Function { public abstract class FunctionSingle implements Function {
private boolean simplified; private boolean simplified;
@ -117,7 +119,7 @@ public abstract class FunctionSingle implements Function {
@Override @Override
public int hashCode() { public int hashCode() {
return parameter.hashCode() + 883 * super.hashCode(); return Objects.hash(parameter);
} }
@Override @Override

View File

@ -8,6 +8,8 @@ import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error; import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.Objects;
public class Variable implements Function { public class Variable implements Function {
protected char var; protected char var;
@ -84,7 +86,7 @@ public class Variable implements Function {
@Override @Override
public int hashCode() { public int hashCode() {
return toString().hashCode(); return Objects.hash(var, type);
} }
@Override @Override