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;
33 namespace System.Linq.Parallel.QueryNodes
35 /* This is the QueryNode used by Take(While) operator
36 * it symbolize operators that are preferably working on the head elements of a query and can prematurely
37 * stop providing elements following the one they were processing is of a greater value in a specific
38 * order to be defined by the instance (e.g. simple numerical order when working on indexes).
40 internal class QueryHeadWorkerNode<TSource> : QueryStreamNode<TSource, TSource>
42 /* This variable will receive an index value that represent the "stop point"
43 * when used with GetOrderedEnumerables i.e. values that are above the indexes are discarded
44 * (if the partitioner is ordered in a partition it even stop the processing) and value below are still tested just
45 * in case and can still lower this gap limit.
48 readonly Func<TSource, int, bool> predicate;
50 internal QueryHeadWorkerNode (QueryBaseNode<TSource> parent, int count)
51 : base (parent, false)
56 internal QueryHeadWorkerNode (QueryBaseNode<TSource> parent, Func<TSource, int, bool> predicate, bool indexed)
57 : base (parent, indexed)
59 this.predicate = predicate;
64 return predicate == null ? count : (int?)null;
68 internal override IEnumerable<TSource> GetSequential ()
70 IEnumerable<TSource> parent = Parent.GetSequential ();
72 return predicate == null ? parent.Take (count) : parent.TakeWhile (predicate);
75 public override void Visit (INodeVisitor visitor)
80 internal override IList<IEnumerable<TSource>> GetEnumerablesIndexed (QueryOptions options)
82 return Parent.GetOrderedEnumerables (options)
83 .Select ((i) => i.TakeWhile ((e) => predicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
87 internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
89 return Parent.GetEnumerables (options)
90 .Select (GetSelector (count))
94 Func<IEnumerable<TSource>, IEnumerable<TSource>> GetSelector (int c)
96 if (predicate == null)
97 return (i) => i.TakeWhile ((e) => c > 0 && Interlocked.Decrement (ref c) >= 0);
99 return (i) => i.TakeWhile ((e) => predicate (e, -1));
102 internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
104 return Parent.GetOrderedEnumerables (options)
105 .Select ((i) => GetEnumerableInternal (i, options))
109 IEnumerable<KeyValuePair<long, TSource>> GetEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source, QueryOptions options)
111 IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
112 long gapIndex = predicate == null ? count : long.MaxValue;
113 Func<KeyValuePair<long, TSource>, bool> cond;
114 if (predicate == null)
115 cond = (kv) => kv.Key < count;
117 cond = (kv) => predicate (kv.Value, (int)kv.Key);
120 while (current.MoveNext ()) {
121 KeyValuePair<long, TSource> kvp = current.Current;
123 /* When filtering is based on a predicate, this short-circuit is only valid
124 * if the partitioner used ensure items are ordered in each partition
125 * (valid w/ default partitioners)
127 if (kvp.Key >= gapIndex && options.PartitionerSettings.Item2)
131 if (gapIndex > kvp.Key && predicate != null)