implement Vector3f, Vector2f Equals, GetHashCode

This commit is contained in:
ikpil 2023-05-08 23:12:11 +09:00
parent 208c1a9e76
commit 7677add89e
3 changed files with 67 additions and 4 deletions

View File

@ -0,0 +1,10 @@
namespace DotRecast.Core
{
public static class RcHashCodes
{
public static int CombineHashCodes(int h1, int h2)
{
return (((h1 << 5) + h1) ^ h2);
}
}
}

View File

@ -19,5 +19,36 @@ namespace DotRecast.Core
throw new IndexOutOfRangeException("vector2f index out of range"); throw new IndexOutOfRangeException("vector2f index out of range");
} }
public override bool Equals(object obj)
{
if (!(obj is Vector2f))
return false;
return Equals((Vector2f)obj);
}
public bool Equals(Vector2f other)
{
return x.Equals(other.x) &&
y.Equals(other.y);
}
public override int GetHashCode()
{
int hash = x.GetHashCode();
hash = RcHashCodes.CombineHashCodes(hash, y.GetHashCode());
return hash;
}
public static bool operator ==(Vector2f left, Vector2f right)
{
return left.Equals(right);
}
public static bool operator !=(Vector2f left, Vector2f right)
{
return !left.Equals(right);
}
} }
} }

View File

@ -88,11 +88,33 @@ namespace DotRecast.Core
} }
} }
public override bool Equals(object obj)
{
if (!(obj is Vector3f))
return false;
return Equals((Vector3f)obj);
}
public bool Equals(Vector3f other)
{
return x.Equals(other.x) &&
y.Equals(other.y) &&
z.Equals(other.z);
}
public override int GetHashCode()
{
int hash = x.GetHashCode();
hash = RcHashCodes.CombineHashCodes(hash, y.GetHashCode());
hash = RcHashCodes.CombineHashCodes(hash, z.GetHashCode());
return hash;
}
public static bool operator ==(Vector3f left, Vector3f right) public static bool operator ==(Vector3f left, Vector3f right)
{ {
return left.x.Equals(right.x) return left.Equals(right);
&& left.y.Equals(right.y)
&& left.z.Equals(right.z);
} }
public static bool operator !=(Vector3f left, Vector3f right) public static bool operator !=(Vector3f left, Vector3f right)
@ -100,4 +122,4 @@ namespace DotRecast.Core
return !left.Equals(right); return !left.Equals(right);
} }
} }
} }