3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // SortQueryOperator.cs
10 // <OWNER>[....]</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Threading;
18 namespace System.Linq.Parallel
21 /// The query operator for OrderBy and ThenBy.
23 /// <typeparam name="TInputOutput"></typeparam>
24 /// <typeparam name="TSortKey"></typeparam>
25 internal sealed class SortQueryOperator<TInputOutput, TSortKey> :
26 UnaryQueryOperator<TInputOutput, TInputOutput>, IOrderedEnumerable<TInputOutput>
28 private readonly Func<TInputOutput, TSortKey> m_keySelector; // Key selector used when sorting.
29 private readonly IComparer<TSortKey> m_comparer; // Key comparison logic to use during sorting.
31 //---------------------------------------------------------------------------------------
32 // Instantiates a new sort operator.
35 internal SortQueryOperator(IEnumerable<TInputOutput> source, Func<TInputOutput, TSortKey> keySelector,
36 IComparer<TSortKey> comparer, bool descending)
39 Contract.Assert(keySelector != null, "key selector must not be null");
41 m_keySelector = keySelector;
43 // If a comparer wasn't supplied, we use the default one for the key type.
46 m_comparer = Util.GetDefaultComparer<TSortKey>();
50 m_comparer = comparer;
55 m_comparer = new ReverseComparer<TSortKey>(m_comparer);
58 SetOrdinalIndexState(OrdinalIndexState.Shuffled);
61 //---------------------------------------------------------------------------------------
62 // IOrderedEnumerable method for nesting an order by operator inside another.
65 IOrderedEnumerable<TInputOutput> IOrderedEnumerable<TInputOutput>.CreateOrderedEnumerable<TKey2>(
66 Func<TInputOutput, TKey2> key2Selector, IComparer<TKey2> key2Comparer, bool descending)
68 key2Comparer = key2Comparer ?? Util.GetDefaultComparer<TKey2>();
72 key2Comparer = new ReverseComparer<TKey2>(key2Comparer);
75 IComparer<Pair<TSortKey, TKey2>> pairComparer = new PairComparer<TSortKey, TKey2>(m_comparer, key2Comparer);
76 Func<TInputOutput, Pair<TSortKey, TKey2>> pairKeySelector =
77 (TInputOutput elem) => new Pair<TSortKey, TKey2>(m_keySelector(elem), key2Selector(elem));
79 return new SortQueryOperator<TInputOutput, Pair<TSortKey, TKey2>>(Child, pairKeySelector, pairComparer, false);
82 //---------------------------------------------------------------------------------------
83 // Accessor the the key selector.
86 internal Func<TInputOutput, TSortKey> KeySelector
88 get { return m_keySelector; }
91 //---------------------------------------------------------------------------------------
92 // Accessor the the key comparer.
95 internal IComparer<TSortKey> KeyComparer
97 get { return m_comparer; }
100 //---------------------------------------------------------------------------------------
101 // Opens the current operator. This involves opening the child operator tree, enumerating
102 // the results, sorting them, and then returning an enumerator that walks the result.
105 internal override QueryResults<TInputOutput> Open(QuerySettings settings, bool preferStriping)
107 QueryResults<TInputOutput> childQueryResults = Child.Open(settings, false);
108 return new SortQueryOperatorResults<TInputOutput, TSortKey>(childQueryResults, this, settings, preferStriping);
112 internal override void WrapPartitionedStream<TKey>(
113 PartitionedStream<TInputOutput, TKey> inputStream, IPartitionedStreamRecipient<TInputOutput> recipient, bool preferStriping, QuerySettings settings)
115 PartitionedStream<TInputOutput, TSortKey> outputStream =
116 new PartitionedStream<TInputOutput, TSortKey>(inputStream.PartitionCount, this.m_comparer, OrdinalIndexState);
118 for (int i = 0; i < outputStream.PartitionCount; i++)
120 outputStream[i] = new SortQueryOperatorEnumerator<TInputOutput, TKey, TSortKey>(
121 inputStream[i], m_keySelector, m_comparer);
124 recipient.Receive<TSortKey>(outputStream);
127 //---------------------------------------------------------------------------------------
128 // Returns an enumerable that represents the query executing sequentially.
131 internal override IEnumerable<TInputOutput> AsSequentialQuery(CancellationToken token)
133 IEnumerable<TInputOutput> wrappedChild = CancellableEnumerable.Wrap(Child.AsSequentialQuery(token), token);
134 return wrappedChild.OrderBy(m_keySelector, m_comparer);
137 //---------------------------------------------------------------------------------------
138 // Whether this operator performs a premature merge that would not be performed in
139 // a similar sequential operation (i.e., in LINQ to Objects).
142 internal override bool LimitsParallelism
144 get { return false; }
148 internal class SortQueryOperatorResults<TInputOutput, TSortKey> : QueryResults<TInputOutput>
150 protected QueryResults<TInputOutput> m_childQueryResults; // Results of the child query
151 private SortQueryOperator<TInputOutput, TSortKey> m_op; // Operator that generated these results
152 private QuerySettings m_settings; // Settings collected from the query
154 private bool m_preferStriping; // If the results are indexible, should we use striping when partitioning them
157 internal SortQueryOperatorResults(
158 QueryResults<TInputOutput> childQueryResults, SortQueryOperator<TInputOutput, TSortKey> op,
159 QuerySettings settings, bool preferStriping)
161 m_childQueryResults = childQueryResults;
163 m_settings = settings;
165 m_preferStriping = preferStriping;
169 internal override bool IsIndexible
171 get { return false; }
174 internal override void GivePartitionedStream(IPartitionedStreamRecipient<TInputOutput> recipient)
176 m_childQueryResults.GivePartitionedStream(new ChildResultsRecipient(recipient, m_op, m_settings));
179 private class ChildResultsRecipient : IPartitionedStreamRecipient<TInputOutput>
181 IPartitionedStreamRecipient<TInputOutput> m_outputRecipient;
182 SortQueryOperator<TInputOutput, TSortKey> m_op;
183 QuerySettings m_settings;
185 internal ChildResultsRecipient(IPartitionedStreamRecipient<TInputOutput> outputRecipient, SortQueryOperator<TInputOutput, TSortKey> op, QuerySettings settings)
187 m_outputRecipient = outputRecipient;
189 m_settings = settings;
192 public void Receive<TKey>(PartitionedStream<TInputOutput, TKey> childPartitionedStream)
194 m_op.WrapPartitionedStream(childPartitionedStream, m_outputRecipient, false, m_settings);
199 //---------------------------------------------------------------------------------------
200 // This enumerator performs sorting based on a key selection and comparison routine.
203 internal class SortQueryOperatorEnumerator<TInputOutput, TKey, TSortKey> : QueryOperatorEnumerator<TInputOutput, TSortKey>
205 private readonly QueryOperatorEnumerator<TInputOutput, TKey> m_source; // Data source to sort.
206 private readonly Func<TInputOutput, TSortKey> m_keySelector; // Key selector used when sorting.
207 private readonly IComparer<TSortKey> m_keyComparer; // Key comparison logic to use during sorting.
209 //---------------------------------------------------------------------------------------
210 // Instantiates a new sort operator enumerator.
213 internal SortQueryOperatorEnumerator(QueryOperatorEnumerator<TInputOutput, TKey> source,
214 Func<TInputOutput, TSortKey> keySelector, IComparer<TSortKey> keyComparer)
216 Contract.Assert(source != null);
217 Contract.Assert(keySelector != null, "need a key comparer");
218 Contract.Assert(keyComparer != null, "expected a compiled operator");
221 m_keySelector = keySelector;
222 m_keyComparer = keyComparer;
224 //---------------------------------------------------------------------------------------
225 // Accessor for the key comparison routine.
228 public IComparer<TSortKey> KeyComparer
230 get { return m_keyComparer; }
233 //---------------------------------------------------------------------------------------
234 // Moves to the next element in the sorted output. When called for the first time, the
235 // descendents in the sort's child tree are executed entirely, the results accumulated
236 // in memory, and the data sorted.
239 internal override bool MoveNext(ref TInputOutput currentElement, ref TSortKey currentKey)
241 Contract.Assert(m_source != null);
243 TKey keyUnused = default(TKey);
244 if (!m_source.MoveNext(ref currentElement, ref keyUnused))
249 currentKey = m_keySelector(currentElement);
253 protected override void Dispose(bool disposing)
255 Contract.Assert(m_source != null);