| | 1 | | using System; |
| | 2 | | using ReFlex.Core.Common.Exceptions; |
| | 3 | |
|
| | 4 | | namespace ReFlex.Core.Common.Components |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Helps to initialize and referencing arrays. |
| | 8 | | /// </summary> |
| | 9 | | public static class ArrayUtils |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Initializes the array. |
| | 13 | | /// </summary> |
| | 14 | | /// <typeparam name="T"></typeparam> |
| | 15 | | /// <param name="array">The array.</param> |
| | 16 | | /// <param name="size">The size.</param> |
| | 17 | | public static void InitializeArray<T>(out T[] array, int size) |
| | 18 | | where T : new() |
| 12 | 19 | | { |
| 12 | 20 | | array = new T[size]; |
| 220102 | 21 | | for (var i = 0; i < array.Length; i++) |
| 110039 | 22 | | array[i] = new T(); |
| 12 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a multidimensional array. |
| | 27 | | /// </summary> |
| | 28 | | /// <typeparam name="T">The inner value type of the array.</typeparam> |
| | 29 | | /// <param name="array">The array.</param> |
| | 30 | | /// <param name="width">The width.</param> |
| | 31 | | /// <param name="height">The height.</param> |
| | 32 | | public static void InitializeArray<T>(out T[,] array, int width, int height) |
| | 33 | | where T : new() |
| 11 | 34 | | { |
| 11 | 35 | | array = new T[width, height]; |
| 4088 | 36 | | for (var i = 0; i < array.GetLength(0); i++) |
| 954646 | 37 | | for (var j = 0; j < array.GetLength(1); j++) |
| 475290 | 38 | | array[i, j] = new T(); |
| 11 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Initializes a jagged array. |
| | 43 | | /// </summary> |
| | 44 | | /// <typeparam name="T">The inner value type of the array.</typeparam> |
| | 45 | | /// <param name="array">The array.</param> |
| | 46 | | /// <param name="width">The width.</param> |
| | 47 | | /// <param name="height">The height.</param> |
| | 48 | | public static void InitializeArray<T>(out T[][] array, int width, int height) |
| | 49 | | where T : new() |
| 11 | 50 | | { |
| 11 | 51 | | array = new T[width][]; |
| 6808 | 52 | | for (var i = 0; i < array.Length; i++) |
| 3393 | 53 | | { |
| 3393 | 54 | | array[i] = new T[height]; |
| 3095090 | 55 | | for (var j = 0; j < array[i].Length; j++) |
| 1544152 | 56 | | array[i][j] = new T(); |
| 3393 | 57 | | } |
| 11 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Referencing the inner values in the arrays. |
| | 62 | | /// </summary> |
| | 63 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 64 | | /// <param name="source">The source.</param> |
| | 65 | | /// <param name="target">The target.</param> |
| | 66 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 67 | | public static void ReferencingArrays<T>(T[] source, T[] target) |
| 3 | 68 | | { |
| 3 | 69 | | if (source.Length != target.Length) |
| 1 | 70 | | throw new ArraysWithDifferentSizesException(); |
| | 71 | |
|
| 1574 | 72 | | for (var i = 0; i < target.Length; i++) |
| 785 | 73 | | target[i] = source[i]; |
| 2 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Referencing the inner values in the arrays. |
| | 78 | | /// </summary> |
| | 79 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 80 | | /// <param name="source">The source.</param> |
| | 81 | | /// <param name="target">The target.</param> |
| | 82 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 83 | | public static void ReferencingArrays<T>(T[] source, T[,] target) |
| 3 | 84 | | { |
| 3 | 85 | | if (source.Length != target.GetLength(1) * target.GetLength(0)) |
| 1 | 86 | | throw new ArraysWithDifferentSizesException(); |
| | 87 | |
|
| 354 | 88 | | for (var x = 0; x < target.GetLength(0); x++) |
| 72320 | 89 | | for (var y = 0; y < target.GetLength(1); y++) |
| 35985 | 90 | | target[x, y] = source[y * target.GetLength(0) + x]; |
| 2 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Referencing the inner values in the arrays. |
| | 95 | | /// </summary> |
| | 96 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 97 | | /// <param name="source">The one dimensional array.</param> |
| | 98 | | /// <param name="target">The two dimensional array.</param> |
| | 99 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 100 | | public static void ReferencingArrays<T>(T[] source, T[][] target) |
| 3 | 101 | | { |
| 970 | 102 | | for (var i = 0; i < target.Length; i++) |
| 483 | 103 | | { |
| 483 | 104 | | if (source.Length != target.Length * target[i].Length) |
| 1 | 105 | | throw new ArraysWithDifferentSizesException(); |
| | 106 | |
|
| 145088 | 107 | | for (var j = 0; j < target[i].Length; j++) |
| 72062 | 108 | | target[i][j] = source[j * target.Length + i]; |
| 482 | 109 | | } |
| 2 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// RReferencing the inner values in the arrays. |
| | 114 | | /// </summary> |
| | 115 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 116 | | /// <param name="source">The source.</param> |
| | 117 | | /// <param name="target">The target.</param> |
| | 118 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 119 | | [Obsolete("Warning: Referencing 2d arrays is extremely slow")] |
| | 120 | | public static void ReferencingArrays<T>(T[,] source, T[,] target) |
| 4 | 121 | | { |
| 4 | 122 | | if (source.GetLength(0) != target.GetLength(0) || source.GetLength(1) != target.GetLength(1)) |
| 3 | 123 | | throw new ArraysWithDifferentSizesException(); |
| | 124 | |
|
| 436 | 125 | | for (var i = 0; i < target.Length; i++) |
| 217 | 126 | | { |
| | 127 | | // if (source.GetLength(0) * source.GetLength(1) != target.GetLength(0) * target.GetLength(1)) |
| | 128 | | // throw new ArraysWithDifferentSizesException(); |
| | 129 | |
|
| 13888 | 130 | | for (var x = 0; x < target.GetLength(0); x++) |
| 107632 | 131 | | for (var y = 0; y < target.GetLength(1); y++) |
| 47089 | 132 | | target[x, y] = source[x, y]; |
| 217 | 133 | | } |
| 1 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// RReferencing the inner values in the arrays. |
| | 138 | | /// </summary> |
| | 139 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 140 | | /// <param name="source">The source.</param> |
| | 141 | | /// <param name="target">The target.</param> |
| | 142 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 143 | | public static void ReferencingArrays<T>(T[][] source, T[][] target) |
| 4 | 144 | | { |
| 4 | 145 | | if (source.GetLength(0) != target.GetLength(0)) |
| 2 | 146 | | throw new ArraysWithDifferentSizesException(); |
| | 147 | |
|
| 266 | 148 | | for (var i = 0; i < target.Length; i++) |
| 132 | 149 | | { |
| 132 | 150 | | if (source[i].Length != target[i].Length) |
| 1 | 151 | | throw new ArraysWithDifferentSizesException(); |
| | 152 | |
|
| 79910 | 153 | | for (var j = 0; j < target[i].Length; j++) |
| 39824 | 154 | | target[i][j] = source[i][j]; |
| 131 | 155 | | } |
| 1 | 156 | | } |
| | 157 | | } |
| | 158 | | } |