MoonTools.ECS/src/FilterSignature.cs

52 lines
1.1 KiB
C#
Raw Normal View History

2022-04-08 05:52:03 +00:00
using System;
using System.Collections.Generic;
2022-03-06 06:12:27 +00:00
2022-04-08 05:52:03 +00:00
namespace MoonTools.ECS
2022-03-06 06:12:27 +00:00
{
2022-04-08 05:52:03 +00:00
public struct FilterSignature
2022-03-06 06:12:27 +00:00
{
2022-04-08 05:52:03 +00:00
private const int HASH_FACTOR = 97;
2022-03-06 06:12:27 +00:00
public readonly HashSet<Type> Included;
public readonly HashSet<Type> Excluded;
2022-03-06 06:12:27 +00:00
2022-04-08 05:52:03 +00:00
public FilterSignature(HashSet<Type> included, HashSet<Type> excluded)
{
Included = included;
Excluded = excluded;
}
2022-03-06 06:12:27 +00:00
2022-04-08 05:52:03 +00:00
public override bool Equals(object? obj)
{
return obj is FilterSignature signature && Equals(signature);
}
2022-03-06 06:12:27 +00:00
2022-04-08 05:52:03 +00:00
public bool Equals(FilterSignature other)
2022-03-06 06:12:27 +00:00
{
2022-04-08 05:52:03 +00:00
return Included.SetEquals(other.Included) && Excluded.SetEquals(other.Excluded);
2022-03-06 06:12:27 +00:00
}
2022-04-08 05:52:03 +00:00
private int GuidToInt(Guid guid)
2022-03-06 06:12:27 +00:00
{
2022-04-08 05:52:03 +00:00
return BitConverter.ToInt32(guid.ToByteArray());
2022-03-06 06:12:27 +00:00
}
2022-04-08 05:52:03 +00:00
public override int GetHashCode()
{
int result = 1;
foreach (var type in Included)
{
result *= HASH_FACTOR + GuidToInt(type.GUID);
}
// FIXME: Is there a way to avoid collisions when this is the same set as included?
foreach (var type in Excluded)
{
result *= HASH_FACTOR + GuidToInt(type.GUID);
}
return result;
}
2022-03-06 06:12:27 +00:00
}
}