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

            Assert.Equal(max, query.Max());
            Assert.Equal(max, query.Select(x => (int?)x).Max());
            Assert.Equal(0, query.Max(x => - x));
            Assert.Equal(0, query.Max(x => - (int?)x));
        }
예제 #2
0
        public static ParallelQuery <XElement> ExtractFormFields([NotNull][ItemNotNull] this ParallelQuery <XElement> documents)
        {
            if (documents is null)
            {
                throw new ArgumentNullException(nameof(documents));
            }

            return(documents.Select(ExtractFormFields));
        }
예제 #3
0
        public static void Select_Unordered_NotPipelined(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;
            IntegerRangeSet     seen  = new IntegerRangeSet(0, count);

            Assert.All(query.Select(x => KeyValuePair.Create(x, x * x)).ToList(),
                       p => { seen.Add(p.Key); Assert.Equal(p.Key * p.Key, p.Value); });
            seen.AssertComplete();
        }
예제 #4
0
        public static void Average_Int(Labeled <ParallelQuery <int> > labeled, int count, double average)
        {
            ParallelQuery <int> query = labeled.Item;

            Assert.Equal(average, query.Average());
            Assert.Equal((double?)average, query.Select(x => (int?)x).Average());
            Assert.Equal(-average, query.Average(x => - x));
            Assert.Equal(-(double?)average, query.Average(x => - (int?)x));
        }
예제 #5
0
 public static ParallelQuery <TSource> DoEvents <TSource>(this ParallelQuery <TSource> source)
 {
     return(source.Select(
                item =>
     {
         Application.DoEvents();
         Thread.Yield();
         return item;
     }));
 }
예제 #6
0
 public static void OfType_Unordered_SomeValid(Labeled<ParallelQuery<int>> labeled, int count)
 {
     ParallelQuery<int> query = labeled.Item;
     IntegerRangeSet seen = new IntegerRangeSet(count / 2, (count + 1) / 2);
     foreach (int i in query.Select(x => x >= count / 2 ? (object)x : x.ToString()).OfType<int>())
     {
         seen.Add(i);
     }
     seen.AssertComplete();
 }
예제 #7
0
 public static void OfType_Unordered_AllValid(Labeled<ParallelQuery<int>> labeled, int count)
 {
     ParallelQuery<int> query = labeled.Item;
     IntegerRangeSet seen = new IntegerRangeSet(0, count);
     foreach (int i in query.Select(x => (object)x).OfType<int>())
     {
         seen.Add(i);
     }
     seen.AssertComplete();
 }
예제 #8
0
 public static void OfType_SomeValid(Labeled<ParallelQuery<int>> labeled, int count)
 {
     ParallelQuery<int> query = labeled.Item;
     int seen = count / 2;
     foreach (int i in query.Select(x => x >= count / 2 ? (object)x : x.ToString()).OfType<int>())
     {
         Assert.Equal(seen++, i);
     }
     Assert.Equal(count, seen);
 }
예제 #9
0
 public static void OfType_AllValid(Labeled<ParallelQuery<int>> labeled, int count)
 {
     ParallelQuery<int> query = labeled.Item;
     int seen = 0;
     foreach (int i in query.Select(x => (object)x).OfType<int>())
     {
         Assert.Equal(seen++, i);
     }
     Assert.Equal(count, seen);
 }
예제 #10
0
 /// <summary>Implements a map-reduce operation.</summary>
 /// <typeparam name="TSource">Specifies the type of the source elements.</typeparam>
 /// <typeparam name="TMapped">Specifies the type of the mapped elements.</typeparam>
 /// <typeparam name="TKey">Specifies the type of the element keys.</typeparam>
 /// <typeparam name="TResult">Specifies the type of the results.</typeparam>
 /// <param name="source">The source elements.</param>
 /// <param name="map">A function used to get the target data from a source element.</param>
 /// <param name="keySelector">A function used to get a key from the target data.</param>
 /// <param name="reduce">A function used to reduce a group of elements.</param>
 /// <returns>The result elements of the reductions.</returns>
 public static ParallelQuery <TResult> MapReduce <TSource, TMapped, TKey, TResult>(
     this ParallelQuery <TSource> source,
     Func <TSource, TMapped> map,
     Func <TMapped, TKey> keySelector,
     Func <IGrouping <TKey, TMapped>, TResult> reduce)
 {
     return(source.
            Select(map).
            GroupBy(keySelector).
            Select(reduce));
 }
예제 #11
0
 public static ParallelQuery <XElement> Detach([NotNull][ItemNotNull] this ParallelQuery <XElement> elements)
 {
     return(elements.Select(x =>
     {
         if (x.Parent != null)
         {
             x.Remove();
         }
         return x;
     }));
 }
예제 #12
0
 /// <summary>
 /// When a thread is started, its culture is initially determined
 /// by using GetUserDefaultLCID from the Windows API. There seems to be no way
 /// to override this on initialization so this extension method provides
 /// functionality, to switch thread after it has been initialized
 /// </summary>
 /// <typeparam name="TSource">The type of the source.</typeparam>
 /// <param name="source">The source.</param>
 /// <param name="cultureInfo">The culture info.</param>
 /// <returns></returns>
 public static ParallelQuery <TSource> SetCulture <TSource>(this ParallelQuery <TSource> source, CultureInfo cultureInfo)
 {
     SetCulture(cultureInfo);
     return(source
            .Select(
                item =>
     {
         SetCulture(cultureInfo);
         return item;
     }));
 }
예제 #13
0
 /// <summary>
 /// <para>Define the culture of the thread as CultureInfo.InvariantCulture</para>
 /// <remarks>This is required since new threads will have the culture of the machine (to be changed in .NET 4.5)</remarks>
 /// </summary>
 /// <typeparam name="TSource"></typeparam>
 /// <param name="source"></param>
 /// <returns></returns>
 public static ParallelQuery <TSource> SetCulture <TSource>(this ParallelQuery <TSource> source)
 {
     SetCulture(CultureInfo.InvariantCulture);
     return(source
            .Select(
                item =>
     {
         SetCulture(CultureInfo.InvariantCulture);
         return item;
     }));
 }
예제 #14
0
        public static void Intersect_Distinct_NotPipelined(Labeled <ParallelQuery <int> > left, int leftCount, ParallelQuery <int> rightQuery, int rightCount, int count)
        {
            ParallelQuery <int> leftQuery = left.Item;

            leftCount  = Math.Min(DuplicateFactor * 2, leftCount);
            rightCount = Math.Min(DuplicateFactor, (rightCount + 1) / 2);
            int seen = 0;

            Assert.All(leftQuery.Intersect(rightQuery.Select(x => Math.Abs(x) % DuplicateFactor), new ModularCongruenceComparer(DuplicateFactor * 2)).ToList(), x => Assert.Equal(seen++, x));
            Assert.Equal(Math.Min(leftCount, rightCount), seen);
        }
예제 #15
0
        public static void Intersect_Unordered_Distinct_NotPipelined(Labeled <ParallelQuery <int> > left, int leftCount, Labeled <ParallelQuery <int> > right, int rightCount, int count)
        {
            ParallelQuery <int> leftQuery  = left.Item;
            ParallelQuery <int> rightQuery = right.Item;

            leftCount  = Math.Min(DuplicateFactor * 2, leftCount);
            rightCount = Math.Min(DuplicateFactor, (rightCount + 1) / 2);
            IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(leftCount, rightCount));

            Assert.All(leftQuery.Intersect(rightQuery.Select(x => Math.Abs(x) % DuplicateFactor), new ModularCongruenceComparer(DuplicateFactor * 2)).ToList(), x => seen.Add(x % (DuplicateFactor * 2)));
            seen.AssertComplete();
        }
예제 #16
0
        public static void Select_Unordered(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;
            IntegerRangeSet     seen  = new IntegerRangeSet(0, count);

            foreach (var p in query.Select(x => KeyValuePair.Create(x, x * x)))
            {
                seen.Add(p.Key);
                Assert.Equal(p.Key * p.Key, p.Value);
            }
            seen.AssertComplete();
        }
예제 #17
0
        public static void Select(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;
            int seen = 0;

            foreach (var p in query.Select(x => KeyValuePair.Create(x, x * x)))
            {
                Assert.Equal(seen++, p.Key);
                Assert.Equal(p.Key * p.Key, p.Value);
            }
            Assert.Equal(count, seen);
        }
예제 #18
0
        public static void ExecuteManyWithAggregateException <T>(this ParallelQuery <T> items, Action <T> function)
        {
            var exceptions = items
                             .Select(i => ExecuteAndCatch(function, i))
                             .Where(e => e != null)
                             .ToList();

            if (exceptions.Any())
            {
                throw new AggregateException(exceptions);
            }
        }
예제 #19
0
        public static ParallelQuery <XElement> RemoveByAll([NotNull][ItemNotNull] this ParallelQuery <XElement> elements, [NotNull] Func <XElement, bool> predicate)
        {
            if (elements is null)
            {
                throw new ArgumentNullException(nameof(elements));
            }
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            return(elements.Select(x => x.RemoveByAll(predicate)));
        }
예제 #20
0
        public static ParallelQuery <XElement> ChangeXAttributeValues([NotNull][ItemNotNull]  this ParallelQuery <XElement> elements, [NotNull] XName name, [CanBeNull] string value)
        {
            if (elements is null)
            {
                throw new ArgumentNullException(nameof(elements));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(elements.Select(x => x.ChangeXAttributeValues(name, value)));
        }
예제 #21
0
        private void DelegateHandle()
        {
            Func <int, int> SelectDivision = (i) =>
            {
                try
                {
                    return(i / (i - 10));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(@$ "{Environment.NewLine}Type Exception: {ex.GetType().Name} | Message: {ex.Message} | StackTrace: {ex.StackTrace} |");
                    return(-1);
                }
            };

            Stopwatch watch = null;

            ParallelQuery <int> query = ParallelQueryRange.Select(i => SelectDivision(i)).WithDegreeOfParallelism(2);

            watch = Stopwatch.StartNew();

            try
            {
                query.ForAll(item => Console.Write($"{item}:{watch.ElapsedMilliseconds} |"));
            }
            catch (AggregateException aggregateException)
            {
                foreach (var ex in aggregateException.InnerExceptions)
                {
                    Console.WriteLine(ex.Message);
                    if (ex is DivideByZeroException)
                    {
                        Console.WriteLine("Attempt to divide by zero. Query stopped.");
                    }
                }
            }

            Console.WriteLine($"{Environment.NewLine}Full Result returned in { watch.ElapsedMilliseconds} ms{Environment.NewLine}");
        }
예제 #22
0
        public static void Select_NotPipelined(Labeled <ParallelQuery <int> > labeled, int count)
        {
            ParallelQuery <int> query = labeled.Item;
            int seen = 0;

            Assert.All(query.Select(x => KeyValuePair.Create(x, x * x)).ToList(),
                       p =>
            {
                Assert.Equal(seen++, p.Key);
                Assert.Equal(p.Key * p.Key, p.Value);
            });
            Assert.Equal(count, seen);
        }
예제 #23
0
        internal static ParallelQuery <int> MakeSelect(bool orderPreserved)
        {
            int[] a = Enumerable.Range(-99, 100).Reverse().ToArray();

            ParallelQuery <int> ipe = a.AsParallel();

            if (orderPreserved)
            {
                ipe = ipe.AsOrdered();
            }

            return(ipe.Select(i => - i));
        }
예제 #24
0
        private static ParallelQuery <IGrouping <int, KeyValuePair <int, T> > > SplitToBatches <T>(IEnumerable <T> source, int poolSize, int batches)
        {
            ParallelQuery <T> asParallel = source.AsParallel().AsOrdered();

            if (poolSize > 0)
            {
                asParallel = asParallel.WithDegreeOfParallelism(poolSize);
            }

            return(asParallel
                   .Select((x, i) => new KeyValuePair <int, T>(i, x))
                   .GroupBy(x => x.Key / batches));
        }
예제 #25
0
        public static ParallelQuery <XElement> RemoveByAllIfEmpty([NotNull][ItemNotNull] this ParallelQuery <XElement> elements, [NotNull] XName name, bool removeElements = true, bool removeAttributes = true)
        {
            if (elements is null)
            {
                throw new ArgumentNullException(nameof(elements));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(elements.Select(x => x.RemoveByAllIfEmpty(name, removeElements, removeAttributes)));
        }
예제 #26
0
        public static void Select_Indexed_Unordered(Labeled <ParallelQuery <int> > labeled, int count)
        {
            // For unordered collections, which element is at which index isn't actually guaranteed, but an effect of the implementation.
            // If this test starts failing it should be updated, and possibly mentioned in release notes.
            ParallelQuery <int> query = labeled.Item;
            IntegerRangeSet     seen  = new IntegerRangeSet(0, count);

            foreach (var p in query.Select((x, index) => KeyValuePair.Create(x, index)))
            {
                seen.Add(p.Key);
                Assert.Equal(p.Key, p.Value);
            }
            seen.AssertComplete();
        }
예제 #27
0
        internal static IReadOnlyList <SudokuSample> ParseCsv()
        {
            var options = new CsvParserOptions(
                /* skipHeader = */ true,
                ',',
                /* degreeOfParallelism = */ 12,
                /* keepOrder = */ true);
            var mapping = new SudokuSampleMapping();
            var parser  = new CsvParser <SudokuSample>(options, mapping);

            ParallelQuery <TinyCsvParser.Mapping.CsvMappingResult <SudokuSample> > results = parser.ReadFromFile(_csvPath, Encoding.ASCII);

            return(results.Select(result => result.Result).ToList());
        }
예제 #28
0
 public static void OfType_SomeValidNull(Labeled<ParallelQuery<int>> labeled, int count)
 {
     ParallelQuery<int> query = labeled.Item;
     int nullCount = 0;
     int seen = count / 2;
     Assert.All(query.Select(x => x >= count / 2 ? (object)(x.ToString()) : (object)(string)null).OfType<string>(),
         x =>
         {
             if (string.IsNullOrEmpty(x)) nullCount++;
             else Assert.Equal(seen++, int.Parse(x));
         });
     Assert.Equal(count, seen);
     Assert.Equal(0, nullCount);
 }
예제 #29
0
        public static void Except_Unordered_Distinct_NotPipelined(int leftCount, int rightStart, int rightCount, int start, int count)
        {
            ParallelQuery <int> leftQuery  = UnorderedSources.Default(leftCount);
            ParallelQuery <int> rightQuery = UnorderedSources.Default(rightStart, rightCount);

            leftCount  = Math.Min(DuplicateFactor * 2, leftCount);
            rightCount = Math.Min(DuplicateFactor, (rightCount + 1) / 2);
            int             expectedCount = Math.Max(0, leftCount - rightCount);
            IntegerRangeSet seen          = new IntegerRangeSet(leftCount - expectedCount, expectedCount);

            Assert.All(leftQuery.Except(rightQuery.Select(x => Math.Abs(x) % DuplicateFactor),
                                        new ModularCongruenceComparer(DuplicateFactor * 2)).ToList(), x => seen.Add(x % (DuplicateFactor * 2)));
            seen.AssertComplete();
        }
예제 #30
0
 public static void OfType_SomeNull(Labeled<ParallelQuery<int>> labeled, int count)
 {
     ParallelQuery<int> query = labeled.Item;
     int nullCount = 0;
     int seen = count / 2;
     Assert.All(query.Select(x => x >= count / 2 ? (object)(int?)x : (int?)null).OfType<int?>(),
         x =>
         {
             if (!x.HasValue) nullCount++;
             else Assert.Equal(seen++, x.Value);
         });
     Assert.Equal(count, seen);
     Assert.Equal(0, nullCount);
 }
예제 #31
0
 public static void Intersect_Distinct_NotPipelined(Labeled<ParallelQuery<int>> left, int leftCount, ParallelQuery<int> rightQuery, int rightCount, int count)
 {
     ParallelQuery<int> leftQuery = left.Item;
     leftCount = Math.Min(DuplicateFactor * 2, leftCount);
     rightCount = Math.Min(DuplicateFactor, (rightCount + 1) / 2);
     int seen = 0;
     Assert.All(leftQuery.Intersect(rightQuery.Select(x => Math.Abs(x) % DuplicateFactor), new ModularCongruenceComparer(DuplicateFactor * 2)).ToList(), x => Assert.Equal(seen++, x));
     Assert.Equal(Math.Min(leftCount, rightCount), seen);
 }
예제 #32
0
 public static void Intersect_Distinct(Labeled<ParallelQuery<int>> left, int leftCount, ParallelQuery<int> rightQuery, int rightCount, int count)
 {
     ParallelQuery<int> leftQuery = left.Item;
     leftCount = Math.Min(DuplicateFactor * 2, leftCount);
     rightCount = Math.Min(DuplicateFactor, (rightCount + 1) / 2);
     int seen = 0;
     foreach (int i in leftQuery.Intersect(rightQuery.Select(x => Math.Abs(x) % DuplicateFactor), new ModularCongruenceComparer(DuplicateFactor * 2)))
     {
         Assert.Equal(seen++, i);
     }
     Assert.Equal(Math.Min(leftCount, rightCount), seen);
 }
예제 #33
0
 public static void Except_Distinct(Labeled<ParallelQuery<int>> left, int leftCount, ParallelQuery<int> rightQuery, int rightCount, int start, int count)
 {
     ParallelQuery<int> leftQuery = left.Item;
     leftCount = Math.Min(DuplicateFactor * 2, leftCount);
     rightCount = Math.Min(DuplicateFactor, (rightCount + 1) / 2);
     int expectedCount = Math.Max(0, leftCount - rightCount);
     int seen = expectedCount == 0 ? 0 : leftCount - expectedCount;
     foreach (int i in leftQuery.Except(rightQuery.Select(x => Math.Abs(x) % DuplicateFactor), new ModularCongruenceComparer(DuplicateFactor * 2)))
     {
         Assert.Equal(seen++, i);
     }
     Assert.Equal(expectedCount == 0 ? 0 : leftCount, seen);
 }