Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Core / System / Linq / Parallel / Enumerables / ParallelQuery.cs
1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 //
8 // ParallelQuery.cs
9 //
10 // <OWNER>Microsoft</OWNER>
11 //
12 // ParallelQuery is an abstract class that represents a PLINQ query.
13 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14
15 using System.Collections;
16 using System.Collections.Generic;
17 using System.Linq.Parallel;
18 using System.Diagnostics.Contracts;
19
20 namespace System.Linq
21 {
22     /// <summary>
23     /// Represents a parallel sequence.
24     /// </summary>
25     public class ParallelQuery : IEnumerable
26     {
27         // Settings that have been specified on the query so far.
28         private QuerySettings m_specifiedSettings;
29
30         internal ParallelQuery(QuerySettings specifiedSettings)
31         {
32             m_specifiedSettings = specifiedSettings;
33         }
34
35         //-----------------------------------------------------------------------------------
36         // Settings that have been specified on the query so far. Some settings may still
37         // be unspecified and will be replaced either by operators further in the query,
38         // or filled in with defaults at query opening time.
39         //
40
41         internal QuerySettings SpecifiedQuerySettings
42         {
43             get { return m_specifiedSettings; }
44         }
45
46         //-----------------------------------------------------------------------------------
47         // Returns a parallel enumerable that represents 'this' enumerable, with each element
48         // casted to TCastTo. If some element is not of type TCastTo, InvalidCastException
49         // is thrown.
50         //
51
52         internal virtual ParallelQuery<TCastTo> Cast<TCastTo>()
53         {
54             Contract.Assert(false, "The derived class must override this method.");
55             throw new NotSupportedException();
56         }
57
58         //-----------------------------------------------------------------------------------
59         // Returns a parallel enumerable that represents 'this' enumerable, with each element
60         // casted to TCastTo. Elements that are not of type TCastTo will be left out from
61         // the results.
62         //
63
64         internal virtual ParallelQuery<TCastTo> OfType<TCastTo>()
65         {
66             Contract.Assert(false, "The derived class must override this method.");
67             throw new NotSupportedException();
68         }
69
70         //-----------------------------------------------------------------------------------
71         // Derived classes implement GetEnumeratorUntyped() instead of IEnumerable.GetEnumerator()
72         // This is to avoid the method name conflict if the derived classes also implement
73         // IEnumerable<T>.
74         //
75
76         internal virtual IEnumerator GetEnumeratorUntyped()
77         {
78             Contract.Assert(false, "The derived class must override this method.");
79             throw new NotSupportedException();
80         }
81
82         /// <summary>
83         /// Returns an enumerator that iterates through the sequence.
84         /// </summary>
85         /// <returns>An enumerator that iterates through the sequence.</returns>
86         IEnumerator IEnumerable.GetEnumerator()
87         {
88             return GetEnumeratorUntyped();
89         }
90     }
91
92     /// <summary>
93     /// Represents a parallel sequence.
94     /// </summary>
95     public class ParallelQuery<TSource> : ParallelQuery, IEnumerable<TSource>
96     {
97         internal ParallelQuery(QuerySettings settings)
98             : base(settings)
99         {
100         }
101
102         internal sealed override ParallelQuery<TCastTo> Cast<TCastTo>()
103         {
104             return ParallelEnumerable.Select<TSource, TCastTo>(this, elem => (TCastTo)(object)elem);
105         }
106
107         internal sealed override ParallelQuery<TCastTo> OfType<TCastTo>()
108         {
109             // @PERF: Currently defined in terms of other operators. This isn't the most performant
110             //      solution (because it results in two operators) but is simple to implement.
111             return this
112                 .Where<TSource>(elem => elem is TCastTo)
113                 .Select<TSource, TCastTo>(elem => (TCastTo)(object)elem);
114         }
115
116         internal override IEnumerator GetEnumeratorUntyped()
117         {
118             return ((IEnumerable<TSource>)this).GetEnumerator();
119         }
120
121         /// <summary>
122         /// Returns an enumerator that iterates through the sequence.
123         /// </summary>
124         /// <returns>An enumerator that iterates through the sequence.</returns>
125         public virtual IEnumerator<TSource> GetEnumerator()
126         {
127             Contract.Assert(false, "The derived class must override this method.");
128             throw new NotSupportedException();
129         }
130     }
131 }