3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // NullableFloatAverageAggregationOperator.cs
10 // <OWNER>[....]</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Threading;
18 namespace System.Linq.Parallel
21 /// An inlined average aggregation operator and its enumerator, for Nullable floats.
23 internal sealed class NullableFloatAverageAggregationOperator : InlinedAggregationOperator<float?, Pair<double, long>, float?>
25 //---------------------------------------------------------------------------------------
26 // Constructs a new instance of an average associative operator.
29 internal NullableFloatAverageAggregationOperator(IEnumerable<float?> child) : base(child)
33 //---------------------------------------------------------------------------------------
34 // Executes the entire query tree, and aggregates the intermediate results into the
35 // final result based on the binary operators and final reduction.
38 // The single result of aggregation.
41 protected override float? InternalAggregate(ref Exception singularExceptionToThrow)
43 // Because the final reduction is typically much cheaper than the intermediate
44 // reductions over the individual partitions, and because each parallel partition
45 // will do a lot of work to produce a single output element, we prefer to turn off
46 // pipelining, and process the final reductions serially.
47 using (IEnumerator<Pair<double, long>> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
49 // If the sequence was empty, return null right away.
50 if (!enumerator.MoveNext())
55 Pair<double, long> result = enumerator.Current;
57 // Simply add together the sums and totals.
58 while (enumerator.MoveNext())
62 result.First += enumerator.Current.First;
63 result.Second += enumerator.Current.Second;
67 // And divide the sum by the total to obtain the final result.
68 return (float)(result.First / result.Second);
72 //---------------------------------------------------------------------------------------
73 // Creates an enumerator that is used internally for the final aggregation step.
76 protected override QueryOperatorEnumerator<Pair<double, long>,int> CreateEnumerator<TKey>(
77 int index, int count, QueryOperatorEnumerator<float?, TKey> source, object sharedData, CancellationToken cancellationToken)
79 return new NullableFloatAverageAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
82 //---------------------------------------------------------------------------------------
83 // This enumerator type encapsulates the intermediary aggregation over the underlying
84 // (possibly partitioned) data source.
87 private class NullableFloatAverageAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<Pair<double, long>>
89 private QueryOperatorEnumerator<float?, TKey> m_source; // The source data.
91 //---------------------------------------------------------------------------------------
92 // Instantiates a new aggregation operator.
95 internal NullableFloatAverageAggregationOperatorEnumerator(QueryOperatorEnumerator<float?, TKey> source, int partitionIndex,
96 CancellationToken cancellationToken) :
97 base(partitionIndex, cancellationToken)
99 Contract.Assert(source != null);
103 //---------------------------------------------------------------------------------------
104 // Tallies up the average of the underlying data source, walking the entire thing the first
105 // time MoveNext is called on this object.
108 protected override bool MoveNextCore(ref Pair<double, long> currentElement)
110 // The temporary result contains the running sum and count, respectively.
114 QueryOperatorEnumerator<float?, TKey> source = m_source;
115 float? current = default(float?);
116 TKey keyUnused = default(TKey);
119 while (source.MoveNext(ref current, ref keyUnused))
121 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
122 CancellationState.ThrowIfCanceled(m_cancellationToken);
124 if (current.HasValue)
126 sum += current.GetValueOrDefault();
131 currentElement = new Pair<double, long>(sum, count);
135 //---------------------------------------------------------------------------------------
136 // Dispose of resources associated with the underlying enumerator.
139 protected override void Dispose(bool disposing)
141 Contract.Assert(m_source != null);