Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using Unity.Jobs;
  8. namespace Unity.Collections.LowLevel.Unsafe
  9. {
  10. /// <summary>
  11. /// An unordered, expandable set of unique values.
  12. /// </summary>
  13. /// <typeparam name="T">The type of the values.</typeparam>
  14. [StructLayout(LayoutKind.Sequential)]
  15. [DebuggerTypeProxy(typeof(UnsafeParallelHashSetDebuggerTypeProxy<>))]
  16. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })]
  17. public unsafe struct UnsafeParallelHashSet<T>
  18. : INativeDisposable
  19. , IEnumerable<T> // Used by collection initializers.
  20. where T : unmanaged, IEquatable<T>
  21. {
  22. internal UnsafeParallelHashMap<T, bool> m_Data;
  23. /// <summary>
  24. /// Initializes and returns an instance of UnsafeParallelHashSet.
  25. /// </summary>
  26. /// <param name="capacity">The number of values that should fit in the initial allocation.</param>
  27. /// <param name="allocator">The allocator to use.</param>
  28. public UnsafeParallelHashSet(int capacity, AllocatorManager.AllocatorHandle allocator)
  29. {
  30. m_Data = new UnsafeParallelHashMap<T, bool>(capacity, allocator);
  31. }
  32. /// <summary>
  33. /// Whether this set is empty.
  34. /// </summary>
  35. /// <value>True if this set is empty.</value>
  36. public readonly bool IsEmpty
  37. {
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. get => m_Data.IsEmpty;
  40. }
  41. /// <summary>
  42. /// Returns the current number of values in this set.
  43. /// </summary>
  44. /// <returns>The current number of values in this set.</returns>
  45. public int Count() => m_Data.Count();
  46. /// <summary>
  47. /// The number of values that fit in the current allocation.
  48. /// </summary>
  49. /// <value>The number of values that fit in the current allocation.</value>
  50. /// <param name="value">A new capacity. Must be larger than current capacity.</param>
  51. /// <exception cref="InvalidOperationException">Thrown if `value` is less than the current capacity.</exception>
  52. public int Capacity
  53. {
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. readonly get => m_Data.Capacity;
  56. set => m_Data.Capacity = value;
  57. }
  58. /// <summary>
  59. /// Whether this set has been allocated (and not yet deallocated).
  60. /// </summary>
  61. /// <value>True if this set has been allocated (and not yet deallocated).</value>
  62. public readonly bool IsCreated
  63. {
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. get => m_Data.IsCreated;
  66. }
  67. /// <summary>
  68. /// Releases all resources (memory).
  69. /// </summary>
  70. public void Dispose() => m_Data.Dispose();
  71. /// <summary>
  72. /// Creates and schedules a job that will dispose this set.
  73. /// </summary>
  74. /// <param name="inputDeps">A job handle. The newly scheduled job will depend upon this handle.</param>
  75. /// <returns>The handle of a new job that will dispose this set.</returns>
  76. public JobHandle Dispose(JobHandle inputDeps) => m_Data.Dispose(inputDeps);
  77. /// <summary>
  78. /// Removes all values.
  79. /// </summary>
  80. /// <remarks>Does not change the capacity.</remarks>
  81. public void Clear() => m_Data.Clear();
  82. /// <summary>
  83. /// Adds a new value (unless it is already present).
  84. /// </summary>
  85. /// <param name="item">The value to add.</param>
  86. /// <returns>True if the value was not already present.</returns>
  87. public bool Add(T item) => m_Data.TryAdd(item, false);
  88. /// <summary>
  89. /// Removes a particular value.
  90. /// </summary>
  91. /// <param name="item">The value to remove.</param>
  92. /// <returns>True if the value was present.</returns>
  93. public bool Remove(T item) => m_Data.Remove(item);
  94. /// <summary>
  95. /// Returns true if a particular value is present.
  96. /// </summary>
  97. /// <param name="item">The value to check for.</param>
  98. /// <returns>True if the value was present.</returns>
  99. public bool Contains(T item) => m_Data.ContainsKey(item);
  100. /// <summary>
  101. /// Returns an array with a copy of this set's values (in no particular order).
  102. /// </summary>
  103. /// <param name="allocator">The allocator to use.</param>
  104. /// <returns>An array with a copy of the set's values.</returns>
  105. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator) => m_Data.GetKeyArray(allocator);
  106. /// <summary>
  107. /// Returns a parallel writer.
  108. /// </summary>
  109. /// <returns>A parallel writer.</returns>
  110. public ParallelWriter AsParallelWriter()
  111. {
  112. return new ParallelWriter { m_Data = m_Data.AsParallelWriter() };
  113. }
  114. /// <summary>
  115. /// A parallel writer for an UnsafeParallelHashSet.
  116. /// </summary>
  117. /// <remarks>
  118. /// Use <see cref="AsParallelWriter"/> to create a parallel writer for a set.
  119. /// </remarks>
  120. [NativeContainerIsAtomicWriteOnly]
  121. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })]
  122. public struct ParallelWriter
  123. {
  124. internal UnsafeParallelHashMap<T, bool>.ParallelWriter m_Data;
  125. /// <summary>
  126. /// The number of values that fit in the current allocation.
  127. /// </summary>
  128. /// <value>The number of values that fit in the current allocation.</value>
  129. public readonly int Capacity
  130. {
  131. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  132. get => m_Data.Capacity;
  133. }
  134. /// <summary>
  135. /// Adds a new value (unless it is already present).
  136. /// </summary>
  137. /// <param name="item">The value to add.</param>
  138. /// <returns>True if the value is not already present.</returns>
  139. public bool Add(T item) => m_Data.TryAdd(item, false);
  140. /// <summary>
  141. /// Adds a new value (unless it is already present).
  142. /// </summary>
  143. /// <param name="item">The value to add.</param>
  144. /// <param name="threadIndexOverride">The thread index which must be set by a field from a job struct with the <see cref="NativeSetThreadIndexAttribute"/> attribute.</param>
  145. /// <returns>True if the value is not already present.</returns>
  146. internal bool Add(T item, int threadIndexOverride) => m_Data.TryAdd(item, false, threadIndexOverride);
  147. }
  148. /// <summary>
  149. /// Returns an enumerator over the values of this set.
  150. /// </summary>
  151. /// <returns>An enumerator over the values of this set.</returns>
  152. public Enumerator GetEnumerator()
  153. {
  154. return new Enumerator { m_Enumerator = new UnsafeParallelHashMapDataEnumerator(m_Data.m_Buffer) };
  155. }
  156. /// <summary>
  157. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  158. /// </summary>
  159. /// <returns>Throws NotImplementedException.</returns>
  160. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  161. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  162. {
  163. throw new NotImplementedException();
  164. }
  165. /// <summary>
  166. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  167. /// </summary>
  168. /// <returns>Throws NotImplementedException.</returns>
  169. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  170. IEnumerator IEnumerable.GetEnumerator()
  171. {
  172. throw new NotImplementedException();
  173. }
  174. /// <summary>
  175. /// An enumerator over the values of a set.
  176. /// </summary>
  177. /// <remarks>
  178. /// In an enumerator's initial state, <see cref="Current"/> is invalid.
  179. /// The first <see cref="MoveNext"/> call advances the enumerator to the first value.
  180. /// </remarks>
  181. public struct Enumerator : IEnumerator<T>
  182. {
  183. internal UnsafeParallelHashMapDataEnumerator m_Enumerator;
  184. /// <summary>
  185. /// Does nothing.
  186. /// </summary>
  187. public void Dispose() { }
  188. /// <summary>
  189. /// Advances the enumerator to the next value.
  190. /// </summary>
  191. /// <returns>True if `Current` is valid to read after the call.</returns>
  192. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  193. public bool MoveNext() => m_Enumerator.MoveNext();
  194. /// <summary>
  195. /// Resets the enumerator to its initial state.
  196. /// </summary>
  197. public void Reset() => m_Enumerator.Reset();
  198. /// <summary>
  199. /// The current value.
  200. /// </summary>
  201. /// <value>The current value.</value>
  202. public T Current
  203. {
  204. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  205. get => m_Enumerator.GetCurrentKey<T>();
  206. }
  207. object IEnumerator.Current => Current;
  208. }
  209. /// <summary>
  210. /// Returns a readonly version of this UnsafeParallelHashSet instance.
  211. /// </summary>
  212. /// <remarks>ReadOnly containers point to the same underlying data as the UnsafeParallelHashSet it is made from.</remarks>
  213. /// <returns>ReadOnly instance for this.</returns>
  214. public ReadOnly AsReadOnly()
  215. {
  216. return new ReadOnly(ref this);
  217. }
  218. /// <summary>
  219. /// A read-only alias for the value of a UnsafeParallelHashSet. Does not have its own allocated storage.
  220. /// </summary>
  221. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(int) })]
  222. public struct ReadOnly
  223. : IEnumerable<T>
  224. {
  225. internal UnsafeParallelHashMap<T, bool> m_Data;
  226. internal ReadOnly(ref UnsafeParallelHashSet<T> data)
  227. {
  228. m_Data = data.m_Data;
  229. }
  230. /// <summary>
  231. /// Whether this hash set has been allocated (and not yet deallocated).
  232. /// </summary>
  233. /// <value>True if this hash set has been allocated (and not yet deallocated).</value>
  234. public readonly bool IsCreated
  235. {
  236. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  237. get => m_Data.IsCreated;
  238. }
  239. /// <summary>
  240. /// Whether this hash set is empty.
  241. /// </summary>
  242. /// <value>True if this hash set is empty or if the map has not been constructed.</value>
  243. public readonly bool IsEmpty
  244. {
  245. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  246. get => !m_Data.IsCreated || m_Data.IsEmpty;
  247. }
  248. /// <summary>
  249. /// The current number of items in this hash set.
  250. /// </summary>
  251. /// <returns>The current number of items in this hash set.</returns>
  252. public readonly int Count() => m_Data.Count();
  253. /// <summary>
  254. /// The number of items that fit in the current allocation.
  255. /// </summary>
  256. /// <value>The number of items that fit in the current allocation.</value>
  257. public readonly int Capacity
  258. {
  259. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  260. get => m_Data.Capacity;
  261. }
  262. /// <summary>
  263. /// Returns true if a given item is present in this hash set.
  264. /// </summary>
  265. /// <param name="item">The item to look up.</param>
  266. /// <returns>True if the item was present.</returns>
  267. public readonly bool Contains(T item)
  268. {
  269. return m_Data.ContainsKey(item);
  270. }
  271. /// <summary>
  272. /// Returns an array with a copy of all this hash set's items (in no particular order).
  273. /// </summary>
  274. /// <param name="allocator">The allocator to use.</param>
  275. /// <returns>An array with a copy of all this hash set's items (in no particular order).</returns>
  276. public readonly NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  277. {
  278. return m_Data.GetKeyArray(allocator);
  279. }
  280. /// <summary>
  281. /// Returns an enumerator over the items of this hash set.
  282. /// </summary>
  283. /// <returns>An enumerator over the items of this hash set.</returns>
  284. public readonly Enumerator GetEnumerator()
  285. {
  286. return new Enumerator
  287. {
  288. m_Enumerator = new UnsafeParallelHashMapDataEnumerator(m_Data.m_Buffer),
  289. };
  290. }
  291. /// <summary>
  292. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  293. /// </summary>
  294. /// <returns>Throws NotImplementedException.</returns>
  295. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  296. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  297. {
  298. throw new NotImplementedException();
  299. }
  300. /// <summary>
  301. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  302. /// </summary>
  303. /// <returns>Throws NotImplementedException.</returns>
  304. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  305. IEnumerator IEnumerable.GetEnumerator()
  306. {
  307. throw new NotImplementedException();
  308. }
  309. }
  310. }
  311. sealed internal class UnsafeParallelHashSetDebuggerTypeProxy<T>
  312. where T : unmanaged, IEquatable<T>
  313. {
  314. UnsafeParallelHashSet<T> Data;
  315. public UnsafeParallelHashSetDebuggerTypeProxy(UnsafeParallelHashSet<T> data)
  316. {
  317. Data = data;
  318. }
  319. public List<T> Items
  320. {
  321. get
  322. {
  323. var result = new List<T>();
  324. using (var item = Data.ToNativeArray(Allocator.Temp))
  325. {
  326. for (var k = 0; k < item.Length; ++k)
  327. {
  328. result.Add(item[k]);
  329. }
  330. }
  331. return result;
  332. }
  333. }
  334. }
  335. }