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;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
34 namespace System.Linq.Parallel.QueryNodes
36 internal class QueryZipNode<TFirst, TSecond, TResult> : QueryMuxNode<TFirst, TSecond, TResult>
38 Func<TFirst, TSecond, TResult> resultSelector;
40 public QueryZipNode (Func<TFirst, TSecond, TResult> resultSelector, QueryBaseNode<TFirst> first, QueryBaseNode<TSecond> second)
41 : base (first, second)
43 this.resultSelector = resultSelector;
46 internal override IEnumerable<TResult> GetSequential ()
48 IEnumerable<TFirst> first = Parent.GetSequential ();
49 IEnumerable<TSecond> second = Second.GetSequential ();
51 return first.Zip (second, resultSelector);
54 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
56 var first = Parent.GetEnumerables (options);
57 var second = Second.GetEnumerables (options);
59 if (first.Count != second.Count)
60 throw new InvalidOperationException ("Internal size mismatch");
63 .Select ((f, i) => GetEnumerable (f, second[i]))
67 IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
69 IEnumerator<TFirst> eFirst = first.GetEnumerator ();
70 IEnumerator<TSecond> eSecond = second.GetEnumerator ();
73 while (eFirst.MoveNext ()) {
74 if (!eSecond.MoveNext ())
77 yield return resultSelector (eFirst.Current, eSecond.Current);
85 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
87 var first = Parent.GetOrderedEnumerables (options);
88 var second = Second.GetOrderedEnumerables (options);
90 if (first.Count != second.Count)
91 throw new InvalidOperationException ("Internal size mismatch");
93 var store1 = new KeyValuePair<long, TFirst>[first.Count];
94 var store2 = new KeyValuePair<long, TSecond>[second.Count];
96 var barrier = new Barrier (first.Count, delegate {
97 Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
98 Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
102 .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
106 IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
107 IEnumerable<KeyValuePair<long, TSecond>> second,
109 KeyValuePair<long, TFirst>[] store1,
110 KeyValuePair<long, TSecond>[] store2,
113 var eFirst = first.GetEnumerator ();
114 var eSecond = second.GetEnumerator ();
117 while (eFirst.MoveNext ()) {
118 if (!eSecond.MoveNext ())
121 store1[index] = eFirst.Current;
122 store2[index] = eSecond.Current;
124 barrier.SignalAndWait ();
126 yield return new KeyValuePair<long, TResult> (store1[index].Key,
127 resultSelector (store1[index].Value, store2[index].Value));
130 barrier.RemoveParticipant ();