MoonTools.ECS/src/FilterSignature.cs

73 lines
1.4 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
{
public struct FilterSignature : IEquatable<FilterSignature>
2022-03-06 06:12:27 +00:00
{
public readonly HashSet<int> Included;
public readonly HashSet<int> Excluded;
2022-03-06 06:12:27 +00:00
public FilterSignature(HashSet<int> included, HashSet<int> excluded)
2022-04-08 05:52:03 +00:00
{
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
{
2023-04-05 19:08:24 +00:00
// workaround for HashSet<T>.SetEquals generating garbage
// maybe fixed in .NET 8?
foreach (var included in Included)
{
if (!other.Included.Contains(included))
{
return false;
}
}
foreach (var excluded in Excluded)
{
if (!other.Excluded.Contains(excluded))
{
return false;
}
}
return true;
2022-03-06 06:12:27 +00:00
}
2022-04-08 05:52:03 +00:00
public override int GetHashCode()
{
var hashcode = 1;
2022-04-08 05:52:03 +00:00
foreach (var type in Included)
{
hashcode = HashCode.Combine(hashcode, type);
2022-04-08 05:52:03 +00:00
}
foreach (var type in Excluded)
{
hashcode = HashCode.Combine(hashcode, type);
2022-04-08 05:52:03 +00:00
}
return hashcode;
}
public static bool operator ==(FilterSignature left, FilterSignature right)
{
return left.Equals(right);
}
public static bool operator !=(FilterSignature left, FilterSignature right)
{
return !(left == right);
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
}
}