123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using Unity.Collections.LowLevel.Unsafe;
- using Unity.Jobs;
- using UnityEngine.Internal;
- using Unity.Burst;
- using System.Runtime.CompilerServices;
-
- namespace Unity.Collections
- {
- /// <summary>
- /// An unordered, expandable set of unique values.
- /// </summary>
- /// <remarks>
- /// Not suitable for parallel write access. Use <see cref="NativeParallelHashSet{T}"/> instead.
- /// </remarks>
- /// <typeparam name="T">The type of the values.</typeparam>
- [StructLayout(LayoutKind.Sequential)]
- [NativeContainer]
- [DebuggerTypeProxy(typeof(NativeHashSetDebuggerTypeProxy<>))]
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(int) })]
- public unsafe struct NativeHashSet<T>
- : INativeDisposable
- , IEnumerable<T> // Used by collection initializers.
- where T : unmanaged, IEquatable<T>
- {
- [NativeDisableUnsafePtrRestriction]
- internal HashMapHelper<T>* m_Data;
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- internal AtomicSafetyHandle m_Safety;
- internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<NativeHashSet<T>>();
- #endif
-
- /// <summary>
- /// Initializes and returns an instance of NativeParallelHashSet.
- /// </summary>
- /// <param name="initialCapacity">The number of values that should fit in the initial allocation.</param>
- /// <param name="allocator">The allocator to use.</param>
- public NativeHashSet(int initialCapacity, AllocatorManager.AllocatorHandle allocator)
- {
- m_Data = HashMapHelper<T>.Alloc(initialCapacity, 0, HashMapHelper<T>.kMinimumCapacity, allocator);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- m_Safety = CollectionHelper.CreateSafetyHandle(allocator);
-
- if (UnsafeUtility.IsNativeContainerType<T>())
- AtomicSafetyHandle.SetNestedContainer(m_Safety, true);
-
- CollectionHelper.SetStaticSafetyId<NativeHashSet<T>>(ref m_Safety, ref s_staticSafetyId.Data);
- AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
- #endif
- }
-
- /// <summary>
- /// Whether this set is empty.
- /// </summary>
- /// <value>True if this set is empty or if the set has not been constructed.</value>
- public readonly bool IsEmpty
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- if (!IsCreated)
- {
- return true;
- }
-
- CheckRead();
- return m_Data->IsEmpty;
- }
- }
-
- /// <summary>
- /// Returns the current number of values in this set.
- /// </summary>
- /// <returns>The current number of values in this set.</returns>
- public readonly int Count
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- CheckRead();
- return m_Data->Count;
- }
- }
-
- /// <summary>
- /// The number of values that fit in the current allocation.
- /// </summary>
- /// <value>The number of values that fit in the current allocation.</value>
- /// <param name="value">A new capacity. Must be larger than current capacity.</param>
- public int Capacity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get
- {
- CheckRead();
- return m_Data->Capacity;
- }
-
- set
- {
- CheckWrite();
- m_Data->Resize(value);
- }
- }
-
- /// <summary>
- /// Whether this set has been allocated (and not yet deallocated).
- /// </summary>
- /// <value>True if this set has been allocated (and not yet deallocated).</value>
- public readonly bool IsCreated
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => m_Data != null && m_Data->IsCreated;
- }
-
- /// <summary>
- /// Releases all resources (memory and safety handles).
- /// </summary>
- public void Dispose()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
- {
- AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
- }
- #endif
- if (!IsCreated)
- {
- return;
- }
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- CollectionHelper.DisposeSafetyHandle(ref m_Safety);
- #endif
-
- HashMapHelper<T>.Free(m_Data);
- m_Data = null;
- }
-
- /// <summary>
- /// Creates and schedules a job that will dispose this set.
- /// </summary>
- /// <param name="inputDeps">A job handle. The newly scheduled job will depend upon this handle.</param>
- /// <returns>The handle of a new job that will dispose this set.</returns>
- public JobHandle Dispose(JobHandle inputDeps)
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
- {
- AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
- }
- #endif
- if (!IsCreated)
- {
- return inputDeps;
- }
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- var jobHandle = new NativeHashMapDisposeJob { Data = new NativeHashMapDispose { m_HashMapData = (UnsafeHashMap<int, int>*)m_Data, m_Safety = m_Safety } }.Schedule(inputDeps);
- AtomicSafetyHandle.Release(m_Safety);
- #else
- var jobHandle = new NativeHashMapDisposeJob { Data = new NativeHashMapDispose { m_HashMapData = (UnsafeHashMap<int, int>*)m_Data } }.Schedule(inputDeps);
- #endif
- m_Data = null;
-
- return jobHandle;
- }
-
- /// <summary>
- /// Removes all values.
- /// </summary>
- /// <remarks>Does not change the capacity.</remarks>
- public void Clear()
- {
- CheckWrite();
- m_Data->Clear();
- }
-
- /// <summary>
- /// Adds a new value (unless it is already present).
- /// </summary>
- /// <param name="item">The value to add.</param>
- /// <returns>True if the value was not already present.</returns>
- public bool Add(T item)
- {
- CheckWrite();
- return -1 != m_Data->TryAdd(item);
- }
-
- /// <summary>
- /// Removes a particular value.
- /// </summary>
- /// <param name="item">The value to remove.</param>
- /// <returns>True if the value was present.</returns>
- public bool Remove(T item)
- {
- CheckWrite();
- return -1 != m_Data->TryRemove(item);
- }
-
- /// <summary>
- /// Returns true if a particular value is present.
- /// </summary>
- /// <param name="item">The item to look up.</param>
- /// <returns>True if the value was present.</returns>
- public bool Contains(T item)
- {
- CheckRead();
- return -1 != m_Data->Find(item);
- }
-
- /// <summary>
- /// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
- /// </summary>
- public void TrimExcess()
- {
- CheckWrite();
- m_Data->TrimExcess();
- }
-
- /// <summary>
- /// Returns an array with a copy of this set's values (in no particular order).
- /// </summary>
- /// <param name="allocator">The allocator to use.</param>
- /// <returns>An array with a copy of the set's values.</returns>
- public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
- {
- CheckRead();
- return m_Data->GetKeyArray(allocator);
- }
-
- /// <summary>
- /// Returns an enumerator over the values of this set.
- /// </summary>
- /// <returns>An enumerator over the values of this set.</returns>
- public Enumerator GetEnumerator()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(m_Safety);
- var ash = m_Safety;
- AtomicSafetyHandle.UseSecondaryVersion(ref ash);
- #endif
- return new Enumerator
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- m_Safety = ash,
- #endif
- m_Enumerator = new HashMapHelper<T>.Enumerator(m_Data),
- };
- }
-
- /// <summary>
- /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
- /// </summary>
- /// <returns>Throws NotImplementedException.</returns>
- /// <exception cref="NotImplementedException">Method is not implemented.</exception>
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
- /// </summary>
- /// <returns>Throws NotImplementedException.</returns>
- /// <exception cref="NotImplementedException">Method is not implemented.</exception>
- IEnumerator IEnumerable.GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// An enumerator over the values of a set.
- /// </summary>
- /// <remarks>
- /// In an enumerator's initial state, <see cref="Current"/> is invalid.
- /// The first <see cref="MoveNext"/> call advances the enumerator to the first value.
- /// </remarks>
- [NativeContainer]
- [NativeContainerIsReadOnly]
- public struct Enumerator : IEnumerator<T>
- {
- [NativeDisableUnsafePtrRestriction]
- internal HashMapHelper<T>.Enumerator m_Enumerator;
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- internal AtomicSafetyHandle m_Safety;
- #endif
-
- /// <summary>
- /// Does nothing.
- /// </summary>
- public void Dispose() { }
-
- /// <summary>
- /// Advances the enumerator to the next value.
- /// </summary>
- /// <returns>True if `Current` is valid to read after the call.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool MoveNext()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
- #endif
- return m_Enumerator.MoveNext();
- }
-
- /// <summary>
- /// Resets the enumerator to its initial state.
- /// </summary>
- public void Reset()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
- #endif
- m_Enumerator.Reset();
- }
-
- /// <summary>
- /// The current value.
- /// </summary>
- /// <value>The current value.</value>
- public T Current
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
- #endif
- return m_Enumerator.GetCurrentKey();
- }
- }
-
- /// <summary>
- /// Gets the element at the current position of the enumerator in the container.
- /// </summary>
- object IEnumerator.Current => Current;
- }
-
- /// <summary>
- /// Returns a readonly version of this NativeHashSet instance.
- /// </summary>
- /// <remarks>ReadOnly containers point to the same underlying data as the NativeHashSet it is made from.</remarks>
- /// <returns>ReadOnly instance for this.</returns>
- public ReadOnly AsReadOnly()
- {
- return new ReadOnly(ref this);
- }
-
- /// <summary>
- /// A read-only alias for the value of a NativeHashSet. Does not have its own allocated storage.
- /// </summary>
- [NativeContainer]
- [NativeContainerIsReadOnly]
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(int) })]
- public struct ReadOnly
- : IEnumerable<T>
- {
- [NativeDisableUnsafePtrRestriction]
- internal HashMapHelper<T>* m_Data;
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle m_Safety;
- internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<ReadOnly>();
- #endif
-
- internal ReadOnly(ref NativeHashSet<T> data)
- {
- m_Data = data.m_Data;
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- m_Safety = data.m_Safety;
- CollectionHelper.SetStaticSafetyId<ReadOnly>(ref m_Safety, ref s_staticSafetyId.Data);
- #endif
- }
-
- /// <summary>
- /// Whether this hash set has been allocated (and not yet deallocated).
- /// </summary>
- /// <value>True if this hash set has been allocated (and not yet deallocated).</value>
- public readonly bool IsCreated
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- CheckRead();
- return m_Data->IsCreated;
- }
- }
-
- /// <summary>
- /// Whether this hash set is empty.
- /// </summary>
- /// <value>True if this hash set is empty or if the map has not been constructed.</value>
- public readonly bool IsEmpty
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- CheckRead();
- if (!m_Data->IsCreated)
- {
- return true;
- }
-
- return m_Data->IsEmpty;
- }
- }
-
- /// <summary>
- /// The current number of items in this hash set.
- /// </summary>
- /// <returns>The current number of items in this hash set.</returns>
- public readonly int Count
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- CheckRead();
- return m_Data->Count;
- }
- }
-
- /// <summary>
- /// The number of items that fit in the current allocation.
- /// </summary>
- /// <value>The number of items that fit in the current allocation.</value>
- public readonly int Capacity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- CheckRead();
- return m_Data->Capacity;
- }
- }
-
- /// <summary>
- /// Returns true if a given item is present in this hash set.
- /// </summary>
- /// <param name="item">The item to look up.</param>
- /// <returns>True if the item was present.</returns>
- public readonly bool Contains(T item)
- {
- CheckRead();
- return -1 != m_Data->Find(item);
- }
-
- /// <summary>
- /// Returns an array with a copy of all this hash set's items (in no particular order).
- /// </summary>
- /// <param name="allocator">The allocator to use.</param>
- /// <returns>An array with a copy of all this hash set's items (in no particular order).</returns>
- public readonly NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
- {
- CheckRead();
- return m_Data->GetKeyArray(allocator);
- }
-
- /// <summary>
- /// Returns an enumerator over the items of this hash set.
- /// </summary>
- /// <returns>An enumerator over the items of this hash set.</returns>
- public readonly Enumerator GetEnumerator()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(m_Safety);
- var ash = m_Safety;
- AtomicSafetyHandle.UseSecondaryVersion(ref ash);
- #endif
- return new Enumerator
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- m_Safety = ash,
- #endif
- m_Enumerator = new HashMapHelper<T>.Enumerator(m_Data),
- };
- }
-
- /// <summary>
- /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
- /// </summary>
- /// <returns>Throws NotImplementedException.</returns>
- /// <exception cref="NotImplementedException">Method is not implemented.</exception>
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
- /// </summary>
- /// <returns>Throws NotImplementedException.</returns>
- /// <exception cref="NotImplementedException">Method is not implemented.</exception>
- IEnumerator IEnumerable.GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly void CheckRead()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
- #endif
- }
- }
-
- [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly void CheckRead()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
- #endif
- }
-
- [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- void CheckWrite()
- {
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety);
- #endif
- }
- }
-
- sealed internal unsafe class NativeHashSetDebuggerTypeProxy<T>
- where T : unmanaged, IEquatable<T>
- {
- HashMapHelper<T>* Data;
-
- public NativeHashSetDebuggerTypeProxy(NativeHashSet<T> data)
- {
- Data = data.m_Data;
- }
-
- public List<T> Items
- {
- get
- {
- if (Data == null)
- {
- return default;
- }
-
- var result = new List<T>();
- using (var items = Data->GetKeyArray(Allocator.Temp))
- {
- for (var k = 0; k < items.Length; ++k)
- {
- result.Add(items[k]);
- }
- }
-
- return result;
- }
- }
- }
- }
|