Example #1
0
        /// <summary>
        /// Remember that this operation os O(N*M*P) where N,M,P are the number of groups where each component
        /// is found.
        /// TODO: I have to find once for ever a solution to be sure that the entities in the groups match
        /// Currently this returns group where the entities are found, but the entities may not match in these
        /// groups.
        /// Checking the size of the entities is an early check, needed, but not sufficient, as entities components may
        /// coincidentally match in number but not from which entities they are generated
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <returns></returns>
        public LocalFasterReadOnlyList <ExclusiveGroupStruct> FindGroups <T1, T2, T3>()
            where T1 : IBaseEntityComponent where T2 : IBaseEntityComponent where T3 : IBaseEntityComponent
        {
            FasterList <FasterDictionary <ExclusiveGroupStruct, ITypeSafeDictionary> > localArray =
                localgroups.Value.listOfGroups;

            if (groupsPerComponent.TryGetValue(TypeRefWrapper <T1> .wrapper, out localArray[0]) == false || localArray[0].count == 0)
            {
                return(new LocalFasterReadOnlyList <ExclusiveGroupStruct>(
                           FasterReadOnlyList <ExclusiveGroupStruct> .DefaultEmptyList));
            }
            if (groupsPerComponent.TryGetValue(TypeRefWrapper <T2> .wrapper, out localArray[1]) == false || localArray[1].count == 0)
            {
                return(new LocalFasterReadOnlyList <ExclusiveGroupStruct>(
                           FasterReadOnlyList <ExclusiveGroupStruct> .DefaultEmptyList));
            }
            if (groupsPerComponent.TryGetValue(TypeRefWrapper <T3> .wrapper, out localArray[2]) == false || localArray[2].count == 0)
            {
                return(new LocalFasterReadOnlyList <ExclusiveGroupStruct>(
                           FasterReadOnlyList <ExclusiveGroupStruct> .DefaultEmptyList));
            }

            localgroups.Value.groups.FastClear();

            FasterDictionary <ExclusiveGroupStruct, ExclusiveGroupStruct> localGroups = localgroups.Value.groups;

            int startIndex = 0;
            int min        = int.MaxValue;

            for (int i = 0; i < 3; i++)
            {
                if (localArray[i].count < min)
                {
                    min        = localArray[i].count;
                    startIndex = i;
                }
            }

            foreach (var value in localArray[startIndex])
            {
                if (value.key.IsEnabled())
                {
                    localGroups.Add(value.key, value.key);
                }
            }

            var groupData = localArray[++startIndex % 3];

            localGroups.Intersect(groupData);
            if (localGroups.count == 0)
            {
                return(new LocalFasterReadOnlyList <ExclusiveGroupStruct>(
                           FasterReadOnlyList <ExclusiveGroupStruct> .DefaultEmptyList));
            }
            groupData = localArray[++startIndex % 3];
            localGroups.Intersect(groupData);

            return(new LocalFasterReadOnlyList <ExclusiveGroupStruct>(localGroups.unsafeValues
                                                                      , (uint)localGroups.count));
        }
Example #2
0
        public void TestIntersect()
        {
            FasterDictionary <int, int> test1 = new FasterDictionary <int, int>();
            FasterDictionary <int, int> test2 = new FasterDictionary <int, int>();

            for (int i = 0; i < 100; ++i)
            {
                test1.Add(i, i);
            }

            for (int i = 0; i < 200; i += 2)
            {
                test2.Add(i, i);
            }

            test1.Intersect(test2);

            Assert.AreEqual(50, test1.count);

            for (int i = 0; i < 100; i += 2)
            {
                Assert.IsTrue(test1.ContainsKey(i));
            }
        }