{"id":379,"date":"2021-02-27T08:55:21","date_gmt":"2021-02-27T08:55:21","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=379"},"modified":"2021-02-27T08:55:21","modified_gmt":"2021-02-27T08:55:21","slug":"entropyex-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/02\/27\/entropyex-cs\/","title":{"rendered":"EntropyEx.cs"},"content":{"rendered":"\n<p>Generic Entropy Class<\/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.Collections.Generic;\nusing System.Text;\npublic class EntropyEx&lt;T>\n{\n    private const    double             NaturalLogOfTwo = 0.69314718055994530941723212145818;\n    private readonly Dictionary&lt;T, int> _histograpm     = new Dictionary&lt;T, int>();\n    private          bool               _histograpmStatus;\n    private readonly int                _size;\n    public EntropyEx()\n    {\n        _histograpm.Clear();\n        _histograpmStatus = false;\n        _size             = GetSize();\n    }\n    public double Entropy(string str)\n    {\n        var s = str.GetBytes(Encoding.Default);\n        var histograpmL = new Dictionary&lt;byte, int>();\n        foreach (var c in s)\n            if (!histograpmL.ContainsKey(c))\n                histograpmL.Add(c, 1);\n            else\n                histograpmL[c] += 1;\n        var e = 0.0;\n        foreach (var v in histograpmL.Values)\n        {\n            if (v &lt;= 0)\n                continue;\n            var r = v             \/ (double)s.Length;\n            e -= r * (Math.Log(r) \/ NaturalLogOfTwo);\n        }\n        return e \/ 8 * 100.0;\n    }\n    public double Entropy(T[] s)\n    {\n        if (_histograpmStatus)\n        {\n            _histograpm.Clear();\n            _histograpmStatus = false;\n        }\n        foreach (var c in s)\n            if (!_histograpm.ContainsKey(c))\n                _histograpm.Add(c, 1);\n            else\n                _histograpm[c] += 1;\n        _histograpmStatus = true;\n        var e = 0.0;\n        foreach (var v in _histograpm.Values)\n        {\n            if (v &lt;= 0)\n                continue;\n            var r = v             \/ (double) s.Length;\n            e -= r * (Math.Log(r) \/ NaturalLogOfTwo);\n        }\n        return e \/ _size * 100.0;\n    }\n    private byte[] ConvertTypeByteArray(T[] ia)\n    {\n        switch (Type.GetTypeCode(typeof(T)))\n        {\n            case TypeCode.Boolean:\n                break;\n            case TypeCode.Char:\n                break;\n            case TypeCode.SByte:\n                break;\n            case TypeCode.Byte:\n                break;\n            case TypeCode.Int16:\n                break;\n            case TypeCode.UInt16:\n                break;\n            case TypeCode.Int32:\n                break;\n            case TypeCode.UInt32:\n                break;\n            case TypeCode.Single:\n                break;\n            case TypeCode.String:\n                break;\n            case TypeCode.Decimal:\n                break;\n            case TypeCode.Int64:\n                break;\n            case TypeCode.UInt64:\n                break;\n            case TypeCode.Double:\n                break;\n            case TypeCode.DateTime:\n                break;\n            default:\n                throw new ArgumentException(\"Type is not a valid primitive.\");\n        }\n        return ia.GetBytes();\n    }\n    private int GetSize()\n    {\n        var size = 0;\n        switch (Type.GetTypeCode(typeof(T)))\n        {\n            case TypeCode.Boolean:\n                size = 1;\n                break;\n            case TypeCode.Char:\n                size = 16;\n                break;\n            case TypeCode.SByte:\n                size = 8;\n                break;\n            case TypeCode.Byte:\n                size = 8;\n                break;\n            case TypeCode.Int16:\n                size = 16;\n                break;\n            case TypeCode.UInt16:\n                size = 16;\n                break;\n            case TypeCode.Int32:\n                size = 32;\n                break;\n            case TypeCode.UInt32:\n                size = 32;\n                break;\n            case TypeCode.Single:\n                size = 32;\n                break;\n            case TypeCode.String:\n                size = 32;\n                break;\n            case TypeCode.Decimal:\n                size = 192;\n                break;\n            case TypeCode.Int64:\n                size = 64;\n                break;\n            case TypeCode.UInt64:\n                size = 64;\n                break;\n            case TypeCode.Double:\n                size = 64;\n                break;\n            case TypeCode.DateTime:\n                size = 64;\n                break;\n            default:\n                throw new ArgumentException(\"Type is not a valid primitive within this context.\");\n        }\n        return size;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Generic Entropy Class<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[44,46,27],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/379"}],"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=379"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/379\/revisions\/380"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}