| | 1 | | using ReFlex.Core.Common.Exceptions; |
| | 2 | |
|
| | 3 | | namespace PointCloud.Benchmark.Common |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Helps to initialize and referencing arrays. |
| | 7 | | /// </summary> |
| | 8 | | public static class ArrayUtils |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Initializes the array. |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="T"></typeparam> |
| | 14 | | /// <param name="array">The array.</param> |
| | 15 | | /// <param name="size">The size.</param> |
| | 16 | | public static void InitializeArray<T>(out T[] array, int size) |
| | 17 | | where T : new() |
| 12 | 18 | | { |
| 12 | 19 | | array = new T[size]; |
| 8793432 | 20 | | for (var i = 0; i < array.Length; i++) |
| 4396704 | 21 | | array[i] = new T(); |
| 12 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes the span with default values. |
| | 26 | | /// </summary> |
| | 27 | | /// <typeparam name="T"></typeparam> |
| | 28 | | /// <param name="span">The resulting span.</param> |
| | 29 | | /// <param name="size">The size.</param> |
| | 30 | | public static void InitializeSpan<T>(out Span<T> span, int size) |
| | 31 | | where T : new() |
| 0 | 32 | | { |
| 0 | 33 | | span = new T[size]; |
| 0 | 34 | | span.Fill(new T()); |
| | 35 | | // for (var i = 0; i < array.Length; i++) |
| | 36 | | // array[i] = new T(); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Initializes a multidimensional array. |
| | 41 | | /// </summary> |
| | 42 | | /// <typeparam name="T">The inner value type of the array.</typeparam> |
| | 43 | | /// <param name="array">The array.</param> |
| | 44 | | /// <param name="width">The width.</param> |
| | 45 | | /// <param name="height">The height.</param> |
| | 46 | | public static void InitializeArray<T>(out T[,] array, int width, int height) |
| | 47 | | where T : new() |
| 0 | 48 | | { |
| 0 | 49 | | array = new T[width, height]; |
| 0 | 50 | | for (var i = 0; i < array.GetLength(0); i++) |
| 0 | 51 | | for (var j = 0; j < array.GetLength(1); j++) |
| 0 | 52 | | array[i, j] = new T(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Initializes a jagged array. |
| | 57 | | /// </summary> |
| | 58 | | /// <typeparam name="T">The inner value type of the array.</typeparam> |
| | 59 | | /// <param name="array">The array.</param> |
| | 60 | | /// <param name="width">The width.</param> |
| | 61 | | /// <param name="height">The height.</param> |
| | 62 | | public static void InitializeArray<T>(out T[][] array, int width, int height) |
| | 63 | | where T : new() |
| 12 | 64 | | { |
| 12 | 65 | | array = new T[width][]; |
| 9976 | 66 | | for (var i = 0; i < array.Length; i++) |
| 4976 | 67 | | { |
| 4976 | 68 | | array[i] = new T[height]; |
| 8803360 | 69 | | for (var j = 0; j < array[i].Length; j++) |
| 4396704 | 70 | | array[i][j] = new T(); |
| 4976 | 71 | | } |
| 12 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Initializes a jagged array. |
| | 76 | | /// </summary> |
| | 77 | | /// <typeparam name="T">The inner value type of the array.</typeparam> |
| | 78 | | /// <param name="array">The array.</param> |
| | 79 | | /// <param name="width">The width.</param> |
| | 80 | | /// <param name="height">The height.</param> |
| | 81 | | public static void InitializeSpan<T>(out Span<T[]> array, int width, int height) |
| | 82 | | where T : new() |
| 0 | 83 | | { |
| 0 | 84 | | array = new Span<T[]>(new T[width][]); |
| 0 | 85 | | for (var i = 0; i < array.Length; i++) |
| 0 | 86 | | { |
| 0 | 87 | | var innerSpan = new T[height].AsSpan(); |
| 0 | 88 | | innerSpan.Fill(new T()); |
| 0 | 89 | | array[i] = innerSpan.ToArray(); |
| 0 | 90 | | } |
| 0 | 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 source.</param> |
| | 98 | | /// <param name="target">The target.</param> |
| | 99 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 100 | | public static void ReferencingArrays<T>(T[] source, T[] target) |
| 0 | 101 | | { |
| 0 | 102 | | if (source.Length != target.Length) |
| 0 | 103 | | throw new ArraysWithDifferentSizesException(); |
| | 104 | |
|
| 0 | 105 | | for (var i = 0; i < target.Length; i++) |
| 0 | 106 | | target[i] = source[i]; |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Referencing the inner values in the arrays. |
| | 111 | | /// </summary> |
| | 112 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 113 | | /// <param name="source">The source.</param> |
| | 114 | | /// <param name="target">The target.</param> |
| | 115 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 116 | | public static void ReferencingArrays<T>(T[] source, T[,] target) |
| 0 | 117 | | { |
| 0 | 118 | | if (source.Length != target.GetLength(1) * target.GetLength(0)) |
| 0 | 119 | | throw new ArraysWithDifferentSizesException(); |
| | 120 | |
|
| 0 | 121 | | for (var x = 0; x < target.GetLength(0); x++) |
| 0 | 122 | | for (var y = 0; y < target.GetLength(1); y++) |
| 0 | 123 | | target[x, y] = source[y * target.GetLength(0) + x]; |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Referencing the inner values in the arrays. |
| | 128 | | /// </summary> |
| | 129 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 130 | | /// <param name="source">The one dimensional array.</param> |
| | 131 | | /// <param name="target">The two dimensional array.</param> |
| | 132 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 133 | | public static void ReferencingArrays<T>(T[] source, T[][] target) |
| 12 | 134 | | { |
| 9976 | 135 | | for (var i = 0; i < target.Length; i++) |
| 4976 | 136 | | { |
| 4976 | 137 | | if (source.Length != target.Length * target[i].Length) |
| 0 | 138 | | throw new ArraysWithDifferentSizesException(); |
| | 139 | |
|
| 8803360 | 140 | | for (var j = 0; j < target[i].Length; j++) |
| 4396704 | 141 | | target[i][j] = source[j * target.Length + i]; |
| 4976 | 142 | | } |
| 12 | 143 | | } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Referencing the inner values in the arrays. |
| | 147 | | /// </summary> |
| | 148 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 149 | | /// <param name="source">The one dimensional array.</param> |
| | 150 | | /// <param name="target">The two dimensional array.</param> |
| | 151 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 152 | | public static void ReferencingArrays<T>(ReadOnlySpan<T> source, Span<T[]> target) |
| 0 | 153 | | { |
| 0 | 154 | | for (var i = 0; i < target.Length; i++) |
| 0 | 155 | | { |
| 0 | 156 | | if (source.Length != target.Length * target[i].Length) |
| 0 | 157 | | throw new ArraysWithDifferentSizesException(); |
| | 158 | |
|
| 0 | 159 | | for (var j = 0; j < target[i].Length; j++) |
| 0 | 160 | | target[i][j] = source[j * target.Length + i]; |
| 0 | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// RReferencing the inner values in the arrays. |
| | 166 | | /// </summary> |
| | 167 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 168 | | /// <param name="source">The source.</param> |
| | 169 | | /// <param name="target">The target.</param> |
| | 170 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 171 | | public static void ReferencingArrays<T>(T[,] source, T[,] target) |
| 0 | 172 | | { |
| 0 | 173 | | for (var i = 0; i < target.Length; i++) |
| 0 | 174 | | { |
| 0 | 175 | | if (source.GetLength(0) * source.GetLength(1) != target.GetLength(0) * target.GetLength(1)) |
| 0 | 176 | | throw new ArraysWithDifferentSizesException(); |
| | 177 | |
|
| 0 | 178 | | for (var x = 0; x < target.GetLength(0); x++) |
| 0 | 179 | | for (var y = 0; y < target.GetLength(1); y++) |
| 0 | 180 | | target[x, y] = source[x, y]; |
| 0 | 181 | | } |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// RReferencing the inner values in the arrays. |
| | 186 | | /// </summary> |
| | 187 | | /// <typeparam name="T">The inner value type of both arrays.</typeparam> |
| | 188 | | /// <param name="source">The source.</param> |
| | 189 | | /// <param name="target">The target.</param> |
| | 190 | | /// <exception cref="ArraysWithDifferentSizesException"></exception> |
| | 191 | | public static void ReferencingArrays<T>(T[][] source, T[][] target) |
| 0 | 192 | | { |
| 0 | 193 | | for (var i = 0; i < target.Length; i++) |
| 0 | 194 | | { |
| 0 | 195 | | if (source.Length * source[i].Length != target.Length * target[i].Length) |
| 0 | 196 | | throw new ArraysWithDifferentSizesException(); |
| | 197 | |
|
| 0 | 198 | | for (var j = 0; j < target[i].Length; j++) |
| 0 | 199 | | target[i][j] = source[i][j]; |
| 0 | 200 | | } |
| 0 | 201 | | } |
| | 202 | | } |
| | 203 | | } |