public void UnsafeMultiHashMap_ForEach_FixedStringInHashMap()
    {
        using (var stringList = new NativeList <FixedString32>(10, Allocator.Persistent)
        {
            "Hello", ",", "World", "!"
        })
        {
            var container = new UnsafeMultiHashMap <FixedString128, float>(50, Allocator.Temp);
            var seen      = new NativeArray <int>(stringList.Length, Allocator.Temp);
            foreach (var str in stringList)
            {
                container.Add(str, 0);
            }

            foreach (var pair in container)
            {
                int index = stringList.IndexOf(pair.Key);
                Assert.AreEqual(stringList[index], pair.Key.ToString());
                seen[index] = seen[index] + 1;
            }

            for (int i = 0; i < stringList.Length; i++)
            {
                Assert.AreEqual(1, seen[i], $"Incorrect value count {stringList[i]}");
            }
        }
    }
Beispiel #2
0
    public void UnsafeMultiHashMap_GetKeys()
    {
        var container = new UnsafeMultiHashMap <int, int>(1, Allocator.Temp);

        for (int i = 0; i < 30; ++i)
        {
            container.Add(i, 2 * i);
            container.Add(i, 3 * i);
        }
        var keys = container.GetKeyArray(Allocator.Temp);

#if !NET_DOTS // Tuple is not supported by TinyBCL
        var(unique, uniqueLength) = container.GetUniqueKeyArray(Allocator.Temp);
        Assert.AreEqual(30, uniqueLength);
#endif

        Assert.AreEqual(60, keys.Length);
        keys.Sort();
        for (int i = 0; i < 30; ++i)
        {
            Assert.AreEqual(i, keys[i * 2 + 0]);
            Assert.AreEqual(i, keys[i * 2 + 1]);
#if !NET_DOTS // Tuple is not supported by TinyBCL
            Assert.AreEqual(i, unique[i]);
#endif
        }
    }
Beispiel #3
0
        NativeMultiHashMap(int capacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            m_MultiHashMapData = new UnsafeMultiHashMap <TKey, TValue>(capacity, allocator, disposeSentinelStackDepth);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator);
#endif
        }
Beispiel #4
0
 public UnsafeMultiHashMapEnumerator(UnsafeMultiHashMap <TKey, TValue> map, TKey key)
 {
     this.map = map;
     this.key = key;
     iterator = default;
     value    = default;
     isFirst  = true;
 }
Beispiel #5
0
 public ConversionDependencies(bool isLiveLink)
 {
     m_IsLiveLink = isLiveLink;
     if (m_IsLiveLink)
     {
         GameObjectDependentsByInstanceId = new UnsafeMultiHashMap <int, int>(0, Allocator.Persistent);
     }
 }
    public void UnsafeMultiHashMap_RemoveOnEmptyMap_DoesNotThrow()
    {
        var container = new UnsafeMultiHashMap <int, int>(0, Allocator.Temp);

        Assert.DoesNotThrow(() => container.Remove(0));
        Assert.DoesNotThrow(() => container.Remove(-425196));
        Assert.DoesNotThrow(() => container.Remove(0, 0));
        Assert.DoesNotThrow(() => container.Remove(-425196, 0));

        container.Dispose();
    }
 static void AssertDependencyExists <T>(ConversionDependencies dependencies, UnsafeMultiHashMap <T, int> map, T key, GameObject dependent) where T : unmanaged, IEquatable <T>
 {
     if (!dependencies.GameObjectIndexByInstanceId.TryGetValue(dependent.GetInstanceID(), out int idx))
     {
         Assert.Fail($"The conversion system didn't store the dependent game object {dependent.name}.");
     }
     Assert.AreEqual(dependent, dependencies.DependentGameObjects[idx]);
     if (!map.TryGetFirstValue(key, out var value, out _))
     {
         Assert.Fail("The dependent wasn't registered to the key of the dependency.");
     }
     Assert.AreEqual(idx, value);
 }
Beispiel #8
0
        NativeMultiHashMap(int capacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            m_MultiHashMapData = new UnsafeMultiHashMap <TKey, TValue>(capacity, allocator);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator);

            if (s_staticSafetyId.Data == 0)
            {
                CreateStaticSafetyId();
            }
            AtomicSafetyHandle.SetStaticSafetyId(ref m_Safety, s_staticSafetyId.Data);
#endif
        }
    public void UnsafeMultiHashMap_AddJob()
    {
        var container = new UnsafeMultiHashMap <int, int>(32, Allocator.TempJob);

        var job = new UnsafeMultiHashMapAddJob()
        {
            Writer = container.AsParallelWriter(),
        };

        job.Schedule(3, 1).Complete();

        Assert.True(container.ContainsKey(123));
        Assert.AreEqual(container.CountValuesForKey(123), 3);

        container.Dispose();
    }
        internal WorldUnmanagedImpl(ulong sequenceNumber, WorldFlags flags)
        {
            CurrentTime = default;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AllowGetSystem = true;
#endif
            SequenceNumber           = sequenceNumber;
            MaximumDeltaTime         = 1.0f / 3.0f;
            Flags                    = flags;
            _unmanagedSlotByTypeHash = new UnsafeMultiHashMap <long, ushort>(32, Allocator.Persistent);
            _pendingDestroys         = new UnsafeList <ushort>(32, Allocator.Persistent);
            _stateMemory             = default;
            _stateMemory.Init();
            Version         = 0;
            ExecutingSystem = default;
        }
    public void UnsafeMultiHashMap_ForEach([Values(10, 1000)] int n)
    {
        var seenKeys   = new NativeArray <int>(n, Allocator.Temp);
        var seenValues = new NativeArray <int>(n * 2, Allocator.Temp);

        using (var container = new UnsafeMultiHashMap <int, int>(1, Allocator.Temp))
        {
            for (int i = 0; i < n; ++i)
            {
                container.Add(i, i);
                container.Add(i, i + n);
            }

            var count = 0;
            foreach (var kv in container)
            {
                if (kv.Value < n)
                {
                    Assert.AreEqual(kv.Key, kv.Value);
                }
                else
                {
                    Assert.AreEqual(kv.Key + n, kv.Value);
                }

                seenKeys[kv.Key]     = seenKeys[kv.Key] + 1;
                seenValues[kv.Value] = seenValues[kv.Value] + 1;

                ++count;
            }

            Assert.AreEqual(container.Count(), count);
            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}");
                Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}");
                Assert.AreEqual(1, seenValues[i + n], $"Incorrect value count {i + n}");
            }
        }
    }
Beispiel #12
0
    protected override JobHandle OnUpdate(JobHandle inputDependencies)
    {
        var brickCount = m_BrickQuery.CalculateEntityCount();

        if (brickCount == 0)
        {
            return(inputDependencies);
        }

        World.EntityManager.CreateEntity(typeof(BrickHashGrid));

        var screenBounds       = GetSingleton <ScreenBoundsData>();
        var gridCellSize       = 2.5f;
        var screenWidthHeight  = math.abs(screenBounds.XYMax - screenBounds.XYMin);
        var screenWidthInCells = (int)math.ceil(screenWidthHeight.x / gridCellSize);
        var hashGrid           = new UnsafeMultiHashMap <int, Entity>(brickCount * 4, Allocator.Persistent);

        new HashBrickPositionsJob
        {
            Grid               = hashGrid,
            GridCellSize       = gridCellSize,
            ScreenWidthInCells = screenWidthInCells
        }.Run(this, inputDependencies);

        var brickHashGrid = new BrickHashGrid
        {
            Grid               = hashGrid,
            GridCellSize       = gridCellSize,
            ScreenWidthInCells = screenWidthInCells
        };

        SetSingleton(brickHashGrid);
        this.Enabled = false;

        return(inputDependencies);
    }
 public static (NativeArray <TKey>, int) GetUniqueKeyArray <TKey, TValue>(this UnsafeMultiHashMap <TKey, TValue> container, Allocator allocator)
 void AssertDependencyExists <T>(UnsafeMultiHashMap <T, int> map, T key, GameObject dependent) where T : unmanaged, IEquatable <T>
 => AssertDependencyExists(m_Dependencies, map, key, dependent);
Beispiel #15
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        if (!m_ObstacleQuery.IsEmptyIgnoreFilter)
        {
            return(inputDeps);
        }

        var   settings         = GetSingleton <AntManagerSettings>();
        var   obstacleSpawner  = GetSingleton <ObstacleSpawner>();
        float radius           = obstacleSpawner.ObstacleRadius;
        var   mapSize          = settings.MapSize;
        var   bucketResolution = settings.BucketResolution;

        World.EntityManager.CreateEntity(typeof(ObstacleBuckets));
        var values  = new UnsafeMultiHashMap <int, Position>(bucketResolution * bucketResolution, Allocator.Persistent);
        var buckets = new ObstacleBuckets {
            Values = values
        };

        SetSingleton(buckets);


        Entities.WithStructuralChanges().ForEach((ref ObstacleSpawner spawner) =>
        {
            var scale = new Vector3(spawner.ObstacleRadius * 2f, spawner.ObstacleRadius * 2f, 1f) / mapSize;
            float obstaclesPerRing = spawner.ObstaclesPerRing;
            for (int ring = 1; ring <= spawner.ObstacleRingCount; ring++)
            {
                float ringRadius    = (ring / (spawner.ObstacleRingCount + 1f)) * (mapSize * .5f);
                float circumference = ringRadius * 2f * Mathf.PI;
                int maxCount        = Mathf.CeilToInt(circumference / (2f * spawner.ObstacleRadius) * 2f);
                int offset          = UnityEngine.Random.Range(0, maxCount);
                int holeCount       = UnityEngine.Random.Range(1, 3);
                NativeList <Vector2> obstaclesToSpawn = new NativeList <Vector2>(maxCount, Allocator.TempJob);

                for (int j = 0; j < maxCount; j++)
                {
                    float t = (float)j / maxCount;
                    if ((t * holeCount) % 1f < obstaclesPerRing)
                    {
                        float angle = (j + offset) / (float)maxCount * (2f * Mathf.PI);
                        Vector2 pos = new Vector2(mapSize * .5f + Mathf.Cos(angle) * ringRadius, mapSize * .5f + Mathf.Sin(angle) * ringRadius);
                        obstaclesToSpawn.Add(pos);
                    }
                }
                using (var obstacles = EntityManager.Instantiate(spawner.ObstaclePrefab, obstaclesToSpawn.Length, Allocator.TempJob))
                {
                    for (int j = 0; j < obstacles.Length; j++)
                    {
                        Vector2 pos = obstaclesToSpawn[j];
                        EntityManager.SetComponentData(obstacles[j], new Position()
                        {
                            Value = pos
                        });

                        EntityManager.SetComponentData(obstacles[j], new LocalToWorld()
                        {
                            Value = Matrix4x4.TRS(pos / mapSize, Quaternion.identity, scale)
                        });
                    }
                    obstacles.Dispose();
                }
                obstaclesToSpawn.Dispose();
            }
        }).Run();

        Entities.WithStructuralChanges().WithAll <TagObstacle>().ForEach((Position pos) =>
        {
            for (int x = Mathf.FloorToInt((pos.Value.x - radius) / mapSize * bucketResolution); x <= Mathf.FloorToInt((pos.Value.x + radius) / mapSize * bucketResolution); x++)
            {
                if (x < 0 || x >= bucketResolution)
                {
                    continue;
                }
                for (int y = Mathf.FloorToInt((pos.Value.y - radius) / mapSize * bucketResolution); y <= Mathf.FloorToInt((pos.Value.y + radius) / mapSize * bucketResolution); y++)
                {
                    if (y < 0 || y >= bucketResolution)
                    {
                        continue;
                    }
                    buckets.Values.Add(ObstacleBuckets.Hash(mapSize, x, y), pos);
                }
            }
        }).Run();

        return(inputDeps);
    }
Beispiel #16
0
 internal DependencyTracker(Allocator allocator)
 {
     _dependentsByInstanceId   = new UnsafeMultiHashMap <int, int>(0, allocator);
     _dependenciesByInstanceId = new UnsafeMultiHashMap <int, int>(0, allocator);
 }
Beispiel #17
0
 public ValueGroupingValues(TKey key, UnsafeMultiHashMap <TKey, TValue> map)
 {
     this.enumerator = new UnsafeMultiHashMapEnumerator <TKey, TValue>(map, key);
 }