using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Unity.Jobs; namespace Unity.Collections.LowLevel.Unsafe { /// /// An unordered, expandable set of unique values. /// /// The type of the values. [StructLayout(LayoutKind.Sequential)] [DebuggerTypeProxy(typeof(UnsafeHashSetDebuggerTypeProxy<>))] [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(int) })] public unsafe struct UnsafeHashSet : INativeDisposable , IEnumerable // Used by collection initializers. where T : unmanaged, IEquatable { internal HashMapHelper m_Data; /// /// Initializes and returns an instance of NativeParallelHashSet. /// /// The number of values that should fit in the initial allocation. /// The allocator to use. public UnsafeHashSet(int initialCapacity, AllocatorManager.AllocatorHandle allocator) { m_Data = default; m_Data.Init(initialCapacity, 0, HashMapHelper.kMinimumCapacity, allocator); } /// /// Whether this set is empty. /// /// True if this set is empty or if the set has not been constructed. public readonly bool IsEmpty { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => !IsCreated || m_Data.IsEmpty; } /// /// Returns the current number of values in this set. /// /// The current number of values in this set. public readonly int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Data.Count; } /// /// The number of values that fit in the current allocation. /// /// The number of values that fit in the current allocation. /// A new capacity. Must be larger than current capacity. public int Capacity { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get => m_Data.Capacity; set => m_Data.Resize(value); } /// /// Whether this set has been allocated (and not yet deallocated). /// /// True if this set has been allocated (and not yet deallocated). public readonly bool IsCreated { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Data.IsCreated; } /// /// Releases all resources (memory and safety handles). /// public void Dispose() { if (!IsCreated) { return; } m_Data.Dispose(); } /// /// Creates and schedules a job that will dispose this set. /// /// A job handle. The newly scheduled job will depend upon this handle. /// The handle of a new job that will dispose this set. public JobHandle Dispose(JobHandle inputDeps) { if (!IsCreated) { return inputDeps; } var jobHandle = new UnsafeDisposeJob { Ptr = m_Data.Ptr, Allocator = m_Data.Allocator }.Schedule(inputDeps); m_Data.Ptr = null; return jobHandle; } /// /// Removes all values. /// /// Does not change the capacity. public void Clear() { m_Data.Clear(); } /// /// Adds a new value (unless it is already present). /// /// The value to add. /// True if the value was not already present. public bool Add(T item) { return -1 != m_Data.TryAdd(item); } /// /// Removes a particular value. /// /// The value to remove. /// True if the value was present. public bool Remove(T item) { return -1 != m_Data.TryRemove(item); } /// /// Returns true if a particular value is present. /// /// The value to check for. /// True if the value was present. public bool Contains(T item) { return -1 != m_Data.Find(item); } /// /// Sets the capacity to match what it would be if it had been originally initialized with all its entries. /// public void TrimExcess() => m_Data.TrimExcess(); /// /// Returns an array with a copy of this set's values (in no particular order). /// /// The allocator to use. /// An array with a copy of the set's values. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { return m_Data.GetKeyArray(allocator); } /// /// Returns an enumerator over the values of this set. /// /// An enumerator over the values of this set. public Enumerator GetEnumerator() { fixed (HashMapHelper* data = &m_Data) { return new Enumerator { m_Enumerator = new HashMapHelper.Enumerator(data) }; } } /// /// This method is not implemented. Use instead. /// /// Throws NotImplementedException. /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Throws NotImplementedException. /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// An enumerator over the values of a set. /// /// /// In an enumerator's initial state, is invalid. /// The first call advances the enumerator to the first value. /// public struct Enumerator : IEnumerator { internal HashMapHelper.Enumerator m_Enumerator; /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next value. /// /// True if `Current` is valid to read after the call. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() => m_Enumerator.MoveNext(); /// /// Resets the enumerator to its initial state. /// public void Reset() => m_Enumerator.Reset(); /// /// The current value. /// /// The current value. public T Current { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Enumerator.m_Data->Keys[m_Enumerator.m_Index]; } /// /// Gets the element at the current position of the enumerator in the container. /// object IEnumerator.Current => Current; } /// /// Returns a readonly version of this UnsafeHashMap instance. /// /// ReadOnly containers point to the same underlying data as the UnsafeHashMap it is made from. /// ReadOnly instance for this. public ReadOnly AsReadOnly() { return new ReadOnly(ref m_Data); } /// /// A read-only alias for the value of a UnsafeHashSet. Does not have its own allocated storage. /// [GenerateTestsForBurstCompatibility(GenericTypeArguments = new[] { typeof(int) })] public struct ReadOnly : IEnumerable { internal HashMapHelper m_Data; internal ReadOnly(ref HashMapHelper data) { m_Data = data; } /// /// Whether this hash map has been allocated (and not yet deallocated). /// /// True if this hash map has been allocated (and not yet deallocated). public readonly bool IsCreated { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Data.IsCreated; } /// /// Whether this hash set is empty. /// /// True if this hash set is empty or if the set has not been constructed. public readonly bool IsEmpty { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Data.IsEmpty; } /// /// The current number of key-value pairs in this hash map. /// /// The current number of key-value pairs in this hash map. public readonly int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Data.Count; } /// /// The number of key-value pairs that fit in the current allocation. /// /// The number of key-value pairs that fit in the current allocation. public readonly int Capacity { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_Data.Capacity; } /// /// Returns true if a particular value is present. /// /// The item to look up. /// True if the item was present. public readonly bool Contains(T item) { return -1 != m_Data.Find(item); } /// /// Returns an array with a copy of this set's values (in no particular order). /// /// The allocator to use. /// An array with a copy of the set's values. public readonly NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { return m_Data.GetKeyArray(allocator); } /// /// Returns an enumerator over the key-value pairs of this hash map. /// /// An enumerator over the key-value pairs of this hash map. public readonly Enumerator GetEnumerator() { fixed (HashMapHelper* data = &m_Data) { return new Enumerator { m_Enumerator = new HashMapHelper.Enumerator(data) }; } } /// /// This method is not implemented. Use instead. /// /// Throws NotImplementedException. /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Throws NotImplementedException. /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } } sealed internal class UnsafeHashSetDebuggerTypeProxy where T : unmanaged, IEquatable { HashMapHelper Data; public UnsafeHashSetDebuggerTypeProxy(UnsafeHashSet data) { Data = data.m_Data; } public List Items { get { var result = new List(); using (var keys = Data.GetKeyArray(Allocator.Temp)) { for (var k = 0; k < keys.Length; ++k) { result.Add(keys[k]); } } return result; } } } }