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 // IntMinMaxAggregationOperator.cs
9 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.Threading;
15 namespace System.Linq.Parallel
18 /// An inlined min/max aggregation and its enumerator, for ints.
20 internal sealed class IntMinMaxAggregationOperator : InlinedAggregationOperator<int, int, int>
22 private readonly int _sign; // The sign (-1 for min, 1 for max).
24 //---------------------------------------------------------------------------------------
25 // Constructs a new instance of a min/max associative operator.
28 internal IntMinMaxAggregationOperator(IEnumerable<int> child, int sign) : base(child)
30 Debug.Assert(sign == -1 || sign == 1, "invalid sign");
34 //---------------------------------------------------------------------------------------
35 // Executes the entire query tree, and aggregates the intermediate results into the
36 // final result based on the binary operators and final reduction.
39 // The single result of aggregation.
42 protected override int InternalAggregate(ref Exception singularExceptionToThrow)
44 // Because the final reduction is typically much cheaper than the intermediate
45 // reductions over the individual partitions, and because each parallel partition
46 // will do a lot of work to produce a single output element, we prefer to turn off
47 // pipelining, and process the final reductions serially.
48 using (IEnumerator<int> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
50 // Throw an error for empty results.
51 if (!enumerator.MoveNext())
53 singularExceptionToThrow = new InvalidOperationException(SR.NoElements);
57 int best = enumerator.Current;
59 // Based on the sign, do either a min or max reduction.
62 while (enumerator.MoveNext())
64 int current = enumerator.Current;
73 while (enumerator.MoveNext())
75 int current = enumerator.Current;
87 //---------------------------------------------------------------------------------------
88 // Creates an enumerator that is used internally for the final aggregation step.
91 protected override QueryOperatorEnumerator<int, int> CreateEnumerator<TKey>(
92 int index, int count, QueryOperatorEnumerator<int, TKey> source, object sharedData, CancellationToken cancellationToken)
94 return new IntMinMaxAggregationOperatorEnumerator<TKey>(source, index, _sign, cancellationToken);
97 //---------------------------------------------------------------------------------------
98 // This enumerator type encapsulates the intermediary aggregation over the underlying
99 // (possibly partitioned) data source.
102 private class IntMinMaxAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<int>
104 private readonly QueryOperatorEnumerator<int, TKey> _source; // The source data.
105 private readonly int _sign; // The sign for comparisons (-1 means min, 1 means max).
107 //---------------------------------------------------------------------------------------
108 // Instantiates a new aggregation operator.
111 internal IntMinMaxAggregationOperatorEnumerator(QueryOperatorEnumerator<int, TKey> source, int partitionIndex, int sign,
112 CancellationToken cancellationToken) :
113 base(partitionIndex, cancellationToken)
115 Debug.Assert(source != null);
120 //---------------------------------------------------------------------------------------
121 // Tallies up the min/max of the underlying data source, walking the entire thing the first
122 // time MoveNext is called on this object.
125 protected override bool MoveNextCore(ref int currentElement)
127 // Based on the sign, do either a min or max reduction.
128 QueryOperatorEnumerator<int, TKey> source = _source;
129 TKey keyUnused = default(TKey);
131 if (source.MoveNext(ref currentElement, ref keyUnused))
134 // We just scroll through the enumerator and find the min or max.
137 int elem = default(int);
138 while (source.MoveNext(ref elem, ref keyUnused))
140 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
141 CancellationState.ThrowIfCanceled(_cancellationToken);
143 if (elem < currentElement)
145 currentElement = elem;
151 int elem = default(int);
152 while (source.MoveNext(ref elem, ref keyUnused))
154 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
155 CancellationState.ThrowIfCanceled(_cancellationToken);
157 if (elem > currentElement)
159 currentElement = elem;
164 // The sum has been calculated. Now just return.
171 //---------------------------------------------------------------------------------------
172 // Dispose of resources associated with the underlying enumerator.
175 protected override void Dispose(bool disposing)
177 Debug.Assert(_source != null);