using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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<>))]
[BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
public unsafe struct UnsafeParallelHashSet
: INativeDisposable
, IEnumerable // Used by collection initializers.
where T : unmanaged, IEquatable
{
internal UnsafeParallelHashMap m_Data;
///
/// Initializes and returns an instance of UnsafeHashSet.
///
/// The number of values that should fit in the initial allocation.
/// The allocator to use.
public UnsafeParallelHashSet(int capacity, AllocatorManager.AllocatorHandle allocator)
{
m_Data = new UnsafeParallelHashMap(capacity, allocator);
}
///
/// Whether this set is empty.
///
/// True if this set is empty.
public bool IsEmpty => m_Data.IsEmpty;
///
/// Returns the current number of values in this set.
///
/// The current number of values in this set.
public int Count() => 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.
/// Thrown if `value` is less than the current capacity.
public int Capacity { get => m_Data.Capacity; set => m_Data.Capacity = value; }
///
/// Whether this set has been allocated (and not yet deallocated).
///
/// True if this set has been allocated (and not yet deallocated).
public bool IsCreated => m_Data.IsCreated;
///
/// Releases all resources (memory).
///
public void Dispose() => 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.
[NotBurstCompatible /* This is not burst compatible because of IJob's use of a static IntPtr. Should switch to IJobBurstSchedulable in the future */]
public JobHandle Dispose(JobHandle inputDeps) => m_Data.Dispose(inputDeps);
///
/// 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) => m_Data.TryAdd(item, false);
///
/// Removes a particular value.
///
/// The value to remove.
/// True if the value was present.
public bool Remove(T item) => m_Data.Remove(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) => m_Data.ContainsKey(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 NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) => m_Data.GetKeyArray(allocator);
///
/// Returns a parallel writer.
///
/// A parallel writer.
public ParallelWriter AsParallelWriter()
{
return new ParallelWriter { m_Data = m_Data.AsParallelWriter() };
}
///
/// A parallel writer for an UnsafeHashSet.
///
///
/// Use to create a parallel writer for a set.
///
[NativeContainerIsAtomicWriteOnly]
[BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
public struct ParallelWriter
{
internal UnsafeParallelHashMap.ParallelWriter m_Data;
///
/// The number of values that fit in the current allocation.
///
/// The number of values that fit in the current allocation.
public int Capacity => m_Data.Capacity;
///
/// Adds a new value (unless it is already present).
///
/// The value to add.
/// True if the value is not already present.
public bool Add(T item) => m_Data.TryAdd(item, false);
}
///
/// Returns an enumerator over the values of this set.
///
/// An enumerator over the values of this set.
public Enumerator GetEnumerator()
{
return new Enumerator { m_Enumerator = new UnsafeParallelHashMapDataEnumerator(m_Data.m_Buffer) };
}
///
/// 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 UnsafeParallelHashMapDataEnumerator m_Enumerator;
///
/// Does nothing.
///
public void Dispose() { }
///
/// Advances the enumerator to the next value.
///
/// True if `Current` is valid to read after the call.
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 => m_Enumerator.GetCurrentKey();
object IEnumerator.Current => Current;
}
}
sealed internal class UnsafeHashSetDebuggerTypeProxy
where T : unmanaged, IEquatable
{
#if !NET_DOTS
UnsafeParallelHashSet Data;
public UnsafeHashSetDebuggerTypeProxy(UnsafeParallelHashSet data)
{
Data = data;
}
public List Items
{
get
{
var result = new List();
using (var item = Data.ToNativeArray(Allocator.Temp))
{
for (var k = 0; k < item.Length; ++k)
{
result.Add(item[k]);
}
}
return result;
}
}
#endif
}
}