using System; using MoonTools.ECS.Collections; namespace MoonTools.ECS; internal class ArchetypeSignature : IEquatable { public static ArchetypeSignature Empty = new ArchetypeSignature(0); IndexableSet Ids; public int Count => Ids.Count; public TypeId this[int i] => Ids[i]; public ArchetypeSignature() { Ids = new IndexableSet(); } public ArchetypeSignature(int capacity) { Ids = new IndexableSet(capacity); } // Maintains sorted order public void Insert(TypeId componentId) { Ids.Add(componentId); } public void Remove(TypeId componentId) { Ids.Remove(componentId); } public bool Contains(TypeId componentId) { return Ids.Contains(componentId); } public void CopyTo(ArchetypeSignature other) { foreach (var id in Ids.AsSpan()) { other.Ids.Add(id); } } public override bool Equals(object? obj) { return obj is ArchetypeSignature signature && Equals(signature); } public bool Equals(ArchetypeSignature? other) { if (other == null) { return false; } if (Ids.Count != other.Ids.Count) { return false; } for (int i = 0; i < Ids.Count; i += 1) { if (Ids[i] != other.Ids[i]) { return false; } } return true; } public override int GetHashCode() { var hashcode = 1; foreach (var id in Ids) { hashcode = HashCode.Combine(hashcode, id); } return hashcode; } }