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
30 using System.Threading;
31 using System.Collections.Generic;
33 namespace System.Linq.Parallel.QueryNodes
36 internal class QueryWhereNode<TSource> : QueryStreamNode<TSource, TSource>
38 Func<TSource, int, bool> indexedPredicate;
39 Func<TSource, bool> predicate;
41 internal QueryWhereNode (QueryBaseNode<TSource> parent, Func<TSource, bool> predicate)
42 : base (parent, false)
44 this.predicate = predicate;
47 internal QueryWhereNode (QueryBaseNode<TSource> parent, Func<TSource, int, bool> predicate)
50 this.indexedPredicate = predicate;
53 internal override IEnumerable<TSource> GetSequential ()
55 IEnumerable<TSource> parent = Parent.GetSequential ();
57 if (indexedPredicate != null)
58 return parent.Where (indexedPredicate);
60 return parent.Where (predicate);
63 internal override IList<IEnumerable<TSource>> GetEnumerablesIndexed (QueryOptions options)
65 return Parent.GetOrderedEnumerables (options)
66 .Select ((i) => i.Where ((e) => indexedPredicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
70 internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
72 return Parent.GetEnumerables (options)
73 .Select ((i) => i.Where (predicate))
77 internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
79 IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
81 Tuple<TSource, long, bool>[] store = new Tuple<TSource, long, bool>[sources.Count];
84 Barrier barrier = new Barrier (sources.Count, delegate (Barrier b) {
86 Array.Sort (store, ArraySortMethod);
88 // Reassign a good index
91 for (i = 0; i < store.Length && store[i].Item3; i++) {
92 Tuple<TSource, long, bool> old = store[i];
93 store[i] = Tuple.Create (old.Item1, lastIndex + i, old.Item3);
96 // Update lastIndex for next round
101 .Select ((s, i) => GetEnumerator (s, barrier, options.MergedToken, store, i))
105 static int ArraySortMethod (Tuple<TSource, long, bool> lhs, Tuple<TSource, long, bool> rhs)
107 if (lhs.Item3 && !rhs.Item3)
109 if (!lhs.Item3 && rhs.Item3)
111 if (!lhs.Item3 && !rhs.Item3)
114 return (lhs.Item2 < rhs.Item2) ? -1 : 1;
117 IEnumerable<KeyValuePair<long, TSource>> GetEnumerator (IEnumerable<KeyValuePair<long, TSource>> source,
119 CancellationToken token,
120 Tuple<TSource, long, bool>[] store, int index)
122 IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
124 Tuple<TSource, long, bool> reset = Tuple.Create (default (TSource), long.MaxValue, false);
127 while (current.MoveNext ()) {
128 KeyValuePair<long, TSource> curr = current.Current;
130 result = IsIndexed ? indexedPredicate (curr.Value, (int)curr.Key) : predicate (curr.Value);
131 store[index] = Tuple.Create (curr.Value, curr.Key, result);
133 barrier.SignalAndWait (token);
135 Tuple<TSource, long, bool> value = store [index];
138 yield return new KeyValuePair<long, TSource> (value.Item2, value.Item1);
141 store[index] = reset;
144 // Remove our participation
145 barrier.RemoveParticipant ();