Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[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
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                 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                         var first = Parent.GetEnumerables (options);
57                         var second = Second.GetEnumerables (options);
58
59                         if (first.Count != second.Count)
60                                 throw new InvalidOperationException ("Internal size mismatch");
61
62                         return first
63                                 .Select ((f, i) => GetEnumerable (f, second[i]))
64                                 .ToList ();
65                 }
66
67                 IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
68                 {
69                         IEnumerator<TFirst> eFirst = first.GetEnumerator ();
70                         IEnumerator<TSecond> eSecond = second.GetEnumerator ();
71
72                         try {
73                                 while (eFirst.MoveNext ()) {
74                                         if (!eSecond.MoveNext ())
75                                                 yield break;
76
77                                         yield return resultSelector (eFirst.Current, eSecond.Current);
78                                 }
79                         } finally {
80                                 eFirst.Dispose ();
81                                 eSecond.Dispose ();
82                         }
83                 }
84
85                 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
86                 {
87                         var first = Parent.GetOrderedEnumerables (options);
88                         var second = Second.GetOrderedEnumerables (options);
89
90                         if (first.Count != second.Count)
91                                 throw new InvalidOperationException ("Internal size mismatch");
92
93                         var store1 = new KeyValuePair<long, TFirst>[first.Count];
94                         var store2 = new KeyValuePair<long, TSecond>[second.Count];
95
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));
99                         });
100
101                         return first
102                                 .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
103                                 .ToList ();
104                 }
105
106                 IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
107                                                                         IEnumerable<KeyValuePair<long, TSecond>> second,
108                                                                         int index,
109                                                                         KeyValuePair<long, TFirst>[] store1,
110                                                                         KeyValuePair<long, TSecond>[] store2,
111                                                                         Barrier barrier)
112                 {
113                         var eFirst = first.GetEnumerator ();
114                         var eSecond = second.GetEnumerator ();
115
116                         try {
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                         } finally {
130                                 barrier.RemoveParticipant ();
131                                 eFirst.Dispose ();
132                                 eSecond.Dispose ();
133                         }
134                 }
135         }
136 }
137
138 #endif