| | 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 | | using Unity.Collections; |
| | 8 | |
|
| | 9 | | namespace GDX.Collections.Generic |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A 2-dimension <see cref="NativeArray{T}" /> backed array. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks>Use X (horizontal) and Y (vertical) arrangement.</remarks> |
| | 15 | | /// <typeparam name="T">Type of objects.</typeparam> |
| | 16 | | public struct NativeArray2D<T> : IDisposable where T : struct |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The backing <see cref="NativeArray{T}" />. |
| | 20 | | /// </summary> |
| | 21 | | public NativeArray<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="NativeArray2D{T}" />. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="rowCount">The number of rows (X).</param> |
| | 39 | | /// <param name="columnCount">The number of columns (Y).</param> |
| | 40 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 41 | | /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> |
| | 42 | | public NativeArray2D(int rowCount, int columnCount, Allocator allocator, |
| | 43 | | NativeArrayOptions nativeArrayOptions) |
| 0 | 44 | | { |
| 0 | 45 | | ColumnCount = columnCount; |
| 0 | 46 | | RowCount = rowCount; |
| 0 | 47 | | Array = new NativeArray<T>(rowCount * columnCount, allocator, nativeArrayOptions); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Get a typed object at a specific 2-dimensional index in <see cref="Array" />. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="x">The row/line number (vertical axis).</param> |
| | 54 | | /// <param name="y">The column number (horizontal axis).</param> |
| | 55 | | public T this[int x, int y] |
| | 56 | | { |
| | 57 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 0 | 58 | | get => Array[x * ColumnCount + y]; |
| | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 0 | 60 | | set => Array[x * ColumnCount + y] = value; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Add additional rows to the dataset. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="numberOfNewRows">The number of rows/arrays to add.</param> |
| | 67 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 68 | | /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> |
| | 69 | | public void AddRows(int numberOfNewRows, Allocator allocator, NativeArrayOptions nativeArrayOptions) |
| 0 | 70 | | { |
| 0 | 71 | | int newArrayCount = RowCount + numberOfNewRows; |
| | 72 | |
|
| 0 | 73 | | NativeArray<T> newArray = new NativeArray<T>(newArrayCount * ColumnCount, allocator, nativeArrayOptions); |
| 0 | 74 | | Array.CopyTo(newArray); |
| 0 | 75 | | Array.Dispose(); |
| 0 | 76 | | Array = newArray; |
| 0 | 77 | | RowCount = newArrayCount; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Add additional columns to the dataset. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="numberOfNewColumns">The number of columns add.</param> |
| | 84 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 85 | | /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> |
| | 86 | | public void AddColumns(int numberOfNewColumns, Allocator allocator, NativeArrayOptions nativeArrayOptions) |
| 0 | 87 | | { |
| 0 | 88 | | int currentLengthOfArrays = ColumnCount; |
| 0 | 89 | | int newLengthOfArrays = currentLengthOfArrays + numberOfNewColumns; |
| 0 | 90 | | NativeArray<T> newArray = new NativeArray<T>(RowCount * newLengthOfArrays, allocator, nativeArrayOptions); |
| | 91 | |
|
| 0 | 92 | | for (int i = 0; i < RowCount; i++) |
| 0 | 93 | | for (int j = 0; j < currentLengthOfArrays; j++) |
| 0 | 94 | | { |
| 0 | 95 | | newArray[i * newLengthOfArrays + j] = Array[i * currentLengthOfArrays + j]; |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | ColumnCount = newLengthOfArrays; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Properly dispose of <see cref="Array" />. |
| | 103 | | /// </summary> |
| | 104 | | public void Dispose() |
| 0 | 105 | | { |
| 0 | 106 | | if (!Array.IsCreated) |
| 0 | 107 | | { |
| 0 | 108 | | return; |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | Array.Dispose(); |
| 0 | 112 | | Array = default; |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Reverse the order of the columns in the backing <see cref="Array" />. |
| | 117 | | /// </summary> |
| | 118 | | public void ReverseColumns() |
| 0 | 119 | | { |
| | 120 | | // ReSharper disable once TooWideLocalVariableScope |
| | 121 | | T temporaryStorage; |
| | 122 | |
|
| 0 | 123 | | int lastIndex = ColumnCount - 1; |
| 0 | 124 | | int middleIndex = ColumnCount / 2; |
| | 125 | |
|
| 0 | 126 | | for (int rowIndex = 0; rowIndex < RowCount; rowIndex++) |
| 0 | 127 | | { |
| 0 | 128 | | for (int columnIndex = 0; columnIndex < middleIndex; columnIndex++) |
| 0 | 129 | | { |
| | 130 | | // Cache our indexes |
| 0 | 131 | | int currentElementIndex = rowIndex * ColumnCount + columnIndex; |
| 0 | 132 | | int swapElementIndex = rowIndex * ColumnCount + (lastIndex - columnIndex); |
| | 133 | |
|
| | 134 | | // Store the swap value |
| 0 | 135 | | temporaryStorage = Array[currentElementIndex]; |
| | 136 | |
|
| | 137 | | // Swap values |
| 0 | 138 | | Array[currentElementIndex] = Array[swapElementIndex]; |
| 0 | 139 | | Array[swapElementIndex] = temporaryStorage; |
| 0 | 140 | | } |
| 0 | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Reverse the order of the rows in the backing <see cref="Array" />. |
| | 146 | | /// </summary> |
| | 147 | | public void ReverseRows() |
| 0 | 148 | | { |
| 0 | 149 | | NativeArray<T> temporaryStorage = |
| | 150 | | new NativeArray<T>(ColumnCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory); |
| | 151 | |
|
| 0 | 152 | | int lastIndex = RowCount - 1; |
| 0 | 153 | | int middleIndex = RowCount / 2; |
| | 154 | |
|
| 0 | 155 | | for (int rowIndex = 0; rowIndex < middleIndex; rowIndex++) |
| 0 | 156 | | { |
| | 157 | | // Save existing line |
| 0 | 158 | | NativeArray<T>.Copy(Array, rowIndex * ColumnCount, temporaryStorage, 0, ColumnCount); |
| | 159 | |
|
| | 160 | | // Get line on other side of the flip and put in place |
| 0 | 161 | | NativeArray<T>.Copy(Array, (lastIndex - rowIndex) * ColumnCount, Array, rowIndex * ColumnCount, |
| | 162 | | ColumnCount); |
| | 163 | |
|
| | 164 | | // Write other side content |
| 0 | 165 | | NativeArray<T>.Copy(temporaryStorage, 0, Array, (lastIndex - rowIndex) * ColumnCount, ColumnCount); |
| 0 | 166 | | } |
| | 167 | |
|
| 0 | 168 | | temporaryStorage.Dispose(); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Creates a copy of the internal array as a traditional multi-dimensional array. |
| | 173 | | /// </summary> |
| | 174 | | /// <remarks>Useful for scenarios where fills need to be done with [,] structured multi-dimensional arrays.</rem |
| | 175 | | /// <returns>A new copy of the backing <see cref="Array" /> in multi-dimensional form.</returns> |
| | 176 | | public T[,] ToMultiDimensionalArray() |
| 0 | 177 | | { |
| 0 | 178 | | T[,] returnArray = new T[RowCount, ColumnCount]; |
| 0 | 179 | | for (int x = 0; x < RowCount; x++) |
| 0 | 180 | | { |
| 0 | 181 | | for (int y = 0; y < ColumnCount; y++) |
| 0 | 182 | | { |
| 0 | 183 | | returnArray[x, y] = Array[x * ColumnCount + y]; |
| 0 | 184 | | } |
| 0 | 185 | | } |
| | 186 | |
|
| 0 | 187 | | return returnArray; |
| 0 | 188 | | } |
| | 189 | | } |
| | 190 | | } |