3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // IndexedSelectQueryOperator.cs
10 // <OWNER>Microsoft</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Threading;
18 namespace System.Linq.Parallel
21 /// A variant of the Select operator that supplies element index while performing the
22 /// projection operation. This requires cooperation with partitioning and merging to
23 /// guarantee ordering is preserved.
26 /// <typeparam name="TInput"></typeparam>
27 /// <typeparam name="TOutput"></typeparam>
28 internal sealed class IndexedSelectQueryOperator<TInput, TOutput> : UnaryQueryOperator<TInput, TOutput>
31 // Selector function. Used to project elements to a transformed view during execution.
32 private readonly Func<TInput, int, TOutput> m_selector;
33 private bool m_prematureMerge = false; // Whether to prematurely merge the input of this operator.
34 private bool m_limitsParallelism = false; // Whether this operator limits parallelism
36 //---------------------------------------------------------------------------------------
37 // Initializes a new select operator.
40 // child - the child operator or data source from which to pull data
41 // selector - a delegate representing the selector function
44 // selector must be non null.
47 internal IndexedSelectQueryOperator(IEnumerable<TInput> child,
48 Func<TInput, int, TOutput> selector)
51 Contract.Assert(child != null, "child data source cannot be null");
52 Contract.Assert(selector != null, "need a selector function");
54 m_selector = selector;
56 // In an indexed Select, elements must be returned in the order in which
57 // indices were assigned.
58 m_outputOrdered = true;
60 InitOrdinalIndexState();
63 private void InitOrdinalIndexState()
65 OrdinalIndexState childIndexState = Child.OrdinalIndexState;
66 OrdinalIndexState indexState = childIndexState;
68 if (ExchangeUtilities.IsWorseThan(childIndexState, OrdinalIndexState.Correct))
70 m_prematureMerge = true;
71 m_limitsParallelism = childIndexState != OrdinalIndexState.Shuffled;
72 indexState = OrdinalIndexState.Correct;
75 Contract.Assert(!ExchangeUtilities.IsWorseThan(indexState, OrdinalIndexState.Correct));
77 SetOrdinalIndexState(indexState);
80 //---------------------------------------------------------------------------------------
81 // Just opens the current operator, including opening the child and wrapping it with
82 // partitions as needed.
85 internal override QueryResults<TOutput> Open(
86 QuerySettings settings, bool preferStriping)
88 QueryResults<TInput> childQueryResults = Child.Open(settings, preferStriping);
89 return IndexedSelectQueryOperatorResults.NewResults(childQueryResults, this, settings, preferStriping);
92 internal override void WrapPartitionedStream<TKey>(
93 PartitionedStream<TInput, TKey> inputStream, IPartitionedStreamRecipient<TOutput> recipient, bool preferStriping, QuerySettings settings)
95 int partitionCount = inputStream.PartitionCount;
97 // If the index is not correct, we need to reindex.
98 PartitionedStream<TInput, int> inputStreamInt;
101 ListQueryResults<TInput> listResults = QueryOperator<TInput>.ExecuteAndCollectResults(
102 inputStream, partitionCount, Child.OutputOrdered, preferStriping, settings);
103 inputStreamInt = listResults.GetPartitionedStream();
107 Contract.Assert(typeof(TKey) == typeof(int));
108 inputStreamInt = (PartitionedStream<TInput, int>)(object)inputStream;
111 // Since the index is correct, the type of the index must be int
112 PartitionedStream<TOutput, int> outputStream =
113 new PartitionedStream<TOutput, int>(partitionCount, Util.GetDefaultComparer<int>(), OrdinalIndexState);
115 for (int i = 0; i < partitionCount; i++)
117 outputStream[i] = new IndexedSelectQueryOperatorEnumerator(inputStreamInt[i], m_selector);
120 recipient.Receive(outputStream);
124 //---------------------------------------------------------------------------------------
125 // Whether this operator performs a premature merge that would not be performed in
126 // a similar sequential operation (i.e., in LINQ to Objects).
129 internal override bool LimitsParallelism
131 get { return m_limitsParallelism; }
134 //---------------------------------------------------------------------------------------
135 // The enumerator type responsible for projecting elements as it is walked.
138 class IndexedSelectQueryOperatorEnumerator : QueryOperatorEnumerator<TOutput, int>
141 private readonly QueryOperatorEnumerator<TInput, int> m_source; // The data source to enumerate.
142 private readonly Func<TInput, int, TOutput> m_selector; // The actual select function.
144 //---------------------------------------------------------------------------------------
145 // Instantiates a new select enumerator.
148 internal IndexedSelectQueryOperatorEnumerator(QueryOperatorEnumerator<TInput, int> source, Func<TInput, int, TOutput> selector)
150 Contract.Assert(source != null);
151 Contract.Assert(selector != null);
153 m_selector = selector;
156 //---------------------------------------------------------------------------------------
157 // Straightforward IEnumerator<T> methods.
160 internal override bool MoveNext(ref TOutput currentElement, ref int currentKey)
162 // So long as the source has a next element, we have an element.
163 TInput element = default(TInput);
164 if (m_source.MoveNext(ref element, ref currentKey))
166 Contract.Assert(m_selector != null, "expected a compiled selection function");
167 currentElement = m_selector(element, currentKey);
174 protected override void Dispose(bool disposing)
182 //---------------------------------------------------------------------------------------
183 // Returns an enumerable that represents the query executing sequentially.
186 internal override IEnumerable<TOutput> AsSequentialQuery(CancellationToken token)
188 return Child.AsSequentialQuery(token).Select(m_selector);
191 //-----------------------------------------------------------------------------------
192 // Query results for an indexed Select operator. The results are indexible if the child
193 // results were indexible.
196 class IndexedSelectQueryOperatorResults : UnaryQueryOperatorResults
198 IndexedSelectQueryOperator<TInput, TOutput> m_selectOp; // Operator that generated the results
199 int m_childCount; // The number of elements in child results
201 public static QueryResults<TOutput> NewResults(
202 QueryResults<TInput> childQueryResults, IndexedSelectQueryOperator<TInput, TOutput> op,
203 QuerySettings settings, bool preferStriping)
205 if (childQueryResults.IsIndexible)
207 return new IndexedSelectQueryOperatorResults(
208 childQueryResults, op, settings, preferStriping);
212 return new UnaryQueryOperatorResults(
213 childQueryResults, op, settings, preferStriping);
217 private IndexedSelectQueryOperatorResults(
218 QueryResults<TInput> childQueryResults, IndexedSelectQueryOperator<TInput, TOutput> op,
219 QuerySettings settings, bool preferStriping)
220 : base(childQueryResults, op, settings, preferStriping)
223 Contract.Assert(m_childQueryResults.IsIndexible);
225 m_childCount = m_childQueryResults.ElementsCount;
228 internal override int ElementsCount
232 Contract.Assert(m_childCount >= 0);
233 return m_childQueryResults.ElementsCount;
237 internal override bool IsIndexible
242 internal override TOutput GetElement(int index)
244 return m_selectOp.m_selector(m_childQueryResults.GetElement(index), index);