Exemplo n.º 1
0
        public void Serial_Int(int seed, int step, int count)
        {
            var serial = EnumerableTool.SerialSequence <double>(seed, step, count);

            Assert.AreEqual(count, serial.Count());
            // Console.WriteLine("Serials: " + serial.ToList().CollectionToString());
        }
Exemplo n.º 2
0
        public void ListContaining_WillRetunListContainingArgumentsSupplied()
        {
            var expected = new List <int>();

            expected.AddRange(new[] { 1, 2, 3 });
            AssertCollectionEquals(expected, EnumerableTool.ToList(1, 2, 3));
        }
Exemplo n.º 3
0
        /// <summary>
        /// <paramref name="step"/>이 있는 for loop 를 병렬 수행합니다.	for(i=fromInclusive; i &lt; toExclusive; i+=step)
        /// </summary>
        /// <param name="fromInclusive">loop 하한 인덱스</param>
        /// <param name="toExclusive">loop 상한 인덱스</param>
        /// <param name="step">반복 인덱스 Step</param>
        /// <param name="parallelOptions">병렬 옵션</param>
        /// <param name="localInit">로컬 변수값 초기화</param>
        /// <param name="body">로컬값을 반환하는 병렬 실행할 함수</param>
        /// <param name="localFinally">최종 로컬 정리 action</param>
        /// <returns>/실행 결과</returns>
        public static ParallelLoopResult ForWithStep <T>(int fromInclusive, int toExclusive, int step,
                                                         ParallelOptions parallelOptions,
                                                         Func <T> localInit,
                                                         Func <int, ParallelLoopState, T, T> body,
                                                         Action <T> localFinally)
        {
            step.ShouldBePositive("step");

            if (step == 1)
            {
                return(Parallel.For(fromInclusive, toExclusive, localInit, body, localFinally));
            }

            if (IsDebugEnabled)
            {
                log.Debug("Step을 가진 For Loop를 병렬 수행합니다... fromInclusive=[{0}], toExclusive=[{1}], step=[{2}]",
                          fromInclusive, toExclusive, step);
            }

            var ranges = EnumerableTool.Step(fromInclusive, toExclusive, step);

            return(Parallel.ForEach(ranges,
                                    parallelOptions,
                                    localInit,
                                    (i, loolState, local) => body(i, loolState, local),
                                    local => localFinally(local)));
        }
Exemplo n.º 4
0
        public void ToBindingList_FromEnumerable()
        {
            var serial  = EnumerableTool.SerialSequence(0, 1, 10);
            var binding = serial.ToBindingList();

            Assert.IsTrue(binding.Count > 0);
            Assert.AreEqual(9, binding.Last());

            // Console.WriteLine("BindingList:" + binding.CollectionToString());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns one trials from the categorical distribution.
        /// </summary>
        /// <param name="rnd">The random number generator to use.</param>
        /// <param name="cdf">The cumulative distribution of the probability distribution.</param>
        /// <returns>sequence of sample from the categorical distribution implied by <paramref name="cdf"/>.</returns>
        internal static IEnumerable <int> DoSamples(Random rnd, double[] cdf)
        {
            while (true)
            {
                var u     = rnd.NextDouble() * cdf[cdf.Length - 1];
                var index = EnumerableTool.IndexOf(cdf, cdf.FirstOrDefault(x => u > x));

                yield return(Math.Max(0, index));
            }
        }
Exemplo n.º 6
0
        private void BuildExpressionGraph(SingleSeriesChart chart)
        {
            var expr = new NCalc.Expression(Expression, EvaluateOptions.IgnoreCase | EvaluateOptions.IterateParameters);

            var step = (Upper - Lower) / VarCount;

            var x = EnumerableTool.Step(Lower, Upper, step).ToArray();

            expr.Parameters["x"] = x;

            var y = ((IEnumerable)expr.Evaluate()).ToListUnsafe <double>();

            for (var i = 0; i < x.Length; i++)
            {
                chart.SetElements.Add(new ValueSetElement(y[i])
                {
                    Label = x[i].ToString("#.0")
                });
            }
        }
Exemplo n.º 7
0
        public void ExecuteCacheApi()
        {
            var type = Assembly.GetExecutingAssembly().GetType(PersonTypeName);

            Assert.IsNotNull(type);
            Assert.AreEqual(PersonTypeName, type.FullName);

            var range = Enumerable.Range(0, 10).ToList();

            // Static Property Getter를 가져옵니다.
            MemberGetter count = type.DelegateForGetFieldValue("InstanceCount", Flags.StaticAnyVisibility);

            // StaticMemberGetter count = type.DelegateForGetStaticFieldValue("InstanceCount");

            Assert.AreEqual(type.GetFieldValue("InstanceCount", Flags.StaticAnyVisibility), count(null).AsInt());

            // Person의 두개의 인자를 사용하는 생성자의 Delegate를 가져옵니다.
            int currentInstanceCount = count(null).AsInt();
            ConstructorInvoker ctor  = type.DelegateForCreateInstance(new[] { typeof(int), typeof(string) });

            Assert.IsNotNull(ctor);

            // 생성자 델리게이트를 이용하여 10개의 인스턴스를 생성합니다.
            range.ForEach(i => {
                object obj = ctor(i, "_" + i);
                Assert.AreEqual(++currentInstanceCount, count(null).AsInt());
                Assert.AreEqual(i, obj.GetFieldValue("id").AsInt());
                Assert.AreEqual(i, obj.GetPropertyValue("Id").AsInt());
                Assert.AreEqual("_" + i, obj.GetPropertyValue("Name").AsText());

                Console.WriteLine(obj);
            });

            // 속성 조회/설정 (Property Getter / Setter) : Field 및 Indexer에 대해서도 가능합니다.

            MemberGetter nameGetter = type.DelegateForGetPropertyValue("Name");
            MemberSetter nameSetter = type.DelegateForSetPropertyValue("Name");

            object person = ctor(1, "John");

            Assert.AreEqual("John", nameGetter(person));
            nameSetter(person, "Jane");
            Assert.AreEqual("Jane", nameGetter(person));


            // 메소드 호출
            person = type.CreateInstance();
            MethodInvoker walkInvoker = type.DelegateForCallMethod("Walk", new[] { typeof(int) });

            EnumerableTool.RunEach(range, i => walkInvoker(person, i));
            Assert.AreEqual(range.Sum(), person.GetFieldValue("milesTraveled").AsInt());

            // Mapping properties
            //
            var ano    = new { Id = 4, Name = "Doe" };
            var mapper = ano.GetType().DelegateForMap(type);

            mapper(ano, person);

            Assert.AreEqual(ano.Id, person.GetPropertyValue("Id").AsInt());
            Assert.AreEqual(ano.Name, person.GetPropertyValue("Name").AsText());
        }
Exemplo n.º 8
0
 private static IList <T> GetSerialList <T>(T seed, T step, int count) where T : struct, IComparable <T>
 {
     return(EnumerableTool.SerialSequence(seed, step, count).ToList());
 }