using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using Unity.Burst; using Unity.Mathematics; using Unity.Jobs; using Unity.Jobs.LowLevel.Unsafe; using UnityEngine.Assertions; namespace Unity.Collections.LowLevel.Unsafe { /// /// A bucket of key-value pairs. Used as the internal storage for hash maps. /// /// Exposed publicly only for advanced use cases. [BurstCompatible] public unsafe struct UnsafeHashMapBucketData { internal UnsafeHashMapBucketData(byte* v, byte* k, byte* n, byte* b, int bcm) { values = v; keys = k; next = n; buckets = b; bucketCapacityMask = bcm; } /// /// The buffer of values. /// /// The buffer of values. public readonly byte* values; /// /// The buffer of keys. /// /// The buffer of keys. public readonly byte* keys; /// /// The next bucket in the chain. /// /// The next bucket in the chain. public readonly byte* next; /// /// The first bucket in the chain. /// /// The first bucket in the chain. public readonly byte* buckets; /// /// One less than the bucket capacity. /// /// One less than the bucket capacity. public readonly int bucketCapacityMask; } [StructLayout(LayoutKind.Explicit)] [BurstCompatible] internal unsafe struct UnsafeParallelHashMapData { [FieldOffset(0)] internal byte* values; // 4-byte padding on 32-bit architectures here [FieldOffset(8)] internal byte* keys; // 4-byte padding on 32-bit architectures here [FieldOffset(16)] internal byte* next; // 4-byte padding on 32-bit architectures here [FieldOffset(24)] internal byte* buckets; // 4-byte padding on 32-bit architectures here [FieldOffset(32)] internal int keyCapacity; [FieldOffset(36)] internal int bucketCapacityMask; // = bucket capacity - 1 [FieldOffset(40)] internal int allocatedIndexLength; [FieldOffset(JobsUtility.CacheLineSize < 64 ? 64 : JobsUtility.CacheLineSize)] internal fixed int firstFreeTLS[JobsUtility.MaxJobThreadCount * IntsPerCacheLine]; // 64 is the cache line size on x86, arm usually has 32 - so it is possible to save some memory there internal const int IntsPerCacheLine = JobsUtility.CacheLineSize / sizeof(int); internal static int GetBucketSize(int capacity) { return capacity * 2; } internal static int GrowCapacity(int capacity) { if (capacity == 0) { return 1; } return capacity * 2; } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal static void AllocateHashMap(int length, int bucketLength, AllocatorManager.AllocatorHandle label, out UnsafeParallelHashMapData* outBuf) where TKey : struct where TValue : struct { CollectionHelper.CheckIsUnmanaged(); CollectionHelper.CheckIsUnmanaged(); UnsafeParallelHashMapData* data = (UnsafeParallelHashMapData*)Memory.Unmanaged.Allocate(sizeof(UnsafeParallelHashMapData), UnsafeUtility.AlignOf(), label); bucketLength = math.ceilpow2(bucketLength); data->keyCapacity = length; data->bucketCapacityMask = bucketLength - 1; int keyOffset, nextOffset, bucketOffset; int totalSize = CalculateDataSize(length, bucketLength, out keyOffset, out nextOffset, out bucketOffset); data->values = (byte*)Memory.Unmanaged.Allocate(totalSize, JobsUtility.CacheLineSize, label); data->keys = data->values + keyOffset; data->next = data->values + nextOffset; data->buckets = data->values + bucketOffset; outBuf = data; } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal static void ReallocateHashMap(UnsafeParallelHashMapData* data, int newCapacity, int newBucketCapacity, AllocatorManager.AllocatorHandle label) where TKey : struct where TValue : struct { newBucketCapacity = math.ceilpow2(newBucketCapacity); if (data->keyCapacity == newCapacity && (data->bucketCapacityMask + 1) == newBucketCapacity) { return; } CheckHashMapReallocateDoesNotShrink(data, newCapacity); int keyOffset, nextOffset, bucketOffset; int totalSize = CalculateDataSize(newCapacity, newBucketCapacity, out keyOffset, out nextOffset, out bucketOffset); byte* newData = (byte*)Memory.Unmanaged.Allocate(totalSize, JobsUtility.CacheLineSize, label); byte* newKeys = newData + keyOffset; byte* newNext = newData + nextOffset; byte* newBuckets = newData + bucketOffset; // The items are taken from a free-list and might not be tightly packed, copy all of the old capcity UnsafeUtility.MemCpy(newData, data->values, data->keyCapacity * UnsafeUtility.SizeOf()); UnsafeUtility.MemCpy(newKeys, data->keys, data->keyCapacity * UnsafeUtility.SizeOf()); UnsafeUtility.MemCpy(newNext, data->next, data->keyCapacity * UnsafeUtility.SizeOf()); for (int emptyNext = data->keyCapacity; emptyNext < newCapacity; ++emptyNext) { ((int*)newNext)[emptyNext] = -1; } // re-hash the buckets, first clear the new bucket list, then insert all values from the old list for (int bucket = 0; bucket < newBucketCapacity; ++bucket) { ((int*)newBuckets)[bucket] = -1; } for (int bucket = 0; bucket <= data->bucketCapacityMask; ++bucket) { int* buckets = (int*)data->buckets; int* nextPtrs = (int*)newNext; while (buckets[bucket] >= 0) { int curEntry = buckets[bucket]; buckets[bucket] = nextPtrs[curEntry]; int newBucket = UnsafeUtility.ReadArrayElement(data->keys, curEntry).GetHashCode() & (newBucketCapacity - 1); nextPtrs[curEntry] = ((int*)newBuckets)[newBucket]; ((int*)newBuckets)[newBucket] = curEntry; } } Memory.Unmanaged.Free(data->values, label); if (data->allocatedIndexLength > data->keyCapacity) { data->allocatedIndexLength = data->keyCapacity; } data->values = newData; data->keys = newKeys; data->next = newNext; data->buckets = newBuckets; data->keyCapacity = newCapacity; data->bucketCapacityMask = newBucketCapacity - 1; } internal static void DeallocateHashMap(UnsafeParallelHashMapData* data, AllocatorManager.AllocatorHandle allocator) { Memory.Unmanaged.Free(data->values, allocator); Memory.Unmanaged.Free(data, allocator); } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal static int CalculateDataSize(int length, int bucketLength, out int keyOffset, out int nextOffset, out int bucketOffset) where TKey : struct where TValue : struct { var sizeOfTValue = UnsafeUtility.SizeOf(); var sizeOfTKey = UnsafeUtility.SizeOf(); var sizeOfInt = UnsafeUtility.SizeOf(); var valuesSize = CollectionHelper.Align(sizeOfTValue * length, JobsUtility.CacheLineSize); var keysSize = CollectionHelper.Align(sizeOfTKey * length, JobsUtility.CacheLineSize); var nextSize = CollectionHelper.Align(sizeOfInt * length, JobsUtility.CacheLineSize); var bucketSize = CollectionHelper.Align(sizeOfInt * bucketLength, JobsUtility.CacheLineSize); var totalSize = valuesSize + keysSize + nextSize + bucketSize; keyOffset = 0 + valuesSize; nextOffset = keyOffset + keysSize; bucketOffset = nextOffset + nextSize; return totalSize; } internal static bool IsEmpty(UnsafeParallelHashMapData* data) { if (data->allocatedIndexLength <= 0) { return true; } var bucketArray = (int*)data->buckets; var bucketNext = (int*)data->next; var capacityMask = data->bucketCapacityMask; for (int i = 0; i <= capacityMask; ++i) { int bucket = bucketArray[i]; if (bucket != -1) { return false; } } return true; } internal static int GetCount(UnsafeParallelHashMapData* data) { if (data->allocatedIndexLength <= 0) { return 0; } var bucketNext = (int*)data->next; var freeListSize = 0; for (int tls = 0; tls < JobsUtility.MaxJobThreadCount; ++tls) { for (var freeIdx = data->firstFreeTLS[tls * IntsPerCacheLine] ; freeIdx >= 0 ; freeIdx = bucketNext[freeIdx] ) { ++freeListSize; } } return math.min(data->keyCapacity, data->allocatedIndexLength) - freeListSize; } internal static bool MoveNext(UnsafeParallelHashMapData* data, ref int bucketIndex, ref int nextIndex, out int index) { var bucketArray = (int*)data->buckets; var bucketNext = (int*)data->next; var capacityMask = data->bucketCapacityMask; if (nextIndex != -1) { index = nextIndex; nextIndex = bucketNext[nextIndex]; return true; } for (int i = bucketIndex; i <= capacityMask; ++i) { var idx = bucketArray[i]; if (idx != -1) { index = idx; bucketIndex = i + 1; nextIndex = bucketNext[idx]; return true; } } index = -1; bucketIndex = capacityMask + 1; nextIndex = -1; return false; } [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] internal static void GetKeyArray(UnsafeParallelHashMapData* data, NativeArray result) where TKey : struct { var bucketArray = (int*)data->buckets; var bucketNext = (int*)data->next; for (int i = 0, count = 0, max = result.Length; i <= data->bucketCapacityMask && count < max; ++i) { int bucket = bucketArray[i]; while (bucket != -1) { result[count++] = UnsafeUtility.ReadArrayElement(data->keys, bucket); bucket = bucketNext[bucket]; } } } [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] internal static void GetValueArray(UnsafeParallelHashMapData* data, NativeArray result) where TValue : struct { var bucketArray = (int*)data->buckets; var bucketNext = (int*)data->next; for (int i = 0, count = 0, max = result.Length, capacityMask = data->bucketCapacityMask ; i <= capacityMask && count < max ; ++i ) { int bucket = bucketArray[i]; while (bucket != -1) { result[count++] = UnsafeUtility.ReadArrayElement(data->values, bucket); bucket = bucketNext[bucket]; } } } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal static void GetKeyValueArrays(UnsafeParallelHashMapData* data, NativeKeyValueArrays result) where TKey : struct where TValue : struct { var bucketArray = (int*)data->buckets; var bucketNext = (int*)data->next; for (int i = 0, count = 0, max = result.Length, capacityMask = data->bucketCapacityMask ; i <= capacityMask && count < max ; ++i ) { int bucket = bucketArray[i]; while (bucket != -1) { result.Keys[count] = UnsafeUtility.ReadArrayElement(data->keys, bucket); result.Values[count] = UnsafeUtility.ReadArrayElement(data->values, bucket); count++; bucket = bucketNext[bucket]; } } } internal UnsafeHashMapBucketData GetBucketData() { return new UnsafeHashMapBucketData(values, keys, next, buckets, bucketCapacityMask); } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] static void CheckHashMapReallocateDoesNotShrink(UnsafeParallelHashMapData* data, int newCapacity) { if (data->keyCapacity > newCapacity) throw new Exception("Shrinking a hash map is not supported"); } } [NativeContainer] [BurstCompatible] internal unsafe struct UnsafeParallelHashMapDataDispose { [NativeDisableUnsafePtrRestriction] internal UnsafeParallelHashMapData* m_Buffer; internal AllocatorManager.AllocatorHandle m_AllocatorLabel; #if ENABLE_UNITY_COLLECTIONS_CHECKS internal AtomicSafetyHandle m_Safety; #endif public void Dispose() { UnsafeParallelHashMapData.DeallocateHashMap(m_Buffer, m_AllocatorLabel); } } [BurstCompile] internal unsafe struct UnsafeParallelHashMapDataDisposeJob : IJob { internal UnsafeParallelHashMapDataDispose Data; public void Execute() { Data.Dispose(); } } [StructLayout(LayoutKind.Sequential)] [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal struct UnsafeParallelHashMapBase where TKey : struct, IEquatable where TValue : struct { internal static unsafe void Clear(UnsafeParallelHashMapData* data) { UnsafeUtility.MemSet(data->buckets, 0xff, (data->bucketCapacityMask + 1) * 4); UnsafeUtility.MemSet(data->next, 0xff, (data->keyCapacity) * 4); for (int tls = 0; tls < JobsUtility.MaxJobThreadCount; ++tls) { data->firstFreeTLS[tls * UnsafeParallelHashMapData.IntsPerCacheLine] = -1; } data->allocatedIndexLength = 0; } internal static unsafe int AllocEntry(UnsafeParallelHashMapData* data, int threadIndex) { int idx; int* nextPtrs = (int*)data->next; do { idx = data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine]; // Check if this thread has a free entry. Negative value means there is nothing free. if (idx < 0) { // Try to refill local cache. The local cache is a linked list of 16 free entries. // Indicate to other threads that we are refilling the cache. // -2 means refilling cache. // -1 means nothing free on this thread. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine], -2); // If it failed try to get one from the never-allocated array if (data->allocatedIndexLength < data->keyCapacity) { idx = Interlocked.Add(ref data->allocatedIndexLength, 16) - 16; if (idx < data->keyCapacity - 1) { int count = math.min(16, data->keyCapacity - idx); // Set up a linked list of free entries. for (int i = 1; i < count; ++i) { nextPtrs[idx + i] = idx + i + 1; } // Last entry points to null. nextPtrs[idx + count - 1] = -1; // The first entry is going to be allocated to someone so it also points to null. nextPtrs[idx] = -1; // Set the TLS first free to the head of the list, which is the one after the entry we are returning. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine], idx + 1); return idx; } if (idx == data->keyCapacity - 1) { // We tried to allocate more entries for this thread but we've already hit the key capacity, // so we are in fact out of space. Record that this thread has no more entries. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine], -1); return idx; } } // If we reach here, then we couldn't allocate more entries for this thread, so it's completely empty. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine], -1); // Failed to get any, try to get one from another free list bool again = true; while (again) { again = false; for (int other = (threadIndex + 1) % JobsUtility.MaxJobThreadCount ; other != threadIndex ; other = (other + 1) % JobsUtility.MaxJobThreadCount ) { // Attempt to grab a free entry from another thread and switch the other thread's free head // atomically. do { idx = data->firstFreeTLS[other * UnsafeParallelHashMapData.IntsPerCacheLine]; if (idx < 0) { break; } } while (Interlocked.CompareExchange( ref data->firstFreeTLS[other * UnsafeParallelHashMapData.IntsPerCacheLine] , nextPtrs[idx] , idx ) != idx ); if (idx == -2) { // If the thread was refilling the cache, then try again. again = true; } else if (idx >= 0) { // We succeeded in getting an entry from another thread so remove this entry from the // linked list. nextPtrs[idx] = -1; return idx; } } } ThrowFull(); } CheckOutOfCapacity(idx, data->keyCapacity); } while (Interlocked.CompareExchange( ref data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine] , nextPtrs[idx] , idx ) != idx ); nextPtrs[idx] = -1; return idx; } internal static unsafe void FreeEntry(UnsafeParallelHashMapData* data, int idx, int threadIndex) { int* nextPtrs = (int*)data->next; int next = -1; do { next = data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine]; nextPtrs[idx] = next; } while (Interlocked.CompareExchange( ref data->firstFreeTLS[threadIndex * UnsafeParallelHashMapData.IntsPerCacheLine] , idx , next ) != next ); } internal static unsafe bool TryAddAtomic(UnsafeParallelHashMapData* data, TKey key, TValue item, int threadIndex) { TValue tempItem; NativeParallelMultiHashMapIterator tempIt; if (TryGetFirstValueAtomic(data, key, out tempItem, out tempIt)) { return false; } // Allocate an entry from the free list int idx = AllocEntry(data, threadIndex); // Write the new value to the entry UnsafeUtility.WriteArrayElement(data->keys, idx, key); UnsafeUtility.WriteArrayElement(data->values, idx, item); int bucket = key.GetHashCode() & data->bucketCapacityMask; // Add the index to the hash-map int* buckets = (int*)data->buckets; // Make the bucket's head idx. If the exchange returns something other than -1, then the bucket had // a non-null head which means we need to do more checks... if (Interlocked.CompareExchange(ref buckets[bucket], idx, -1) != -1) { int* nextPtrs = (int*)data->next; int next = -1; do { // Link up this entry with the rest of the bucket under the assumption that this key // doesn't already exist in the bucket. This assumption could be wrong, which will be // checked later. next = buckets[bucket]; nextPtrs[idx] = next; // If the key already exists then we should free the entry we took earlier. if (TryGetFirstValueAtomic(data, key, out tempItem, out tempIt)) { // Put back the entry in the free list if someone else added it while trying to add FreeEntry(data, idx, threadIndex); return false; } } while (Interlocked.CompareExchange(ref buckets[bucket], idx, next) != next); } return true; } internal static unsafe void AddAtomicMulti(UnsafeParallelHashMapData* data, TKey key, TValue item, int threadIndex) { // Allocate an entry from the free list int idx = AllocEntry(data, threadIndex); // Write the new value to the entry UnsafeUtility.WriteArrayElement(data->keys, idx, key); UnsafeUtility.WriteArrayElement(data->values, idx, item); int bucket = key.GetHashCode() & data->bucketCapacityMask; // Add the index to the hash-map int* buckets = (int*)data->buckets; int nextPtr; int* nextPtrs = (int*)data->next; do { nextPtr = buckets[bucket]; nextPtrs[idx] = nextPtr; } while (Interlocked.CompareExchange(ref buckets[bucket], idx, nextPtr) != nextPtr); } internal static unsafe bool TryAdd(UnsafeParallelHashMapData* data, TKey key, TValue item, bool isMultiHashMap, AllocatorManager.AllocatorHandle allocation) { TValue tempItem; NativeParallelMultiHashMapIterator tempIt; if (!isMultiHashMap && TryGetFirstValueAtomic(data, key, out tempItem, out tempIt)) { return false; } // Allocate an entry from the free list int idx; int* nextPtrs; if (data->allocatedIndexLength >= data->keyCapacity && data->firstFreeTLS[0] < 0) { for (int tls = 1; tls < JobsUtility.MaxJobThreadCount; ++tls) { if (data->firstFreeTLS[tls * UnsafeParallelHashMapData.IntsPerCacheLine] >= 0) { idx = data->firstFreeTLS[tls * UnsafeParallelHashMapData.IntsPerCacheLine]; nextPtrs = (int*)data->next; data->firstFreeTLS[tls * UnsafeParallelHashMapData.IntsPerCacheLine] = nextPtrs[idx]; nextPtrs[idx] = -1; data->firstFreeTLS[0] = idx; break; } } if (data->firstFreeTLS[0] < 0) { int newCap = UnsafeParallelHashMapData.GrowCapacity(data->keyCapacity); UnsafeParallelHashMapData.ReallocateHashMap(data, newCap, UnsafeParallelHashMapData.GetBucketSize(newCap), allocation); } } idx = data->firstFreeTLS[0]; if (idx >= 0) { data->firstFreeTLS[0] = ((int*)data->next)[idx]; } else { idx = data->allocatedIndexLength++; } CheckIndexOutOfBounds(data, idx); // Write the new value to the entry UnsafeUtility.WriteArrayElement(data->keys, idx, key); UnsafeUtility.WriteArrayElement(data->values, idx, item); int bucket = key.GetHashCode() & data->bucketCapacityMask; // Add the index to the hash-map int* buckets = (int*)data->buckets; nextPtrs = (int*)data->next; nextPtrs[idx] = buckets[bucket]; buckets[bucket] = idx; return true; } internal static unsafe int Remove(UnsafeParallelHashMapData* data, TKey key, bool isMultiHashMap) { if (data->keyCapacity == 0) { return 0; } var removed = 0; // First find the slot based on the hash var buckets = (int*)data->buckets; var nextPtrs = (int*)data->next; var bucket = key.GetHashCode() & data->bucketCapacityMask; var prevEntry = -1; var entryIdx = buckets[bucket]; while (entryIdx >= 0 && entryIdx < data->keyCapacity) { if (UnsafeUtility.ReadArrayElement(data->keys, entryIdx).Equals(key)) { ++removed; // Found matching element, remove it if (prevEntry < 0) { buckets[bucket] = nextPtrs[entryIdx]; } else { nextPtrs[prevEntry] = nextPtrs[entryIdx]; } // And free the index int nextIdx = nextPtrs[entryIdx]; nextPtrs[entryIdx] = data->firstFreeTLS[0]; data->firstFreeTLS[0] = entryIdx; entryIdx = nextIdx; // Can only be one hit in regular hashmaps, so return if (!isMultiHashMap) { break; } } else { prevEntry = entryIdx; entryIdx = nextPtrs[entryIdx]; } } return removed; } internal static unsafe void Remove(UnsafeParallelHashMapData* data, NativeParallelMultiHashMapIterator it) { // First find the slot based on the hash int* buckets = (int*)data->buckets; int* nextPtrs = (int*)data->next; int bucket = it.key.GetHashCode() & data->bucketCapacityMask; int entryIdx = buckets[bucket]; if (entryIdx == it.EntryIndex) { buckets[bucket] = nextPtrs[entryIdx]; } else { while (entryIdx >= 0 && nextPtrs[entryIdx] != it.EntryIndex) { entryIdx = nextPtrs[entryIdx]; } if (entryIdx < 0) { ThrowInvalidIterator(); } nextPtrs[entryIdx] = nextPtrs[it.EntryIndex]; } // And free the index nextPtrs[it.EntryIndex] = data->firstFreeTLS[0]; data->firstFreeTLS[0] = it.EntryIndex; } [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] internal static unsafe void RemoveKeyValue(UnsafeParallelHashMapData* data, TKey key, TValueEQ value) where TValueEQ : struct, IEquatable { if (data->keyCapacity == 0) { return; } var buckets = (int*)data->buckets; var keyCapacity = (uint)data->keyCapacity; var prevNextPtr = buckets + (key.GetHashCode() & data->bucketCapacityMask); var entryIdx = *prevNextPtr; if ((uint)entryIdx >= keyCapacity) { return; } var nextPtrs = (int*)data->next; var keys = data->keys; var values = data->values; var firstFreeTLS = data->firstFreeTLS; do { if (UnsafeUtility.ReadArrayElement(keys, entryIdx).Equals(key) && UnsafeUtility.ReadArrayElement(values, entryIdx).Equals(value)) { int nextIdx = nextPtrs[entryIdx]; nextPtrs[entryIdx] = firstFreeTLS[0]; firstFreeTLS[0] = entryIdx; *prevNextPtr = entryIdx = nextIdx; } else { prevNextPtr = nextPtrs + entryIdx; entryIdx = *prevNextPtr; } } while ((uint)entryIdx < keyCapacity); } internal static unsafe bool TryGetFirstValueAtomic(UnsafeParallelHashMapData* data, TKey key, out TValue item, out NativeParallelMultiHashMapIterator it) { it.key = key; if (data->allocatedIndexLength <= 0) { it.EntryIndex = it.NextEntryIndex = -1; item = default; return false; } // First find the slot based on the hash int* buckets = (int*)data->buckets; int bucket = key.GetHashCode() & data->bucketCapacityMask; it.EntryIndex = it.NextEntryIndex = buckets[bucket]; return TryGetNextValueAtomic(data, out item, ref it); } internal static unsafe bool TryGetNextValueAtomic(UnsafeParallelHashMapData* data, out TValue item, ref NativeParallelMultiHashMapIterator it) { int entryIdx = it.NextEntryIndex; it.NextEntryIndex = -1; it.EntryIndex = -1; item = default; if (entryIdx < 0 || entryIdx >= data->keyCapacity) { return false; } int* nextPtrs = (int*)data->next; while (!UnsafeUtility.ReadArrayElement(data->keys, entryIdx).Equals(it.key)) { entryIdx = nextPtrs[entryIdx]; if (entryIdx < 0 || entryIdx >= data->keyCapacity) { return false; } } it.NextEntryIndex = nextPtrs[entryIdx]; it.EntryIndex = entryIdx; // Read the value item = UnsafeUtility.ReadArrayElement(data->values, entryIdx); return true; } internal static unsafe bool SetValue(UnsafeParallelHashMapData* data, ref NativeParallelMultiHashMapIterator it, ref TValue item) { int entryIdx = it.EntryIndex; if (entryIdx < 0 || entryIdx >= data->keyCapacity) { return false; } UnsafeUtility.WriteArrayElement(data->values, entryIdx, item); return true; } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] static void CheckOutOfCapacity(int idx, int keyCapacity) { if (idx >= keyCapacity) { throw new InvalidOperationException(string.Format("nextPtr idx {0} beyond capacity {1}", idx, keyCapacity)); } } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] static unsafe void CheckIndexOutOfBounds(UnsafeParallelHashMapData* data, int idx) { if (idx < 0 || idx >= data->keyCapacity) throw new InvalidOperationException("Internal HashMap error"); } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] static void ThrowFull() { throw new InvalidOperationException("HashMap is full"); } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] static void ThrowInvalidIterator() { throw new InvalidOperationException("Invalid iterator passed to HashMap remove"); } } /// /// A key-value pair. /// /// Used for enumerators. /// The type of the keys. /// The type of the values. [DebuggerDisplay("Key = {Key}, Value = {Value}")] [BurstCompatible(GenericTypeArguments = new[] {typeof(int), typeof(int)})] public unsafe struct KeyValue where TKey : struct, IEquatable where TValue : struct { internal UnsafeParallelHashMapData* m_Buffer; internal int m_Index; internal int m_Next; /// /// An invalid KeyValue. /// /// In a hash map enumerator's initial state, its value is Null. public static KeyValue Null => new KeyValue{m_Index = -1}; /// /// The key. /// /// The key. If this KeyValue is Null, returns the default of TKey. public TKey Key { get { if (m_Index != -1) { return UnsafeUtility.ReadArrayElement(m_Buffer->keys, m_Index); } return default; } } /// /// Value of key/value pair. /// public ref TValue Value { get { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (m_Index == -1) throw new ArgumentException("must be valid"); #endif return ref UnsafeUtility.AsRef(m_Buffer->values + UnsafeUtility.SizeOf() * m_Index); } } /// /// Gets the key and the value. /// /// Outputs the key. If this KeyValue is Null, outputs the default of TKey. /// Outputs the value. If this KeyValue is Null, outputs the default of TValue. /// True if the key-value pair is valid. public bool GetKeyValue(out TKey key, out TValue value) { if (m_Index != -1) { key = UnsafeUtility.ReadArrayElement(m_Buffer->keys, m_Index); value = UnsafeUtility.ReadArrayElement(m_Buffer->values, m_Index); return true; } key = default; value = default; return false; } } internal unsafe struct UnsafeParallelHashMapDataEnumerator { [NativeDisableUnsafePtrRestriction] internal UnsafeParallelHashMapData* m_Buffer; internal int m_Index; internal int m_BucketIndex; internal int m_NextIndex; internal unsafe UnsafeParallelHashMapDataEnumerator(UnsafeParallelHashMapData* data) { m_Buffer = data; m_Index = -1; m_BucketIndex = 0; m_NextIndex = -1; } internal bool MoveNext() { return UnsafeParallelHashMapData.MoveNext(m_Buffer, ref m_BucketIndex, ref m_NextIndex, out m_Index); } internal void Reset() { m_Index = -1; m_BucketIndex = 0; m_NextIndex = -1; } internal KeyValue GetCurrent() where TKey : struct, IEquatable where TValue : struct { return new KeyValue { m_Buffer = m_Buffer, m_Index = m_Index }; } internal TKey GetCurrentKey() where TKey : struct, IEquatable { if (m_Index != -1) { return UnsafeUtility.ReadArrayElement(m_Buffer->keys, m_Index); } return default; } } /// /// An unordered, expandable associative array. /// /// The type of the keys. /// The type of the values. [StructLayout(LayoutKind.Sequential)] [DebuggerDisplay("Count = {Count()}, Capacity = {Capacity}, IsCreated = {IsCreated}, IsEmpty = {IsEmpty}")] [DebuggerTypeProxy(typeof(UnsafeParallelHashMapDebuggerTypeProxy<,>))] [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public unsafe struct UnsafeParallelHashMap : INativeDisposable , IEnumerable> // Used by collection initializers. where TKey : struct, IEquatable where TValue : struct { [NativeDisableUnsafePtrRestriction] internal UnsafeParallelHashMapData* m_Buffer; internal AllocatorManager.AllocatorHandle m_AllocatorLabel; /// /// Initializes and returns an instance of UnsafeHashMap. /// /// The number of key-value pairs that should fit in the initial allocation. /// The allocator to use. public UnsafeParallelHashMap(int capacity, AllocatorManager.AllocatorHandle allocator) { CollectionHelper.CheckIsUnmanaged(); CollectionHelper.CheckIsUnmanaged(); m_AllocatorLabel = allocator; // Bucket size if bigger to reduce collisions UnsafeParallelHashMapData.AllocateHashMap(capacity, capacity * 2, allocator, out m_Buffer); Clear(); } /// /// Whether this hash map is empty. /// /// True if this hash map is empty or the hash map has not been constructed. public bool IsEmpty => !IsCreated || UnsafeParallelHashMapData.IsEmpty(m_Buffer); /// /// The current number of key-value pairs in this hash map. /// /// The current number of key-value pairs in this hash map. public int Count() => UnsafeParallelHashMapData.GetCount(m_Buffer); /// /// The number of key-value pairs that fit in the current allocation. /// /// The number of key-value pairs that fit in the current allocation. /// A new capacity. Must be larger than the current capacity. /// Thrown if `value` is less than the current capacity. public int Capacity { get { UnsafeParallelHashMapData* data = m_Buffer; return data->keyCapacity; } set { UnsafeParallelHashMapData* data = m_Buffer; UnsafeParallelHashMapData.ReallocateHashMap(data, value, UnsafeParallelHashMapData.GetBucketSize(value), m_AllocatorLabel); } } /// /// Removes all key-value pairs. /// /// Does not change the capacity. public void Clear() { UnsafeParallelHashMapBase.Clear(m_Buffer); } /// /// Adds a new key-value pair. /// /// If the key is already present, this method returns false without modifying the hash map. /// The key to add. /// The value to add. /// True if the key-value pair was added. public bool TryAdd(TKey key, TValue item) { return UnsafeParallelHashMapBase.TryAdd(m_Buffer, key, item, false, m_AllocatorLabel); } /// /// Adds a new key-value pair. /// /// If the key is already present, this method throws without modifying the hash map. /// The key to add. /// The value to add. /// Thrown if the key was already present. public void Add(TKey key, TValue item) { TryAdd(key, item); } /// /// Removes a key-value pair. /// /// The key to remove. /// True if a key-value pair was removed. public bool Remove(TKey key) { return UnsafeParallelHashMapBase.Remove(m_Buffer, key, false) != 0; } /// /// Returns the value associated with a key. /// /// The key to look up. /// Outputs the value associated with the key. Outputs default if the key was not present. /// True if the key was present. public bool TryGetValue(TKey key, out TValue item) { NativeParallelMultiHashMapIterator tempIt; return UnsafeParallelHashMapBase.TryGetFirstValueAtomic(m_Buffer, key, out item, out tempIt); } /// /// Returns true if a given key is present in this hash map. /// /// The key to look up. /// True if the key was present. public bool ContainsKey(TKey key) { return UnsafeParallelHashMapBase.TryGetFirstValueAtomic(m_Buffer, key, out var tempValue, out var tempIt); } /// /// Gets and sets values by key. /// /// Getting a key that is not present will throw. Setting a key that is not already present will add the key. /// The key to look up. /// The value associated with the key. /// For getting, thrown if the key was not present. public TValue this[TKey key] { get { TValue res; TryGetValue(key, out res); return res; } set { if (UnsafeParallelHashMapBase.TryGetFirstValueAtomic(m_Buffer, key, out var item, out var iterator)) { UnsafeParallelHashMapBase.SetValue(m_Buffer, ref iterator, ref value); } else { UnsafeParallelHashMapBase.TryAdd(m_Buffer, key, value, false, m_AllocatorLabel); } } } /// /// Whether this hash map has been allocated (and not yet deallocated). /// /// True if this hash map has been allocated (and not yet deallocated). public bool IsCreated => m_Buffer != null; /// /// Releases all resources (memory). /// public void Dispose() { UnsafeParallelHashMapData.DeallocateHashMap(m_Buffer, m_AllocatorLabel); m_Buffer = null; } /// /// Creates and schedules a job that will dispose this hash map. /// /// A job handle. The newly scheduled job will depend upon this handle. /// The handle of a new job that will dispose this hash map. [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) { var jobHandle = new UnsafeParallelHashMapDisposeJob { Data = m_Buffer, Allocator = m_AllocatorLabel }.Schedule(inputDeps); m_Buffer = null; return jobHandle; } /// /// Returns an array with a copy of all this hash map's keys (in no particular order). /// /// The allocator to use. /// An array with a copy of all this hash map's keys (in no particular order). public NativeArray GetKeyArray(AllocatorManager.AllocatorHandle allocator) { var result = CollectionHelper.CreateNativeArray(UnsafeParallelHashMapData.GetCount(m_Buffer), allocator, NativeArrayOptions.UninitializedMemory); UnsafeParallelHashMapData.GetKeyArray(m_Buffer, result); return result; } /// /// Returns an array with a copy of all this hash map's values (in no particular order). /// /// The allocator to use. /// An array with a copy of all this hash map's values (in no particular order). public NativeArray GetValueArray(AllocatorManager.AllocatorHandle allocator) { var result = CollectionHelper.CreateNativeArray(UnsafeParallelHashMapData.GetCount(m_Buffer), allocator, NativeArrayOptions.UninitializedMemory); UnsafeParallelHashMapData.GetValueArray(m_Buffer, result); return result; } /// /// Returns a NativeKeyValueArrays with a copy of all this hash map's keys and values. /// /// The key-value pairs are copied in no particular order. For all `i`, `Values[i]` will be the value associated with `Keys[i]`. /// The allocator to use. /// A NativeKeyValueArrays with a copy of all this hash map's keys and values. public NativeKeyValueArrays GetKeyValueArrays(AllocatorManager.AllocatorHandle allocator) { var result = new NativeKeyValueArrays(UnsafeParallelHashMapData.GetCount(m_Buffer), allocator, NativeArrayOptions.UninitializedMemory); UnsafeParallelHashMapData.GetKeyValueArrays(m_Buffer, result); return result; } /// /// Returns a parallel writer for this hash map. /// /// A parallel writer for this hash map. public ParallelWriter AsParallelWriter() { ParallelWriter writer; writer.m_ThreadIndex = 0; writer.m_Buffer = m_Buffer; return writer; } /// /// A parallel writer for a UnsafeParallelHashMap. /// /// /// Use to create a parallel writer for a UnsafeParallelHashMap. /// [NativeContainerIsAtomicWriteOnly] [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public unsafe struct ParallelWriter { [NativeDisableUnsafePtrRestriction] internal UnsafeParallelHashMapData* m_Buffer; [NativeSetThreadIndex] internal int m_ThreadIndex; /// /// 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 int Capacity { get { UnsafeParallelHashMapData* data = m_Buffer; return data->keyCapacity; } } /// /// Adds a new key-value pair. /// /// If the key is already present, this method returns false without modifying the hash map. /// The key to add. /// The value to add. /// True if the key-value pair was added. public bool TryAdd(TKey key, TValue item) { Assert.IsTrue(m_ThreadIndex >= 0); return UnsafeParallelHashMapBase.TryAddAtomic(m_Buffer, key, item, m_ThreadIndex); } } /// /// Returns an enumerator over the key-value pairs of this hash map. /// /// An enumerator over the key-value pairs of this hash map. public Enumerator GetEnumerator() { return new Enumerator { m_Enumerator = new UnsafeParallelHashMapDataEnumerator(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 key-value pairs of a hash map. /// /// /// In an enumerator's initial state, is not valid to read. /// From this state, the first call advances the enumerator to the first key-value pair. /// public struct Enumerator : IEnumerator> { internal UnsafeParallelHashMapDataEnumerator m_Enumerator; /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next key-value pair. /// /// True if 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 key-value pair. /// /// The current key-value pair. public KeyValue Current => m_Enumerator.GetCurrent(); object IEnumerator.Current => Current; } } [BurstCompile] internal unsafe struct UnsafeParallelHashMapDisposeJob : IJob { [NativeDisableUnsafePtrRestriction] public UnsafeParallelHashMapData* Data; public AllocatorManager.AllocatorHandle Allocator; public void Execute() { UnsafeParallelHashMapData.DeallocateHashMap(Data, Allocator); } } sealed internal class UnsafeParallelHashMapDebuggerTypeProxy where TKey : struct, IEquatable where TValue : struct { #if !NET_DOTS UnsafeParallelHashMap m_Target; public UnsafeParallelHashMapDebuggerTypeProxy(UnsafeParallelHashMap target) { m_Target = target; } public List> Items { get { var result = new List>(); using (var kva = m_Target.GetKeyValueArrays(Allocator.Temp)) { for (var i = 0; i < kva.Length; ++i) { result.Add(new Pair(kva.Keys[i], kva.Values[i])); } } return result; } } #endif } /// /// For internal use only. /// [StructLayout(LayoutKind.Sequential)] public unsafe struct UntypedUnsafeParallelHashMap { #pragma warning disable 169 [NativeDisableUnsafePtrRestriction] UnsafeParallelHashMapData* m_Buffer; AllocatorManager.AllocatorHandle m_AllocatorLabel; #pragma warning restore 169 } }