Normalize line endings.
[mono.git] / mcs / class / System.Core / System.Linq / Internal / QueryNodes / QueryZipNode.cs
1 #if NET_4_0
2 //
3 // QueryZipNode.cs
4 //
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 //
8 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 //
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:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
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
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30 using System.Linq;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33
34 namespace System.Linq
35 {
36         internal class QueryZipNode<TFirst, TSecond, TResult> : QueryMuxNode<TFirst, TSecond, TResult>
37         {
38                 Func<TFirst, TSecond, TResult> resultSelector;
39
40                 public QueryZipNode (Func<TFirst, TSecond, TResult> resultSelector, QueryBaseNode<TFirst> first, QueryBaseNode<TSecond> second)
41                         : base (first, second)
42                 {
43                         this.resultSelector = resultSelector;
44                 }
45
46                 internal override IEnumerable<TResult> GetSequential ()
47                 {
48                         IEnumerable<TFirst> first = Parent.GetSequential ();
49                         IEnumerable<TSecond> second = Second.GetSequential ();
50
51                         return first.Zip (second, resultSelector);
52                 }
53
54                 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
55                 {
56                         IList<IEnumerable<TFirst>> first = Parent.GetEnumerables (options);
57                         IList<IEnumerable<TSecond>> second = Second.GetEnumerables (options);
58
59                         if (first.Count != second.Count)
60                                 throw new InvalidOperationException ("Internal size mismatch");
61
62                         IEnumerable<TResult>[] result = new IEnumerable<TResult>[first.Count];
63
64                         for (int i = 0; i < result.Length; i++)
65                                 result[i] = GetEnumerable (first[i], second[i]);
66
67                         return result;
68                 }
69
70                 IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
71                 {
72                         IEnumerator<TFirst> eFirst = first.GetEnumerator ();
73                         IEnumerator<TSecond> eSecond = second.GetEnumerator ();
74
75                         while (eFirst.MoveNext ()) {
76                                 if (!eSecond.MoveNext ())
77                                         yield break;
78
79                                 yield return resultSelector (eFirst.Current, eSecond.Current);
80                         }
81                 }
82
83                 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
84                 {
85                         IList<IEnumerable<KeyValuePair<long, TFirst>>> first = Parent.GetOrderedEnumerables (options);
86                         IList<IEnumerable<KeyValuePair<long, TSecond>>> second = Second.GetOrderedEnumerables (options);
87
88                         if (first.Count != second.Count)
89                                 throw new InvalidOperationException ("Internal size mismatch");
90
91                         IEnumerable<KeyValuePair<long, TResult>>[] result = new IEnumerable<KeyValuePair<long, TResult>>[first.Count];
92
93                         KeyValuePair<long, TFirst>[] store1 = new KeyValuePair<long, TFirst>[result.Length];
94                         KeyValuePair<long, TSecond>[] store2 = new KeyValuePair<long, TSecond>[result.Length];
95
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));
99                         });
100
101                         for (int i = 0; i < result.Length; i++)
102                                 result[i] = GetEnumerable (first[i], second[i], i, store1, store2, barrier);
103
104                         return result;
105                 }
106
107                 IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
108                                                                         IEnumerable<KeyValuePair<long, TSecond>> second,
109                                                                         int index,
110                                                                         KeyValuePair<long, TFirst>[] store1,
111                                                                         KeyValuePair<long, TSecond>[] store2,
112                                                                         Barrier barrier)
113                 {
114                         IEnumerator<KeyValuePair<long, TFirst>> eFirst = first.GetEnumerator ();
115                         IEnumerator<KeyValuePair<long, TSecond>> eSecond = second.GetEnumerator ();
116
117                         while (eFirst.MoveNext ()) {
118                                 if (!eSecond.MoveNext ())
119                                         break;
120
121                                 store1[index] = eFirst.Current;
122                                 store2[index] = eSecond.Current;
123
124                                 barrier.SignalAndWait ();
125
126                                 yield return new KeyValuePair<long, TResult> (store1[index].Key,
127                                                                                  resultSelector (store1[index].Value, store2[index].Value));
128                         }
129
130                         barrier.RemoveParticipant ();
131                 }
132         }
133 }
134
135 #endif