9209fd02692a9e375d15defb551249437975e7f6
[platform/upstream/dotnet/runtime.git] /
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.
4
5 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 //
7 // CountAggregationOperator.cs
8 //
9 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
10
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.Threading;
14
15 namespace System.Linq.Parallel
16 {
17     /// <summary>
18     /// An inlined count aggregation and its enumerator.
19     /// </summary>
20     /// <typeparam name="TSource"></typeparam>
21     internal sealed class LongCountAggregationOperator<TSource> : InlinedAggregationOperator<TSource, long, long>
22     {
23         //---------------------------------------------------------------------------------------
24         // Constructs a new instance of the operator.
25         //
26
27         internal LongCountAggregationOperator(IEnumerable<TSource> child) : base(child)
28         {
29         }
30
31         //---------------------------------------------------------------------------------------
32         // Executes the entire query tree, and aggregates the intermediate results into the
33         // final result based on the binary operators and final reduction.
34         //
35         // Return Value:
36         //     The single result of aggregation.
37         //
38
39         protected override long InternalAggregate(ref Exception singularExceptionToThrow)
40         {
41             // Because the final reduction is typically much cheaper than the intermediate
42             // reductions over the individual partitions, and because each parallel partition
43             // will do a lot of work to produce a single output element, we prefer to turn off
44             // pipelining, and process the final reductions serially.
45             using (IEnumerator<long> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
46             {
47                 // We just reduce the elements in each output partition.
48                 long count = 0;
49                 while (enumerator.MoveNext())
50                 {
51                     checked
52                     {
53                         count += enumerator.Current;
54                     }
55                 }
56
57                 return count;
58             }
59         }
60
61         //---------------------------------------------------------------------------------------
62         // Creates an enumerator that is used internally for the final aggregation step.
63         //
64
65         protected override QueryOperatorEnumerator<long, int> CreateEnumerator<TKey>(
66             int index, int count, QueryOperatorEnumerator<TSource, TKey> source, object sharedData, CancellationToken cancellationToken)
67         {
68             return new LongCountAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
69         }
70
71         //---------------------------------------------------------------------------------------
72         // This enumerator type encapsulates the intermediary aggregation over the underlying
73         // (possibly partitioned) data source.
74         //
75
76         private class LongCountAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<long>
77         {
78             private readonly QueryOperatorEnumerator<TSource, TKey> _source; // The source data.
79
80             //---------------------------------------------------------------------------------------
81             // Instantiates a new aggregation operator.
82             //
83
84             internal LongCountAggregationOperatorEnumerator(QueryOperatorEnumerator<TSource, TKey> source, int partitionIndex,
85                 CancellationToken cancellationToken) :
86                 base(partitionIndex, cancellationToken)
87             {
88                 Debug.Assert(source != null);
89                 _source = source;
90             }
91
92             //---------------------------------------------------------------------------------------
93             // Counts the elements in the underlying data source, walking the entire thing the first
94             // time MoveNext is called on this object.
95             //
96
97             protected override bool MoveNextCore(ref long currentElement)
98             {
99                 TSource elementUnused = default(TSource);
100                 TKey keyUnused = default(TKey);
101
102                 QueryOperatorEnumerator<TSource, TKey> source = _source;
103                 if (source.MoveNext(ref elementUnused, ref keyUnused))
104                 {
105                     // We just scroll through the enumerator and keep a running count.
106                     long count = 0;
107                     int i = 0;
108                     do
109                     {
110                         if ((i++ & CancellationState.POLL_INTERVAL) == 0)
111                             CancellationState.ThrowIfCanceled(_cancellationToken);
112
113                         checked
114                         {
115                             count++;
116                         }
117                     }
118                     while (source.MoveNext(ref elementUnused, ref keyUnused));
119
120                     currentElement = count;
121                     return true;
122                 }
123
124                 return false;
125             }
126
127             //---------------------------------------------------------------------------------------
128             // Dispose of resources associated with the underlying enumerator.
129             //
130
131             protected override void Dispose(bool disposing)
132             {
133                 Debug.Assert(_source != null);
134                 _source.Dispose();
135             }
136         }
137     }
138 }