| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using Unity.Collections; |
| | | 3 | | |
| | | 4 | | namespace Hertzole.ScriptableValues |
| | | 5 | | { |
| | | 6 | | public static class CollectionExtensions |
| | | 7 | | { |
| | | 8 | | internal static bool TryEnsureCapacity<T>(this ICollection<T> list, int capacity) |
| | 78 | 9 | | { |
| | 78 | 10 | | if (list is List<T> genericList) |
| | 36 | 11 | | { |
| | 36 | 12 | | if (genericList.Capacity < capacity) |
| | 24 | 13 | | { |
| | 24 | 14 | | genericList.Capacity = capacity; |
| | 24 | 15 | | } |
| | | 16 | | |
| | 36 | 17 | | return true; |
| | | 18 | | } |
| | | 19 | | |
| | 42 | 20 | | if (list is ScriptableList<T> scriptableList) |
| | 36 | 21 | | { |
| | 36 | 22 | | scriptableList.EnsureCapacity(capacity); |
| | 36 | 23 | | return true; |
| | | 24 | | } |
| | | 25 | | |
| | 6 | 26 | | return false; |
| | 78 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns a <see cref="NativeArray{T}" /> that is a copy of this list. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <remarks>This method supports both <c>struct</c> and <c>unmanaged</c> types.</remarks> |
| | | 33 | | /// <typeparam name="T">The type of elements in the list.</typeparam> |
| | | 34 | | /// <param name="list">The list to copy.</param> |
| | | 35 | | /// <param name="allocator">The allocator to use.</param> |
| | | 36 | | /// <returns>An array that is a copy of this list.</returns> |
| | | 37 | | public static NativeArray<T> ToNativeArray<T>(this ScriptableList<T> list, Allocator allocator) where T : struct |
| | 6 | 38 | | { |
| | 6 | 39 | | NativeArray<T> nativeArray = new NativeArray<T>(list.Count, allocator, NativeArrayOptions.UninitializedMemor |
| | 48 | 40 | | for (int i = 0; i < list.Count; i++) |
| | 18 | 41 | | { |
| | 18 | 42 | | nativeArray[i] = list[i]; |
| | 18 | 43 | | } |
| | | 44 | | |
| | 6 | 45 | | return nativeArray; |
| | 6 | 46 | | } |
| | | 47 | | |
| | | 48 | | #if SCRIPTABLE_VALUES_UNITY_COLLECTIONS |
| | | 49 | | /// <summary> |
| | | 50 | | /// Returns a <see cref="NativeArray{T}" /> that is a copy of this list. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <remarks>This method only supports <c>unmanaged</c> types.</remarks> |
| | | 53 | | /// <typeparam name="T">The type of elements in the list.</typeparam> |
| | | 54 | | /// <param name="list">The list to copy.</param> |
| | | 55 | | /// <param name="allocator">The allocator to use.</param> |
| | | 56 | | /// <returns>An array that is a copy of this list.</returns> |
| | | 57 | | public static NativeArray<T> ToNativeArray<T>(this ScriptableList<T> list, AllocatorManager.AllocatorHandle allo |
| | 6 | 58 | | { |
| | 6 | 59 | | return list.list.ToNativeArray(allocator); |
| | 6 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Returns a <see cref="NativeList{T}" /> that is a copy of this list. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <remarks>This method only supports <c>unmanaged</c> types.</remarks> |
| | | 66 | | /// <typeparam name="T">The type of elements in the list.</typeparam> |
| | | 67 | | /// <param name="list">The list to copy.</param> |
| | | 68 | | /// <param name="allocator">The allocator to use.</param> |
| | | 69 | | /// <returns>A <see cref="NativeList{T}" /> that is a copy of this list.</returns> |
| | | 70 | | public static NativeList<T> ToNativeList<T>(this ScriptableList<T> list, AllocatorManager.AllocatorHandle alloca |
| | 6 | 71 | | { |
| | 6 | 72 | | return list.list.ToNativeList(allocator); |
| | 6 | 73 | | } |
| | | 74 | | #endif |
| | | 75 | | } |
| | | 76 | | } |