QueryJoinNode.cs 6.5 KB

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