1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 // NullableDoubleSumAggregationOperator.cs
9 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.Threading;
15 namespace System.Linq.Parallel
18 /// An inlined sum aggregation and its enumerator, for nullable doubles.
20 internal sealed class NullableDoubleSumAggregationOperator : InlinedAggregationOperator<double?, double?, double?>
22 //---------------------------------------------------------------------------------------
23 // Constructs a new instance of a sum associative operator.
26 internal NullableDoubleSumAggregationOperator(IEnumerable<double?> child) : base(child)
30 //---------------------------------------------------------------------------------------
31 // Executes the entire query tree, and aggregates the intermediate results into the
32 // final result based on the binary operators and final reduction.
35 // The single result of aggregation.
38 protected override double? InternalAggregate(ref Exception singularExceptionToThrow)
40 // Because the final reduction is typically much cheaper than the intermediate
41 // reductions over the individual partitions, and because each parallel partition
42 // will do a lot of work to produce a single output element, we prefer to turn off
43 // pipelining, and process the final reductions serially.
44 using (IEnumerator<double?> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
46 // We just reduce the elements in each output partition.
48 while (enumerator.MoveNext())
50 sum += enumerator.Current.GetValueOrDefault();
57 //---------------------------------------------------------------------------------------
58 // Creates an enumerator that is used internally for the final aggregation step.
61 protected override QueryOperatorEnumerator<double?, int> CreateEnumerator<TKey>(
62 int index, int count, QueryOperatorEnumerator<double?, TKey> source, object sharedData, CancellationToken cancellationToken)
64 return new NullableDoubleSumAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
67 //---------------------------------------------------------------------------------------
68 // This enumerator type encapsulates the intermediary aggregation over the underlying
69 // (possibly partitioned) data source.
72 private class NullableDoubleSumAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<double?>
74 private readonly QueryOperatorEnumerator<double?, TKey> _source; // The source data.
76 //---------------------------------------------------------------------------------------
77 // Instantiates a new aggregation operator.
80 internal NullableDoubleSumAggregationOperatorEnumerator(QueryOperatorEnumerator<double?, TKey> source, int partitionIndex,
81 CancellationToken cancellationToken) :
82 base(partitionIndex, cancellationToken)
84 Debug.Assert(source != null);
88 //---------------------------------------------------------------------------------------
89 // Tallies up the sum of the underlying data source, walking the entire thing the first
90 // time MoveNext is called on this object.
93 protected override bool MoveNextCore(ref double? currentElement)
95 double? element = default(double?);
96 TKey keyUnused = default(TKey);
98 QueryOperatorEnumerator<double?, TKey> source = _source;
99 if (source.MoveNext(ref element, ref keyUnused))
101 // We just scroll through the enumerator and accumulate the sum.
102 double tempSum = 0.0;
106 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
107 CancellationState.ThrowIfCanceled(_cancellationToken);
109 tempSum += element.GetValueOrDefault();
111 while (source.MoveNext(ref element, ref keyUnused));
113 // The sum has been calculated. Now just return.
114 currentElement = tempSum;
122 //---------------------------------------------------------------------------------------
123 // Dispose of resources associated with the underlying enumerator.
126 protected override void Dispose(bool disposing)
128 Debug.Assert(_source != null);