< Summary

Class:GDX.Collections.Generic.NativeArray2D[T]
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Collections/Generic/NativeArray2D.cs
Covered lines:0
Uncovered lines:71
Coverable lines:71
Total lines:190
Line coverage:0% (0 of 71)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:9
Method coverage:0% (0 of 9)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NativeArray2D(...)0%2100%
AddRows(...)0%2100%
AddColumns(...)0%12300%
Dispose()0%6200%
ReverseColumns()0%12300%
ReverseRows()0%6200%
ToMultiDimensionalArray()0%12300%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Collections/Generic/NativeArray2D.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 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
 5using System;
 6using System.Runtime.CompilerServices;
 7using Unity.Collections;
 8
 9namespace 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)
 044        {
 045            ColumnCount = columnCount;
 046            RowCount = rowCount;
 047            Array = new NativeArray<T>(rowCount * columnCount, allocator, nativeArrayOptions);
 048        }
 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)]
 058            get => Array[x * ColumnCount + y];
 59            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 060            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)
 070        {
 071            int newArrayCount = RowCount + numberOfNewRows;
 72
 073            NativeArray<T> newArray = new NativeArray<T>(newArrayCount * ColumnCount, allocator, nativeArrayOptions);
 074            Array.CopyTo(newArray);
 075            Array.Dispose();
 076            Array = newArray;
 077            RowCount = newArrayCount;
 078        }
 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)
 087        {
 088            int currentLengthOfArrays = ColumnCount;
 089            int newLengthOfArrays = currentLengthOfArrays + numberOfNewColumns;
 090            NativeArray<T> newArray = new NativeArray<T>(RowCount * newLengthOfArrays, allocator, nativeArrayOptions);
 91
 092            for (int i = 0; i < RowCount; i++)
 093            for (int j = 0; j < currentLengthOfArrays; j++)
 094            {
 095                newArray[i * newLengthOfArrays + j] = Array[i * currentLengthOfArrays + j];
 096            }
 97
 098            ColumnCount = newLengthOfArrays;
 099        }
 100
 101        /// <summary>
 102        ///     Properly dispose of <see cref="Array" />.
 103        /// </summary>
 104        public void Dispose()
 0105        {
 0106            if (!Array.IsCreated)
 0107            {
 0108                return;
 109            }
 110
 0111            Array.Dispose();
 0112            Array = default;
 0113        }
 114
 115        /// <summary>
 116        ///     Reverse the order of the columns in the backing <see cref="Array" />.
 117        /// </summary>
 118        public void ReverseColumns()
 0119        {
 120            // ReSharper disable once TooWideLocalVariableScope
 121            T temporaryStorage;
 122
 0123            int lastIndex = ColumnCount - 1;
 0124            int middleIndex = ColumnCount / 2;
 125
 0126            for (int rowIndex = 0; rowIndex < RowCount; rowIndex++)
 0127            {
 0128                for (int columnIndex = 0; columnIndex < middleIndex; columnIndex++)
 0129                {
 130                    // Cache our indexes
 0131                    int currentElementIndex = rowIndex * ColumnCount + columnIndex;
 0132                    int swapElementIndex = rowIndex * ColumnCount + (lastIndex - columnIndex);
 133
 134                    // Store the swap value
 0135                    temporaryStorage = Array[currentElementIndex];
 136
 137                    // Swap values
 0138                    Array[currentElementIndex] = Array[swapElementIndex];
 0139                    Array[swapElementIndex] = temporaryStorage;
 0140                }
 0141            }
 0142        }
 143
 144        /// <summary>
 145        ///     Reverse the order of the rows in the backing <see cref="Array" />.
 146        /// </summary>
 147        public void ReverseRows()
 0148        {
 0149            NativeArray<T> temporaryStorage =
 150                new NativeArray<T>(ColumnCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
 151
 0152            int lastIndex = RowCount - 1;
 0153            int middleIndex = RowCount / 2;
 154
 0155            for (int rowIndex = 0; rowIndex < middleIndex; rowIndex++)
 0156            {
 157                // Save existing line
 0158                NativeArray<T>.Copy(Array, rowIndex * ColumnCount, temporaryStorage, 0, ColumnCount);
 159
 160                // Get line on other side of the flip and put in place
 0161                NativeArray<T>.Copy(Array, (lastIndex - rowIndex) * ColumnCount, Array, rowIndex * ColumnCount,
 162                    ColumnCount);
 163
 164                // Write other side content
 0165                NativeArray<T>.Copy(temporaryStorage, 0, Array, (lastIndex - rowIndex) * ColumnCount, ColumnCount);
 0166            }
 167
 0168            temporaryStorage.Dispose();
 0169        }
 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()
 0177        {
 0178            T[,] returnArray = new T[RowCount, ColumnCount];
 0179            for (int x = 0; x < RowCount; x++)
 0180            {
 0181                for (int y = 0; y < ColumnCount; y++)
 0182                {
 0183                    returnArray[x, y] = Array[x * ColumnCount + y];
 0184                }
 0185            }
 186
 0187            return returnArray;
 0188        }
 189    }
 190}