{"id":120,"date":"2020-07-30T08:32:53","date_gmt":"2020-07-30T08:32:53","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=120"},"modified":"2020-07-30T08:32:53","modified_gmt":"2020-07-30T08:32:53","slug":"sizehelper-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/30\/sizehelper-cs\/","title":{"rendered":"SizeHelper.cs"},"content":{"rendered":"\n<p>Non-Linear Array Size Adjustment 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 Microsoft.VisualBasic.Devices;\npublic class SizeHelper&lt;T>\n{\n    private static readonly FixedIntXPrimality bp = new FixedIntXPrimality(64);\n    private readonly        int                AllocatedSizeLimit;\n    public                  int[]              Curve;\n    private                 int                Resizes;\n    public SizeHelper()\n    {\n        var measure   = new MeasureSize&lt;T>();\n        var sizeOfOne = measure.GetByteSize();\n        var am        = new ComputerInfo().AvailablePhysicalMemory;\n        AllocatedSizeLimit = (int) ((long) am \/ sizeOfOne);\n    }\n    public int GetNewSize(int currentSize)\n    {\n        Resizes++;\n        if (Curve == null)\n            BuildCurve(currentSize);\n        foreach (var v in Curve)\n            if (v > currentSize)\n                return v;\n        var nv = GetNextValue(currentSize);\n        return nv != -1 ? nv : int.MaxValue;\n    }\n    private int GetNextValue(int currentValue)\n    {\n        for (var value = currentValue | 1; value &lt; AllocatedSizeLimit; value += 16384)\n        {\n            if (value &lt; 0)\n                break;\n            if (bp.IsPrime(value))\n                return value + 1;\n        }\n        return -1;\n    }\n    private void BuildCurve(int Size)\n    {\n        int Sizer(int oldSize)\n        {\n            try\n            {\n                oldSize = (int) (uint) oldSize;\n                var log     = Math.Log(oldSize);\n                var inv     = 1.0 \/ log * 4;\n                var newSize = oldSize   * (1.0 + inv);\n                return (int) (uint) newSize;\n            }\n            catch\n            {\n                return AllocatedSizeLimit;\n            }\n        }\n        var curlst = new List&lt;int>();\n        var value  = Size | 1;\n        do\n        {\n            value = Sizer(value);\n            if (value &lt; 0)\n                break;\n            if (value &lt; AllocatedSizeLimit)\n                curlst.Add(value);\n        } while (value &lt; AllocatedSizeLimit);\n        Curve = curlst.ToArray();\n        var dl   = new List&lt;int>();\n        var last = 0;\n        for (var i = 0; i &lt; Curve.Length; ++i)\n        {\n            if (i > 0)\n                last = Curve[i - 1];\n            var v = Curve[i];\n            dl.Add(v - last);\n        }\n        var str = \"\";\n        foreach (var v in dl)\n            str += $\"{v},\";\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Non-Linear Array Size Adjustment Class<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[35,55,53,54],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/120"}],"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=120"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions\/121"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}