{"id":140,"date":"2020-08-02T10:28:56","date_gmt":"2020-08-02T10:28:56","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=140"},"modified":"2020-08-02T10:28:56","modified_gmt":"2020-08-02T10:28:56","slug":"miniarray-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/02\/miniarray-cs\/","title":{"rendered":"MiniArray.cs"},"content":{"rendered":"\n<p>A Small Generic Array 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;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\n[Serializable]\npublic class MiniArray&lt;T> : IEnumerable&lt;T>\n{\n    public T[] Array;\n    public MiniArray() : this(101)\n    {\n    }\n    public MiniArray(int cap)\n    {\n        Count = 0;\n        Array = new T[cap];\n    }\n    public int Count\n    {\n        get;\n        private set;\n    }\n    public T this[int index]\n    {\n        get\n        {\n            if (index > Array.Length)\n                throw new Exception(\"Error: Index out of range.\");\n            return Array[index];\n        }\n        set\n        {\n            EnsureSize();\n            Array[index] = value;\n            Count++;\n        }\n    }\n    IEnumerator&lt;T> IEnumerable&lt;T>.GetEnumerator()\n    {\n        return new Enumerator&lt;T>(this);\n    }\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return new Enumerator&lt;T>(this);\n    }\n    private void EnsureSize()\n    {\n        if (Count >= Array.Length)\n        {\n            var NewLength = Array.Length == 0 ? 1 : Array.Length * 2;\n            var newtArray = new T[NewLength];\n            System.Array.Copy(Array, 0, newtArray, 0, Array.Length);\n            Array = newtArray;\n        }\n    }\n    public void Add(T item)\n    {\n        EnsureSize();\n        Array[Count] = item;\n        Count++;\n    }\n    public T[] ToArray()\n    {\n        var newtArray = new T[Count];\n        System.Array.Copy(Array, 0, newtArray, 0, Count);\n        return newtArray;\n    }\n    public IEnumerable&lt;T> All()\n    {\n        for (var i = 0; i &lt; Count; ++i)\n            yield return Array[i];\n    }\n    public void Clean()\n    {\n        var newtArray = new T[Count];\n        System.Array.Copy(Array, 0, newtArray, 0, Count);\n        Array = newtArray;\n    }\n    public void Clear()\n    {\n        System.Array.Clear(Array, 0, Count);\n        Count = 0;\n    }\n}\n[Serializable]\n[StructLayout(LayoutKind.Sequential)]\npublic struct Enumerator&lt;T> : IEnumerator&lt;T>\n{\n    private readonly MiniArray&lt;T> thing;\n    private          int          index;\n    internal Enumerator(MiniArray&lt;T> thing)\n    {\n        this.thing = thing;\n        index      = 0;\n        Current    = default;\n    }\n    public void Dispose()\n    {\n    }\n    public bool MoveNext()\n    {\n        var tthing = thing;\n        if (index &lt; tthing.Count)\n        {\n            Current = tthing[index];\n            index++;\n            return true;\n        }\n        index   = thing.Count + 1;\n        Current = default;\n        return false;\n    }\n    public T Current\n    {\n        get;\n        private set;\n    }\n    object IEnumerator.Current => Current;\n    void IEnumerator.Reset()\n    {\n        index   = 0;\n        Current = default;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Small Generic Array 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,5,46],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/140"}],"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=140"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}