Implemented IEquatable<T> to speed up equality comparisons.
Actor, CPos, CVec, WAngle, WPos, WRange, WRot and WVec structs now implement IEquatable<T> which means unboxing/casting costs can be eliminated. Also simplified the ToString method by concatenating components directly rather than using a format string since the overhead is a bit high for simple cases like this.
This commit is contained in:
@@ -21,7 +21,7 @@ using OpenRA.Traits;
|
|||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
public class Actor : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding
|
public class Actor : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding, IEquatable<Actor>
|
||||||
{
|
{
|
||||||
public readonly ActorInfo Info;
|
public readonly ActorInfo Info;
|
||||||
|
|
||||||
@@ -158,7 +158,12 @@ namespace OpenRA
|
|||||||
public override bool Equals(object obj)
|
public override bool Equals(object obj)
|
||||||
{
|
{
|
||||||
var o = obj as Actor;
|
var o = obj as Actor;
|
||||||
return o != null && o.ActorID == ActorID;
|
return o != null && Equals(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(Actor other)
|
||||||
|
{
|
||||||
|
return ActorID == other.ActorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ using OpenRA.Scripting;
|
|||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
public struct CPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding
|
public struct CPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<CPos>
|
||||||
{
|
{
|
||||||
public readonly int X, Y;
|
public readonly int X, Y;
|
||||||
|
|
||||||
@@ -52,13 +52,10 @@ namespace OpenRA
|
|||||||
|
|
||||||
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(CPos other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is CPos && Equals((CPos)obj); }
|
||||||
var o = obj as CPos?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() { return "{0},{1}".F(X, Y); }
|
public override string ToString() { return X + "," + Y; }
|
||||||
|
|
||||||
#region Scripting interface
|
#region Scripting interface
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ using OpenRA.Scripting;
|
|||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
public struct CVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding
|
public struct CVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<CVec>
|
||||||
{
|
{
|
||||||
public readonly int X, Y;
|
public readonly int X, Y;
|
||||||
|
|
||||||
@@ -63,13 +63,10 @@ namespace OpenRA
|
|||||||
|
|
||||||
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(CVec other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is CVec && Equals((CVec)obj); }
|
||||||
var o = obj as CVec?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() { return "{0},{1}".F(X, Y); }
|
public override string ToString() { return X + "," + Y; }
|
||||||
|
|
||||||
public static readonly CVec[] directions =
|
public static readonly CVec[] directions =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace OpenRA
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 1D angle - 1024 units = 360 degrees.
|
/// 1D angle - 1024 units = 360 degrees.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public struct WAngle
|
public struct WAngle : IEquatable<WAngle>
|
||||||
{
|
{
|
||||||
public readonly int Angle;
|
public readonly int Angle;
|
||||||
|
|
||||||
@@ -37,11 +37,8 @@ namespace OpenRA
|
|||||||
|
|
||||||
public override int GetHashCode() { return Angle.GetHashCode(); }
|
public override int GetHashCode() { return Angle.GetHashCode(); }
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
public bool Equals(WAngle other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is WAngle && Equals((WAngle)obj); }
|
||||||
var o = obj as WAngle?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Sin() { return new WAngle(Angle - 256).Cos(); }
|
public int Sin() { return new WAngle(Angle - 256).Cos(); }
|
||||||
|
|
||||||
@@ -103,7 +100,7 @@ namespace OpenRA
|
|||||||
public float RendererRadians() { return (float)(Angle * Math.PI / 512f); }
|
public float RendererRadians() { return (float)(Angle * Math.PI / 512f); }
|
||||||
public float RendererDegrees() { return Angle * 0.3515625f; }
|
public float RendererDegrees() { return Angle * 0.3515625f; }
|
||||||
|
|
||||||
public override string ToString() { return "{0}".F(Angle); }
|
public override string ToString() { return Angle.ToString(); }
|
||||||
|
|
||||||
static int[] CosineTable =
|
static int[] CosineTable =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Eluant;
|
using Eluant;
|
||||||
@@ -16,7 +17,7 @@ using OpenRA.Scripting;
|
|||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
public struct WPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding
|
public struct WPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<WPos>
|
||||||
{
|
{
|
||||||
public readonly int X, Y, Z;
|
public readonly int X, Y, Z;
|
||||||
|
|
||||||
@@ -52,13 +53,10 @@ namespace OpenRA
|
|||||||
|
|
||||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
|
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
public bool Equals(WPos other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is WPos && Equals((WPos)obj); }
|
||||||
var o = obj as WPos?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); }
|
public override string ToString() { return X + "," + Y + "," + Z; }
|
||||||
|
|
||||||
#region Scripting interface
|
#region Scripting interface
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace OpenRA
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 1d world distance - 1024 units = 1 cell.
|
/// 1d world distance - 1024 units = 1 cell.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public struct WRange : IComparable, IComparable<WRange>
|
public struct WRange : IComparable, IComparable<WRange>, IEquatable<WRange>
|
||||||
{
|
{
|
||||||
public readonly int Range;
|
public readonly int Range;
|
||||||
|
|
||||||
@@ -31,6 +31,10 @@ namespace OpenRA
|
|||||||
public static WRange operator /(WRange a, int b) { return new WRange(a.Range / b); }
|
public static WRange operator /(WRange a, int b) { return new WRange(a.Range / b); }
|
||||||
public static WRange operator *(WRange a, int b) { return new WRange(a.Range * b); }
|
public static WRange operator *(WRange a, int b) { return new WRange(a.Range * b); }
|
||||||
public static WRange operator *(int a, WRange b) { return new WRange(a * b.Range); }
|
public static WRange operator *(int a, WRange b) { return new WRange(a * b.Range); }
|
||||||
|
public static bool operator <(WRange a, WRange b) { return a.Range < b.Range; }
|
||||||
|
public static bool operator >(WRange a, WRange b) { return a.Range > b.Range; }
|
||||||
|
public static bool operator <=(WRange a, WRange b) { return a.Range <= b.Range; }
|
||||||
|
public static bool operator >=(WRange a, WRange b) { return a.Range >= b.Range; }
|
||||||
|
|
||||||
public static bool operator ==(WRange me, WRange other) { return (me.Range == other.Range); }
|
public static bool operator ==(WRange me, WRange other) { return (me.Range == other.Range); }
|
||||||
public static bool operator !=(WRange me, WRange other) { return !(me == other); }
|
public static bool operator !=(WRange me, WRange other) { return !(me == other); }
|
||||||
@@ -78,23 +82,17 @@ namespace OpenRA
|
|||||||
|
|
||||||
public override int GetHashCode() { return Range.GetHashCode(); }
|
public override int GetHashCode() { return Range.GetHashCode(); }
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
public bool Equals(WRange other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is WRange && Equals((WRange)obj); }
|
||||||
var o = obj as WRange?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int CompareTo(object obj)
|
public int CompareTo(object obj)
|
||||||
{
|
{
|
||||||
var o = obj as WRange?;
|
if (!(obj is WRange))
|
||||||
if (o == null)
|
|
||||||
return 1;
|
return 1;
|
||||||
|
return Range.CompareTo(((WRange)obj).Range);
|
||||||
return Range.CompareTo(o.Value.Range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CompareTo(WRange other) { return Range.CompareTo(other.Range); }
|
public int CompareTo(WRange other) { return Range.CompareTo(other.Range); }
|
||||||
|
|
||||||
public override string ToString() { return "{0}".F(Range); }
|
public override string ToString() { return Range.ToString(); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,14 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 3d World rotation.
|
/// 3d World rotation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public struct WRot
|
public struct WRot : IEquatable<WRot>
|
||||||
{
|
{
|
||||||
public readonly WAngle Roll, Pitch, Yaw;
|
public readonly WAngle Roll, Pitch, Yaw;
|
||||||
|
|
||||||
@@ -92,12 +94,9 @@ namespace OpenRA
|
|||||||
|
|
||||||
public override int GetHashCode() { return Roll.GetHashCode() ^ Pitch.GetHashCode() ^ Yaw.GetHashCode(); }
|
public override int GetHashCode() { return Roll.GetHashCode() ^ Pitch.GetHashCode() ^ Yaw.GetHashCode(); }
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
public bool Equals(WRot other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is WRot && Equals((WRot)obj); }
|
||||||
var o = obj as WRot?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() { return "{0},{1},{2}".F(Roll, Pitch, Yaw); }
|
public override string ToString() { return Roll + "," + Pitch + "," + Yaw; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ using OpenRA.Support;
|
|||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
public struct WVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding
|
public struct WVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<WVec>
|
||||||
{
|
{
|
||||||
public readonly int X, Y, Z;
|
public readonly int X, Y, Z;
|
||||||
|
|
||||||
@@ -81,13 +81,10 @@ namespace OpenRA
|
|||||||
|
|
||||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
|
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
public bool Equals(WVec other) { return other == this; }
|
||||||
{
|
public override bool Equals(object obj) { return obj is WVec && Equals((WVec)obj); }
|
||||||
var o = obj as WVec?;
|
|
||||||
return o != null && o == this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); }
|
public override string ToString() { return X + "," + Y + "," + Z; }
|
||||||
|
|
||||||
#region Scripting interface
|
#region Scripting interface
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user