3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
10 // <OWNER>Microsoft</OWNER>
12 // ParallelQuery is an abstract class that represents a PLINQ query.
13 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
15 using System.Collections;
16 using System.Collections.Generic;
17 using System.Linq.Parallel;
18 using System.Diagnostics.Contracts;
23 /// Represents a parallel sequence.
25 public class ParallelQuery : IEnumerable
27 // Settings that have been specified on the query so far.
28 private QuerySettings m_specifiedSettings;
30 internal ParallelQuery(QuerySettings specifiedSettings)
32 m_specifiedSettings = specifiedSettings;
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.
41 internal QuerySettings SpecifiedQuerySettings
43 get { return m_specifiedSettings; }
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
52 internal virtual ParallelQuery<TCastTo> Cast<TCastTo>()
54 Contract.Assert(false, "The derived class must override this method.");
55 throw new NotSupportedException();
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
64 internal virtual ParallelQuery<TCastTo> OfType<TCastTo>()
66 Contract.Assert(false, "The derived class must override this method.");
67 throw new NotSupportedException();
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
76 internal virtual IEnumerator GetEnumeratorUntyped()
78 Contract.Assert(false, "The derived class must override this method.");
79 throw new NotSupportedException();
83 /// Returns an enumerator that iterates through the sequence.
85 /// <returns>An enumerator that iterates through the sequence.</returns>
86 IEnumerator IEnumerable.GetEnumerator()
88 return GetEnumeratorUntyped();
93 /// Represents a parallel sequence.
95 public class ParallelQuery<TSource> : ParallelQuery, IEnumerable<TSource>
97 internal ParallelQuery(QuerySettings settings)
102 internal sealed override ParallelQuery<TCastTo> Cast<TCastTo>()
104 return ParallelEnumerable.Select<TSource, TCastTo>(this, elem => (TCastTo)(object)elem);
107 internal sealed override ParallelQuery<TCastTo> OfType<TCastTo>()
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.
112 .Where<TSource>(elem => elem is TCastTo)
113 .Select<TSource, TCastTo>(elem => (TCastTo)(object)elem);
116 internal override IEnumerator GetEnumeratorUntyped()
118 return ((IEnumerable<TSource>)this).GetEnumerator();
122 /// Returns an enumerator that iterates through the sequence.
124 /// <returns>An enumerator that iterates through the sequence.</returns>
125 public virtual IEnumerator<TSource> GetEnumerator()
127 Contract.Assert(false, "The derived class must override this method.");
128 throw new NotSupportedException();