Update mcs/class/System.Core/System/TimeZoneInfo.cs
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryHeadWorkerNode.cs
1 //
2 // QueryConcatNode.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
33 namespace System.Linq.Parallel.QueryNodes
34 {
35         internal interface QueryHeadWorkerNode : IVisitableNode {
36                 int? Count { get; }
37         }
38         /* This is the QueryNode used by Take(While) operator
39          * it symbolize operators that are preferably working on the head elements of a query and can prematurely
40          * stop providing elements following the one they were processing is of a greater value in a specific 
41          * order to be defined by the instance (e.g. simple numerical order when working on indexes).
42          */
43         internal class QueryHeadWorkerNode<TSource> : QueryStreamNode<TSource, TSource>, QueryHeadWorkerNode
44         {
45                 /* This variable will receive an index value that represent the "stop point"
46                  * when used with GetOrderedEnumerables i.e. values that are above the indexes are discarded
47                  * (if the partitioner is ordered in a partition it even stop the processing) and value below are still tested just
48                  * in case and can still lower this gap limit.
49                  */
50                 readonly int count;
51                 readonly Func<TSource, int, bool> predicate;
52                 
53                 internal QueryHeadWorkerNode (QueryBaseNode<TSource> parent, int count)
54                         : base (parent, false)
55                 {
56                         this.count = count;
57                 }
58
59                 internal QueryHeadWorkerNode (QueryBaseNode<TSource> parent, Func<TSource, int, bool> predicate, bool indexed)
60                         : base (parent, indexed)
61                 {
62                         this.predicate = predicate;
63                 }
64
65                 public int? Count {
66                         get {
67                                 return predicate == null ? count : (int?)null;
68                         }
69                 }
70
71                 internal override IEnumerable<TSource> GetSequential ()
72                 {
73                         IEnumerable<TSource> parent = Parent.GetSequential ();
74
75                         return predicate == null ? parent.Take (count) : parent.TakeWhile (predicate);
76                 }
77
78                 public override void Visit (INodeVisitor visitor)
79                 {
80                         visitor.Visit ((QueryHeadWorkerNode)this);
81                 }
82
83                 internal override IList<IEnumerable<TSource>> GetEnumerablesIndexed (QueryOptions options)
84                 {       
85                         return Parent.GetOrderedEnumerables (options)
86                                 .Select ((i) => i.TakeWhile ((e) => predicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
87                                 .ToList ();
88                 }
89
90                 internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
91                 {
92                         return Parent.GetEnumerables (options)
93                                 .Select (GetSelector (count))
94                                 .ToList ();
95                 }
96
97                 Func<IEnumerable<TSource>, IEnumerable<TSource>> GetSelector (int c)
98                 {
99                         if (predicate == null)
100                                 return (i) => i.TakeWhile ((e) => c > 0 && Interlocked.Decrement (ref c) >= 0);
101                         else
102                                 return (i) => i.TakeWhile ((e) => predicate (e, -1));
103                 }
104
105                 internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
106                 {
107                         return Parent.GetOrderedEnumerables (options)
108                                 .Select ((i) => GetEnumerableInternal (i, options))
109                                 .ToList ();
110                 }
111
112                 IEnumerable<KeyValuePair<long, TSource>> GetEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source, QueryOptions options)
113                 {
114                         IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
115                         long gapIndex = predicate == null ? count : long.MaxValue;
116                         Func<KeyValuePair<long, TSource>, bool> cond;
117                         if (predicate == null)
118                                 cond = (kv) => kv.Key < count;
119                         else
120                                 cond = (kv) => predicate (kv.Value, (int)kv.Key);
121                         
122                         try {
123                                 while (current.MoveNext ()) {
124                                         KeyValuePair<long, TSource> kvp = current.Current;
125
126                                         /* When filtering is based on a predicate, this short-circuit is only valid 
127                                          * if the partitioner used ensure items are ordered in each partition 
128                                          * (valid w/ default partitioners)
129                                          */
130                                         if (kvp.Key >= gapIndex && options.PartitionerSettings.Item2)
131                                                 break;
132
133                                         if (!cond (kvp)) {
134                                                 if (gapIndex > kvp.Key && predicate != null)
135                                                         gapIndex = kvp.Key;
136
137                                                 continue;
138                                         }
139                                         
140                                         yield return kvp;
141                                 }
142                         } finally {
143                                 current.Dispose ();
144                         }
145                 }
146         }
147
148 }
149
150 #endif