Skip to content

Instantly share code, notes, and snippets.

@SonicZentropy
Created September 22, 2017 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SonicZentropy/e1b92c0a6de1ccbe95b7b7bf0d0746ee to your computer and use it in GitHub Desktop.
Save SonicZentropy/e1b92c0a6de1ccbe95b7b7bf0d0746ee to your computer and use it in GitHub Desktop.
private Dictionary<ComponentTypesEnum, List<ComponentEcs>> _componentPools =
new Dictionary<ComponentTypes, List<ComponentEcs>>(new FastEnumIntEqualityComparer<ComponentTypes>()); // This is the custom comparator passed into the dict constructor
struct FastEnumIntEqualityComparer<TEnum> : IEqualityComparer<TEnum>
where TEnum : struct
{
static class BoxAvoidance
{
static readonly Func<TEnum, int> _wrapper;
public static int ToInt(TEnum enu)
{
return _wrapper(enu);
}
static BoxAvoidance()
{
var p = Expression.Parameter(typeof(TEnum), null);
var c = Expression.ConvertChecked(p, typeof(int));
_wrapper = Expression.Lambda<Func<TEnum, int>>(c, p).Compile();
}
}
public bool Equals(TEnum firstEnum, TEnum secondEnum)
{
return BoxAvoidance.ToInt(firstEnum) ==
BoxAvoidance.ToInt(secondEnum);
}
public int GetHashCode(TEnum firstEnum)
{
return BoxAvoidance.ToInt(firstEnum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment