From 4948f73154fdee8b37fcf19b2962098988362256 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Tue, 29 Dec 2015 15:27:00 +0000 Subject: [PATCH] Ensure some structs implement IEquatable. --- OpenRA.Game/Primitives/Pair.cs | 15 +++++---------- OpenRA.Game/Primitives/float2.cs | 13 ++++++------- OpenRA.Game/Primitives/int2.cs | 18 ++++++++---------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/OpenRA.Game/Primitives/Pair.cs b/OpenRA.Game/Primitives/Pair.cs index b4c2ab747a..f171cd25dd 100644 --- a/OpenRA.Game/Primitives/Pair.cs +++ b/OpenRA.Game/Primitives/Pair.cs @@ -8,12 +8,13 @@ */ #endregion +using System; using System.Collections.Generic; using System.Drawing; namespace OpenRA.Primitives { - public struct Pair + public struct Pair : IEquatable> { public T First; public U Second; @@ -37,16 +38,10 @@ namespace OpenRA.Primitives return !(a == b); } - public override bool Equals(object obj) - { - var o = obj as Pair?; - return o != null && o == this; - } + public override int GetHashCode() { return First.GetHashCode() ^ Second.GetHashCode(); } - public override int GetHashCode() - { - return First.GetHashCode() ^ Second.GetHashCode(); - } + public bool Equals(Pair other) { return this == other; } + public override bool Equals(object obj) { return obj is Pair && Equals((Pair)obj); } public Pair WithFirst(T t) { return new Pair(t, Second); } public Pair WithSecond(U u) { return new Pair(First, u); } diff --git a/OpenRA.Game/Primitives/float2.cs b/OpenRA.Game/Primitives/float2.cs index ecf24c2680..720b5eba4b 100644 --- a/OpenRA.Game/Primitives/float2.cs +++ b/OpenRA.Game/Primitives/float2.cs @@ -17,7 +17,7 @@ namespace OpenRA { [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")] [StructLayout(LayoutKind.Sequential)] - public struct float2 + public struct float2 : IEquatable { public float X, Y; @@ -72,13 +72,13 @@ namespace OpenRA public static bool operator ==(float2 me, float2 other) { return me.X == other.X && me.Y == other.Y; } public static bool operator !=(float2 me, float2 other) { return !(me == other); } + public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } - public override bool Equals(object obj) - { - var o = obj as float2?; - return o != null && o == this; - } + public bool Equals(float2 other) { return this == other; } + public override bool Equals(object obj) { return obj is float2 && Equals((float2)obj); } + + public override string ToString() { return X + "," + Y; } public static readonly float2 Zero = new float2(0, 0); @@ -92,7 +92,6 @@ namespace OpenRA public static float Dot(float2 a, float2 b) { return a.X * b.X + a.Y * b.Y; } public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); } - public override string ToString() { return "{0},{1}".F(X, Y); } public int2 ToInt2() { return new int2((int)X, (int)Y); } public static float2 Max(float2 a, float2 b) { return new float2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); } diff --git a/OpenRA.Game/Primitives/int2.cs b/OpenRA.Game/Primitives/int2.cs index 58bf18f37e..2d0a31c4e1 100644 --- a/OpenRA.Game/Primitives/int2.cs +++ b/OpenRA.Game/Primitives/int2.cs @@ -15,7 +15,7 @@ using System.Drawing; namespace OpenRA { [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")] - public struct int2 + public struct int2 : IEquatable { public readonly int X, Y; public int2(int x, int y) { this.X = x; this.Y = y; } @@ -33,11 +33,17 @@ namespace OpenRA public static bool operator ==(int2 me, int2 other) { return me.X == other.X && me.Y == other.Y; } public static bool operator !=(int2 me, int2 other) { return !(me == other); } + public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } + + public bool Equals(int2 other) { return this == other; } + public override bool Equals(object obj) { return obj is int2 && Equals((int2)obj); } + + public override string ToString() { return X + "," + Y; } + public int2 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); } public int2 Abs() { return new int2(Math.Abs(X), Math.Abs(Y)); } public int LengthSquared { get { return X * X + Y * Y; } } public int Length { get { return Exts.ISqrt(LengthSquared); } } - public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public int2 WithX(int newX) { @@ -52,19 +58,11 @@ namespace OpenRA public static int2 Max(int2 a, int2 b) { return new int2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); } public static int2 Min(int2 a, int2 b) { return new int2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); } - public override bool Equals(object obj) - { - var o = obj as int2?; - return o != null && o == this; - } - public static readonly int2 Zero = new int2(0, 0); public Point ToPoint() { return new Point(X, Y); } public PointF ToPointF() { return new PointF(X, Y); } public float2 ToFloat2() { return new float2(X, Y); } - public override string ToString() { return "{0},{1}".F(X, Y); } - // Change endianness of a uint32 public static uint Swap(uint orig) {