ComparerEx.cs

Custom Comparer using mapped Hashing

using System;
using System.Collections.Generic;

[Serializable]
public class ComparerEx<T> : IEqualityComparer<T>
{
    private static Type _type;
    private static Mapping64BitToHash32Bit Hash;
    public bool Equals(T x, T y)
    {
        if (x == null || y == null)
            return false;
        if (_type == null)
            _type = x.GetType();
        if (_type.IsArray)
        {
            var xb = x.GetBytesObject(_type);
            var yb = y.GetBytesObject(_type);
            return xb.Compare(yb);
        }
        return x.Equals(y);
    }
    public unsafe int GetHashCode(T obj)
    {
        if (_type == null) _type = obj.GetType();
        if (Hash  == null) Hash  = new Mapping64BitToHash32Bit();
        var objB   = obj.GetBytesObject(_type);
        return Hash.ComputeHash(objB).ToInt();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *