Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryZipNode.cs
1 //
2 // QueryZipNode.cs
3 //
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26
27 #if NET_4_0 || MOBILE
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.Parallel.QueryNodes
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                 // If strict is set to `true', the zip'ing process will throw if sequences length mistmatches
47                 public bool Strict {
48                         get;
49                         set;
50                 }
51
52                 internal override IEnumerable<TResult> GetSequential ()
53                 {
54                         IEnumerable<TFirst> first = Parent.GetSequential ();
55                         IEnumerable<TSecond> second = Second.GetSequential ();
56
57                         return first.Zip (second, resultSelector);
58                 }
59
60                 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
61                 {
62                         var first = Parent.GetEnumerables (options);
63                         var second = Second.GetEnumerables (options);
64
65                         if (first.Count != second.Count)
66                                 throw new InvalidOperationException ("Internal size mismatch");
67
68                         return first
69                                 .Select ((f, i) => GetEnumerable (f, second[i]))
70                                 .ToList ();
71                 }
72
73                 IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
74                 {
75                         IEnumerator<TFirst> eFirst = first.GetEnumerator ();
76                         IEnumerator<TSecond> eSecond = second.GetEnumerator ();
77
78                         try {
79                                 while (eFirst.MoveNext ()) {
80                                         if (!eSecond.MoveNext ()) {
81                                                 if (Strict)
82                                                         throw new QueryZipException ();
83                                                 else
84                                                         yield break;
85                                         }
86
87                                         yield return resultSelector (eFirst.Current, eSecond.Current);
88                                 }
89                                 if (Strict && eSecond.MoveNext ())
90                                         throw new QueryZipException ();
91                         } finally {
92                                 eFirst.Dispose ();
93                                 eSecond.Dispose ();
94                         }
95                 }
96
97                 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
98                 {
99                         var first = Parent.GetOrderedEnumerables (options);
100                         var second = Second.GetOrderedEnumerables (options);
101
102                         if (first.Count != second.Count)
103                                 throw new InvalidOperationException ("Internal size mismatch");
104
105                         var store1 = new KeyValuePair<long, TFirst>[first.Count];
106                         var store2 = new KeyValuePair<long, TSecond>[second.Count];
107
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));
111                         });
112
113                         return first
114                                 .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
115                                 .ToList ();
116                 }
117
118                 IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
119                                                                         IEnumerable<KeyValuePair<long, TSecond>> second,
120                                                                         int index,
121                                                                         KeyValuePair<long, TFirst>[] store1,
122                                                                         KeyValuePair<long, TSecond>[] store2,
123                                                                         Barrier barrier)
124                 {
125                         var eFirst = first.GetEnumerator ();
126                         var eSecond = second.GetEnumerator ();
127
128                         try {
129                                 while (eFirst.MoveNext ()) {
130                                         if (!eSecond.MoveNext ()) {
131                                                 if (Strict)
132                                                         throw new QueryZipException ();
133                                                 else
134                                                         break;
135                                         }
136
137                                         store1[index] = eFirst.Current;
138                                         store2[index] = eSecond.Current;
139
140                                         barrier.SignalAndWait ();
141
142                                         yield return new KeyValuePair<long, TResult> (store1[index].Key,
143                                                                                       resultSelector (store1[index].Value, store2[index].Value));
144                                 }
145                                 if (Strict && eSecond.MoveNext ())
146                                         throw new QueryZipException ();
147                         } finally {
148                                 barrier.RemoveParticipant ();
149                                 eFirst.Dispose ();
150                                 eSecond.Dispose ();
151                         }
152                 }
153         }
154
155         internal class QueryZipException : Exception
156         {
157         }
158 }
159
160 #endif