3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // FloatMinMaxAggregationOperator.cs
10 // <OWNER>[....]</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Threading;
18 using System.Core; // for System.Core.SR
20 namespace System.Linq.Parallel
23 // Note that normally float.NaN < anything is false, as is anything < NaN. This would
24 // lead to some strangeness in Min and Max, e.g. Min({ NaN, 5.0 } == NaN, yet
25 // Min({ 5.0, NaN }) == 5.0! We impose a total ordering so that NaN is smaller than
26 // everything, including -infinity, which is consistent with Comparer<T>.
28 /// An inlined min/max aggregation and its enumerator, for floats.
30 internal sealed class FloatMinMaxAggregationOperator : InlinedAggregationOperator<float, float, float>
32 private readonly int m_sign; // The sign (-1 for min, 1 for max).
34 //---------------------------------------------------------------------------------------
35 // Constructs a new instance of a min/max associative operator.
38 internal FloatMinMaxAggregationOperator(IEnumerable<float> child, int sign) : base(child)
40 Contract.Assert(sign == -1 || sign == 1, "invalid sign");
44 //---------------------------------------------------------------------------------------
45 // Executes the entire query tree, and aggregates the intermediate results into the
46 // final result based on the binary operators and final reduction.
49 // The single result of aggregation.
52 protected override float InternalAggregate(ref Exception singularExceptionToThrow)
54 // Because the final reduction is typically much cheaper than the intermediate
55 // reductions over the individual partitions, and because each parallel partition
56 // will do a lot of work to produce a single output element, we prefer to turn off
57 // pipelining, and process the final reductions serially.
58 using (IEnumerator<float> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
60 // Throw an error for empty results.
61 if (!enumerator.MoveNext())
63 singularExceptionToThrow = new InvalidOperationException(SR.GetString(SR.NoElements));
64 return default(float);
67 float best = enumerator.Current;
69 // Based on the sign, do either a min or max reduction.
72 while (enumerator.MoveNext())
74 float current = enumerator.Current;
75 if (current < best || float.IsNaN(current))
83 while (enumerator.MoveNext())
85 float current = enumerator.Current;
86 if (current > best || float.IsNaN(best))
97 //---------------------------------------------------------------------------------------
98 // Creates an enumerator that is used internally for the final aggregation step.
101 protected override QueryOperatorEnumerator<float, int> CreateEnumerator<TKey>(
102 int index, int count, QueryOperatorEnumerator<float, TKey> source, object sharedData, CancellationToken cancellationToken)
104 return new FloatMinMaxAggregationOperatorEnumerator<TKey>(source, index, m_sign, cancellationToken);
107 //---------------------------------------------------------------------------------------
108 // This enumerator type encapsulates the intermediary aggregation over the underlying
109 // (possibly partitioned) data source.
112 private class FloatMinMaxAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<float>
114 private QueryOperatorEnumerator<float, TKey> m_source; // The source data.
115 private int m_sign; // The sign for comparisons (-1 means min, 1 means max).
117 //---------------------------------------------------------------------------------------
118 // Instantiates a new aggregation operator.
121 internal FloatMinMaxAggregationOperatorEnumerator(QueryOperatorEnumerator<float, TKey> source, int partitionIndex, int sign,
122 CancellationToken cancellationToken) :
123 base(partitionIndex, cancellationToken)
125 Contract.Assert(source != null);
130 //---------------------------------------------------------------------------------------
131 // Tallies up the min/max of the underlying data source, walking the entire thing the first
132 // time MoveNext is called on this object.
135 protected override bool MoveNextCore(ref float currentElement)
137 // Based on the sign, do either a min or max reduction.
138 QueryOperatorEnumerator<float, TKey> source = m_source;
139 TKey keyUnused = default(TKey);
141 if (source.MoveNext(ref currentElement, ref keyUnused))
144 // We just scroll through the enumerator and find the min or max.
147 float elem = default(float);
148 while (source.MoveNext(ref elem, ref keyUnused))
150 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
151 CancellationState.ThrowIfCanceled(m_cancellationToken);
152 if (elem < currentElement || float.IsNaN(elem))
154 currentElement = elem;
160 float elem = default(float);
161 while (source.MoveNext(ref elem, ref keyUnused))
163 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
164 CancellationState.ThrowIfCanceled(m_cancellationToken);
166 if (elem > currentElement || float.IsNaN(currentElement))
168 currentElement = elem;
173 // The sum has been calculated. Now just return.
180 //---------------------------------------------------------------------------------------
181 // Dispose of resources associated with the underlying enumerator.
184 protected override void Dispose(bool disposing)
186 Contract.Assert(m_source != null);