Merge pull request #10323 from RoosterDragon/struct-equatable

Ensure some structs implement IEquatable<T>.
This commit is contained in:
Matthias Mailänder
2015-12-30 09:28:09 +01:00
3 changed files with 19 additions and 27 deletions

View File

@@ -8,12 +8,13 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
namespace OpenRA.Primitives namespace OpenRA.Primitives
{ {
public struct Pair<T, U> public struct Pair<T, U> : IEquatable<Pair<T, U>>
{ {
public T First; public T First;
public U Second; public U Second;
@@ -37,16 +38,10 @@ namespace OpenRA.Primitives
return !(a == b); return !(a == b);
} }
public override bool Equals(object obj) public override int GetHashCode() { return First.GetHashCode() ^ Second.GetHashCode(); }
{
var o = obj as Pair<T, U>?;
return o != null && o == this;
}
public override int GetHashCode() public bool Equals(Pair<T, U> other) { return this == other; }
{ public override bool Equals(object obj) { return obj is Pair<T, U> && Equals((Pair<T, U>)obj); }
return First.GetHashCode() ^ Second.GetHashCode();
}
public Pair<T, U> WithFirst(T t) { return new Pair<T, U>(t, Second); } public Pair<T, U> WithFirst(T t) { return new Pair<T, U>(t, Second); }
public Pair<T, U> WithSecond(U u) { return new Pair<T, U>(First, u); } public Pair<T, U> WithSecond(U u) { return new Pair<T, U>(First, u); }

View File

@@ -17,7 +17,7 @@ namespace OpenRA
{ {
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")] [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")]
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct float2 public struct float2 : IEquatable<float2>
{ {
public float X, Y; 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.X == other.X && me.Y == other.Y; }
public static bool operator !=(float2 me, float2 other) { return !(me == other); } public static bool operator !=(float2 me, float2 other) { return !(me == other); }
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
public override bool Equals(object obj) public bool Equals(float2 other) { return this == other; }
{ public override bool Equals(object obj) { return obj is float2 && Equals((float2)obj); }
var o = obj as float2?;
return o != null && o == this; public override string ToString() { return X + "," + Y; }
}
public static readonly float2 Zero = new float2(0, 0); 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 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 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 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)); } public static float2 Max(float2 a, float2 b) { return new float2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }

View File

@@ -15,7 +15,7 @@ using System.Drawing;
namespace OpenRA namespace OpenRA
{ {
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")] [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")]
public struct int2 public struct int2 : IEquatable<int2>
{ {
public readonly int X, Y; public readonly int X, Y;
public int2(int x, int y) { this.X = x; this.Y = 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.X == other.X && me.Y == other.Y; }
public static bool operator !=(int2 me, int2 other) { return !(me == other); } 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 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); }
public int2 Abs() { return new int2(Math.Abs(X), Math.Abs(Y)); } public int2 Abs() { return new int2(Math.Abs(X), Math.Abs(Y)); }
public int LengthSquared { get { return X * X + Y * Y; } } public int LengthSquared { get { return X * X + Y * Y; } }
public int Length { get { return Exts.ISqrt(LengthSquared); } } public int Length { get { return Exts.ISqrt(LengthSquared); } }
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
public int2 WithX(int newX) 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 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 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 static readonly int2 Zero = new int2(0, 0);
public Point ToPoint() { return new Point(X, Y); } public Point ToPoint() { return new Point(X, Y); }
public PointF ToPointF() { return new PointF(X, Y); } public PointF ToPointF() { return new PointF(X, Y); }
public float2 ToFloat2() { return new float2(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 // Change endianness of a uint32
public static uint Swap(uint orig) public static uint Swap(uint orig)
{ {