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