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)); }
public static ParallelQuery <XElement> ExtractFormFields([NotNull][ItemNotNull] this ParallelQuery <XElement> documents) { if (documents is null) { throw new ArgumentNullException(nameof(documents)); } return(documents.Select(ExtractFormFields)); }
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(); }
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)); }
public static ParallelQuery <TSource> DoEvents <TSource>(this ParallelQuery <TSource> source) { return(source.Select( item => { Application.DoEvents(); Thread.Yield(); return item; })); }
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(); }
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(); }
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); }
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); }
/// <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)); }
public static ParallelQuery <XElement> Detach([NotNull][ItemNotNull] this ParallelQuery <XElement> elements) { return(elements.Select(x => { if (x.Parent != null) { x.Remove(); } return x; })); }
/// <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; })); }
/// <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; })); }
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); }
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(); }
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(); }
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); }
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); } }
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))); }
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))); }
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}"); }
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); }
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)); }
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)); }
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))); }
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(); }
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()); }
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); }
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(); }
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); }
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); }
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); }
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); }