refactoring, add mask valide check for Debug

This commit is contained in:
Mikhail 2024-11-01 12:48:53 +08:00
parent 45acde2db3
commit 50681715ff

View File

@ -29,8 +29,14 @@ namespace DCFApixels.DragonECS
static EcsStaticMask() static EcsStaticMask()
{ {
Empty = CreateMask(new Key(new EcsTypeCode[0], new EcsTypeCode[0])); EcsStaticMask createMask(Key key)
Broken = CreateMask(new Key(new EcsTypeCode[1] { (EcsTypeCode)1 }, new EcsTypeCode[1] { (EcsTypeCode)1 })); {
EcsStaticMask result = new EcsStaticMask(_idDIspenser.UseFree(), key);
_ids[key] = result;
return result;
}
Empty = createMask(new Key(new EcsTypeCode[0], new EcsTypeCode[0]));
Broken = createMask(new Key(new EcsTypeCode[1] { (EcsTypeCode)1 }, new EcsTypeCode[1] { (EcsTypeCode)1 }));
} }
public readonly int ID; public readonly int ID;
@ -67,8 +73,8 @@ namespace DCFApixels.DragonECS
private EcsStaticMask(int id, Key key) private EcsStaticMask(int id, Key key)
{ {
ID = id; ID = id;
_incs = key.incs; _incs = key.Incs;
_excs = key.excs; _excs = key.Excs;
} }
public static Builder New() { return Builder.New(); } public static Builder New() { return Builder.New(); }
public static Builder Inc<T>() { return Builder.New().Inc<T>(); } public static Builder Inc<T>() { return Builder.New().Inc<T>(); }
@ -85,6 +91,9 @@ namespace DCFApixels.DragonECS
{ {
if (_ids.TryGetValue(key, out result) == false) if (_ids.TryGetValue(key, out result) == false)
{ {
#if DEBUG
CheckConstraints(key.Incs, key.Excs);
#endif
result = new EcsStaticMask(_idDIspenser.UseFree(), key); result = new EcsStaticMask(_idDIspenser.UseFree(), key);
_ids[key] = result; _ids[key] = result;
} }
@ -173,7 +182,6 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region Methods #region Methods
public EcsMask ToMask(EcsWorld world) { return EcsMask.FromStatic(world, this); } public EcsMask ToMask(EcsWorld world) { return EcsMask.FromStatic(world, this); }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -187,25 +195,25 @@ namespace DCFApixels.DragonECS
#region Builder #region Builder
private readonly struct Key : IEquatable<Key> private readonly struct Key : IEquatable<Key>
{ {
public readonly EcsTypeCode[] incs; public readonly EcsTypeCode[] Incs;
public readonly EcsTypeCode[] excs; public readonly EcsTypeCode[] Excs;
public readonly int hash; public readonly int Hash;
#region Constructors #region Constructors
public Key(EcsTypeCode[] inc, EcsTypeCode[] exc) public Key(EcsTypeCode[] inc, EcsTypeCode[] exc)
{ {
this.incs = inc; this.Incs = inc;
this.excs = exc; this.Excs = exc;
unchecked unchecked
{ {
hash = inc.Length + exc.Length; Hash = inc.Length + exc.Length;
for (int i = 0, iMax = inc.Length; i < iMax; i++) for (int i = 0, iMax = inc.Length; i < iMax; i++)
{ {
hash = hash * EcsConsts.MAGIC_PRIME + (int)inc[i]; Hash = Hash * EcsConsts.MAGIC_PRIME + (int)inc[i];
} }
for (int i = 0, iMax = exc.Length; i < iMax; i++) for (int i = 0, iMax = exc.Length; i < iMax; i++)
{ {
hash = hash * EcsConsts.MAGIC_PRIME - (int)exc[i]; Hash = Hash * EcsConsts.MAGIC_PRIME - (int)exc[i];
} }
} }
} }
@ -215,21 +223,21 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Key other) public bool Equals(Key other)
{ {
if (incs.Length != other.incs.Length) { return false; } if (Incs.Length != other.Incs.Length) { return false; }
if (excs.Length != other.excs.Length) { return false; } if (Excs.Length != other.Excs.Length) { return false; }
for (int i = 0; i < incs.Length; i++) for (int i = 0; i < Incs.Length; i++)
{ {
if (incs[i] != other.incs[i]) { return false; } if (Incs[i] != other.Incs[i]) { return false; }
} }
for (int i = 0; i < excs.Length; i++) for (int i = 0; i < Excs.Length; i++)
{ {
if (excs[i] != other.excs[i]) { return false; } if (Excs[i] != other.Excs[i]) { return false; }
} }
return true; return true;
} }
public override bool Equals(object obj) { return Equals((Key)obj); } public override bool Equals(object obj) { return Equals((Key)obj); }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() { return hash; } public override int GetHashCode() { return Hash; }
#endregion #endregion
} }
public readonly struct Builder public readonly struct Builder