{"id":82,"date":"2020-06-16T07:11:04","date_gmt":"2020-06-16T07:11:04","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=82"},"modified":"2020-06-16T07:11:04","modified_gmt":"2020-06-16T07:11:04","slug":"arrayhelper-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/16\/arrayhelper-cs\/","title":{"rendered":"ArrayHelper.cs"},"content":{"rendered":"\n<p>Generic Array Helper 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;\npublic static class ArrayHelper\n{\n    \/\/\/ &lt;summary>\n    \/\/\/     Clear T type single dimensional array to either the default value or a set value.  (ArrayHelper.cs)\n    \/\/\/ &lt;\/summary>\n    public static void Clear&lt;T>(this T[] array, T value = default)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        for (var i = 0; i &lt; array.Length; i++)\n            array[i] = value;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Clear T type multi dimensional array to either the default value or a set value. (ArrayHelper.cs)\n    \/\/\/ &lt;\/summary>\n    public static void Clear&lt;T>(this T[][] array, T value = default)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        var xlen = array.GetLength(0);\n        var ylen = array.GetLength(1);\n        for (var x = 0; x &lt; xlen; x++)\n        for (var y = 0; y &lt; ylen; y++)\n            array[x][y] = value;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Copies a total or portion of a source array of type T starting at offset\n    \/\/\/     and copying count bytes.  (ArrayHelper.cs)\n    \/\/\/ &lt;\/summary>\n    public static T[] SubArray&lt;T>(this T[] array, int offset, int count)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        if (count - offset > array.Length)\n            throw new Exception($\"Count {count} + Offset {offset} must be less than the length of the array {array.Length}\");\n        var result = new T[count];\n        Array.Copy(array, offset, result, 0, count);\n        return result;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Copies the source array of type T into the target array.\n    \/\/\/     (ArrayHelper.cs)\n    \/\/\/ &lt;\/summary>\n    public static void Copy&lt;T>(this T[] array, T[] array1)\n    {\n        if (array == null || array1 == null)\n            throw new Exception(\"Neither the source or target array can be null.\");\n        if (array == null || array1 == null)\n            throw new Exception(\"Neither the source or target array can be null.\");\n        var len  = array.Length;\n        var len1 = array1.Length;\n        if (len > len1)\n            throw new Exception(\"The target array must be the same length.\");\n        Array.Copy(array, 0, array1, 0, len);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Inverts the order of the array.\n    \/\/\/     (ArrayHelper.cs)\n    \/\/\/ &lt;\/summary>\n    public static T[] Invert&lt;T>(this T[] array)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        var len = array.Length;\n        var t   = new T[len];\n        for (int i = len - 1, j = 0; i >= 0; t[j++] = array[i--]) ;\n        return t;\n    }\n    public static T[] ReSize&lt;T>(this T[] array, int NewSize)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        if (array.Length >= NewSize)\n            return array;\n        var temp = new T[NewSize];\n        Array.Copy(array, 0, temp, 0, array.Length);\n        return temp;\n    }\n    public static T[] ReSize&lt;T>(this T[] array)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        var cSize = (long) array.Length;\n        var mul   = 1.2;\n        if (cSize &lt; int.MaxValue >> 1)\n            mul = 8;\n        var nSize = (long) (cSize * mul);\n        if (nSize > int.MaxValue)\n            nSize = int.MaxValue;\n        var temp = new T[nSize];\n        Array.Copy(array, 0, temp, 0, array.Length);\n        return temp;\n    }\n    public static T[][] ReSize&lt;T>(this T[][] array, int NewX, int NewY)\n    {\n        if (array == null)\n            throw new Exception(\"The array cannot be null.\");\n        var xlen = array.GetLength(0);\n        var ylen = array.GetLength(1);\n        if (xlen >= NewX &amp;&amp; ylen >= NewY)\n            return array;\n        var temp = new T[NewX][];\n        for (var i = 0; i &lt; NewX; ++i)\n            temp[i] = new T[NewY];\n        for (var i = 0; i &lt; NewX; ++i)\n            Array.Copy(array[i], temp[i], ylen);\n        return temp;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Generic Array Helper 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":[],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/82"}],"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=82"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}