Exemplo n.º 1
0
        public static Func <FastDictionary3 <TKey, TValue> > CreateFastDictionary3Generator <TKey, TValue>(
            this IEqualityComparerExpression <TKey> comparerExp, int capacity, Func <TKey, TKey, bool> equalsFunc, Func <TKey, int> getHashCodeFunc, QueryContainer container)
        {
            if (EqualityComparerExpression <TKey> .IsSimpleDefault(comparerExp))
            {
                return(() => new FastDictionary3 <TKey, TValue>());
            }
            if (container == null)
            {
                return(() => new FastDictionary3 <TKey, TValue>(capacity, equalsFunc, getHashCodeFunc));
            }

            var equalsExp      = comparerExp.GetEqualsExpr();
            var getHashCodeExp = comparerExp.GetGetHashCodeExpr();
            var vars           = VariableFinder.Find(equalsExp).Select(o => o.GetHashCode()).ToList();

            if (!vars.Any())
            {
                vars.Add(string.Empty.StableHash());
            }
            var hashvars = VariableFinder.Find(getHashCodeExp).Select(o => o.GetHashCode()).ToList();

            if (!hashvars.Any())
            {
                hashvars.Add(string.Empty.StableHash());
            }
            var key =
                Tuple.Create(
                    equalsExp.ToString() + getHashCodeExp.ToString() + string.Concat(vars.Aggregate((a, i) => a ^ i)) + string.Concat(hashvars.Aggregate((a, i) => a ^ i)),
                    typeof(TKey), typeof(TValue));

            Type temp;

            lock (sentinel)
            {
                if (!generatorCache.TryGetValue(key, out temp))
                {
                    string typeName    = Prefix + classCounter++;
                    var    builderCode = new GeneratedFastDictionary(typeName, "3").TransformText();
                    var    a           = Transformer.CompileSourceCode(builderCode, Array.Empty <Assembly>(), out string errorMessages);

                    temp = a.GetType(typeName + "`2");
                    temp = temp.MakeGenericType(typeof(TKey), typeof(TValue));
                    MethodInfo init = temp.GetTypeInfo().GetMethod("Initialize", BindingFlags.Static | BindingFlags.Public);
                    init.Invoke(null, new object[] { equalsFunc, getHashCodeFunc, capacity });
                    generatorCache.Add(key, temp);
                }
                if (!container.TryGetFastDictionary3Type(key, out Type other))
                {
                    container.RegisterFastDictionary3Type(key, temp);
                }
            }

            return(() => (FastDictionary3 <TKey, TValue>)Activator.CreateInstance(temp));
        }