6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2010 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // 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;
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 IList<IEnumerable<TFirst>> first = Parent.GetEnumerables (options);
57 IList<IEnumerable<TSecond>> second = Second.GetEnumerables (options);
59 if (first.Count != second.Count)
60 throw new InvalidOperationException ("Internal size mismatch");
62 IEnumerable<TResult>[] result = new IEnumerable<TResult>[first.Count];
64 for (int i = 0; i < result.Length; i++)
65 result[i] = GetEnumerable (first[i], second[i]);
70 IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
72 IEnumerator<TFirst> eFirst = first.GetEnumerator ();
73 IEnumerator<TSecond> eSecond = second.GetEnumerator ();
75 while (eFirst.MoveNext ()) {
76 if (!eSecond.MoveNext ())
79 yield return resultSelector (eFirst.Current, eSecond.Current);
83 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
85 IList<IEnumerable<KeyValuePair<long, TFirst>>> first = Parent.GetOrderedEnumerables (options);
86 IList<IEnumerable<KeyValuePair<long, TSecond>>> second = Second.GetOrderedEnumerables (options);
88 if (first.Count != second.Count)
89 throw new InvalidOperationException ("Internal size mismatch");
91 IEnumerable<KeyValuePair<long, TResult>>[] result = new IEnumerable<KeyValuePair<long, TResult>>[first.Count];
93 KeyValuePair<long, TFirst>[] store1 = new KeyValuePair<long, TFirst>[result.Length];
94 KeyValuePair<long, TSecond>[] store2 = new KeyValuePair<long, TSecond>[result.Length];
96 Barrier barrier = new Barrier (result.Length, delegate {
97 Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
98 Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
101 for (int i = 0; i < result.Length; i++)
102 result[i] = GetEnumerable (first[i], second[i], i, store1, store2, barrier);
107 IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
108 IEnumerable<KeyValuePair<long, TSecond>> second,
110 KeyValuePair<long, TFirst>[] store1,
111 KeyValuePair<long, TSecond>[] store2,
114 IEnumerator<KeyValuePair<long, TFirst>> eFirst = first.GetEnumerator ();
115 IEnumerator<KeyValuePair<long, TSecond>> 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 ();