{"id":284,"date":"2020-12-15T04:57:15","date_gmt":"2020-12-15T04:57:15","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=284"},"modified":"2020-12-18T13:18:31","modified_gmt":"2020-12-18T13:18:31","slug":"bigcomparer-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/12\/15\/bigcomparer-cs\/","title":{"rendered":"BigComparer.cs"},"content":{"rendered":"\n<p>Big Comparer Class<\/p>\n\n\n\n<p>Includes: IBigEqualityComparer.cs<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.IO;\nusing System.Runtime.Serialization.Formatters.Binary;\n[Serializable]\npublic class BigComparer&lt;T> : IBigEqualityComparer&lt;T>\n{\n    private static   Type            _type = typeof(T);\n    private readonly BinaryFormatter _bf   = new BinaryFormatter();\n    private readonly bool            _isPrimitive;\n    private readonly MemoryStream    _ms = new MemoryStream();\n    private          bool            _disposed;\n    public BigComparer()\n    {\n        _isPrimitive = IsPrimitive();\n    }\n    public bool Equals(T x, T y)\n    {\n        if (x == null || y == null)\n            return false;\n        if (_isPrimitive)\n            return x.Equals(y);\n        if (_type == null)\n            _type = x.GetType();\n        if (_type.IsArray)\n        {\n            if (!_isPrimitive)\n            {\n                var xb = GetBytesObjectSerial(x);\n                var yb = GetBytesObjectSerial(y);\n                return xb.Compare(yb);\n            }\n            else\n            {\n                var xb = GetBytesObject(x);\n                var yb = GetBytesObject(y);\n                return xb.Compare(yb);\n            }\n        }\n        return x.Equals(y);\n    }\n    public long GetHashCode(T obj)\n    {\n        if (_isPrimitive)\n            return obj.GetHashCode();\n        if (_type == null)\n            _type = obj.GetType();\n        var    result = 0xCBF29CE484222325;\n        byte[] buf;\n        buf = !_isPrimitive ? GetBytesObjectSerial(obj) : GetBytesObject(obj);\n        foreach (var b in buf)\n            result = (result ^ b) * 0x100000001B3;\n        return (long) result;\n    }\n    private void Dispose(bool disposing)\n    {\n        if (!_disposed)\n            if (disposing)\n                _ms?.Close();\n        _disposed = true;\n    }\n    public byte[] GetBytesObjectSerial(object value)\n    {\n        if (_type == null)\n            _type = value.GetType();\n        if (!_type.IsSerializable)\n            throw new Exception(\"Error: Object is not Serializable.\");\n        _ms.SetLength(0);\n        _bf.Serialize(_ms, value);\n        return _ms.ToArray().SubArray(0, (int) _ms.Length);\n    }\n    public bool IsPrimitive()\n    {\n        if (_type == null)\n            _type = typeof(T);\n        switch (Type.GetTypeCode(_type))\n        {\n            case TypeCode.Boolean:\n            case TypeCode.Char:\n            case TypeCode.SByte:\n            case TypeCode.Byte:\n            case TypeCode.Int16:\n            case TypeCode.UInt16:\n            case TypeCode.Int32:\n            case TypeCode.UInt32:\n            case TypeCode.Single:\n            case TypeCode.String:\n            case TypeCode.Decimal:\n            case TypeCode.DateTime:\n            case TypeCode.Int64:\n            case TypeCode.UInt64:\n            case TypeCode.Double:\n                return true;\n            default:\n                return false;\n        }\n    }\n    public byte[] GetBytesObject(object obj)\n    {\n        if (_type == null)\n            _type = obj.GetType();\n        switch (_type.Name.Trim('[', ']'))\n        {\n            case \"Byte\":\n                return !_type.IsArray ? new[] {(byte) obj} : (byte[]) obj;\n            case \"Boolean\":\n                return !_type.IsArray ? ((bool) obj).GetBytes() : ((bool[]) obj).GetBytes();\n            case \"SByte\":\n                return !_type.IsArray ? ((sbyte) obj).GetBytes() : ((sbyte[]) obj).GetBytes();\n            case \"Char\":\n                return !_type.IsArray ? ((char) obj).GetBytes() : ((char[]) obj).GetBytes();\n            case \"Int16\":\n                return !_type.IsArray ? ((short) obj).GetBytes() : ((short[]) obj).GetBytes();\n            case \"UInt16\":\n                return !_type.IsArray ? ((ushort) obj).GetBytes() : ((ushort[]) obj).GetBytes();\n            case \"Int32\":\n                return !_type.IsArray ? ((int) obj).GetBytes() : ((int[]) obj).GetBytes();\n            case \"UInt32\":\n                return !_type.IsArray ? ((uint) obj).GetBytes() : ((uint[]) obj).GetBytes();\n            case \"Int64\":\n                return !_type.IsArray ? ((long) obj).GetBytes() : ((long[]) obj).GetBytes();\n            case \"UInt64\":\n                return !_type.IsArray ? ((ulong) obj).GetBytes() : ((ulong[]) obj).GetBytes();\n            case \"Single\":\n                return !_type.IsArray ? ((float) obj).GetBytes() : ((float[]) obj).GetBytes();\n            case \"Double\":\n                return !_type.IsArray ? ((double) obj).GetBytes() : ((double[]) obj).GetBytes();\n            case \"String\":\n                return !_type.IsArray ? ((string) obj).GetBytes() : ((string[]) obj).GetBytes();\n            case \"Decimal\":\n                return !_type.IsArray ? ((decimal) obj).GetBytes() : ((decimal[]) obj).GetBytes();\n            case \"DateTime\":\n                return !_type.IsArray ? ((DateTime) obj).GetBytes() : ((DateTime[]) obj).GetBytes();\n        }\n        return GetBytesObjectSerial(obj);\n    }\n}\n\npublic interface IBigEqualityComparer&lt;in T>\n{\n    bool Equals(T      x, T y);\n    long GetHashCode(T obj);\n}\n\npublic interface IBigEqualityComparer32&lt;in T>\n{\n    bool Equals(T      x, T y);\n    int GetHashCode(T obj);\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Big Comparer Class Includes: IBigEqualityComparer.cs<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/284"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":4,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/284\/revisions\/301"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}