mscorlib(4.0.0.0) API with additions
AggregationMinMaxHelpers.cs
2 using System.Linq.Parallel;
3 
4 namespace System.Linq
5 {
6  internal static class AggregationMinMaxHelpers<T>
7  {
8  private static T Reduce(IEnumerable<T> source, int sign)
9  {
10  Func<Pair<bool, T>, T, Pair<bool, T>> intermediateReduce = MakeIntermediateReduceFunction(sign);
11  Func<Pair<bool, T>, Pair<bool, T>, Pair<bool, T>> finalReduce = MakeFinalReduceFunction(sign);
12  Func<Pair<bool, T>, T> resultSelector = MakeResultSelectorFunction();
13  AssociativeAggregationOperator<T, Pair<bool, T>, T> associativeAggregationOperator = new AssociativeAggregationOperator<T, Pair<bool, T>, T>(source, new Pair<bool, T>(first: false, default(T)), null, seedIsSpecified: true, intermediateReduce, finalReduce, resultSelector, default(T) != null, QueryAggregationOptions.AssociativeCommutative);
14  return associativeAggregationOperator.Aggregate();
15  }
16 
17  internal static T ReduceMin(IEnumerable<T> source)
18  {
19  return Reduce(source, -1);
20  }
21 
22  internal static T ReduceMax(IEnumerable<T> source)
23  {
24  return Reduce(source, 1);
25  }
26 
27  private static Func<Pair<bool, T>, T, Pair<bool, T>> MakeIntermediateReduceFunction(int sign)
28  {
29  Comparer<T> comparer = Util.GetDefaultComparer<T>();
30  return delegate(Pair<bool, T> accumulator, T element)
31  {
32  if ((default(T) != null || element != null) && (!accumulator.First || Util.Sign(comparer.Compare(element, accumulator.Second)) == sign))
33  {
34  return new Pair<bool, T>(first: true, element);
35  }
36  return accumulator;
37  };
38  }
39 
40  private static Func<Pair<bool, T>, Pair<bool, T>, Pair<bool, T>> MakeFinalReduceFunction(int sign)
41  {
42  Comparer<T> comparer = Util.GetDefaultComparer<T>();
43  return delegate(Pair<bool, T> accumulator, Pair<bool, T> element)
44  {
45  if (element.First && (!accumulator.First || Util.Sign(comparer.Compare(element.Second, accumulator.Second)) == sign))
46  {
47  return new Pair<bool, T>(first: true, element.Second);
48  }
49  return accumulator;
50  };
51  }
52 
53  private static Func<Pair<bool, T>, T> MakeResultSelectorFunction()
54  {
55  return (Pair<bool, T> accumulator) => accumulator.Second;
56  }
57  }
58 }
Provides a base class for implementations of the T:System.Collections.Generic.IComparer`1 generic int...
Definition: Comparer.cs:11
Definition: __Canon.cs:3
abstract int Compare(T x, T y)
When overridden in a derived class, performs a comparison of two objects of the same type and returns...