Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryStartNode.cs
1 //
2 // QueryStartNode.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.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
32 using System.Threading;
33 using System.Threading.Tasks;
34
35 namespace System.Linq.Parallel.QueryNodes
36 {
37         internal interface QueryStartNode : IVisitableNode {
38                 int Count { get; }
39         }
40
41         internal class QueryStartNode<T> : QueryBaseNode<T>, QueryStartNode
42         {
43                 readonly IEnumerable<T> source;
44                 readonly Partitioner<T> customPartitioner;
45
46                 internal QueryStartNode (IEnumerable<T> source)
47                 {
48                         if (source == null)
49                                 throw new ArgumentNullException ("source");
50
51                         this.source = source;
52                 }
53
54                 internal QueryStartNode (Partitioner<T> custom)
55                 {
56                         if (custom == null)
57                                 throw new ArgumentNullException ("custom");
58
59                         this.customPartitioner = custom;
60                 }
61
62                 // If possible, this property will return the number of element the query
63                 // is going to process. If that number if pretty low, executing the query
64                 // sequentially is better
65                 public int Count {
66                         get {
67                                 if (source == null)
68                                         return -1;
69
70                                 ICollection coll = source as ICollection;
71                                 return coll == null ? -1 : coll.Count;
72                         }
73                 }
74
75                 public override void Visit (INodeVisitor visitor)
76                 {
77                         visitor.Visit ((QueryStartNode)this);
78                 }
79
80                 internal override IEnumerable<T> GetSequential ()
81                 {
82                         if (source != null)
83                                 return source;
84
85                         return WrapHelper.Wrap (customPartitioner.GetPartitions (1))[0];
86                 }
87
88                 internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
89                 {
90                         if (customPartitioner != null) {
91                                 return WrapHelper.Wrap (customPartitioner.GetPartitions (options.PartitionCount));
92                         }
93
94                         Partitioner<T> partitioner
95                                 = (options.UseStrip) ? ParallelPartitioner.CreateForStrips (source, 1) : ParallelPartitioner.CreateBest (source);
96
97                         return WrapHelper.Wrap (partitioner.GetPartitions (options.PartitionCount));
98                 }
99
100                 internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
101                 {
102                         OrderablePartitioner<T> partitioner = null;
103                         if (customPartitioner != null) {
104                                 partitioner = customPartitioner as OrderablePartitioner<T>;
105                                 if (partitioner == null)
106                                         throw new InvalidOperationException ("The partitionner you are using doesn't support ordered partitionning");
107                         } else {
108                                 partitioner =
109                                         (options.UseStrip) ? ParallelPartitioner.CreateForStrips (source, 1) : ParallelPartitioner.CreateBest (source);
110                         }
111
112                         options.PartitionerSettings = Tuple.Create (partitioner.KeysOrderedAcrossPartitions,
113                                                                     partitioner.KeysOrderedInEachPartition,
114                                                                     partitioner.KeysNormalized);
115
116                         // We only support one style of partitioning at the moment.
117                         // Standard partitioners follow this style.
118                         if (options.UseStrip && (!partitioner.KeysOrderedInEachPartition || partitioner.KeysOrderedAcrossPartitions))
119                                 throw new NotImplementedException ("Partitioner must have KeysOrderedInEachPartition "
120                                                                    + "and !KeysOrderedAcrossPartitions"
121                                                                    + "to be used with indexed operators");
122
123                         return WrapHelper.Wrap (partitioner.GetOrderablePartitions (options.PartitionCount));
124                 }
125         }
126 }
127 #endif