DragonECS-Graphs/src/Internal/RelationInfo.cs

73 lines
2.5 KiB
C#
Raw Normal View History

2023-12-24 18:18:49 +08:00
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
2024-03-16 13:54:50 +08:00
namespace DCFApixels.DragonECS.Graphs.Internal
2023-12-24 18:18:49 +08:00
{
[Serializable]
2024-11-19 17:03:15 +08:00
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 8)]
2024-03-16 14:13:43 +08:00
internal readonly struct RelationInfo : IEquatable<RelationInfo>
2023-12-24 18:18:49 +08:00
{
2024-03-16 14:13:43 +08:00
public static readonly RelationInfo Empty = new RelationInfo();
2023-12-24 18:18:49 +08:00
/// <summary>Start vertex entity ID.</summary>
public readonly int start;
/// <summary>End vertex entity ID.</summary>
public readonly int end;
#region Properties
2024-03-16 14:13:43 +08:00
public bool IsNull
2023-12-24 18:18:49 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-16 14:13:43 +08:00
get { return start == 0 && end == 0; }
}
public bool IsLoop
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return start == end; }
2023-12-24 18:18:49 +08:00
}
#endregion
2024-03-16 14:13:43 +08:00
#region Constructor/Deconstruct
2023-12-24 18:18:49 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-16 14:13:43 +08:00
internal RelationInfo(int startEntity, int endEntity)
2023-12-24 18:18:49 +08:00
{
start = startEntity;
end = endEntity;
}
2024-01-28 02:19:49 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int start, out int end)
{
start = this.start;
end = this.end;
}
2024-03-16 14:13:43 +08:00
#endregion
2023-12-24 18:18:49 +08:00
#region operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-16 14:13:43 +08:00
public static bool operator ==(RelationInfo a, RelationInfo b) { return a.start == b.start && a.end == b.end; }
2023-12-24 18:18:49 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-16 14:13:43 +08:00
public static bool operator !=(RelationInfo a, RelationInfo b) { return a.start != b.start || a.end != b.end; }
2023-12-24 18:18:49 +08:00
#endregion
#region Other
2024-03-16 14:13:43 +08:00
public override bool Equals(object obj) { return obj is RelationInfo targets && targets == this; }
2023-12-24 18:18:49 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-16 14:13:43 +08:00
public bool Equals(RelationInfo other) { return this == other; }
2023-12-24 18:18:49 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-11-17 21:17:06 +08:00
public override int GetHashCode()
{
unchecked
{
uint endHash = (uint)end;
endHash ^= endHash << 13;
endHash ^= endHash >> 17;
endHash ^= endHash << 5;
return start ^ (int)endHash;
}
}
2024-03-16 14:13:43 +08:00
public override string ToString() { return $"arc({start} -> {end})"; }
2023-12-24 18:18:49 +08:00
#endregion
}
2024-03-16 14:13:43 +08:00
}