forked from JavaTX/JavaCompilerCore
hashcode, equals and compareTo
This commit is contained in:
parent
b0153be1cd
commit
3d38ea2e08
@ -2,16 +2,18 @@ package de.dhbwstuttgart.typinference.unify.model;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author DH10STF
|
* @author DH10STF
|
||||||
*
|
*
|
||||||
* SPEEDUP
|
* TODO SPEEDUP
|
||||||
* - Caching of the results
|
* - Caching of the results
|
||||||
|
* - Global map from int to mtype und mtype[] array mit int[] austauschen und wenn nötig nachschlagen
|
||||||
|
* - Vorteil: Jeder Typ wäre globally unique -> Memory effizient
|
||||||
|
* - Nachteil: Nachschlagen kostet zeit, Dictionary muss gemanaged werden
|
||||||
|
* - TypeArgs in HashCode berechnung stärker / weniger einbeziehen
|
||||||
*/
|
*/
|
||||||
public class MType {
|
public class MType implements Comparable<MType>{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* First three bits indicate the meta type:
|
* First three bits indicate the meta type:
|
||||||
@ -23,11 +25,12 @@ public class MType {
|
|||||||
*
|
*
|
||||||
* Advantages of using a unique identifier for mapping:
|
* Advantages of using a unique identifier for mapping:
|
||||||
* - Fast checks (Equality and Membership)
|
* - Fast checks (Equality and Membership)
|
||||||
* - Perfect Hashing
|
* - Easy Hashing
|
||||||
|
* - Memory efficient
|
||||||
*/
|
*/
|
||||||
private int identifier = 0;
|
private int identifier = 0;
|
||||||
|
|
||||||
private int[] typeArgs = null;
|
private MType[] typeArgs = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to mask the first three bits of an integer.
|
* Used to mask the first three bits of an integer.
|
||||||
@ -35,14 +38,11 @@ public class MType {
|
|||||||
*/
|
*/
|
||||||
private final int mask = -53870912;
|
private final int mask = -53870912;
|
||||||
|
|
||||||
public MType(int identifier, int...typeArgs) {
|
public MType(int identifier, MType... typeArgs) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
|
|
||||||
if(typeArgs.length != 0) {
|
|
||||||
this.typeArgs = Arrays.copyOf(typeArgs, typeArgs.length);
|
this.typeArgs = Arrays.copyOf(typeArgs, typeArgs.length);
|
||||||
Arrays.sort(typeArgs);
|
Arrays.sort(typeArgs);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSimpleType() {
|
public boolean isSimpleType() {
|
||||||
return (identifier & mask) == 0;
|
return (identifier & mask) == 0;
|
||||||
@ -68,17 +68,48 @@ public class MType {
|
|||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MType[] getTypeArgs() {
|
||||||
|
return typeArgs;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(!(obj instanceof MType))
|
if(!(obj instanceof MType))
|
||||||
return false;
|
return false;
|
||||||
|
if(obj.hashCode() != hashCode())
|
||||||
|
return false;
|
||||||
|
|
||||||
throw new NotImplementedException();
|
MType other = (MType) obj;
|
||||||
|
|
||||||
|
if(other.getIdentifier() != identifier)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
MType[] otherTypeArgs = other.getTypeArgs();
|
||||||
|
|
||||||
|
if(otherTypeArgs.length != typeArgs.length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for(int i = 0; i < typeArgs.length; i++)
|
||||||
|
if(!typeArgs[i].equals(otherTypeArgs[i]))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
// TODO Auto-generated method stub
|
return identifier + 31 * typeArgs.length;
|
||||||
return super.hashCode();
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(MType o) {
|
||||||
|
|
||||||
|
// Comparison is insensitive to type arguments.
|
||||||
|
|
||||||
|
if(o.getIdentifier() > identifier)
|
||||||
|
return -1;
|
||||||
|
if(o.getIdentifier() < identifier)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user