MoonTools.ECS/src/ArchetypeSignature.cs

90 lines
1.4 KiB
C#

using System;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS;
internal class ArchetypeSignature : IEquatable<ArchetypeSignature>
{
public static ArchetypeSignature Empty = new ArchetypeSignature(0);
IndexableSet<TypeId> Ids;
public int Count => Ids.Count;
public TypeId this[int i] => Ids[i];
public ArchetypeSignature()
{
Ids = new IndexableSet<TypeId>();
}
public ArchetypeSignature(int capacity)
{
Ids = new IndexableSet<TypeId>(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;
}
}