QueryJoinNode.cs 6.5 KB

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