using System; using Unity.Collections.LowLevel.Unsafe; namespace Unity.Collections { /// /// Provides extension methods for hash maps. /// [BurstCompatible] public static class NativeParallelHashMapExtensions { /// /// Removes duplicate values from this sorted array and returns the number of values remaining. /// /// /// Uses `Equals` to determine whether values are duplicates. /// /// Expects the array to already be sorted. /// /// The remaining elements will be tightly packed at the front of the array. /// /// The type of values in the array. /// The array from which to remove duplicates. /// The number of unique elements in this array. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public static int Unique(this NativeArray array) where T : struct, IEquatable { if (array.Length == 0) { return 0; } int first = 0; int last = array.Length; var result = first; while (++first != last) { if (!array[result].Equals(array[first])) { array[++result] = array[first]; } } return ++result; } #if !NET_DOTS // Tuple is not supported by TinyBCL /// /// Returns an array populated with the unique keys from this multi hash map. /// /// The type of the keys. /// The type of the values. /// The multi hash map. /// The allocator to use. /// An array populated with the unique keys from this multi hash map. [BurstCompatible(GenericTypeArguments = new[] { typeof(int), typeof(int) })] public static (NativeArray, int) GetUniqueKeyArray(this UnsafeParallelMultiHashMap container, AllocatorManager.AllocatorHandle allocator) where TKey : struct, IEquatable, IComparable where TValue : struct { var result = container.GetKeyArray(allocator); result.Sort(); int uniques = result.Unique(); return (result, uniques); } /// /// Returns an array populated with the unique keys from this multi hash map. /// /// The type of the keys. /// The type of the values. /// The multi hash map. /// The allocator to use. /// An array populated with the unique keys from this multi hash map. [BurstCompatible(GenericTypeArguments = new[] { typeof(int), typeof(int) })] public static (NativeArray, int) GetUniqueKeyArray(this NativeParallelMultiHashMap container, AllocatorManager.AllocatorHandle allocator) where TKey : struct, IEquatable, IComparable where TValue : struct { var result = container.GetKeyArray(allocator); result.Sort(); int uniques = result.Unique(); return (result, uniques); } #endif /// /// Returns a "bucket" view of this hash map. /// /// /// Internally, the elements of a hash map are split into buckets of type . /// /// With buckets, a job can safely access the elements of a hash map concurrently as long as each individual bucket is accessed /// only from an individual thread. Effectively, it is not safe to read elements of an individual bucket concurrently, /// but it is safe to read elements of separate buckets concurrently. /// /// The type of the keys. /// The type of the values. /// The hash map. /// A "bucket" view of this hash map. [Obsolete("GetBucketData is deprecated, please use GetUnsafeBucketData instead. (RemovedAfter 2021-07-08) (UnityUpgradable) -> GetUnsafeBucketData(*)", false)] [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static unsafe UnsafeHashMapBucketData GetBucketData(this NativeParallelHashMap container) where TKey : struct, IEquatable where TValue : struct { return container.m_HashMapData.m_Buffer->GetBucketData(); } /// /// Returns a "bucket" view of this hash map. /// /// /// Internally, the elements of a hash map are split into buckets of type . /// /// With buckets, a job can safely access the elements of a hash map concurrently as long as each individual bucket is accessed /// only from an individual thread. Effectively, it is not safe to read elements of an individual bucket concurrently, /// but it is safe to read elements of separate buckets concurrently. /// /// The type of the keys. /// The type of the values. /// The hash map. /// A "bucket" view of this hash map. [BurstCompatible(GenericTypeArguments = new[] { typeof(int), typeof(int) })] public static unsafe UnsafeHashMapBucketData GetUnsafeBucketData(this NativeParallelHashMap container) where TKey : struct, IEquatable where TValue : struct { return container.m_HashMapData.m_Buffer->GetBucketData(); } /// /// Returns a "bucket" view of this multi hash map. /// /// /// Internally, the elements of a hash map are split into buckets of type . /// /// With buckets, a job can safely access the elements of a hash map concurrently as long as each individual bucket is accessed /// only from an individual thread. Effectively, it is not safe to read elements of an individual bucket concurrently, /// but it is safe to read elements of separate buckets concurrently. /// /// The type of the keys. /// The type of the values. /// The multi hash map. /// A "bucket" view of this multi hash map. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static unsafe UnsafeHashMapBucketData GetUnsafeBucketData(this NativeParallelMultiHashMap container) where TKey : struct, IEquatable where TValue : struct { return container.m_MultiHashMapData.m_Buffer->GetBucketData(); } /// /// Removes all occurrences of a particular key-value pair. /// /// Removes all key-value pairs which have a particular key and which *also have* a particular value. /// In other words: (key *AND* value) rather than (key *OR* value). /// The type of the keys. /// The type of the values. /// The multi hash map. /// The key of the key-value pairs to remove. /// The value of the key-value pairs to remove. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static void Remove(this NativeParallelMultiHashMap container, TKey key, TValue value) where TKey : struct, IEquatable where TValue : struct, IEquatable { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(container.m_Safety); #endif container.m_MultiHashMapData.Remove(key, value); } } }