Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeNotBurstCompatible.cs 4.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //#define USE_NOT_BURST_COMPATIBLE_EXTENSIONS
  2. using System;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace Unity.Collections.NotBurstCompatible
  5. {
  6. /// <summary>
  7. /// Provides some extension methods for various collections.
  8. /// </summary>
  9. public static class Extensions
  10. {
  11. /// <summary>
  12. /// Returns a new managed array with all the elements copied from a set.
  13. /// </summary>
  14. /// <typeparam name="T">The type of elements.</typeparam>
  15. /// <param name="set">The set whose elements are copied to the array.</param>
  16. /// <returns>A new managed array with all the elements copied from a set.</returns>
  17. [NotBurstCompatible]
  18. public static T[] ToArray<T>(this NativeHashSet<T> set)
  19. where T : unmanaged, IEquatable<T>
  20. {
  21. var array = set.ToNativeArray(Allocator.TempJob);
  22. var managed = array.ToArray();
  23. array.Dispose();
  24. return managed;
  25. }
  26. /// <summary>
  27. /// Returns a new managed array which is a copy of this list.
  28. /// </summary>
  29. /// <typeparam name="T">The type of elements.</typeparam>
  30. /// <param name="list">The list to copy.</param>
  31. /// <returns>A new managed array which is a copy of this list.</returns>
  32. [NotBurstCompatible]
  33. public static T[] ToArrayNBC<T>(this NativeList<T> list)
  34. where T : unmanaged
  35. {
  36. return list.AsArray().ToArray();
  37. }
  38. /// <summary>
  39. /// Clears this list and then copies all the elements of an array to this list.
  40. /// </summary>
  41. /// <typeparam name="T">The type of elements.</typeparam>
  42. /// <param name="list">This list.</param>
  43. /// <param name="array">The managed array to copy from.</param>
  44. [NotBurstCompatible]
  45. public static void CopyFromNBC<T>(this NativeList<T> list, T[] array)
  46. where T : unmanaged
  47. {
  48. list.Clear();
  49. list.Resize(array.Length, NativeArrayOptions.UninitializedMemory);
  50. NativeArray<T> na = list.AsArray();
  51. na.CopyFrom(array);
  52. }
  53. #if !NET_DOTS // Tuple is not supported by TinyBCL
  54. /// <summary>
  55. /// Returns an array with the unique keys of this multi hash map.
  56. /// </summary>
  57. /// <typeparam name="TKey">The type of the keys.</typeparam>
  58. /// <typeparam name="TValue">The type of the values.</typeparam>
  59. /// <param name="hashmap">The multi hash map.</param>
  60. /// <param name="allocator">The allocator to use.</param>
  61. /// <returns>An array with the unique keys of this multi hash map.</returns>
  62. [BurstCompatible(GenericTypeArguments = new[] { typeof(int), typeof(int) })]
  63. [Obsolete("Burst now supports tuple, please use `GetUniqueKeyArray` method from `Unity.Collections.UnsafeMultiHashMap` instead.", false)]
  64. public static (NativeArray<TKey>, int) GetUniqueKeyArrayNBC<TKey, TValue>(this UnsafeMultiHashMap<TKey, TValue> hashmap, AllocatorManager.AllocatorHandle allocator)
  65. where TKey : struct, IEquatable<TKey>, IComparable<TKey>
  66. where TValue : struct => hashmap.GetUniqueKeyArray(allocator);
  67. /// <summary>
  68. /// Returns an array with the unique keys of this multi hash map.
  69. /// </summary>
  70. /// <typeparam name="TKey">The type of the keys.</typeparam>
  71. /// <typeparam name="TValue">The type of the values.</typeparam>
  72. /// <param name="hashmap">The multi hash map.</param>
  73. /// <param name="allocator">The allocator to use.</param>
  74. /// <returns>An array with the unique keys of this multi hash map.</returns>
  75. [BurstCompatible(GenericTypeArguments = new[] { typeof(int), typeof(int) })]
  76. [Obsolete("Burst now supports tuple, please use `GetUniqueKeyArray` method from `Unity.Collections.NativeMultiHashMap` instead.", false)]
  77. public static (NativeArray<TKey>, int) GetUniqueKeyArrayNBC<TKey, TValue>(this NativeMultiHashMap<TKey, TValue> hashmap, AllocatorManager.AllocatorHandle allocator)
  78. where TKey : struct, IEquatable<TKey>, IComparable<TKey>
  79. where TValue : struct => hashmap.GetUniqueKeyArray(allocator);
  80. #endif
  81. }
  82. }