do not check order sequence if option /order was not used
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryJoinNode.cs
1 //
2 // QueryJoinNode.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.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33
34 namespace System.Linq.Parallel.QueryNodes
35 {
36         internal class QueryJoinNode<TFirst, TSecond, TKey, TResult> : QueryMuxNode<TFirst, TSecond, TResult>
37         {
38                 struct VSlot<T>
39                 {
40                         public readonly bool HasValue;
41                         public readonly T Value;
42
43                         public VSlot (T value)
44                         {
45                                 HasValue = true;
46                                 Value = value;
47                         }
48                 }
49
50                 Func<TFirst, TKey> firstKeySelector;
51                 Func<TSecond, TKey> secondKeySelector;
52                 Func<TFirst, TSecond, TResult> resultSelector;
53                 IEqualityComparer<TKey> comparer;
54
55                 internal QueryJoinNode (QueryBaseNode<TFirst> first,
56                                         QueryBaseNode<TSecond> second, 
57                                         Func<TFirst, TKey> firstKeySelector,
58                                         Func<TSecond, TKey> secondKeySelector,
59                                         Func<TFirst, TSecond, TResult> resultSelector,
60                                         IEqualityComparer<TKey> comparer) : base (first, second)
61                 {
62                         this.firstKeySelector = firstKeySelector;
63                         this.secondKeySelector = secondKeySelector;
64                         this.resultSelector = resultSelector;
65                         this.comparer = comparer;
66                 }
67
68                 internal override IEnumerable<TResult> GetSequential ()
69                 {
70                         return Parent.GetSequential ().Join (Second.GetSequential (), 
71                                                              firstKeySelector, 
72                                                              secondKeySelector, 
73                                                              resultSelector, 
74                                                              comparer);
75                 }
76
77                 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
78                 {
79                         var first = Parent.GetEnumerables (options);
80                         var second = Second.GetEnumerables (options);
81
82                         if (first.Count != second.Count)
83                                 throw new InvalidOperationException ("Internal size mismatch");
84
85                         var store = new TemporaryArea<TKey, Tuple<VSlot<TFirst>, VSlot<TSecond>>> (comparer);
86
87                         return first
88                                 .Select ((f, i) => GetEnumerable (f, second[i], store, firstKeySelector, secondKeySelector, resultSelector))
89                                 .ToList ();
90
91                 }               
92
93                 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
94                 {
95                         var first = Parent.GetOrderedEnumerables (options);
96                         var second = Second.GetOrderedEnumerables (options);
97
98                         if (first.Count != second.Count)
99                                 throw new InvalidOperationException ("Internal size mismatch");
100
101                         var store = new TemporaryArea<TKey, Tuple<VSlot<KeyValuePair<long, TFirst>>, VSlot<KeyValuePair<long, TSecond>>>> (comparer);
102                         
103                         return first
104                                 .Select ((f, i) => GetEnumerable<KeyValuePair<long, TFirst>, KeyValuePair<long, TSecond>, KeyValuePair<long, TResult>> (f, 
105                                                                   second[i], 
106                                                                   store,
107                                                                   (e) => firstKeySelector (e.Value),
108                                                                   (e) => secondKeySelector (e.Value),
109                                                                   (e1, e2) => new KeyValuePair<long, TResult> (e1.Key, resultSelector (e1.Value, e2.Value))))
110                                 .ToList ();
111                 }
112
113                 IEnumerable<T> GetEnumerable<U, V, T> (IEnumerable<U> first, 
114                                                        IEnumerable<V> second,
115                                                        TemporaryArea<TKey, Tuple<VSlot<U>, VSlot<V>>> store,
116                                                        Func<U, TKey> fKeySelect,
117                                                        Func<V, TKey> sKeySelect,
118                                                        Func<U, V, T> resultor)
119                 {
120                         IEnumerator<U> eFirst = first.GetEnumerator ();
121                         IEnumerator<V> eSecond = second.GetEnumerator ();
122
123                         try {
124                                 bool fstHasCurrent = false, sndHasCurrent = false;
125                                 Tuple<VSlot<U>, VSlot<V>> kvp;
126
127                                 while ((fstHasCurrent = eFirst.MoveNext ()) & (sndHasCurrent = eSecond.MoveNext ())) {
128
129                                         U e1 = eFirst.Current;
130                                         V e2 = eSecond.Current;
131
132                                         TKey key1 = fKeySelect (e1);
133                                         TKey key2 = sKeySelect (e2);
134
135                                         if (comparer.Equals (key1, key2)) {
136                                                 yield return resultor (e1, e2);
137                                                 continue;
138                                         }
139                                         
140                                         do {
141                                                 if (store.TryRemove (key1, out kvp) && kvp.Item2.HasValue) {
142                                                         yield return resultor (e1, kvp.Item2.Value);
143                                                         break;
144                                                 }
145                                         } while (!store.TryAdd (key1, Tuple.Create (new VSlot<U> (e1), new VSlot<V> ())));
146                                                         
147                                         do {
148                                                 if (store.TryRemove (key2, out kvp) && kvp.Item1.HasValue) {
149                                                         yield return resultor (kvp.Item1.Value, e2);
150                                                         break;
151                                                 }
152                                         } while (!store.TryAdd (key2, Tuple.Create (new VSlot<U> (), new VSlot<V> (e2))));
153                                 }
154                                 if (fstHasCurrent) {
155                                         do {
156                                                 U e1 = eFirst.Current;
157                                                 TKey key1 = fKeySelect (e1);
158
159                                                 do {
160                                                         if (store.TryRemove (key1, out kvp) && kvp.Item2.HasValue) {
161                                                                 yield return resultor (e1, kvp.Item2.Value);
162                                                                 break;
163                                                         }
164                                                 } while (!store.TryAdd (key1, Tuple.Create (new VSlot<U> (e1), new VSlot<V> ())));
165                                         } while (eFirst.MoveNext ());
166                                 }
167                                 if (sndHasCurrent) {
168                                         do {
169                                                 V e2 = eSecond.Current;
170                                                 TKey key2 = sKeySelect (e2);
171
172                                                 do {
173                                                         if (store.TryRemove (key2, out kvp) && kvp.Item1.HasValue) {
174                                                                 yield return resultor (kvp.Item1.Value, e2);
175                                                                 break;
176                                                         }
177                                                 } while (!store.TryAdd (key2, Tuple.Create (new VSlot<U> (), new VSlot<V> (e2))));
178                                         } while (eSecond.MoveNext ());
179                                 }
180                         } finally {
181                                 eFirst.Dispose ();
182                                 eSecond.Dispose ();
183                         }
184                 }
185         }
186 }
187
188 #endif