Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryOptionNode.cs
1 //
2 // QueryOptionNode.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 || MOBILE
28 using System;
29 using System.Threading;
30 using System.Collections.Generic;
31
32 namespace System.Linq.Parallel.QueryNodes
33 {
34         // The first four elements correspond to the public operator With*
35         // Last CancellationToken parameter is used internally for ImplementerToken
36         using OptionsList = Tuple<ParallelMergeOptions?, ParallelExecutionMode?, CancellationToken?, int, CancellationTokenSource>;
37
38
39         interface QueryOptionNode : IVisitableNode {
40                 OptionsList GetOptions ();
41         }
42
43         internal class QueryOptionNode<T> : QueryChildNode<T, T>, QueryOptionNode
44         {
45
46                 public QueryOptionNode (QueryBaseNode<T> parent)
47                         : base (parent)
48                 {
49
50                 }
51
52                 public virtual OptionsList GetOptions ()
53                 {
54                         return new OptionsList (null, null, null, -1, null);
55                 }
56
57                 internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
58                 {
59                         return Parent.GetEnumerables (options);
60                 }
61
62                 internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
63                 {
64                         return Parent.GetOrderedEnumerables (options);
65                 }
66
67                 internal override IEnumerable<T> GetSequential ()
68                 {
69                         return Parent.GetSequential ();
70                 }
71
72                 public override void Visit (INodeVisitor visitor)
73                 {
74                         visitor.Visit ((QueryOptionNode)this);
75                 }
76         }
77
78         internal class ParallelExecutionModeNode<T> : QueryOptionNode<T>
79         {
80                 ParallelExecutionMode mode;
81
82                 internal ParallelExecutionModeNode (ParallelExecutionMode mode, QueryBaseNode<T> parent)
83                         : base (parent)
84                 {
85                         this.mode = mode;
86                 }
87
88                 public override OptionsList GetOptions ()
89                 {
90                         return new OptionsList (null, mode, null, -1, null);
91                 }
92         }
93
94
95         internal class ParallelMergeOptionsNode<T> : QueryOptionNode<T>
96         {
97                 ParallelMergeOptions opts;
98
99                 internal ParallelMergeOptionsNode (ParallelMergeOptions opts, QueryBaseNode<T> parent)
100                         : base (parent)
101                 {
102                         this.opts = opts;
103                 }
104
105                 public override OptionsList GetOptions ()
106                 {
107                         return new OptionsList (opts, null, null, -1, null);
108                 }
109         }
110
111
112         internal class CancellationTokenNode<T> : QueryOptionNode<T>
113         {
114                 CancellationToken token;
115
116                 internal CancellationTokenNode (CancellationToken token, QueryBaseNode<T> parent)
117                         : base (parent)
118                 {
119                         this.token = token;
120                 }
121
122                 public override OptionsList GetOptions ()
123                 {
124                         return new OptionsList (null, null, token, -1, null);
125                 }
126         }
127
128         internal class DegreeOfParallelismNode<T> : QueryOptionNode<T>
129         {
130                 int degreeParallelism;
131
132                 internal DegreeOfParallelismNode (int degreeParallelism, QueryBaseNode<T> parent)
133                         : base (parent)
134                 {
135                         this.degreeParallelism = degreeParallelism;
136                 }
137
138                 public override OptionsList GetOptions ()
139                 {
140                         return new OptionsList (null, null, null, degreeParallelism, null);
141                 }
142         }
143
144         internal class ImplementerTokenNode<T> : QueryOptionNode<T>
145         {
146                 CancellationTokenSource source;
147
148                 internal ImplementerTokenNode (CancellationTokenSource token, QueryBaseNode<T> parent)
149                         : base (parent)
150                 {
151                         this.source = token;
152                 }
153
154                 public override OptionsList GetOptions ()
155                 {
156                         return new OptionsList (null, null, null, -1, source);
157                 }
158         }
159 }
160 #endif