A game about forced loneliness, made by TACStudios
1using System; 2using System.Threading; 3using Unity.Collections; 4using Unity.Collections.LowLevel.Unsafe; 5using UnityEngine.Assertions; 6using UnityEngine.Jobs; 7 8namespace UnityEngine.Rendering 9{ 10 /// <summary> 11 /// Array utilities functions 12 /// </summary> 13 public static class ArrayExtensions 14 { 15 /// <summary> 16 /// Resizes a native array. If an empty native array is passed, it will create a new one. 17 /// </summary> 18 /// <typeparam name="T">The type of the array</typeparam> 19 /// <param name="array">Target array to resize</param> 20 /// <param name="capacity">New size of native array to resize</param> 21 public static void ResizeArray<T>(this ref NativeArray<T> array, int capacity) where T : struct 22 { 23 var newArray = new NativeArray<T>(capacity, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); 24 if (array.IsCreated) 25 { 26 NativeArray<T>.Copy(array, newArray, array.Length); 27 array.Dispose(); 28 } 29 array = newArray; 30 } 31 32 /// <summary> 33 /// Resizes a transform access array. 34 /// </summary> 35 /// <param name="array">Target array to resize</param> 36 /// <param name="capacity">New size of transform access array to resize</param> 37 public static void ResizeArray(this ref TransformAccessArray array, int capacity) 38 { 39 var newArray = new TransformAccessArray(capacity); 40 if (array.isCreated) 41 { 42 for (int i = 0; i < array.length; ++i) 43 newArray.Add(array[i]); 44 45 array.Dispose(); 46 } 47 array = newArray; 48 } 49 50 /// <summary> 51 /// Resizes an array. If a null reference is passed, it will allocate the desired array. 52 /// </summary> 53 /// <typeparam name="T">The type of the array</typeparam> 54 /// <param name="array">Target array to resize</param> 55 /// <param name="capacity">New size of array to resize</param> 56 public static void ResizeArray<T>(ref T[] array, int capacity) 57 { 58 if (array == null) 59 { 60 array = new T[capacity]; 61 return; 62 } 63 64 Array.Resize<T>(ref array, capacity); 65 } 66 67 /// <summary> 68 /// Fills an array with the same value. 69 /// </summary> 70 /// <typeparam name="T">The type of the array</typeparam> 71 /// <param name="array">Target array to fill</param> 72 /// <param name="value">Value to fill</param> 73 /// <param name="startIndex">Start index to fill</param> 74 /// <param name="length">The number of entries to write, or -1 to fill until the end of the array</param> 75 public static void FillArray<T>(this ref NativeArray<T> array, in T value, int startIndex = 0, int length = -1) where T : unmanaged 76 { 77 Assert.IsTrue(startIndex >= 0); 78 79 unsafe 80 { 81 T* ptr = (T*)array.GetUnsafePtr<T>(); 82 83 int endIndex = length == -1 ? array.Length : startIndex + length; 84 85 for (int i = startIndex; i < endIndex; ++i) 86 ptr[i] = value; 87 } 88 } 89 } 90}