QueryZipNode.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // QueryZipNode.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.Linq;
  30. using System.Collections.Generic;
  31. using System.Collections.Concurrent;
  32. namespace System.Linq.Parallel.QueryNodes
  33. {
  34. internal class QueryZipNode<TFirst, TSecond, TResult> : QueryMuxNode<TFirst, TSecond, TResult>
  35. {
  36. Func<TFirst, TSecond, TResult> resultSelector;
  37. public QueryZipNode (Func<TFirst, TSecond, TResult> resultSelector, QueryBaseNode<TFirst> first, QueryBaseNode<TSecond> second)
  38. : base (first, second)
  39. {
  40. this.resultSelector = resultSelector;
  41. }
  42. internal override IEnumerable<TResult> GetSequential ()
  43. {
  44. IEnumerable<TFirst> first = Parent.GetSequential ();
  45. IEnumerable<TSecond> second = Second.GetSequential ();
  46. return first.Zip (second, resultSelector);
  47. }
  48. internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
  49. {
  50. var first = Parent.GetEnumerables (options);
  51. var second = Second.GetEnumerables (options);
  52. if (first.Count != second.Count)
  53. throw new InvalidOperationException ("Internal size mismatch");
  54. return first
  55. .Select ((f, i) => GetEnumerable (f, second[i]))
  56. .ToArray ();
  57. }
  58. IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
  59. {
  60. IEnumerator<TFirst> eFirst = first.GetEnumerator ();
  61. IEnumerator<TSecond> eSecond = second.GetEnumerator ();
  62. try {
  63. while (eFirst.MoveNext ()) {
  64. if (!eSecond.MoveNext ())
  65. yield break;
  66. yield return resultSelector (eFirst.Current, eSecond.Current);
  67. }
  68. } finally {
  69. eFirst.Dispose ();
  70. eSecond.Dispose ();
  71. }
  72. }
  73. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  74. {
  75. var first = Parent.GetOrderedEnumerables (options);
  76. var second = Second.GetOrderedEnumerables (options);
  77. if (first.Count != second.Count)
  78. throw new InvalidOperationException ("Internal size mismatch");
  79. var store1 = new KeyValuePair<long, TFirst>[first.Count];
  80. var store2 = new KeyValuePair<long, TSecond>[second.Count];
  81. var barrier = new Barrier (first.Count, delegate {
  82. Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
  83. Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
  84. });
  85. return first
  86. .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
  87. .ToArray ();
  88. }
  89. IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
  90. IEnumerable<KeyValuePair<long, TSecond>> second,
  91. int index,
  92. KeyValuePair<long, TFirst>[] store1,
  93. KeyValuePair<long, TSecond>[] store2,
  94. Barrier barrier)
  95. {
  96. var eFirst = first.GetEnumerator ();
  97. var eSecond = second.GetEnumerator ();
  98. try {
  99. while (eFirst.MoveNext ()) {
  100. if (!eSecond.MoveNext ())
  101. break;
  102. store1[index] = eFirst.Current;
  103. store2[index] = eSecond.Current;
  104. barrier.SignalAndWait ();
  105. yield return new KeyValuePair<long, TResult> (store1[index].Key,
  106. resultSelector (store1[index].Value, store2[index].Value));
  107. }
  108. } finally {
  109. barrier.RemoveParticipant ();
  110. eFirst.Dispose ();
  111. eSecond.Dispose ();
  112. }
  113. }
  114. }
  115. }
  116. #endif