예제 #1
0
        public static void Aggregate_Sum(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;

            if (count == 0)
            {
                Assert.Throws <InvalidOperationException>(() => query.Aggregate((x, y) => x + y));
            }
            else
            {
                // The operation will overflow for long-running sizes, but that's okay:
                // The helper is overflowing too!
                Assert.Equal(Functions.SumRange(0, count), query.Aggregate((x, y) => x + y));
            }
        }
예제 #2
0
        /// <summary>Takes the top elements as if they were sorted.</summary>
        /// <typeparam name="TSource">Specifies the type of the elements.</typeparam>
        /// <typeparam name="TKey">Specifies the type of the keys used to compare elements.</typeparam>
        /// <param name="source">The source elements.</param>
        /// <param name="keySelector">A function used to extract a key from each element.</param>
        /// <param name="count">The number of elements to take.</param>
        /// <returns></returns>
        public static IEnumerable <TSource> TakeTop <TSource, TKey>(this ParallelQuery <TSource> source,
                                                                    Func <TSource, TKey> keySelector,
                                                                    int count)
        {
            // We want to sort in descending order, so we need the opposite of the default comparer
            var comparer = new DescendingDefaultComparer <TKey>();

            // Aggregate, using a sorted list per thread to keep track of the best N elements,
            // then merge those at the end.
            return(source.Aggregate(
                       () => new Nequeo.Linq.Common.SortedTopN <TKey, TSource>(count, comparer),

                       (accum, item) =>
            {
                accum.Add(keySelector(item), item);
                return accum;
            },

                       (accum1, accum2) =>
            {
                foreach (var item in accum2)
                {
                    accum1.Add(item);
                }
                return accum1;
            },

                       (accum) => accum.Values));
        }
예제 #3
0
        /// <summary>
        /// 정렬이 되었다면, 지정된 갯수의 최상위 요소중 <paramref name="count"/> 수만큼 반환합니다.
        /// </summary>
        /// <typeparam name="TSource">요소의 수형</typeparam>
        /// <typeparam name="TKey">요소 비교를 위한 Key의 수형</typeparam>
        /// <param name="source">요소 집합</param>
        /// <param name="keySelector">요소로부터 Key를 추출하는 Selector</param>
        /// <param name="count">취할 요소의 수</param>
        /// <returns></returns>
        public static IEnumerable <TSource> TakeTop <TSource, TKey>(this ParallelQuery <TSource> source,
                                                                    Func <TSource, TKey> keySelector,
                                                                    int count = 1)
        {
            keySelector.ShouldNotBeNull("keySelector");

            if (IsDebugEnabled)
            {
                log.Debug("최상의 요소 중 [{0}]만큼 열거합니다.", count);
            }

            var comparer = new DescendingDefaultComparer <TKey>();

            return
                (source.Aggregate(() => new SortedTopN <TKey, TSource>(count, comparer),
                                  (accum, item) => {
                accum.Add(keySelector(item), item);
                return accum;
            },
                                  (accum1, accum2) => {
                foreach (var item in accum2)
                {
                    accum1.Add(item);
                }
                return accum1;
            },
                                  accum => accum.Values));
        }
예제 #4
0
    /// <summary>
    /// An aggregate parallel query to return the minimum and the maximum of <paramref name="data"/> together, faster than two successive parallel queries to minimum and maximum.
    /// </summary>
    /// <param name="data">The list whose extrema we are to find.</param>
    /// <returns>A <see cref="Tuple{double, double}"/> instance whose <see cref="Tuple{double, double}.Item1"/> represents the minimum and whose <see cref="Tuple{double, double}.Item2"/> contains the maximum of <paramref name="data"/>.</returns>
    public static Tuple <double, double> Extrema(this IList <double> data)
    {
        ParallelQuery <double> query = data.AsParallel();

        return(query.Aggregate(
                   // Initialise accumulator:
                   () => new ExtremumAccumulator()
        {
            Min = Double.MaxValue, Max = Double.MinValue
        },
                   // Aggregate calculations:
                   (accumulator, item) => { if (item < accumulator.Min)
                                            {
                                                accumulator.Min = item;
                                            }
                                            if (item > accumulator.Max)
                                            {
                                                accumulator.Max = item;
                                            }
                                            return accumulator; },
                   // Combine accumulators:
                   (accumulator1, accumulator2) => new ExtremumAccumulator()
        {
            Min = Math.Min(accumulator1.Min, accumulator2.Min), Max = Math.Max(accumulator1.Max, accumulator2.Max)
        },
                   // Get result:
                   accumulator => new Tuple <double, double>(accumulator.Min, accumulator.Max)
                   ));
    }
예제 #5
0
        public static void Aggregate_Product_Seed(Labeled <ParallelQuery <int> > labeled, int count, int start)
        {
            ParallelQuery <int> query = labeled.Item;

            // The operation will overflow for long-running sizes, but that's okay:
            // The helper is overflowing too!
            Assert.Equal(Functions.ProductRange(start, count), query.Aggregate(1L, (x, y) => x * y));
        }
예제 #6
0
        public static void Aggregate_Product_SeedFunction(Labeled <ParallelQuery <int> > labeled, int count, int start)
        {
            ParallelQuery <int> query = labeled.Item;
            long actual = query.Aggregate(
                () => 1L,
                (accumulator, x) => accumulator * x,
                (left, right) => left * right,
                result => result + ResultFuncModifier);

            Assert.Equal(Functions.ProductRange(start, count) + ResultFuncModifier, actual);
        }
예제 #7
0
        public static void Aggregate_Collection_SeedFunction(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query  = labeled.Item;
            IList <int>         actual = query.Aggregate(
                () => (IList <int>) new List <int>(),
                (accumulator, x) => accumulator.AddToCopy(x),
                (left, right) => left.ConcatCopy(right),
                result => result.OrderBy(x => x).ToList());

            Assert.Equal(Enumerable.Range(0, count), actual);
        }
예제 #8
0
        public static void Aggregate_Collection_SeedFunction(int count)
        {
            ParallelQuery <int> query  = UnorderedSources.Default(count);
            IList <int>         actual = query.Aggregate(
                () => ImmutableList <int> .Empty,
                (accumulator, x) => accumulator.Add(x),
                (left, right) => left.AddRange(right),
                result => result.OrderBy(x => x).ToList());

            Assert.Equal(Enumerable.Range(0, count), actual);
        }
예제 #9
0
        public static void Aggregate_Sum_SeedFunction(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;
            int actual = query.Aggregate(
                () => 0,
                (accumulator, x) => accumulator + x,
                (left, right) => left + right,
                result => result + ResultFuncModifier);

            Assert.Equal(Functions.SumRange(0, count) + ResultFuncModifier, actual);
        }
예제 #10
0
        public static void Aggregate_Product_SeedFunction(int count)
        {
            ParallelQuery <int> query = ParallelEnumerable.Range(1, count);
            long actual = query.Aggregate(
                () => 1L,
                (accumulator, x) => unchecked (accumulator * x),
                (left, right) => left * right,
                result => result + ResultFuncModifier);

            Assert.Equal(Functions.ProductRange(1, count) + ResultFuncModifier, actual);
        }
예제 #11
0
        public static void Aggregate_Sum_SeedFunction(int count)
        {
            ParallelQuery <int> query = UnorderedSources.Default(count);
            int actual = query.Aggregate(
                () => 0,
                (accumulator, x) => accumulator + x,
                (left, right) => left + right,
                result => result + ResultFuncModifier);

            Assert.Equal(Functions.SumRange(0, count) + ResultFuncModifier, actual);
        }
예제 #12
0
        public void AggregateTestCase()
        {
            ParallelTestHelper.Repeat(() => {
                ParallelQuery <int> range = ParallelEnumerable.Repeat(5, 2643);
                double average            = range.Aggregate(() => new double[2],
                                                            (acc, elem) => { acc[0] += elem; acc[1]++; return(acc); },
                                                            (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return(acc1); },
                                                            acc => acc[0] / acc[1]);

                Assert.AreEqual(5.0, average, "#1");
            });
        }
예제 #13
0
파일: PLinqTools.cs 프로젝트: xeekst/C-
        public static void InvokeJH()
        {
            List <int> numList = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };
            ParallelQuery <int> pList = numList.AsParallel();
            var pResult = pList.Aggregate(
                (result, item) =>
            {
                result += item;
                return(result);
            }
                );

            Console.WriteLine(pResult);
            var parallelQuery = from t in GetTypes().AsParallel() select t;

            //第一个参数种子值为累加器的初始值
            var parallelAggregator = parallelQuery.Aggregate(
                () =>
            {
                ConcurrentDictionary <char, int> cdic = new ConcurrentDictionary <char, int>();
                cdic.TryAdd('#', 9595);
                return(cdic);
            },
                //对单个分区的处理 返回单个分区结果
                (taskTotal, item) => AccumulateLettersInfomation(taskTotal, item),
                //对单个分区结果进行聚合,返回最终累加器结果
                (total, taskTotal) => MergeAccumulators(taskTotal),
                //对最后结果的的转换
                total =>
            {
                total.TryAdd('-', 10000);
                return(total);
            });

            Console.WriteLine();
            Console.WriteLine("There were the names");
            var orderedKeys = from k in parallelAggregator.Keys
                              orderby parallelAggregator[k] descending
                              select k;

            foreach (var c in orderedKeys)
            {
                Console.WriteLine("letter {0} --- {1}times", c, parallelAggregator[c]);
            }
        }
예제 #14
0
 /// <summary>Takes the top elements as if they were sorted.</summary>
 /// <typeparam name="TSource">Specifies the type of the elements.</typeparam>
 /// <typeparam name="TKey">Specifies the type of the keys used to compare elements.</typeparam>
 /// <param name="source">The source elements.</param>
 /// <param name="keySelector">A function used to extract a key from each element.</param>
 /// <param name="count">The number of elements to take.</param>
 /// <returns></returns>
 public static IEnumerable <TSource> TakeTop <TSource, TKey>(this ParallelQuery <TSource> source,
                                                             Func <TSource, TKey> keySelector, int count)
 {
     ParallelLinqExtensions.DescendingDefaultComparer <TKey> comparer =
         new ParallelLinqExtensions.DescendingDefaultComparer <TKey>();
     return(source.Aggregate <TSource, SortedTopN <TKey, TSource>, IEnumerable <TSource> >(
                (Func <SortedTopN <TKey, TSource> >)(() =>
                                                     new SortedTopN <TKey, TSource>(count, (IComparer <TKey>)comparer)),
                (Func <SortedTopN <TKey, TSource>, TSource, SortedTopN <TKey, TSource> >)((accum, item) =>
     {
         accum.Add(keySelector(item), item);
         return accum;
     }), (Func <SortedTopN <TKey, TSource>, SortedTopN <TKey, TSource>, SortedTopN <TKey, TSource> >)((accum1,
                                                                                                       accum2) =>
     {
         foreach (KeyValuePair <TKey, TSource> keyValuePair in accum2)
         {
             accum1.Add(keyValuePair);
         }
         return accum1;
     }), (Func <SortedTopN <TKey, TSource>, IEnumerable <TSource> >)(accum => accum.Values)));
 }
예제 #15
0
        /// <summary>Takes the top elements as if they were sorted.</summary>
        /// <typeparam name="TSource">Specifies the type of the elements.</typeparam>
        /// <typeparam name="TKey">Specifies the type of the keys used to compare elements.</typeparam>
        /// <param name="source">The source elements.</param>
        /// <param name="keySelector">A function used to extract a key from each element.</param>
        /// <param name="count">The number of elements to take.</param>
        /// <returns></returns>
        public static IEnumerable <TSource> TakeTop <TSource, TKey>(this ParallelQuery <TSource> source,
                                                                    Func <TSource, TKey> keySelector, int count)
        {
            DescendingDefaultComparer <TKey> comparer =
                new DescendingDefaultComparer <TKey>();

            return(source.Aggregate(
                       () =>
                       new SortedTopN <TKey, TSource>(count, comparer),
                       (accum, item) =>
            {
                accum.Add(keySelector(item), item);
                return accum;
            }, (accum1,
                accum2) =>
            {
                foreach (KeyValuePair <TKey, TSource> keyValuePair in accum2)
                {
                    accum1.Add(keyValuePair);
                }
                return accum1;
            }, accum => accum.Values));
        }
예제 #16
0
        public static void Aggregate_Sum_Seed(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(Functions.SumRange(0, count), query.Aggregate(0, (x, y) => x + y));
        }
예제 #17
0
        public static void Aggregate_Collection_Results(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(Enumerable.Range(0, count), query.Aggregate((IList <int>) new List <int>(), (l, x) => l.AddToCopy(x), l => l.OrderBy(x => x)));
        }
예제 #18
0
        public static void Aggregate_Product_Result(Labeled <ParallelQuery <int> > labeled, int count, int start)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(Functions.ProductRange(start, count) + ResultFuncModifier, query.Aggregate(1L, (x, y) => x * y, result => result + ResultFuncModifier));
        }
예제 #19
0
        public static void Aggregate_Sum_Result(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(Functions.SumRange(0, count) + ResultFuncModifier, query.Aggregate(0, (x, y) => x + y, result => result + ResultFuncModifier));
        }