Skip to content

Commit

Permalink
chore: use HashCode struct
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Aug 20, 2024
1 parent faebf76 commit 41db9da
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 81 deletions.
4 changes: 1 addition & 3 deletions Editor/AnimatorParserV2/PropModNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public bool Equals(ValueInfo<T> other)

public override bool Equals(object obj) => obj is ValueInfo<T> other && Equals(other);

public override int GetHashCode() => _possibleValues == null
? 0
: _possibleValues.Aggregate(0, (current, value) => current ^ value.GetHashCode());
public override int GetHashCode() => _possibleValues == null ? 0 : _possibleValues.GetSetHashCode();

public override string ToString() =>
_possibleValues == null
Expand Down
12 changes: 1 addition & 11 deletions Editor/ObjectMapping/ObjectMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,7 @@ public PropertyDescriptor(int instanceId, Type type, string name)
Name = name;
}

public override int GetHashCode()
{
unchecked
{
var hashCode = InstanceId;
hashCode = (hashCode * 397) ^ Type.GetHashCode();
hashCode = (hashCode * 397) ^ Name.GetHashCode();
return hashCode;
}
}

public override int GetHashCode() => HashCode.Combine(InstanceId, Type, Name);
public bool Equals(PropertyDescriptor other) =>
InstanceId == other.InstanceId && Type == other.Type && Name == other.Name;
public override bool Equals(object? obj) => obj is PropertyDescriptor other && Equals(other);
Expand Down
6 changes: 2 additions & 4 deletions Editor/Processors/MergeBoneProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,9 @@ public bool Equals(BoneUniqKey other) =>
Equals(Transform, other.Transform) && _bindPoseInfo == other._bindPoseInfo;

public override bool Equals(object? obj) => obj is BoneUniqKey other && Equals(other);

public override int GetHashCode() =>
unchecked(_bindPoseInfo.GetHashCode() * 397) ^ (Transform != null ? Transform.GetHashCode() : 0);
public override int GetHashCode() => HashCode.Combine(_bindPoseInfo, Transform);
}


public struct MergeBoneTransParentInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected EditSkinnedMeshProcessor(TComponent component)

protected bool Equals(EditSkinnedMeshProcessor<TComponent> other) => Component == other.Component;

public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj != null &&
(ReferenceEquals(this, obj) ||
obj.GetType() == this.GetType() && Equals((EditSkinnedMeshProcessor<TComponent>)obj));
Expand Down
4 changes: 2 additions & 2 deletions Editor/Processors/SkinnedMeshes/MergeSkinnedMeshProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ class MeshInfoComputer : IMeshInfoComputer

private class BlendShapeNameComparator : IEqualityComparer<(string name, float weight)>
{
public static readonly BlendShapeNameComparator Instance = new BlendShapeNameComparator();
public static readonly BlendShapeNameComparator Instance = new();

public bool Equals((string name, float weight) x, (string name, float weight) y)
{
Expand All @@ -530,7 +530,7 @@ public bool Equals((string name, float weight) x, (string name, float weight) y)

public int GetHashCode((string name, float weight) obj)
{
return obj.name?.GetHashCode() ?? 0;
return obj.name.GetHashCode();
}
}
}
Expand Down
40 changes: 18 additions & 22 deletions Editor/Processors/TraceAndOptimize/AutoMergeSkinnedMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,28 +720,24 @@ public override bool Equals(object? obj)

public override int GetHashCode()
{
unchecked
{
var hashCode = HasNormals.GetHashCode();
hashCode = (hashCode * 397) ^ ActivenessAnimationLocations.GetHashCode();
hashCode = (hashCode * 397) ^ RendererAnimationLocations.GetHashCode();
hashCode = (hashCode * 397) ^ Activeness.GetHashCode();
hashCode = (hashCode * 397) ^ Bounds.GetHashCode();
hashCode = (hashCode * 397) ^ (int)ShadowCastingMode;
hashCode = (hashCode * 397) ^ ReceiveShadows.GetHashCode();
hashCode = (hashCode * 397) ^ (int)LightProbeUsage;
hashCode = (hashCode * 397) ^ (int)ReflectionProbeUsage;
hashCode = (hashCode * 397) ^ AllowOcclusionWhenDynamic.GetHashCode();
hashCode = (hashCode * 397) ^ (LightProbeProxyVolumeOverride != null
? LightProbeProxyVolumeOverride.GetHashCode()
: 0);
hashCode = (hashCode * 397) ^ (ProbeAnchor != null ? ProbeAnchor.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (int)Quality;
hashCode = (hashCode * 397) ^ UpdateWhenOffscreen.GetHashCode();
hashCode = (hashCode * 397) ^ (RootBone != null ? RootBone.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ SkinnedMotionVectors.GetHashCode();
return hashCode;
}
var hashCode = new HashCode();
hashCode.Add(HasNormals);
hashCode.Add(ActivenessAnimationLocations);
hashCode.Add(RendererAnimationLocations);
hashCode.Add(Activeness);
hashCode.Add(Bounds);
hashCode.Add(ShadowCastingMode);
hashCode.Add(ReceiveShadows);
hashCode.Add(LightProbeUsage);
hashCode.Add(ReflectionProbeUsage);
hashCode.Add(AllowOcclusionWhenDynamic);
hashCode.Add(LightProbeProxyVolumeOverride);
hashCode.Add(ProbeAnchor);
hashCode.Add(Quality);
hashCode.Add(UpdateWhenOffscreen);
hashCode.Add(RootBone);
hashCode.Add(SkinnedMotionVectors);
return hashCode.ToHashCode();
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions Editor/Utils/AnimationLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,15 @@ public override bool Equals(object? obj) =>

public override int GetHashCode()
{
unchecked
{
var hashCode = Component.GetHashCode();
hashCode = (hashCode * 397) ^ PlayableLayerIndex;
hashCode = (hashCode * 397) ^ AnimationLayerIndex;
hashCode = (hashCode * 397) ^ AnimatorState.GetHashCode();
hashCode = (hashCode * 397) ^ BlendTreeLocation.Length;
foreach (var location in BlendTreeLocation)
hashCode = (hashCode * 397) ^ location;
hashCode = (hashCode * 397) ^ Curve.GetHashCode2();
return hashCode;
}
var hashCode = new HashCode();
hashCode.Add(Component);
hashCode.Add(PlayableLayerIndex);
hashCode.Add(AnimationLayerIndex);
hashCode.Add(AnimatorState);
foreach (var location in BlendTreeLocation)
hashCode.Add(location);
hashCode.Add(Curve);
return hashCode.ToHashCode();
}

public IEnumerable<ObjectReference> ContextReferences => new[]
Expand Down
2 changes: 1 addition & 1 deletion Editor/Utils/ComponentOrGameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool TryAs<T>(out T gameObject) where T : Object

public bool Equals(ComponentOrGameObject other) => Equals(_object, other._object);
public override bool Equals(object obj) => obj is ComponentOrGameObject other && Equals(other);
public override int GetHashCode() => _object != null ? _object.GetHashCode() : 0;
public override int GetHashCode() => HashCode.Combine(_object);
public override string ToString() => _object != null ? _object.ToString() : string.Empty;
}
}
9 changes: 1 addition & 8 deletions Internal/Utils/EqualsHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ public EqualsHashSet(IEnumerable<T> collection) : this(new HashSet<T>(collection

public int Count => backedSet.Count;

public override int GetHashCode()
{
// we use XOR to make the hashcode order-independent
var hash = backedSet.Count;
foreach (var item in backedSet)
hash ^= item == null ? 0 : item.GetHashCode();
return hash;
}
public override int GetHashCode() => backedSet.GetSetHashCode();

public bool Equals(EqualsHashSet<T> other) =>
!ReferenceEquals(null, other) && (ReferenceEquals(this, other) || backedSet.SetEquals(other.backedSet));
Expand Down
24 changes: 11 additions & 13 deletions Internal/Utils/Matrix3x3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,17 @@ public Matrix3x3(

public override int GetHashCode()
{
unchecked
{
var hashCode = m00.GetHashCode();
hashCode = (hashCode * 397) ^ m10.GetHashCode();
hashCode = (hashCode * 397) ^ m20.GetHashCode();
hashCode = (hashCode * 397) ^ m01.GetHashCode();
hashCode = (hashCode * 397) ^ m11.GetHashCode();
hashCode = (hashCode * 397) ^ m21.GetHashCode();
hashCode = (hashCode * 397) ^ m02.GetHashCode();
hashCode = (hashCode * 397) ^ m12.GetHashCode();
hashCode = (hashCode * 397) ^ m22.GetHashCode();
return hashCode;
}
var hashCode = new HashCode();
hashCode.Add(m00);
hashCode.Add(m10);
hashCode.Add(m20);
hashCode.Add(m01);
hashCode.Add(m11);
hashCode.Add(m21);
hashCode.Add(m02);
hashCode.Add(m12);
hashCode.Add(m22);
return hashCode.ToHashCode();
}

public bool Equals(Matrix3x3 other) =>
Expand Down
8 changes: 4 additions & 4 deletions Internal/Utils/Utils.GetHashCode2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public static int GetHashCode2(this Keyframe curve)
return code.ToHashCode();
}

// The HashSet doesn't implement HashCode
public static int GetHashCode2<T>(this HashSet<T> set)
// Create a hashcode for a order-independent set
public static int GetSetHashCode<T>(this ICollection<T> collection)
{
// we use XOR to make the hashcode order-independent
var hash = set.Count;
foreach (var item in set)
var hash = collection.Count;
foreach (var item in collection)
hash ^= HashCode.Combine(item);
return hash;
}
Expand Down

0 comments on commit 41db9da

Please sign in to comment.