2 // ParallelEnumerable.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33 using System.Linq.Parallel;
34 using System.Linq.Parallel.QueryNodes;
38 public static class ParallelEnumerable
40 #region Range & Repeat
41 public static ParallelQuery<int> Range (int start, int count)
43 if (int.MaxValue - start < count)
44 throw new ArgumentOutOfRangeException ("count", "start + count - 1 is larger than Int32.MaxValue");
46 throw new ArgumentOutOfRangeException ("count", "count is less than 0");
48 return (new RangeList (start, count)).AsParallel ();
51 public static ParallelQuery<TResult> Repeat<TResult> (TResult obj, int count)
54 throw new ArgumentOutOfRangeException ("count", "count is less than 0");
56 return (new RepeatList<TResult> (obj, count)).AsParallel ();
61 public static ParallelQuery<TResult> Empty<TResult> ()
63 return Repeat<TResult> (default (TResult), 0);
68 public static ParallelQuery<TSource> AsParallel<TSource> (this IEnumerable<TSource> source)
71 throw new ArgumentNullException ("source");
73 return new ParallelQuery<TSource> (new QueryStartNode<TSource> (source));
76 public static ParallelQuery<TSource> AsParallel<TSource> (this Partitioner<TSource> source)
79 throw new ArgumentNullException ("source");
81 return new ParallelQuery<TSource> (new QueryStartNode<TSource> (source));
84 public static ParallelQuery AsParallel (this IEnumerable source)
87 throw new ArgumentNullException ("source");
89 return new ParallelQuery<object> (new QueryStartNode<object> (source.Cast<object> ()));
92 public static IEnumerable<TSource> AsEnumerable<TSource> (this ParallelQuery<TSource> source)
95 throw new ArgumentNullException ("source");
97 return source.AsSequential ();
100 public static IEnumerable<TSource> AsSequential<TSource> (this ParallelQuery<TSource> source)
103 throw new ArgumentNullException ("source");
105 return source.Node.GetSequential ();
109 #region AsOrdered / AsUnordered
110 public static ParallelQuery<TSource> AsOrdered<TSource> (this ParallelQuery<TSource> source)
113 throw new ArgumentNullException ("source");
115 return new ParallelQuery<TSource> (new QueryAsOrderedNode<TSource> (source.Node));
118 public static ParallelQuery<TSource> AsUnordered<TSource> (this ParallelQuery<TSource> source)
121 throw new ArgumentNullException ("source");
123 return new ParallelQuery<TSource> (new QueryAsUnorderedNode<TSource> (source.Node));
126 public static ParallelQuery AsOrdered (this ParallelQuery source)
129 throw new ArgumentNullException ("source");
131 return source.TypedQuery.AsOrdered ();
136 public static ParallelQuery<TSource> WithExecutionMode<TSource> (this ParallelQuery<TSource> source,
137 ParallelExecutionMode executionMode)
140 throw new ArgumentNullException ("source");
142 return new ParallelQuery<TSource> (new ParallelExecutionModeNode<TSource> (executionMode, source.Node));
145 public static ParallelQuery<TSource> WithCancellation<TSource> (this ParallelQuery<TSource> source,
146 CancellationToken cancellationToken)
149 throw new ArgumentNullException ("source");
151 return new ParallelQuery<TSource> (new CancellationTokenNode<TSource> (cancellationToken, source.Node));
154 public static ParallelQuery<TSource> WithMergeOptions<TSource> (this ParallelQuery<TSource> source,
155 ParallelMergeOptions mergeOptions)
158 throw new ArgumentNullException ("source");
160 return new ParallelQuery<TSource> (new ParallelMergeOptionsNode<TSource> (mergeOptions, source.Node));
163 public static ParallelQuery<TSource> WithDegreeOfParallelism<TSource> (this ParallelQuery<TSource> source,
164 int degreeParallelism)
166 if (degreeParallelism < 1 || degreeParallelism > 63)
167 throw new ArgumentException ("degreeOfParallelism is less than 1 or greater than 63", "degreeParallelism");
169 throw new ArgumentNullException ("source");
171 return new ParallelQuery<TSource> (new DegreeOfParallelismNode<TSource> (degreeParallelism, source.Node));
174 internal static ParallelQuery<TSource> WithImplementerToken<TSource> (this ParallelQuery<TSource> source,
175 CancellationTokenSource token)
177 return new ParallelQuery<TSource> (new ImplementerTokenNode<TSource> (token, source.Node));
182 public static ParallelQuery<TResult> Select<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, TResult> selector)
185 throw new ArgumentNullException ("source");
186 if (selector == null)
187 throw new ArgumentNullException ("selector");
189 return new ParallelQuery<TResult> (new QuerySelectNode<TResult, TSource> (source.Node, selector));
192 public static ParallelQuery<TResult> Select<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, int, TResult> selector)
195 throw new ArgumentNullException ("source");
196 if (selector == null)
197 throw new ArgumentNullException ("selector");
199 return new ParallelQuery<TResult> (new QuerySelectNode<TResult, TSource> (source.Node, selector));
204 public static ParallelQuery<TResult> SelectMany<TSource, TResult> (this ParallelQuery<TSource> source,
205 Func<TSource, IEnumerable<TResult>> selector)
207 return source.SelectMany (selector, (s, e) => e);
210 public static ParallelQuery<TResult> SelectMany<TSource, TResult> (this ParallelQuery<TSource> source,
211 Func<TSource, int, IEnumerable<TResult>> selector)
213 return source.SelectMany (selector, (s, e) => e);
216 public static ParallelQuery<TResult> SelectMany<TSource, TCollection, TResult> (this ParallelQuery<TSource> source,
217 Func<TSource, IEnumerable<TCollection>> collectionSelector,
218 Func<TSource, TCollection, TResult> resultSelector)
220 return new ParallelQuery<TResult> (new QuerySelectManyNode<TSource, TCollection, TResult> (source.Node,
225 public static ParallelQuery<TResult> SelectMany<TSource, TCollection, TResult> (this ParallelQuery<TSource> source,
226 Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
227 Func<TSource, TCollection, TResult> resultSelector)
229 return new ParallelQuery<TResult> (new QuerySelectManyNode<TSource, TCollection, TResult> (source.Node,
236 public static ParallelQuery<TSource> Where<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
239 throw new ArgumentNullException ("source");
240 if (predicate == null)
241 throw new ArgumentNullException ("predicate");
243 return new ParallelQuery<TSource> (new QueryWhereNode<TSource> (source.Node, predicate));
246 public static ParallelQuery<TSource> Where<TSource> (this ParallelQuery<TSource> source, Func<TSource, int, bool> predicate)
249 throw new ArgumentNullException ("source");
250 if (predicate == null)
251 throw new ArgumentNullException ("predicate");
253 return new ParallelQuery<TSource> (new QueryWhereNode<TSource> (source.Node, predicate));
258 public static TSource Aggregate<TSource> (this ParallelQuery<TSource> source, Func<TSource, TSource, TSource> func)
261 throw new ArgumentNullException ("source");
263 throw new ArgumentNullException ("func");
265 return source.Aggregate<TSource, TSource, TSource> ((Func<TSource>)null,
271 public static TAccumulate Aggregate<TSource, TAccumulate> (this ParallelQuery<TSource> source,
273 Func<TAccumulate, TSource, TAccumulate> func)
276 throw new ArgumentNullException ("source");
278 throw new ArgumentNullException ("func");
280 return source.Aggregate (seed, func, (e) => e);
283 public static TResult Aggregate<TSource, TAccumulate, TResult> (this ParallelQuery<TSource> source,
285 Func<TAccumulate, TSource, TAccumulate> func,
286 Func<TAccumulate, TResult> resultSelector)
289 throw new ArgumentNullException ("source");
291 throw new ArgumentNullException ("func");
292 if (resultSelector == null)
293 throw new ArgumentNullException ("resultSelector");
295 TAccumulate accumulator = seed;
297 foreach (TSource value in source)
298 accumulator = func (accumulator, value);
300 return resultSelector (accumulator);
303 public static TResult Aggregate<TSource, TAccumulate, TResult> (this ParallelQuery<TSource> source,
305 Func<TAccumulate, TSource, TAccumulate> updateAccumulatorFunc,
306 Func<TAccumulate, TAccumulate, TAccumulate> combineAccumulatorsFunc,
307 Func<TAccumulate, TResult> resultSelector)
310 throw new ArgumentNullException ("source");
311 if (updateAccumulatorFunc == null)
312 throw new ArgumentNullException ("updateAccumulatorFunc");
313 if (combineAccumulatorsFunc == null)
314 throw new ArgumentNullException ("combineAccumulatorsFunc");
315 if (resultSelector == null)
316 throw new ArgumentNullException ("resultSelector");
318 return source.Aggregate (() => seed, updateAccumulatorFunc, combineAccumulatorsFunc, resultSelector);
321 public static TResult Aggregate<TSource, TAccumulate, TResult> (this ParallelQuery<TSource> source,
322 Func<TAccumulate> seedFunc,
323 Func<TAccumulate, TSource, TAccumulate> updateAccumulatorFunc,
324 Func<TAccumulate, TAccumulate, TAccumulate> combineAccumulatorsFunc,
325 Func<TAccumulate, TResult> resultSelector)
328 throw new ArgumentNullException ("source");
329 if (seedFunc == null)
330 throw new ArgumentNullException ("seedFunc");
331 if (updateAccumulatorFunc == null)
332 throw new ArgumentNullException ("updateAccumulatorFunc");
333 if (combineAccumulatorsFunc == null)
334 throw new ArgumentNullException ("combineAccumulatorsFunc");
335 if (resultSelector == null)
336 throw new ArgumentNullException ("resultSelector");
338 TAccumulate accumulator = default (TAccumulate);
340 ParallelExecuter.ProcessAndAggregate<TSource, TAccumulate> (source.Node, seedFunc, updateAccumulatorFunc, (list) => {
341 accumulator = list [0];
342 for (int i = 1; i < list.Count; i++)
343 accumulator = combineAccumulatorsFunc (accumulator, list[i]);
346 return resultSelector (accumulator);;
351 public static void ForAll<TSource> (this ParallelQuery<TSource> source, Action<TSource> action)
354 throw new ArgumentNullException ("source");
356 throw new ArgumentNullException ("action");
358 ParallelExecuter.ProcessAndBlock (source.Node, (e, c) => action (e));
363 public static OrderedParallelQuery<TSource> OrderByDescending<TSource, TKey> (this ParallelQuery<TSource> source,
364 Func<TSource, TKey> keySelector,
365 IComparer<TKey> comparer)
368 throw new ArgumentNullException ("source");
369 if (keySelector == null)
370 throw new ArgumentNullException ("keySelector");
371 if (comparer == null)
372 comparer = Comparer<TKey>.Default;
374 Comparison<TSource> comparison = (e1, e2) => -comparer.Compare (keySelector (e1), keySelector (e2));
376 return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
379 public static OrderedParallelQuery<TSource> OrderByDescending<TSource, TKey> (this ParallelQuery<TSource> source,
380 Func<TSource, TKey> keySelector)
382 return OrderByDescending (source, keySelector, Comparer<TKey>.Default);
385 public static OrderedParallelQuery<TSource> OrderBy<TSource, TKey> (this ParallelQuery<TSource> source,
386 Func<TSource, TKey> keySelector)
388 return OrderBy (source, keySelector, Comparer<TKey>.Default);
391 public static OrderedParallelQuery<TSource> OrderBy<TSource, TKey> (this ParallelQuery<TSource> source,
392 Func<TSource, TKey> keySelector,
393 IComparer<TKey> comparer)
396 throw new ArgumentNullException ("source");
397 if (keySelector == null)
398 throw new ArgumentNullException ("keySelector");
399 if (comparer == null)
400 comparer = Comparer<TKey>.Default;
402 Comparison<TSource> comparison = (e1, e2) => comparer.Compare (keySelector (e1), keySelector (e2));
404 return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
409 public static OrderedParallelQuery<TSource> ThenBy<TSource, TKey> (this OrderedParallelQuery<TSource> source,
410 Func<TSource, TKey> keySelector)
412 return ThenBy (source, keySelector, Comparer<TKey>.Default);
415 public static OrderedParallelQuery<TSource> ThenBy<TSource, TKey> (this OrderedParallelQuery<TSource> source,
416 Func<TSource, TKey> keySelector,
417 IComparer<TKey> comparer)
420 throw new ArgumentNullException ("source");
421 if (keySelector == null)
422 throw new ArgumentNullException ("keySelector");
423 if (comparer == null)
424 comparer = Comparer<TKey>.Default;
426 Comparison<TSource> comparison = (e1, e2) => comparer.Compare (keySelector (e1), keySelector (e2));
428 return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
431 public static OrderedParallelQuery<TSource> ThenByDescending<TSource, TKey> (this OrderedParallelQuery<TSource> source,
432 Func<TSource, TKey> keySelector)
434 return ThenByDescending (source, keySelector, Comparer<TKey>.Default);
437 public static OrderedParallelQuery<TSource> ThenByDescending<TSource, TKey> (this OrderedParallelQuery<TSource> source,
438 Func<TSource, TKey> keySelector,
439 IComparer<TKey> comparer)
442 throw new ArgumentNullException ("source");
443 if (keySelector == null)
444 throw new ArgumentNullException ("keySelector");
445 if (comparer == null)
446 comparer = Comparer<TKey>.Default;
448 Comparison<TSource> comparison = (e1, e2) => -comparer.Compare (keySelector (e1), keySelector (e2));
450 return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
455 public static bool All<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
458 throw new ArgumentNullException ("source");
459 if (predicate == null)
460 throw new ArgumentNullException ("predicate");
462 CancellationTokenSource src = new CancellationTokenSource ();
463 ParallelQuery<TSource> innerQuery = source.WithImplementerToken (src);
467 innerQuery.ForAll ((e) => {
468 if (!predicate (e)) {
473 } catch (OperationCanceledException e) {
474 if (e.CancellationToken != src.Token)
483 public static bool Any<TSource> (this ParallelQuery<TSource> source)
485 return Any<TSource> (source, (_) => true);
488 public static bool Any<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
491 throw new ArgumentNullException ("source");
492 if (predicate == null)
493 throw new ArgumentNullException ("predicate");
495 return !source.All ((e) => !predicate (e));
500 public static bool Contains<TSource> (this ParallelQuery<TSource> source, TSource value)
502 return Contains<TSource> (source, value, EqualityComparer<TSource>.Default);
505 public static bool Contains<TSource> (this ParallelQuery<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
508 throw new ArgumentNullException ("source");
509 if (comparer == null)
510 comparer = EqualityComparer<TSource>.Default;
512 return Any<TSource> (source, (e) => comparer.Equals (value));
516 #region SequenceEqual
517 public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first,
518 ParallelQuery<TSource> second)
521 throw new ArgumentNullException ("first");
523 throw new ArgumentNullException ("second");
525 return first.SequenceEqual (second, EqualityComparer<TSource>.Default);
528 public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first,
529 ParallelQuery<TSource> second,
530 IEqualityComparer<TSource> comparer)
533 throw new ArgumentNullException ("first");
535 throw new ArgumentNullException ("second");
536 if (comparer == null)
537 comparer = EqualityComparer<TSource>.Default;
539 CancellationTokenSource source = new CancellationTokenSource ();
540 ParallelQuery<bool> innerQuery
541 = first.Zip (second, (e1, e2) => comparer.Equals (e1, e2)).Where ((e) => !e).WithImplementerToken (source);
546 innerQuery.ForAll ((value) => {
550 } catch (OperationCanceledException e) {
551 if (e.CancellationToken != source.Token)
558 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
559 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
560 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
561 public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first, IEnumerable<TSource> second)
563 throw new NotSupportedException ();
566 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
567 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
568 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
569 public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first,
570 IEnumerable<TSource> second,
571 IEqualityComparer<TSource> comparer)
573 throw new NotSupportedException ();
579 public static ParallelQuery<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this ParallelQuery<TSource> source,
580 Func<TSource, TKey> keySelector)
582 return source.GroupBy (keySelector, EqualityComparer<TKey>.Default);
585 public static ParallelQuery<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this ParallelQuery<TSource> source,
586 Func<TSource, TKey> keySelector,
587 IEqualityComparer<TKey> comparer)
589 return source.GroupBy (keySelector, (e) => e, comparer);
592 public static ParallelQuery<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
593 Func<TSource, TKey> keySelector,
594 Func<TSource, TElement> elementSelector)
596 return source.GroupBy (keySelector, elementSelector, EqualityComparer<TKey>.Default);
599 public static ParallelQuery<TResult> GroupBy<TSource, TKey, TResult> (this ParallelQuery<TSource> source,
600 Func<TSource, TKey> keySelector,
601 Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
603 return source.GroupBy (keySelector)
604 .Select ((g) => resultSelector (g.Key, (IEnumerable<TSource>)g));
607 public static ParallelQuery<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
608 Func<TSource, TKey> keySelector,
609 Func<TSource, TElement> elementSelector,
610 IEqualityComparer<TKey> comparer)
613 throw new ArgumentNullException ("source");
614 if (keySelector == null)
615 throw new ArgumentNullException ("keySelector");
616 if (elementSelector == null)
617 throw new ArgumentNullException ("elementSelector");
618 if (comparer == null)
619 comparer = EqualityComparer<TKey>.Default;
621 return new ParallelQuery<IGrouping<TKey, TElement>> (new QueryGroupByNode<TSource, TKey, TElement> (source.Node, keySelector, elementSelector, comparer));
624 public static ParallelQuery<TResult> GroupBy<TSource, TKey, TElement, TResult> (this ParallelQuery<TSource> source,
625 Func<TSource, TKey> keySelector,
626 Func<TSource, TElement> elementSelector,
627 Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
629 return source.GroupBy (keySelector, elementSelector)
630 .Select ((g) => resultSelector (g.Key, (IEnumerable<TElement>)g));
633 public static ParallelQuery<TResult> GroupBy<TSource, TKey, TResult> (this ParallelQuery<TSource> source,
634 Func<TSource, TKey> keySelector,
635 Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
636 IEqualityComparer<TKey> comparer)
638 return source.GroupBy (keySelector, comparer)
639 .Select ((g) => resultSelector (g.Key, (IEnumerable<TSource>)g));
642 public static ParallelQuery<TResult> GroupBy<TSource, TKey, TElement, TResult> (this ParallelQuery<TSource> source,
643 Func<TSource, TKey> keySelector,
644 Func<TSource, TElement> elementSelector,
645 Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
646 IEqualityComparer<TKey> comparer)
648 return source.GroupBy (keySelector, elementSelector, comparer)
649 .Select ((g) => resultSelector (g.Key, (IEnumerable<TElement>)g));
654 public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
655 ParallelQuery<TInner> inner,
656 Func<TOuter, TKey> outerKeySelector,
657 Func<TInner, TKey> innerKeySelector,
658 Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
660 return outer.GroupJoin (inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
663 public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
664 ParallelQuery<TInner> inner,
665 Func<TOuter, TKey> outerKeySelector,
666 Func<TInner, TKey> innerKeySelector,
667 Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
668 IEqualityComparer<TKey> comparer)
670 return outer.Join (inner.GroupBy (innerKeySelector, (e) => e), outerKeySelector, (e) => e.Key, (e1, e2) => resultSelector (e1, e2), comparer);
673 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
674 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
675 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
676 public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
677 IEnumerable<TInner> inner,
678 Func<TOuter, TKey> outerKeySelector,
679 Func<TInner, TKey> innerKeySelector,
680 Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
682 throw new NotSupportedException ();
685 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
686 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
687 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
688 public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
689 IEnumerable<TInner> inner,
690 Func<TOuter, TKey> outerKeySelector,
691 Func<TInner, TKey> innerKeySelector,
692 Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
693 IEqualityComparer<TKey> comparer)
695 throw new NotSupportedException ();
700 public static TSource ElementAt<TSource> (this ParallelQuery<TSource> source, int index)
703 throw new ArgumentNullException ("source");
705 throw new ArgumentOutOfRangeException ("index");
708 return source.First ();
709 } catch (InvalidOperationException) {
710 throw new ArgumentOutOfRangeException ("index");
714 TSource result = default (TSource);
716 ParallelQuery<TSource> innerQuery = source.Where ((e, i) => i == index);
719 result = innerQuery.First ();
720 } catch (InvalidOperationException) {
721 throw new ArgumentOutOfRangeException ("index");
727 public static TSource ElementAtOrDefault<TSource> (this ParallelQuery<TSource> source, int index)
730 throw new ArgumentNullException ("source");
733 return source.ElementAt (index);
734 } catch (ArgumentOutOfRangeException) {
735 return default (TSource);
741 public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first,
742 ParallelQuery<TSource> second)
744 return Intersect<TSource> (first, second, EqualityComparer<TSource>.Default);
747 public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first,
748 ParallelQuery<TSource> second,
749 IEqualityComparer<TSource> comparer)
752 throw new ArgumentNullException ("first");
754 throw new ArgumentNullException ("second");
755 if (comparer == null)
756 comparer = EqualityComparer<TSource>.Default;
758 return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Intersect, comparer, first.Node, second.Node));
761 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
762 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
763 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
764 public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first, IEnumerable<TSource> second)
766 throw new NotSupportedException ();
769 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
770 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
771 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
772 public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first,
773 IEnumerable<TSource> second,
774 IEqualityComparer<TSource> comparer)
776 throw new NotSupportedException ();
781 public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
782 ParallelQuery<TInner> inner,
783 Func<TOuter, TKey> outerKeySelector,
784 Func<TInner, TKey> innerKeySelector,
785 Func<TOuter, TInner, TResult> resultSelector)
787 return outer.Join (inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
790 public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
791 ParallelQuery<TInner> inner,
792 Func<TOuter, TKey> outerKeySelector,
793 Func<TInner, TKey> innerKeySelector,
794 Func<TOuter, TInner, TResult> resultSelector,
795 IEqualityComparer<TKey> comparer)
797 return new ParallelQuery<TResult> (new QueryJoinNode<TOuter, TInner, TKey, TResult> (outer.Node, inner.Node, outerKeySelector, innerKeySelector, resultSelector, comparer));
800 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
801 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
802 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
803 public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
804 IEnumerable<TInner> inner,
805 Func<TOuter, TKey> outerKeySelector,
806 Func<TInner, TKey> innerKeySelector,
807 Func<TOuter, TInner, TResult> resultSelector)
809 throw new NotSupportedException ();
812 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
813 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
814 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
815 public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
816 IEnumerable<TInner> inner,
817 Func<TOuter, TKey> outerKeySelector,
818 Func<TInner, TKey> innerKeySelector,
819 Func<TOuter, TInner, TResult> resultSelector,
820 IEqualityComparer<TKey> comparer)
822 throw new NotSupportedException ();
827 public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
828 ParallelQuery<TSource> second)
830 return Except<TSource> (first, second, EqualityComparer<TSource>.Default);
833 public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
834 ParallelQuery<TSource> second,
835 IEqualityComparer<TSource> comparer)
838 throw new ArgumentNullException ("first");
840 throw new ArgumentNullException ("second");
841 if (comparer == null)
842 comparer = EqualityComparer<TSource>.Default;
844 return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Except,
845 comparer, first.Node, second.Node));
848 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
849 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
850 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
851 public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
852 IEnumerable<TSource> second)
854 throw new NotSupportedException ();
857 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
858 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
859 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
860 public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
861 IEnumerable<TSource> second,
862 IEqualityComparer<TSource> comparer)
864 throw new NotSupportedException ();
869 public static ParallelQuery<TSource> Distinct<TSource> (this ParallelQuery<TSource> source)
871 return Distinct<TSource> (source, EqualityComparer<TSource>.Default);
874 public static ParallelQuery<TSource> Distinct<TSource> (this ParallelQuery<TSource> source, IEqualityComparer<TSource> comparer)
877 throw new ArgumentNullException ("source");
878 if (comparer == null)
879 comparer = EqualityComparer<TSource>.Default;
881 return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Distinct, comparer,
887 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
888 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
889 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
890 public static ParallelQuery<TSource> Union<TSource> (this ParallelQuery<TSource> first,
891 IEnumerable<TSource> second)
893 throw new NotSupportedException ();
896 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
897 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
898 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
899 public static ParallelQuery<TSource> Union<TSource>(this ParallelQuery<TSource> first,
900 IEnumerable<TSource> second,
901 IEqualityComparer<TSource> comparer)
903 throw new NotSupportedException ();
906 public static ParallelQuery<TSource> Union<TSource> (this ParallelQuery<TSource> first,
907 ParallelQuery<TSource> second)
909 return first.Union (second, EqualityComparer<TSource>.Default);
912 public static ParallelQuery<TSource> Union<TSource> (this ParallelQuery<TSource> first,
913 ParallelQuery<TSource> second,
914 IEqualityComparer<TSource> comparer)
917 throw new ArgumentNullException ("first");
919 throw new ArgumentNullException ("second");
920 if (comparer == null)
921 comparer = EqualityComparer<TSource>.Default;
923 return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Union, comparer, first.Node, second.Node));
928 public static ParallelQuery<TSource> Take<TSource> (this ParallelQuery<TSource> source, int count)
931 throw new ArgumentNullException ("source");
933 return new ParallelQuery<TSource> (new QueryHeadWorkerNode<TSource> (source.Node, count));
936 public static ParallelQuery<TSource> TakeWhile<TSource> (this ParallelQuery<TSource> source,
937 Func<TSource, bool> predicate)
940 throw new ArgumentNullException ("source");
941 if (predicate == null)
942 throw new ArgumentNullException ("predicate");
944 return new ParallelQuery<TSource> (new QueryHeadWorkerNode<TSource> (source.Node, (e, _) => predicate (e), false));
947 public static ParallelQuery<TSource> TakeWhile<TSource> (this ParallelQuery<TSource> source,
948 Func<TSource, int, bool> predicate)
951 throw new ArgumentNullException ("source");
952 if (predicate == null)
953 throw new ArgumentNullException ("predicate");
955 return new ParallelQuery<TSource> (new QueryHeadWorkerNode<TSource> (source.Node, predicate, true));
960 public static ParallelQuery<TSource> Skip<TSource> (this ParallelQuery<TSource> source, int count)
963 throw new ArgumentNullException ("source");
965 return source.Node.IsOrdered () ?
966 source.Where ((e, i) => i >= count) :
967 source.Where ((e) => count < 0 || Interlocked.Decrement (ref count) < 0);
971 public static ParallelQuery<TSource> SkipWhile<TSource> (this ParallelQuery<TSource> source,
972 Func<TSource, bool> predicate)
975 throw new ArgumentNullException ("source");
976 if (predicate == null)
977 throw new ArgumentNullException ("predicate");
979 return source.Node.IsOrdered () ?
980 source.SkipWhile ((e, i) => predicate (e)) :
981 source.Where ((e) => !predicate (e));
984 public static ParallelQuery<TSource> SkipWhile<TSource> (this ParallelQuery<TSource> source,
985 Func<TSource, int, bool> predicate)
988 throw new ArgumentNullException ("source");
989 if (predicate == null)
990 throw new ArgumentNullException ("predicate");
992 int indexCache = int.MaxValue;
994 return source.Where ((e, i) => i >= indexCache || (!predicate (e, i) && (indexCache = i) == i));
999 static TSource SingleInternal<TSource> (this ParallelQuery<TSource> source, params TSource[] init)
1001 TSource result = default(TSource);
1002 bool hasValue = false;
1004 foreach (TSource element in source) {
1006 throw new InvalidOperationException ("The input sequence contains more than one element.");
1012 if (!hasValue && init.Length != 0) {
1018 throw new InvalidOperationException ("The input sequence is empty.");
1023 public static TSource Single<TSource> (this ParallelQuery<TSource> source)
1026 throw new ArgumentNullException ("source");
1028 return SingleInternal<TSource> (source);
1031 public static TSource Single<TSource> (this ParallelQuery<TSource> source,
1032 Func<TSource, bool> predicate)
1035 throw new ArgumentNullException ("source");
1036 if (predicate == null)
1037 throw new ArgumentNullException ("predicate");
1039 return source.Where (predicate).Single ();
1042 public static TSource SingleOrDefault<TSource> (this ParallelQuery<TSource> source)
1045 throw new ArgumentNullException ("source");
1047 return SingleInternal<TSource> (source, default (TSource));
1050 public static TSource SingleOrDefault<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
1053 throw new ArgumentNullException ("source");
1054 if (predicate == null)
1055 throw new ArgumentNullException ("predicate");
1057 return source.Where (predicate).SingleOrDefault ();
1062 public static int Count<TSource> (this ParallelQuery<TSource> source)
1065 throw new ArgumentNullException ("source");
1067 return source.Aggregate<TSource, int, int> (() => 0,
1068 (acc, e) => acc + 1,
1069 (acc1, acc2) => acc1 + acc2,
1070 (result) => result);
1073 public static int Count<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
1076 throw new ArgumentNullException ("source");
1077 if (predicate == null)
1078 throw new ArgumentNullException ("predicate");
1080 return source.Where (predicate).Count ();
1083 public static long LongCount<TSource> (this ParallelQuery<TSource> source)
1086 throw new ArgumentNullException ("source");
1088 return source.Aggregate<TSource, long, long> (() => 0,
1089 (acc, e) => acc + 1,
1090 (acc1, acc2) => acc1 + acc2,
1091 (result) => result);
1094 public static long LongCount<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
1097 throw new ArgumentNullException ("source");
1098 if (predicate == null)
1099 throw new ArgumentNullException ("predicate");
1101 return source.Where (predicate).LongCount ();
1106 public static double Average (this ParallelQuery<int> source)
1109 throw new ArgumentNullException ("source");
1111 return source.Aggregate (() => new int[2],
1112 (acc, e) => { acc[0] += e; acc[1]++; return acc; },
1113 (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
1114 (acc) => acc[0] / ((double)acc[1]));
1117 public static double Average (this ParallelQuery<long> source)
1120 throw new ArgumentNullException ("source");
1122 return source.Aggregate (() => new long[2],
1123 (acc, e) => { acc[0] += e; acc[1]++; return acc; },
1124 (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
1125 (acc) => acc[0] / ((double)acc[1]));
1128 public static decimal Average (this ParallelQuery<decimal> source)
1131 throw new ArgumentNullException ("source");
1133 return source.Aggregate (() => new decimal[2],
1134 (acc, e) => { acc[0] += e; acc[1]++; return acc; },
1135 (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
1136 (acc) => acc[0] / acc[1]);
1139 public static double Average (this ParallelQuery<double> source)
1142 throw new ArgumentNullException ("source");
1144 return source.Aggregate (() => new double[2],
1145 (acc, e) => { acc[0] += e; acc[1]++; return acc; },
1146 (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
1147 (acc) => acc[0] / ((double)acc[1]));
1150 public static float Average (this ParallelQuery<float> source)
1153 throw new ArgumentNullException ("source");
1155 return source.Aggregate (() => new float[2],
1156 (acc, e) => { acc[0] += e; acc[1]++; return acc; },
1157 (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
1158 (acc) => acc[0] / acc[1]);
1162 #region More Average
1163 public static double? Average (this ParallelQuery<int?> source)
1166 throw new ArgumentNullException ("source");
1168 return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();;
1171 public static double? Average (this ParallelQuery<long?> source)
1174 throw new ArgumentNullException ("source");
1176 return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
1179 public static decimal? Average (this ParallelQuery<decimal?> source)
1182 throw new ArgumentNullException ("source");
1184 return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
1187 public static double? Average (this ParallelQuery<double?> source)
1190 throw new ArgumentNullException ("source");
1192 return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
1195 public static float? Average (this ParallelQuery<float?> source)
1198 throw new ArgumentNullException ("source");
1200 return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
1203 public static double Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> func)
1206 throw new ArgumentNullException ("source");
1208 throw new ArgumentNullException ("func");
1210 return source.Select (func).Average ();
1213 public static double Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> func)
1216 throw new ArgumentNullException ("source");
1218 throw new ArgumentNullException ("func");
1220 return source.Select (func).Average ();
1223 public static float Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> func)
1226 throw new ArgumentNullException ("source");
1228 throw new ArgumentNullException ("func");
1230 return source.Select (func).Average ();
1233 public static double Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> func)
1236 throw new ArgumentNullException ("source");
1238 throw new ArgumentNullException ("func");
1240 return source.Select (func).Average ();
1243 public static decimal Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> func)
1246 throw new ArgumentNullException ("source");
1248 throw new ArgumentNullException ("func");
1250 return source.Select (func).Average ();
1253 public static double? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> func)
1256 throw new ArgumentNullException ("source");
1258 throw new ArgumentNullException ("func");
1260 return source.Select (func).Average ();
1263 public static double? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> func)
1266 throw new ArgumentNullException ("source");
1268 throw new ArgumentNullException ("func");
1270 return source.Select (func).Average ();
1273 public static float? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> func)
1276 throw new ArgumentNullException ("source");
1278 throw new ArgumentNullException ("func");
1280 return source.Select (func).Average ();
1283 public static double? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> func)
1286 throw new ArgumentNullException ("source");
1288 throw new ArgumentNullException ("func");
1290 return source.Select (func).Average ();
1293 public static decimal? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> func)
1296 throw new ArgumentNullException ("source");
1298 throw new ArgumentNullException ("func");
1300 return source.Select (func).Average ();
1305 public static int Sum (this ParallelQuery<int> source)
1308 throw new ArgumentNullException ("source");
1310 return source.Aggregate (0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
1313 public static long Sum (this ParallelQuery<long> source)
1316 throw new ArgumentNullException ("source");
1318 return source.Aggregate ((long)0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
1321 public static float Sum (this ParallelQuery<float> source)
1324 throw new ArgumentNullException ("source");
1326 return source.Aggregate (0.0f, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
1329 public static double Sum (this ParallelQuery<double> source)
1332 throw new ArgumentNullException ("source");
1334 return source.Aggregate (0.0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
1337 public static decimal Sum (this ParallelQuery<decimal> source)
1340 throw new ArgumentNullException ("source");
1342 return source.Aggregate ((decimal)0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
1345 public static int? Sum (this ParallelQuery<int?> source)
1347 return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
1350 public static long? Sum (this ParallelQuery<long?> source)
1353 throw new ArgumentNullException ("source");
1355 return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
1358 public static float? Sum (this ParallelQuery<float?> source)
1361 throw new ArgumentNullException ("source");
1363 return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
1366 public static double? Sum (this ParallelQuery<double?> source)
1369 throw new ArgumentNullException ("source");
1371 return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
1374 public static decimal? Sum (this ParallelQuery<decimal?> source)
1377 throw new ArgumentNullException ("source");
1379 return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
1382 public static int Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> func)
1385 throw new ArgumentNullException ("source");
1387 throw new ArgumentNullException ("func");
1389 return source.Select (func).Sum ();
1392 public static long Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> func)
1395 throw new ArgumentNullException ("source");
1397 throw new ArgumentNullException ("func");
1399 return source.Select (func).Sum ();
1402 public static decimal Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> func)
1405 throw new ArgumentNullException ("source");
1407 throw new ArgumentNullException ("func");
1409 return source.Select (func).Sum ();
1412 public static float Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> func)
1415 throw new ArgumentNullException ("source");
1417 throw new ArgumentNullException ("func");
1419 return source.Select (func).Sum ();
1422 public static double Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> func)
1425 throw new ArgumentNullException ("source");
1427 throw new ArgumentNullException ("func");
1429 return source.Select (func).Sum ();
1432 public static int? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> func)
1435 throw new ArgumentNullException ("source");
1437 throw new ArgumentNullException ("func");
1439 return source.Select (func).Sum ();
1442 public static long? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> func)
1445 throw new ArgumentNullException ("source");
1447 throw new ArgumentNullException ("func");
1449 return source.Select (func).Sum ();
1452 public static decimal? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> func)
1455 throw new ArgumentNullException ("source");
1457 throw new ArgumentNullException ("func");
1459 return source.Select (func).Sum ();
1462 public static float? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> func)
1465 throw new ArgumentNullException ("source");
1467 throw new ArgumentNullException ("func");
1469 return source.Select (func).Sum ();
1472 public static double? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> func)
1475 throw new ArgumentNullException ("source");
1477 throw new ArgumentNullException ("func");
1479 return source.Select (func).Sum ();
1484 static T BestOrder<T> (ParallelQuery<T> source, Func<T, T, bool> bestSelector, T seed)
1487 throw new ArgumentNullException ("source");
1491 best = source.Aggregate (() => seed,
1492 (first, second) => (bestSelector(first, second)) ? first : second,
1493 (first, second) => (bestSelector(first, second)) ? first : second,
1498 public static int Min (this ParallelQuery<int> source)
1500 return BestOrder (source, (first, second) => first < second, int.MaxValue);
1503 public static long Min (this ParallelQuery<long> source)
1505 return BestOrder (source, (first, second) => first < second, long.MaxValue);
1508 public static float Min (this ParallelQuery<float> source)
1510 return BestOrder (source, (first, second) => first < second, float.MaxValue);
1513 public static double Min (this ParallelQuery<double> source)
1515 return BestOrder (source, (first, second) => first < second, double.MaxValue);
1518 public static decimal Min (this ParallelQuery<decimal> source)
1520 return BestOrder (source, (first, second) => first < second, decimal.MaxValue);
1523 public static TSource Min<TSource> (this ParallelQuery<TSource> source)
1525 IComparer<TSource> comparer = Comparer<TSource>.Default;
1527 return BestOrder (source, (first, second) => comparer.Compare (first, second) < 0, default (TSource));
1530 public static TResult Min<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, TResult> func)
1533 throw new ArgumentNullException ("source");
1535 throw new ArgumentNullException ("func");
1537 return source.Select (func).Min ();
1540 public static int? Min (this ParallelQuery<int?> source)
1543 throw new ArgumentNullException ("source");
1545 return source.Select ((e) => e.HasValue ? e.Value : int.MaxValue).Min ();
1548 public static long? Min (this ParallelQuery<long?> source)
1551 throw new ArgumentNullException ("source");
1553 return source.Select ((e) => e.HasValue ? e.Value : long.MaxValue).Min ();
1556 public static float? Min (this ParallelQuery<float?> source)
1559 throw new ArgumentNullException ("source");
1561 return source.Select ((e) => e.HasValue ? e.Value : float.MaxValue).Min ();
1564 public static double? Min (this ParallelQuery<double?> source)
1567 throw new ArgumentNullException ("source");
1569 return source.Select ((e) => e.HasValue ? e.Value : double.MaxValue).Min ();
1572 public static decimal? Min (this ParallelQuery<decimal?> source)
1575 throw new ArgumentNullException ("source");
1577 return source.Select ((e) => e.HasValue ? e.Value : decimal.MaxValue).Min ();
1580 public static int Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> func)
1583 throw new ArgumentNullException ("source");
1585 throw new ArgumentNullException ("func");
1587 return source.Select (func).Min ();
1590 public static long Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> func)
1593 throw new ArgumentNullException ("source");
1595 throw new ArgumentNullException ("func");
1597 return source.Select (func).Min ();
1600 public static float Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> func)
1603 throw new ArgumentNullException ("source");
1605 throw new ArgumentNullException ("func");
1607 return source.Select (func).Min ();
1610 public static double Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> func)
1613 throw new ArgumentNullException ("source");
1615 throw new ArgumentNullException ("func");
1617 return source.Select (func).Min ();
1620 public static decimal Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> func)
1623 throw new ArgumentNullException ("source");
1625 throw new ArgumentNullException ("func");
1627 return source.Select (func).Min ();
1630 public static int? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> func)
1633 throw new ArgumentNullException ("source");
1635 throw new ArgumentNullException ("func");
1637 return source.Select (func).Min ();
1640 public static long? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> func)
1643 throw new ArgumentNullException ("source");
1645 throw new ArgumentNullException ("func");
1647 return source.Select (func).Min ();
1650 public static float? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> func)
1653 throw new ArgumentNullException ("source");
1655 throw new ArgumentNullException ("func");
1657 return source.Select (func).Min ();
1660 public static double? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> func)
1663 throw new ArgumentNullException ("source");
1665 throw new ArgumentNullException ("func");
1667 return source.Select (func).Min ();
1670 public static decimal? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> func)
1673 throw new ArgumentNullException ("source");
1675 throw new ArgumentNullException ("func");
1677 return source.Select (func).Min ();
1680 public static int Max (this ParallelQuery<int> source)
1682 return BestOrder (source, (first, second) => first > second, int.MinValue);
1685 public static long Max(this ParallelQuery<long> source)
1687 return BestOrder(source, (first, second) => first > second, long.MinValue);
1690 public static float Max (this ParallelQuery<float> source)
1692 return BestOrder(source, (first, second) => first > second, float.MinValue);
1695 public static double Max (this ParallelQuery<double> source)
1697 return BestOrder(source, (first, second) => first > second, double.MinValue);
1700 public static decimal Max (this ParallelQuery<decimal> source)
1702 return BestOrder(source, (first, second) => first > second, decimal.MinValue);
1705 public static TSource Max<TSource> (this ParallelQuery<TSource> source)
1707 IComparer<TSource> comparer = Comparer<TSource>.Default;
1709 return BestOrder (source, (first, second) => comparer.Compare (first, second) > 0, default (TSource));
1712 public static TResult Max<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, TResult> func)
1715 throw new ArgumentNullException ("source");
1717 throw new ArgumentNullException ("func");
1719 return source.Select (func).Max ();
1722 public static int? Max (this ParallelQuery<int?> source)
1725 throw new ArgumentNullException ("source");
1727 return source.Select ((e) => e.HasValue ? e.Value : int.MinValue).Max ();
1730 public static long? Max (this ParallelQuery<long?> source)
1733 throw new ArgumentNullException ("source");
1735 return source.Select ((e) => e.HasValue ? e.Value : long.MinValue).Max ();
1738 public static float? Max (this ParallelQuery<float?> source)
1741 throw new ArgumentNullException ("source");
1743 return source.Select ((e) => e.HasValue ? e.Value : float.MinValue).Max ();
1746 public static double? Max (this ParallelQuery<double?> source)
1749 throw new ArgumentNullException ("source");
1751 return source.Select ((e) => e.HasValue ? e.Value : double.MinValue).Max ();
1754 public static decimal? Max (this ParallelQuery<decimal?> source)
1757 throw new ArgumentNullException ("source");
1759 return source.Select ((e) => e.HasValue ? e.Value : decimal.MinValue).Max ();
1762 public static int Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> func)
1765 throw new ArgumentNullException ("source");
1767 throw new ArgumentNullException ("func");
1769 return source.Select (func).Max ();
1772 public static long Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> func)
1775 throw new ArgumentNullException ("source");
1777 throw new ArgumentNullException ("func");
1779 return source.Select (func).Max ();
1782 public static float Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> func)
1785 throw new ArgumentNullException ("source");
1787 throw new ArgumentNullException ("func");
1789 return source.Select (func).Max ();
1792 public static double Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> func)
1795 throw new ArgumentNullException ("source");
1797 throw new ArgumentNullException ("func");
1799 return source.Select (func).Max ();
1802 public static decimal Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> func)
1805 throw new ArgumentNullException ("source");
1807 throw new ArgumentNullException ("func");
1809 return source.Select (func).Max ();
1812 public static int? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> func)
1815 throw new ArgumentNullException ("source");
1817 throw new ArgumentNullException ("func");
1819 return source.Select (func).Max ();
1822 public static long? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> func)
1825 throw new ArgumentNullException ("source");
1827 throw new ArgumentNullException ("func");
1829 return source.Select (func).Max ();
1832 public static float? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> func)
1835 throw new ArgumentNullException ("source");
1837 throw new ArgumentNullException ("func");
1839 return source.Select (func).Max ();
1842 public static double? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> func)
1845 throw new ArgumentNullException ("source");
1847 throw new ArgumentNullException ("func");
1849 return source.Select (func).Max ();
1852 public static decimal? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> func)
1855 throw new ArgumentNullException ("source");
1857 throw new ArgumentNullException ("func");
1859 return source.Select (func).Max ();
1863 #region Cast / OfType
1864 public static ParallelQuery<TResult> Cast<TResult> (this ParallelQuery source)
1867 throw new ArgumentNullException ("source");
1869 return source.TypedQuery.Select ((e) => (TResult)e);
1872 public static ParallelQuery<TResult> OfType<TResult> (this ParallelQuery source)
1875 throw new ArgumentNullException ("source");
1877 return source.TypedQuery.Where ((e) => e is TResult).Cast<TResult> ();
1882 public static ParallelQuery<TSource> Reverse<TSource> (this ParallelQuery<TSource> source)
1885 throw new ArgumentNullException ("source");
1887 return new ParallelQuery<TSource> (new QueryReverseNode<TSource> (source));
1891 #region ToArray - ToList - ToDictionary - ToLookup
1892 public static List<TSource> ToList<TSource> (this ParallelQuery<TSource> source)
1895 throw new ArgumentNullException ("source");
1897 if (source.Node.IsOrdered ())
1898 return ToListOrdered (source);
1900 List<TSource> temp = source.Aggregate (() => new List<TSource>(50),
1901 (list, e) => { list.Add (e); return list; },
1902 (list, list2) => { list.AddRange (list2); return list; },
1907 internal static List<TSource> ToListOrdered<TSource> (this ParallelQuery<TSource> source)
1909 List<TSource> result = new List<TSource> ();
1911 foreach (TSource element in source)
1912 result.Add (element);
1917 public static TSource[] ToArray<TSource> (this ParallelQuery<TSource> source)
1920 throw new ArgumentNullException ("source");
1922 if (source.Node.IsOrdered ())
1923 return ToListOrdered (source).ToArray ();
1925 TSource[] result = null;
1927 Func<List<TSource>, TSource, List<TSource>> intermediate = (list, e) => {
1928 list.Add (e); return list;
1931 Action<IList<List<TSource>>> final = (list) => {
1934 for (int i = 0; i < list.Count; i++)
1935 count += list[i].Count;
1937 result = new TSource[count];
1938 int insertIndex = -1;
1940 for (int i = 0; i < list.Count; i++)
1941 for (int j = 0; j < list[i].Count; j++)
1942 result [++insertIndex] = list[i][j];
1945 ParallelExecuter.ProcessAndAggregate<TSource, List<TSource>> (source.Node,
1946 () => new List<TSource> (),
1953 public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this ParallelQuery<TSource> source,
1954 Func<TSource, TKey> keySelector,
1955 IEqualityComparer<TKey> comparer)
1957 return ToDictionary<TSource, TKey, TSource> (source, keySelector, (e) => e, comparer);
1960 public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this ParallelQuery<TSource> source,
1961 Func<TSource, TKey> keySelector)
1963 return ToDictionary<TSource, TKey, TSource> (source, keySelector, (e) => e, EqualityComparer<TKey>.Default);
1966 public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
1967 Func<TSource, TKey> keySelector,
1968 Func<TSource, TElement> elementSelector)
1970 return ToDictionary<TSource, TKey, TElement> (source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
1973 public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
1974 Func<TSource, TKey> keySelector,
1975 Func<TSource, TElement> elementSelector,
1976 IEqualityComparer<TKey> comparer)
1979 throw new ArgumentNullException ("source");
1980 if (keySelector == null)
1981 throw new ArgumentNullException ("keySelector");
1982 if (comparer == null)
1983 comparer = EqualityComparer<TKey>.Default;
1984 if (elementSelector == null)
1985 throw new ArgumentNullException ("elementSelector");
1987 return source.Aggregate (() => new Dictionary<TKey, TElement> (comparer),
1988 (d, e) => { d.Add (keySelector (e), elementSelector (e)); return d; },
1989 (d1, d2) => { foreach (var couple in d2) d1.Add (couple.Key, couple.Value); return d1; },
1993 public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this ParallelQuery<TSource> source,
1994 Func<TSource, TKey> keySelector)
1996 return ToLookup<TSource, TKey, TSource> (source, keySelector, (e) => e, EqualityComparer<TKey>.Default);
1999 public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this ParallelQuery<TSource> source,
2000 Func<TSource, TKey> keySelector,
2001 IEqualityComparer<TKey> comparer)
2003 return ToLookup<TSource, TKey, TSource> (source, keySelector, (e) => e, comparer);
2006 public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
2007 Func<TSource, TKey> keySelector,
2008 Func<TSource, TElement> elementSelector)
2010 return ToLookup<TSource, TKey, TElement> (source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
2013 public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
2014 Func<TSource, TKey> keySelector,
2015 Func<TSource, TElement> elementSelector,
2016 IEqualityComparer<TKey> comparer)
2019 throw new ArgumentNullException ("source");
2020 if (keySelector == null)
2021 throw new ArgumentNullException ("keySelector");
2022 if (comparer == null)
2023 comparer = EqualityComparer<TKey>.Default;
2024 if (elementSelector == null)
2025 throw new ArgumentNullException ("elementSelector");
2027 ConcurrentLookup<TKey, TElement> lookup = new ConcurrentLookup<TKey, TElement> (comparer);
2028 source.ForAll ((e) => lookup.Add (keySelector (e), elementSelector (e)));
2035 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather than "
2036 + "System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() extension method "
2037 + "to convert the right data source to System.Linq.ParallelQuery<T>.")]
2038 public static ParallelQuery<TSource> Concat<TSource>(this ParallelQuery<TSource> first,
2039 IEnumerable<TSource> second)
2041 throw new NotSupportedException ();
2044 public static ParallelQuery<TSource> Concat<TSource> (this ParallelQuery<TSource> first, ParallelQuery<TSource> second)
2046 return new ParallelQuery<TSource> (new QueryConcatNode<TSource> (first.Node, second.Node));
2050 #region DefaultIfEmpty
2051 public static ParallelQuery<TSource> DefaultIfEmpty<TSource> (this ParallelQuery<TSource> source)
2053 return source.DefaultIfEmpty (default (TSource));
2056 public static ParallelQuery<TSource> DefaultIfEmpty<TSource> (this ParallelQuery<TSource> source, TSource defaultValue)
2058 return new ParallelQuery<TSource> (new QueryDefaultEmptyNode<TSource> (source.Node, defaultValue));
2063 public static TSource First<TSource> (this ParallelQuery<TSource> source)
2065 CancellationTokenSource src = new CancellationTokenSource ();
2066 IEnumerator<TSource> enumerator = source.WithImplementerToken (src).GetEnumerator ();
2068 if (enumerator == null || !enumerator.MoveNext ())
2069 throw new InvalidOperationException ("source contains no element");
2071 TSource result = enumerator.Current;
2073 enumerator.Dispose ();
2078 public static TSource First<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
2080 return source.Where (predicate).First ();
2083 public static TSource FirstOrDefault<TSource> (this ParallelQuery<TSource> source)
2085 return source.DefaultIfEmpty ().First ();
2088 public static TSource FirstOrDefault<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
2090 return source.Where (predicate).FirstOrDefault ();
2095 public static TSource Last<TSource> (this ParallelQuery<TSource> source)
2097 return source.Reverse ().First ();
2100 public static TSource Last<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
2102 return source.Reverse ().First (predicate);
2105 public static TSource LastOrDefault<TSource> (this ParallelQuery<TSource> source)
2107 return source.Reverse ().FirstOrDefault ();
2110 public static TSource LastOrDefault<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
2112 return source.Reverse ().FirstOrDefault (predicate);
2117 public static ParallelQuery<TResult> Zip<TFirst, TSecond, TResult> (this ParallelQuery<TFirst> first,
2118 ParallelQuery<TSecond> second,
2119 Func<TFirst, TSecond, TResult> resultSelector)
2122 throw new ArgumentNullException ("first");
2124 throw new ArgumentNullException ("second");
2125 if (resultSelector == null)
2126 throw new ArgumentNullException ("resultSelector");
2128 return new ParallelQuery<TResult> (new QueryZipNode<TFirst, TSecond, TResult> (resultSelector, first.Node, second.Node));
2131 [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
2132 + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
2133 + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
2134 public static ParallelQuery<TResult> Zip<TFirst, TSecond, TResult> (this ParallelQuery<TFirst> first,
2135 IEnumerable<TSecond> second,
2136 Func<TFirst, TSecond, TResult> resultSelector)
2138 throw new NotSupportedException ();