6f78b8177a515dd87cc9803ecf2c1194abfe08bf
[mono.git] / mcs / class / referencesource / System.Core / System / Linq / Parallel / QueryOperators / QueryOperatorEnumerator.cs
1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 //
8 // QueryOperatorEnumerator.cs
9 //
10 // <OWNER>Microsoft</OWNER>
11 //
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
13
14 using System.Collections;
15 using System.Collections.Generic;
16 using System.Diagnostics.Contracts;
17 #if SILVERLIGHT
18 using System.Core; // for System.Core.SR
19 #endif
20
21 namespace System.Linq.Parallel
22 {
23     /// <summary>
24     /// A common enumerator type that unifies all query operator enumerators. 
25     /// </summary>
26     /// <typeparam name="TElement"></typeparam>
27     /// <typeparam name="TKey"></typeparam>
28     internal abstract class QueryOperatorEnumerator<TElement, TKey>
29     {
30         // Moves the position of the enumerator forward by one, and simultaneously returns
31         // the (new) current element and key. If empty, false is returned.
32         internal abstract bool MoveNext(ref TElement currentElement, ref TKey currentKey);
33
34         // Standard implementation of the disposable pattern.
35         public void Dispose()
36         {
37             Dispose(true);
38         }
39
40         protected virtual void Dispose(bool disposing)
41         {
42             // This is a no-op by default.  Subclasses can override.
43         }
44
45         internal virtual void Reset()
46         {
47             // This is a no-op by default.  Subclasses can override.
48         }
49
50         //-----------------------------------------------------------------------------------
51         // A simple way to turn a query operator enumerator into a "classic" one.
52         //
53
54         internal IEnumerator<TElement> AsClassicEnumerator()
55         {
56             return new QueryOperatorClassicEnumerator(this);
57         }
58
59         class QueryOperatorClassicEnumerator : IEnumerator<TElement>
60         {
61             private QueryOperatorEnumerator<TElement, TKey> m_operatorEnumerator;
62             private TElement m_current;
63
64             internal QueryOperatorClassicEnumerator(QueryOperatorEnumerator<TElement, TKey> operatorEnumerator)
65             {
66                 Contract.Assert(operatorEnumerator != null);
67                 m_operatorEnumerator = operatorEnumerator;
68             }
69
70             public bool MoveNext()
71             {
72                 TKey keyUnused = default(TKey);
73                 return m_operatorEnumerator.MoveNext(ref m_current, ref keyUnused);
74             }
75
76             public TElement Current
77             {
78                 get { return m_current; }
79             }
80
81             object IEnumerator.Current
82             {
83                 get { return m_current; }
84             }
85
86             public void Dispose()
87             {
88                 m_operatorEnumerator.Dispose();
89                 m_operatorEnumerator = null;
90             }
91
92             public void Reset()
93             {
94                 m_operatorEnumerator.Reset();
95             }
96         }
97     }
98 }