protected void ResampleGaussian()
    {
        while (transform.childCount != 0) {
            GameObject.DestroyImmediate(transform.GetChild(0).gameObject);
        }

        Assert.IsTrue(_dot != null);

        Dictionary<int, int> distributionMapping = new Dictionary<int, int>();
        if (_dot != null) {
            for (int i = 0; i < _pointsToGenerate; i++) {
                int val = (int)Mathf.Round(MathUtil.SampleGaussian(_mean, _standardDeviation));
                int oldCount = distributionMapping.SafeGet(val, 0);

                GameObject dotClone = MonoBehaviour.Instantiate(_dot, Vector3.zero, Quaternion.identity) as GameObject;
                this.SetSelfAsParent(dotClone);
                dotClone.transform.localPosition = new Vector3(val, oldCount, 0.0f);
                dotClone.GetComponent<SpriteRenderer>().color = ColorExtensions.RandomPleasingColor();

                distributionMapping[val] = oldCount + 1;
            }
        }
    }
Example #2
0
        public void CanSafeGetValueTypeDictionaryValueWhenTheValueNotExists()
        {
            var seq = new Dictionary<int, int>() { { 2, 3 }, { 4, 5 } };

            seq.SafeGet(3).Should().Be(0);
        }
Example #3
0
        public void CanSafeGetReferenceTypeDictionaryValueWhenTheValueNotExists()
        {
            var seq = new Dictionary<string, string>() { { "2", "3" }, { "4", "5" } };

            seq.SafeGet("3").Should().Be(null);
        }
Example #4
0
        private void GenerateProxyMethods(IEnumerable<Type> interfaces, TypeDefinition proxyType)
        {
            var methodsExcludingGetters = interfaces
                .SelectMany(x => x.GetMethods().Except(x.GetProperties().SelectMany(GetPropertyMethods)));

            foreach (var targetMethod in methodsExcludingGetters)
            {
                var baseDef = this.proxyBaseTypeDef;
                if (BaseTypeHasMatchingPublicMethod(baseDef, targetMethod))
                    continue;

                var genArgMapping = new Dictionary<Type, Type>();

                if (targetMethod.DeclaringType == null)
                    throw new InvalidOperationException($"{targetMethod} has no declaring type.");

                if (targetMethod.DeclaringType.IsGenericType)
                {
                    var declTypeGenArgs = targetMethod.DeclaringType
                                                      .GetGenericArguments()
                                                      .Zip(targetMethod.DeclaringType.GetGenericTypeDefinition().GetGenericArguments(),
                                                           (a, p) => new { a, p });

                    foreach (var declTypeGenArg in declTypeGenArgs)
                        genArgMapping.Add(declTypeGenArg.p, declTypeGenArg.a);
                }

                Func<Type, Type> typeReplacer =
                    t => TypeUtils.ReplaceInGenericArguments(t, x => genArgMapping.SafeGet(x, x));
                var parameters = targetMethod.GetParameters();
                var paramTypes = parameters.Select(x => x.ParameterType).ToArray();
                var method = proxyType.DefineMethod(targetMethod.Name,
                                                    MethodAttributes.NewSlot
                                                    | MethodAttributes.HideBySig
                                                    | MethodAttributes.Virtual
                                                    | MethodAttributes.Public,
                                                    targetMethod.ReturnType,
                                                    paramTypes);

                CopyGenericMethodParameters(targetMethod, method, genArgMapping, typeReplacer);

                AdjustParameterTypes(parameters, typeReplacer, method);

                var proxyOnGetMethod = baseDef
                    .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                    .FirstOrDefault(x => x.Name == "OnInvokeMethod");

                if (proxyOnGetMethod == null)
                {
                    var message =
                        $"Unable to generate proxy for {targetMethod.GetFullNameWithSignature()} because {baseDef.FullName}.OnInvokeMethod() is missing.";
                    throw new MissingMethodException(message);
                }

                GenerateInvokeMethodIl(method, paramTypes, parameters, proxyOnGetMethod);
            }
        }