Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UnsafeHashSet.cs 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using Unity.Jobs;
  7. namespace Unity.Collections.LowLevel.Unsafe
  8. {
  9. /// <summary>
  10. /// An unordered, expandable set of unique values.
  11. /// </summary>
  12. /// <typeparam name="T">The type of the values.</typeparam>
  13. [StructLayout(LayoutKind.Sequential)]
  14. [DebuggerTypeProxy(typeof(UnsafeHashSetDebuggerTypeProxy<>))]
  15. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  16. public unsafe struct UnsafeHashSet<T>
  17. : INativeDisposable
  18. , IEnumerable<T> // Used by collection initializers.
  19. where T : unmanaged, IEquatable<T>
  20. {
  21. internal UnsafeHashMap<T, bool> m_Data;
  22. /// <summary>
  23. /// Initializes and returns an instance of UnsafeHashSet.
  24. /// </summary>
  25. /// <param name="capacity">The number of values that should fit in the initial allocation.</param>
  26. /// <param name="allocator">The allocator to use.</param>
  27. public UnsafeHashSet(int capacity, AllocatorManager.AllocatorHandle allocator)
  28. {
  29. m_Data = new UnsafeHashMap<T, bool>(capacity, allocator);
  30. }
  31. /// <summary>
  32. /// Whether this set is empty.
  33. /// </summary>
  34. /// <value>True if this set is empty.</value>
  35. public bool IsEmpty => m_Data.IsEmpty;
  36. /// <summary>
  37. /// Returns the current number of values in this set.
  38. /// </summary>
  39. /// <returns>The current number of values in this set.</returns>
  40. public int Count() => m_Data.Count();
  41. /// <summary>
  42. /// The number of values that fit in the current allocation.
  43. /// </summary>
  44. /// <value>The number of values that fit in the current allocation.</value>
  45. /// <param name="value">A new capacity. Must be larger than current capacity.</param>
  46. /// <exception cref="Exception">Thrown if `value` is less than the current capacity.</exception>
  47. public int Capacity { get => m_Data.Capacity; set => m_Data.Capacity = value; }
  48. /// <summary>
  49. /// Whether this set has been allocated (and not yet deallocated).
  50. /// </summary>
  51. /// <value>True if this set has been allocated (and not yet deallocated).</value>
  52. public bool IsCreated => m_Data.IsCreated;
  53. /// <summary>
  54. /// Releases all resources (memory).
  55. /// </summary>
  56. public void Dispose() => m_Data.Dispose();
  57. /// <summary>
  58. /// Creates and schedules a job that will dispose this set.
  59. /// </summary>
  60. /// <param name="inputDeps">A job handle. The newly scheduled job will depend upon this handle.</param>
  61. /// <returns>The handle of a new job that will dispose this set.</returns>
  62. [NotBurstCompatible /* This is not burst compatible because of IJob's use of a static IntPtr. Should switch to IJobBurstSchedulable in the future */]
  63. public JobHandle Dispose(JobHandle inputDeps) => m_Data.Dispose(inputDeps);
  64. /// <summary>
  65. /// Removes all values.
  66. /// </summary>
  67. /// <remarks>Does not change the capacity.</remarks>
  68. public void Clear() => m_Data.Clear();
  69. /// <summary>
  70. /// Adds a new value (unless it is already present).
  71. /// </summary>
  72. /// <param name="item">The value to add.</param>
  73. /// <returns>True if the value was not already present.</returns>
  74. public bool Add(T item) => m_Data.TryAdd(item, false);
  75. /// <summary>
  76. /// Removes a particular value.
  77. /// </summary>
  78. /// <param name="item">The value to remove.</param>
  79. /// <returns>True if the value was present.</returns>
  80. public bool Remove(T item) => m_Data.Remove(item);
  81. /// <summary>
  82. /// Returns true if a particular value is present.
  83. /// </summary>
  84. /// <param name="item">The value to check for.</param>
  85. /// <returns>True if the value was present.</returns>
  86. public bool Contains(T item) => m_Data.ContainsKey(item);
  87. /// <summary>
  88. /// Returns an array with a copy of this set's values (in no particular order).
  89. /// </summary>
  90. /// <param name="allocator">The allocator to use.</param>
  91. /// <returns>An array with a copy of the set's values.</returns>
  92. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator) => m_Data.GetKeyArray(allocator);
  93. /// <summary>
  94. /// Returns a parallel writer.
  95. /// </summary>
  96. /// <returns>A parallel writer.</returns>
  97. public ParallelWriter AsParallelWriter()
  98. {
  99. return new ParallelWriter { m_Data = m_Data.AsParallelWriter() };
  100. }
  101. /// <summary>
  102. /// A parallel writer for an UnsafeHashSet.
  103. /// </summary>
  104. /// <remarks>
  105. /// Use <see cref="AsParallelWriter"/> to create a parallel writer for a set.
  106. /// </remarks>
  107. [NativeContainerIsAtomicWriteOnly]
  108. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  109. public struct ParallelWriter
  110. {
  111. internal UnsafeHashMap<T, bool>.ParallelWriter m_Data;
  112. /// <summary>
  113. /// The number of values that fit in the current allocation.
  114. /// </summary>
  115. /// <value>The number of values that fit in the current allocation.</value>
  116. public int Capacity => m_Data.Capacity;
  117. /// <summary>
  118. /// Adds a new value (unless it is already present).
  119. /// </summary>
  120. /// <param name="item">The value to add.</param>
  121. /// <returns>True if the value is not already present.</returns>
  122. public bool Add(T item) => m_Data.TryAdd(item, false);
  123. }
  124. /// <summary>
  125. /// Returns an enumerator over the values of this set.
  126. /// </summary>
  127. /// <returns>An enumerator over the values of this set.</returns>
  128. public Enumerator GetEnumerator()
  129. {
  130. return new Enumerator { m_Enumerator = new UnsafeHashMapDataEnumerator(m_Data.m_Buffer) };
  131. }
  132. /// <summary>
  133. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  134. /// </summary>
  135. /// <returns>Throws NotImplementedException.</returns>
  136. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  137. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  138. {
  139. throw new NotImplementedException();
  140. }
  141. /// <summary>
  142. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  143. /// </summary>
  144. /// <returns>Throws NotImplementedException.</returns>
  145. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  146. IEnumerator IEnumerable.GetEnumerator()
  147. {
  148. throw new NotImplementedException();
  149. }
  150. /// <summary>
  151. /// An enumerator over the values of a set.
  152. /// </summary>
  153. /// <remarks>
  154. /// In an enumerator's initial state, <see cref="Current"/> is invalid.
  155. /// The first <see cref="MoveNext"/> call advances the enumerator to the first value.
  156. /// </remarks>
  157. public struct Enumerator : IEnumerator<T>
  158. {
  159. internal UnsafeHashMapDataEnumerator m_Enumerator;
  160. /// <summary>
  161. /// Does nothing.
  162. /// </summary>
  163. public void Dispose() { }
  164. /// <summary>
  165. /// Advances the enumerator to the next value.
  166. /// </summary>
  167. /// <returns>True if `Current` is valid to read after the call.</returns>
  168. public bool MoveNext() => m_Enumerator.MoveNext();
  169. /// <summary>
  170. /// Resets the enumerator to its initial state.
  171. /// </summary>
  172. public void Reset() => m_Enumerator.Reset();
  173. /// <summary>
  174. /// The current value.
  175. /// </summary>
  176. /// <value>The current value.</value>
  177. public T Current => m_Enumerator.GetCurrentKey<T>();
  178. object IEnumerator.Current => Current;
  179. }
  180. }
  181. sealed internal class UnsafeHashSetDebuggerTypeProxy<T>
  182. where T : unmanaged, IEquatable<T>
  183. {
  184. #if !NET_DOTS
  185. UnsafeHashSet<T> Data;
  186. public UnsafeHashSetDebuggerTypeProxy(UnsafeHashSet<T> data)
  187. {
  188. Data = data;
  189. }
  190. public List<T> Items
  191. {
  192. get
  193. {
  194. var result = new List<T>();
  195. using (var item = Data.ToNativeArray(Allocator.Temp))
  196. {
  197. for (var k = 0; k < item.Length; ++k)
  198. {
  199. result.Add(item[k]);
  200. }
  201. }
  202. return result;
  203. }
  204. }
  205. #endif
  206. }
  207. }