5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
34 namespace System.Linq.Parallel.QueryNodes
36 internal class QueryOrderByNode<T> : QueryOrderGuardNode<T>
38 Comparison<T> comparison;
40 public QueryOrderByNode (QueryBaseNode<T> parent, Comparison<T> comparison)
43 this.comparison = comparison;
47 public QueryOrderByNode (QueryOrderByNode<T> parent, Comparison<T> comparison)
48 : base (parent.Parent, true)
50 this.comparison = MergeComparison (parent.ComparisonFunc, comparison);
53 public Comparison<T> ComparisonFunc {
59 internal override IEnumerable<T> GetSequential ()
61 return Parent.GetSequential ().OrderBy ((e) => e, new ComparisonComparer (comparison));
64 private class ComparisonComparer : IComparer<T>
66 Comparison<T> comparison;
68 internal ComparisonComparer (Comparison<T> comparison)
70 this.comparison = comparison;
73 int IComparer<T>.Compare (T x, T y)
75 return comparison (x, y);
79 internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
81 throw new InvalidOperationException ("Shouldn't be called");
84 internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
87 IList<T> aggregList = GetAggregatedList (out partitionCount);
88 IList<T> result = ParallelQuickSort<T>.Sort (aggregList, comparison);
90 OrderablePartitioner<T> partitioner = ParallelPartitioner.CreateForStrips (result, 1);
92 return WrapHelper.Wrap (partitioner.GetOrderablePartitions (options.PartitionCount));
95 IList<T> GetAggregatedList (out int partitionCount)
97 AggregationList<T> result = null;
100 ParallelExecuter.ProcessAndAggregate<T, IList<T>> (Parent, () => new List<T> (),
102 (ls) => { result = new AggregationList<T> (ls); });
107 IList<T> LocalCall (IList<T> list, T element)
113 static Comparison<T> MergeComparison (Comparison<T> source, Comparison<T> other)
116 int result = source (e1, e2);
117 return result == 0 ? other (e1, e2) : result;