Пример #1
0
 public void Execute()
 {
     UnsafeHashMapData.DeallocateHashMap(Data, Allocator);
 }
Пример #2
0
        internal static unsafe bool TryAdd(UnsafeHashMapData *data, TKey key, TValue item, bool isMultiHashMap, Allocator allocation)
        {
            if (!isMultiHashMap && TryGetFirstValueAtomic(data, key, out _, out _))
            {
                return(false);
            }

            // Allocate an entry from the free list
            int  idx;
            int *pNextIndexChain;

            if (data->allocatedIndexLength >= data->keyCapacity && data->firstFreeTLS[0] < 0)
            {
                for (int tls = 1; tls < JobsUtility.MaxJobThreadCount; ++tls)
                {
                    if (data->firstFreeTLS[tls * UnsafeHashMapData.IntPerCacheLine] >= 0)
                    {
                        idx             = data->firstFreeTLS[tls * UnsafeHashMapData.IntPerCacheLine];
                        pNextIndexChain = (int *)data->next;
                        data->firstFreeTLS[tls * UnsafeHashMapData.IntPerCacheLine] = pNextIndexChain[idx];
                        pNextIndexChain[idx]  = -1;
                        data->firstFreeTLS[0] = idx;
                        break;
                    }
                }

                if (data->firstFreeTLS[0] < 0)
                {
                    int newCap = UnsafeHashMapData.GrowCapacity(data->keyCapacity);
                    UnsafeHashMapData.ReallocateHashMap <TKey, TValue>(data, newCap, UnsafeHashMapData.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->keyCapacity)
            {
                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;

            pNextIndexChain = (int *)data->next;

            pNextIndexChain[idx] = buckets[bucket];
            buckets[bucket]      = idx;

            return(true);
        }