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 // If strict is set to `true', the zip'ing process will throw if sequences length mistmatches
52 internal override IEnumerable<TResult> GetSequential ()
54 IEnumerable<TFirst> first = Parent.GetSequential ();
55 IEnumerable<TSecond> second = Second.GetSequential ();
57 return first.Zip (second, resultSelector);
60 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
62 var first = Parent.GetEnumerables (options);
63 var second = Second.GetEnumerables (options);
65 if (first.Count != second.Count)
66 throw new InvalidOperationException ("Internal size mismatch");
69 .Select ((f, i) => GetEnumerable (f, second[i]))
73 IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
75 IEnumerator<TFirst> eFirst = first.GetEnumerator ();
76 IEnumerator<TSecond> eSecond = second.GetEnumerator ();
79 while (eFirst.MoveNext ()) {
80 if (!eSecond.MoveNext ()) {
82 throw new QueryZipException ();
87 yield return resultSelector (eFirst.Current, eSecond.Current);
89 if (Strict && eSecond.MoveNext ())
90 throw new QueryZipException ();
97 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
99 var first = Parent.GetOrderedEnumerables (options);
100 var second = Second.GetOrderedEnumerables (options);
102 if (first.Count != second.Count)
103 throw new InvalidOperationException ("Internal size mismatch");
105 var store1 = new KeyValuePair<long, TFirst>[first.Count];
106 var store2 = new KeyValuePair<long, TSecond>[second.Count];
108 var barrier = new Barrier (first.Count, delegate {
109 Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
110 Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
114 .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
118 IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
119 IEnumerable<KeyValuePair<long, TSecond>> second,
121 KeyValuePair<long, TFirst>[] store1,
122 KeyValuePair<long, TSecond>[] store2,
125 var eFirst = first.GetEnumerator ();
126 var eSecond = second.GetEnumerator ();
129 while (eFirst.MoveNext ()) {
130 if (!eSecond.MoveNext ()) {
132 throw new QueryZipException ();
137 store1[index] = eFirst.Current;
138 store2[index] = eSecond.Current;
140 barrier.SignalAndWait ();
142 yield return new KeyValuePair<long, TResult> (store1[index].Key,
143 resultSelector (store1[index].Value, store2[index].Value));
145 if (Strict && eSecond.MoveNext ())
146 throw new QueryZipException ();
148 barrier.RemoveParticipant ();
155 internal class QueryZipException : Exception