| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | |
|
| | 8 | | namespace GDX.Collections.Generic |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// A 2-dimensional array backed by a flat array. |
| | 12 | | /// </summary> |
| | 13 | | /// <remarks>Mimics multi-dimensional array format.</remarks> |
| | 14 | | /// <typeparam name="T">Type of objects.</typeparam> |
| | 15 | | [VisualScriptingCompatible(1)] |
| | 16 | | public struct Array2D<T> : IDisposable |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The backing flat array. |
| | 20 | | /// </summary> |
| | 21 | | public T[] Array; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// The length of each pseudo-array in the dataset. |
| | 25 | | /// </summary> |
| | 26 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | 27 | | public int ColumnCount; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// The number of pseudo-arrays created to support the dimensionality. |
| | 31 | | /// </summary> |
| | 32 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | 33 | | public int RowCount; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Create a <see cref="Array2D{T}" />. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="rowCount">The number of rows (X).</param> |
| | 39 | | /// <param name="columnCount">The number of columns (Y).</param> |
| | 40 | | public Array2D(int rowCount, int columnCount) |
| 13 | 41 | | { |
| 13 | 42 | | ColumnCount = columnCount; |
| 13 | 43 | | RowCount = rowCount; |
| 13 | 44 | | Array = new T[rowCount * columnCount]; |
| 13 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Create a <see cref="Array2D{T}" /> providing an existing <paramref name="arrayToUse" />. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="rowCount">The number of rows (X).</param> |
| | 51 | | /// <param name="columnCount">The number of columns (Y).</param> |
| | 52 | | /// <param name="arrayToUse">An existing array to use in the <see cref="Array2D{T}" />.</param> |
| | 53 | | public Array2D(int rowCount, int columnCount, T[] arrayToUse) |
| 0 | 54 | | { |
| 0 | 55 | | ColumnCount = columnCount; |
| 0 | 56 | | RowCount = rowCount; |
| 0 | 57 | | Array = arrayToUse; |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Get a typed object at a specific 2-dimensional index in <see cref="Array" />. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="x">The row/line number (vertical axis).</param> |
| | 64 | | /// <param name="y">The column number (horizontal axis).</param> |
| | 65 | | public T this[int x, int y] |
| | 66 | | { |
| | 67 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 88 | 68 | | get => Array[x * ColumnCount + y]; |
| | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 68 | 70 | | set => Array[x * ColumnCount + y] = value; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Properly dispose of the <see cref="Array2D{T}" />. |
| | 75 | | /// </summary> |
| | 76 | | public void Dispose() |
| 0 | 77 | | { |
| 0 | 78 | | Array = default; |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Add additional rows to the dataset. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="numberOfNewRows">The number of rows/arrays to add.</param> |
| | 85 | | public void AddRows(int numberOfNewRows) |
| 1 | 86 | | { |
| 1 | 87 | | int newArrayCount = RowCount + numberOfNewRows; |
| 1 | 88 | | System.Array.Resize(ref Array, newArrayCount * ColumnCount); |
| 1 | 89 | | RowCount = newArrayCount; |
| 1 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Add additional columns to the dataset. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="numberOfNewColumns">The number of columns add.</param> |
| | 96 | | public void AddColumns(int numberOfNewColumns) |
| 1 | 97 | | { |
| 1 | 98 | | int currentLengthOfArrays = ColumnCount; |
| 1 | 99 | | int newLengthOfArrays = currentLengthOfArrays + numberOfNewColumns; |
| 1 | 100 | | T[] newArray = new T[RowCount * newLengthOfArrays]; |
| | 101 | |
|
| 6 | 102 | | for (int i = 0; i < RowCount; i++) |
| 16 | 103 | | for (int j = 0; j < currentLengthOfArrays; j++) |
| 6 | 104 | | { |
| 6 | 105 | | newArray[i * newLengthOfArrays + j] = Array[i * currentLengthOfArrays + j]; |
| 6 | 106 | | } |
| | 107 | |
|
| 1 | 108 | | Array = newArray; |
| 1 | 109 | | ColumnCount = newLengthOfArrays; |
| 1 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Get the column index of the provided <paramref name="index" />. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="index">A valid index contained within <see cref="Array" />.</param> |
| | 116 | | /// <returns>The column index.</returns> |
| | 117 | | public int GetColumnIndex(int index) |
| 1 | 118 | | { |
| 1 | 119 | | int leftOvers = index % ColumnCount; |
| 1 | 120 | | return leftOvers; |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Get the row index of the provided <paramref name="index" />. |
| | 125 | | /// </summary> |
| | 126 | | /// <param name="index">A valid index contained within <see cref="Array" />.</param> |
| | 127 | | /// <returns>The row index.</returns> |
| | 128 | | public int GetRowIndex(int index) |
| 1 | 129 | | { |
| 1 | 130 | | int leftOvers = index % ColumnCount; |
| 1 | 131 | | return (index - leftOvers) / ColumnCount; |
| 1 | 132 | | } |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Reverse the order of the columns in the backing <see cref="Array" />. |
| | 136 | | /// </summary> |
| | 137 | | public void ReverseColumns() |
| 2 | 138 | | { |
| | 139 | | // ReSharper disable once TooWideLocalVariableScope |
| | 140 | | T temporaryStorage; |
| | 141 | |
|
| 2 | 142 | | int lastIndex = ColumnCount - 1; |
| 2 | 143 | | int middleIndex = ColumnCount / 2; |
| | 144 | |
|
| 12 | 145 | | for (int rowIndex = 0; rowIndex < RowCount; rowIndex++) |
| 4 | 146 | | { |
| 24 | 147 | | for (int columnIndex = 0; columnIndex < middleIndex; columnIndex++) |
| 8 | 148 | | { |
| | 149 | | // Cache our indexes |
| 8 | 150 | | int currentElementIndex = rowIndex * ColumnCount + columnIndex; |
| 8 | 151 | | int swapElementIndex = rowIndex * ColumnCount + (lastIndex - columnIndex); |
| | 152 | |
|
| | 153 | | // Store the swap value |
| 8 | 154 | | temporaryStorage = Array[currentElementIndex]; |
| | 155 | |
|
| | 156 | | // Swap values |
| 8 | 157 | | Array[currentElementIndex] = Array[swapElementIndex]; |
| 8 | 158 | | Array[swapElementIndex] = temporaryStorage; |
| 8 | 159 | | } |
| 4 | 160 | | } |
| 2 | 161 | | } |
| | 162 | |
|
| | 163 | | /// <summary> |
| | 164 | | /// Reverse the order of the rows in the backing <see cref="Array" />. |
| | 165 | | /// </summary> |
| | 166 | | public void ReverseRows() |
| 2 | 167 | | { |
| 2 | 168 | | T[] temporaryStorage = new T[ColumnCount]; |
| | 169 | |
|
| 2 | 170 | | int lastIndex = RowCount - 1; |
| 2 | 171 | | int middleIndex = RowCount / 2; |
| | 172 | |
|
| 14 | 173 | | for (int rowIndex = 0; rowIndex < middleIndex; rowIndex++) |
| 5 | 174 | | { |
| | 175 | | // Save existing line |
| 5 | 176 | | System.Array.Copy(Array, rowIndex * ColumnCount, temporaryStorage, 0, ColumnCount); |
| | 177 | |
|
| | 178 | | // Get line on other side of the flip and put in place |
| 5 | 179 | | System.Array.Copy(Array, (lastIndex - rowIndex) * ColumnCount, Array, rowIndex * ColumnCount, |
| | 180 | | ColumnCount); |
| | 181 | |
|
| | 182 | | // Write other side content |
| 5 | 183 | | System.Array.Copy(temporaryStorage, 0, Array, (lastIndex - rowIndex) * ColumnCount, ColumnCount); |
| 5 | 184 | | } |
| 2 | 185 | | } |
| | 186 | |
|
| | 187 | | /// <summary> |
| | 188 | | /// Rotate internal dataset clockwise. |
| | 189 | | /// </summary> |
| | 190 | | public void RotateClockwise() |
| 1 | 191 | | { |
| | 192 | | // TODO: There should be a way to do this without making a transient array. |
| | 193 | |
|
| | 194 | | // Make our new array |
| 1 | 195 | | T[] newArray = new T[Array.Length]; |
| | 196 | |
|
| 1 | 197 | | int newColumnCount = RowCount; |
| 1 | 198 | | int runningIndex = 0; |
| | 199 | |
|
| | 200 | | // Transpose values to new array |
| 6 | 201 | | for (int column = 0; column < ColumnCount; column++) |
| 2 | 202 | | { |
| 24 | 203 | | for (int row = RowCount - 1; row >= 0; row--) |
| 10 | 204 | | { |
| 10 | 205 | | newArray[runningIndex] = Array[row * ColumnCount + column]; |
| 10 | 206 | | runningIndex++; |
| 10 | 207 | | } |
| 2 | 208 | | } |
| | 209 | |
|
| | 210 | | // Assign Data |
| 1 | 211 | | RowCount = ColumnCount; |
| 1 | 212 | | ColumnCount = newColumnCount; |
| 1 | 213 | | Array = newArray; |
| 1 | 214 | | } |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// Rotate internal dataset counter-clockwise. |
| | 218 | | /// </summary> |
| | 219 | | public void RotateCounterClockwise() |
| 1 | 220 | | { |
| | 221 | | // TODO: There should be a way to do this without making a transient array. |
| | 222 | |
|
| | 223 | | // Make our new array |
| 1 | 224 | | T[] newArray = new T[Array.Length]; |
| | 225 | |
|
| 1 | 226 | | int newColumnCount = RowCount; |
| 1 | 227 | | int runningIndex = 0; |
| | 228 | |
|
| | 229 | | // Transpose values to new array |
| 6 | 230 | | for (int column = ColumnCount - 1; column >= 0; column--) |
| 2 | 231 | | { |
| 24 | 232 | | for (int row = 0; row < RowCount; row++) |
| 10 | 233 | | { |
| 10 | 234 | | newArray[runningIndex] = Array[row * ColumnCount + column]; |
| 10 | 235 | | runningIndex++; |
| 10 | 236 | | } |
| 2 | 237 | | } |
| | 238 | |
|
| | 239 | | // Assign Data |
| 1 | 240 | | RowCount = ColumnCount; |
| 1 | 241 | | ColumnCount = newColumnCount; |
| 1 | 242 | | Array = newArray; |
| 1 | 243 | | } |
| | 244 | |
|
| | 245 | | /// <summary> |
| | 246 | | /// Creates a copy of the internal array as a traditional multi-dimensional array. |
| | 247 | | /// </summary> |
| | 248 | | /// <remarks>Useful for scenarios where fills need to be done with [,] structured multi-dimensional arrays.</rem |
| | 249 | | /// <returns>A new copy of the backing <see cref="Array" /> in multi-dimensional form.</returns> |
| | 250 | | public T[,] ToMultiDimensionalArray() |
| 1 | 251 | | { |
| 1 | 252 | | T[,] returnArray = new T[RowCount, ColumnCount]; |
| 6 | 253 | | for (int x = 0; x < RowCount; x++) |
| 2 | 254 | | { |
| 12 | 255 | | for (int y = 0; y < ColumnCount; y++) |
| 4 | 256 | | { |
| 4 | 257 | | returnArray[x, y] = Array[x * ColumnCount + y]; |
| 4 | 258 | | } |
| 2 | 259 | | } |
| | 260 | |
|
| 1 | 261 | | return returnArray; |
| 1 | 262 | | } |
| | 263 | | } |
| | 264 | | } |