do not check order sequence if option /order was not used
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryOrderByNode.cs
1 //
2 // QueryOrderByNode.cs
3 //
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26
27 #if NET_4_0
28 using System;
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33
34 namespace System.Linq.Parallel.QueryNodes
35 {
36         internal class QueryOrderByNode<T> : QueryOrderGuardNode<T>
37         {
38                 Comparison<T> comparison;
39
40                 public QueryOrderByNode (QueryBaseNode<T> parent, Comparison<T> comparison)
41                         : base (parent, true)
42                 {
43                         this.comparison = comparison;
44                 }
45
46
47                 public QueryOrderByNode (QueryOrderByNode<T> parent, Comparison<T> comparison)
48                         : base (parent.Parent, true)
49                 {
50                         this.comparison = MergeComparison (parent.ComparisonFunc, comparison);
51                 }
52
53                 public Comparison<T> ComparisonFunc {
54                         get {
55                                 return comparison;
56                         }
57                 }
58
59                 internal override IEnumerable<T> GetSequential ()
60                 {
61                         return Parent.GetSequential ().OrderBy ((e) => e, new ComparisonComparer (comparison));
62                 }
63
64                 private class ComparisonComparer : IComparer<T>
65                 {
66                         Comparison<T> comparison;
67
68                         internal ComparisonComparer (Comparison<T> comparison)
69                         {
70                                 this.comparison = comparison;
71                         }
72
73                         int IComparer<T>.Compare (T x, T y)
74                         {
75                                 return comparison (x, y);
76                         }
77                 }
78
79                 internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
80                 {
81                         throw new InvalidOperationException ("Shouldn't be called");
82                 }
83
84                 internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
85                 {
86                         int partitionCount;
87                         IList<T> aggregList = GetAggregatedList (out partitionCount);
88                         IList<T> result = ParallelQuickSort<T>.Sort (aggregList, comparison);
89
90                         OrderablePartitioner<T> partitioner = ParallelPartitioner.CreateForStrips (result, 1);
91
92                         return WrapHelper.Wrap (partitioner.GetOrderablePartitions (options.PartitionCount));
93                 }
94
95                 IList<T> GetAggregatedList (out int partitionCount)
96                 {
97                         AggregationList<T> result = null;
98                         partitionCount = -1;
99
100                         ParallelExecuter.ProcessAndAggregate<T, IList<T>> (Parent, () => new List<T> (),
101                                                                            LocalCall,
102                                                                            (ls) => { result = new AggregationList<T> (ls); });
103
104                         return result;
105                 }
106
107                 IList<T> LocalCall (IList<T> list, T element)
108                 {
109                         list.Add (element);
110                         return list;
111                 }
112
113                 static Comparison<T> MergeComparison (Comparison<T> source, Comparison<T> other)
114                 {
115                         return (e1, e2) => {
116                                 int result = source (e1, e2);
117                                 return result == 0 ? other (e1, e2) : result;
118                         };
119                 }
120         }
121 }
122 #endif