コード例 #1
0
        public void TestUniqueKey()
        {
            FasterDictionary <int, string> test = new FasterDictionary <int, string>();

            test.Add(1, "one.a");
            void TestAddDup()
            {
                test.Add(1, "one.b");
            }

            Assert.Throws(typeof(SveltoDictionaryException), TestAddDup);
        }
コード例 #2
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));
        }
コード例 #3
0
        void CopyEntityToDictionary(EGID entityGID, EGID toEntityGID,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup, Type entityViewType)
        {
            var wrapper = new RefWrapper <Type>(entityViewType);

            if (fromGroup.TryGetValue(wrapper, out var fromTypeSafeDictionary) == false)
            {
                throw new ECSException("no entities in from group eid: ".FastConcat(entityGID.entityID)
                                       .FastConcat(" group: ").FastConcat(entityGID.groupID));
            }

#if DEBUG && !PROFILER
            if (fromTypeSafeDictionary.Has(entityGID.entityID) == false)
            {
                throw new EntityNotFoundException(entityGID, entityViewType);
            }
#endif
            if (toGroup.TryGetValue(wrapper, out var toEntitiesDictionary) == false)
            {
                toEntitiesDictionary = fromTypeSafeDictionary.Create();
                toGroup.Add(wrapper, toEntitiesDictionary);
            }

            //todo: this must be unit tested properly
            if (_groupsPerEntity.TryGetValue(wrapper, out var groupedGroup) == false)
            {
                groupedGroup = _groupsPerEntity[wrapper] =
                    new FasterDictionary <uint, ITypeSafeDictionary>();
            }

            groupedGroup[toEntityGID.groupID] = toEntitiesDictionary;

            fromTypeSafeDictionary.AddEntityToDictionary(entityGID, toEntityGID, toEntitiesDictionary);
        }
コード例 #4
0
        public void TestUnion()
        {
            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.Union(test2);

            Assert.AreEqual(150, test1.count);

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

            for (int i = 100; i < 200; i += 2)
            {
                Assert.IsTrue(test1.ContainsKey(i));
            }
        }
コード例 #5
0
        static void AddEngine <T>(T engine, FasterDictionary <RefWrapper <Type>, FasterList <IEngine> > engines, Type type)
            where T : class, IEngine
        {
            if (engines.TryGetValue(new RefWrapper <Type>(type), out var list) == false)
            {
                list = new FasterList <IEngine>();

                engines.Add(new RefWrapper <Type>(type), list);
            }

            list.Add(engine);
        }
コード例 #6
0
        public void TestFastClear()
        {
            FasterDictionary <int, int> test = new FasterDictionary <int, int>();

            test.Add(0, 0);

            Assert.IsTrue(test.ContainsKey(0));

            test.FastClear();

            Assert.IsFalse(test.ContainsKey(0));
        }
コード例 #7
0
        static Dictionary <Type, ITypeSafeDictionary> FetchEntityGroup(int groupID,
                                                                       FasterDictionary <int, Dictionary <Type, ITypeSafeDictionary> > groupEntityViewsByType)
        {
            Dictionary <Type, ITypeSafeDictionary> group;

            if (groupEntityViewsByType.TryGetValue(groupID, out @group) == false)
            {
                @group = new Dictionary <Type, ITypeSafeDictionary>();
                groupEntityViewsByType.Add(groupID, @group);
            }

            return(@group);
        }
コード例 #8
0
        static void BuildEntity(EGID entityID, FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> group,
                                Type entityViewType, IEntityBuilder entityBuilder, IEnumerable <object> implementors)
        {
            var entityViewsPoolWillBeCreated =
                group.TryGetValue(new RefWrapper <Type>(entityViewType), out var safeDictionary) == false;

            //passing the undefined entityViewsByType inside the entityViewBuilder will allow it to be created with the
            //correct type and casted back to the undefined list. that's how the list will be eventually of the target
            //type.
            entityBuilder.BuildEntityAndAddToList(ref safeDictionary, entityID, implementors);

            if (entityViewsPoolWillBeCreated)
            {
                group.Add(new RefWrapper <Type>(entityViewType), safeDictionary);
            }
        }
コード例 #9
0
        static void AddEngineToList <T>
            (T engine, Type[] entityComponentTypes, FasterDictionary <RefWrapperType, FasterList <IReactEngine> > engines)
            where T : class, IReactEngine
        {
            for (var i = 0; i < entityComponentTypes.Length; i++)
            {
                var type = entityComponentTypes[i];

                if (engines.TryGetValue(new RefWrapperType(type), out var list) == false)
                {
                    list = new FasterList <IReactEngine>();

                    engines.Add(new RefWrapperType(type), list);
                }

                list.Add(engine);
            }
        }