Skip to content

Commit

Permalink
Added some hash strats
Browse files Browse the repository at this point in the history
  • Loading branch information
Rongmario committed Apr 26, 2021
1 parent 62cdeda commit fd51093
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/zone/rong/loliasm/api/HashingStrategies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package zone.rong.loliasm.api;

import it.unimi.dsi.fastutil.Hash;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.block.model.ItemTransformVec3f;

import java.util.Arrays;
import java.util.Objects;

public class HashingStrategies {

public static final Hash.Strategy<Object> IDENTITY_OBJECT_HASH = new Hash.Strategy<Object>() {
@Override
public int hashCode(Object o) {
return System.identityHashCode(o);
}
@Override
public boolean equals(Object a, Object b) {
return a == b;
}
};

public static final Hash.Strategy<Object> GENERIC_OBJECT_HASH = new Hash.Strategy<Object>() {
@Override
public int hashCode(Object o) {
return Objects.hashCode(o);
}
@Override
public boolean equals(Object o1, Object o2) {
return Objects.equals(o1, o2);
}
};

public static final Hash.Strategy<float[][]> FLOAT_2D_ARRAY_HASH = new Hash.Strategy<float[][]>() {
@Override
public int hashCode(float[][] o) {
return Arrays.deepHashCode(o);
}
@Override
public boolean equals(float[][] a, float[][] b) {
return Arrays.deepEquals(a, b);
}
};

public static final Hash.Strategy<ItemCameraTransforms> ITEM_CAMERA_TRANSFORMS_HASH = new Hash.Strategy<ItemCameraTransforms>() {
@Override
public int hashCode(ItemCameraTransforms ict) {
int hash = vectorHash(ict.firstperson_left);
hash = hash * 31 + vectorHash(ict.firstperson_right);
hash = hash * 31 + vectorHash(ict.fixed);
hash = hash * 31 + vectorHash(ict.ground);
hash = hash * 31 + vectorHash(ict.gui);
hash = hash * 31 + vectorHash(ict.head);
hash = hash * 31 + vectorHash(ict.thirdperson_left);
return hash * 31 + vectorHash(ict.thirdperson_right);
}
@Override
public boolean equals(ItemCameraTransforms ict1, ItemCameraTransforms ict2) {
if (ict1 == null) {
return ict2 == null;
} else {
return Objects.equals(ict1.firstperson_left, ict2.firstperson_left) && Objects.equals(ict1.firstperson_right, ict2.firstperson_right) && Objects.equals(ict1.fixed, ict2.fixed) && Objects.equals(ict1.ground, ict2.ground) && Objects.equals(ict1.gui, ict2.gui) && Objects.equals(ict1.head, ict2.head) && Objects.equals(ict1.thirdperson_left, ict2.thirdperson_left) && Objects.equals(ict1.thirdperson_right, ict2.thirdperson_right);
}
}
};

private static int vectorHash(ItemTransformVec3f vector) {
int hash = ((Float.floatToIntBits(vector.rotation.getX())) * 31 + Float.floatToIntBits(vector.rotation.getY())) * 31 + Float.floatToIntBits(vector.rotation.getZ());
hash = hash * 31 + ((Float.floatToIntBits(vector.scale.getX())) * 31 + Float.floatToIntBits(vector.scale.getY())) * 31 + Float.floatToIntBits(vector.scale.getZ());
return hash * 31 + ((Float.floatToIntBits(vector.translation.getX())) * 31 + Float.floatToIntBits(vector.translation.getY())) * 31 + Float.floatToIntBits(vector.translation.getZ());
}

}

0 comments on commit fd51093

Please sign in to comment.