public void Dispose()
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Dispose(m_Safety, ref m_DisposeSentinel);
#endif

            NativeHashMapData.DeallocateHashMap(m_Buffer, m_AllocatorLabel);
            m_Buffer = null;
        }
Exemplo n.º 2
0
        public NativeArray <TKey> GetKeyArray(Allocator allocator)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
#endif
            var result = new NativeArray <TKey>(Length, allocator, NativeArrayOptions.UninitializedMemory);
            NativeHashMapData.GetKeyArray(m_Buffer, result);
            return(result);
        }
        public NativeMultiHashMap(int capacity, Allocator label)
        {
            m_AllocatorLabel = label;
            // Bucket size if bigger to reduce collisions
            NativeHashMapData.AllocateHashMap <TKey, TValue> (capacity, capacity * 2, label, out m_Buffer);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0);
#endif

            Clear();
        }
Exemplo n.º 4
0
        public static unsafe bool TryAdd(NativeHashMapData *data, TKey key, TValue item, bool isMultiHashMap,
                                         Allocator allocation)
        {
            TValue tempItem;
            NativeMultiHashMapIterator <TKey> 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->capacity && data->firstFreeTLS[0] < 0)
            {
                for (int tls = 1; tls < JobsUtility.MaxJobThreadCount; ++tls)
                {
                    if (data->firstFreeTLS[tls * NativeHashMapData.IntsPerCacheLine] >= 0)
                    {
                        idx      = data->firstFreeTLS[tls * NativeHashMapData.IntsPerCacheLine];
                        nextPtrs = (int *)data->next;
                        data->firstFreeTLS[tls * NativeHashMapData.IntsPerCacheLine] = nextPtrs[idx];
                        nextPtrs[idx]         = -1;
                        data->firstFreeTLS[0] = idx;
                        break;
                    }
                }
                if (data->firstFreeTLS[0] < 0)
                {
                    int newCap = NativeHashMapData.GrowCapacity(data->capacity);
                    NativeHashMapData.ReallocateHashMap <TKey, TValue>(data, newCap,
                                                                       NativeHashMapData.GetBucketSize(newCap), allocation);
                }
            }
            idx = data->firstFreeTLS[0];
            if (idx >= 0)
            {
                data->firstFreeTLS[0] = ((int *)data->next)[idx];
            }
            else
            {
                idx = data->allocatedIndexLength++;
            }

            if (idx < 0 || idx >= data->capacity)
            {
                throw new InvalidOperationException("Internal HashMap error");
            }

            // 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);
        }