QueryZipNode.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // If strict is set to `true', the zip'ing process will throw if sequences length mistmatches
  43. public bool Strict {
  44. get;
  45. set;
  46. }
  47. internal override IEnumerable<TResult> GetSequential ()
  48. {
  49. IEnumerable<TFirst> first = Parent.GetSequential ();
  50. IEnumerable<TSecond> second = Second.GetSequential ();
  51. return first.Zip (second, resultSelector);
  52. }
  53. internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
  54. {
  55. var first = Parent.GetEnumerables (options);
  56. var second = Second.GetEnumerables (options);
  57. if (first.Count != second.Count)
  58. throw new InvalidOperationException ("Internal size mismatch");
  59. return first
  60. .Select ((f, i) => GetEnumerable (f, second[i]))
  61. .ToList ();
  62. }
  63. IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
  64. {
  65. IEnumerator<TFirst> eFirst = first.GetEnumerator ();
  66. IEnumerator<TSecond> eSecond = second.GetEnumerator ();
  67. try {
  68. while (eFirst.MoveNext ()) {
  69. if (!eSecond.MoveNext ()) {
  70. if (Strict)
  71. throw new QueryZipException ();
  72. else
  73. yield break;
  74. }
  75. yield return resultSelector (eFirst.Current, eSecond.Current);
  76. }
  77. if (Strict && eSecond.MoveNext ())
  78. throw new QueryZipException ();
  79. } finally {
  80. eFirst.Dispose ();
  81. eSecond.Dispose ();
  82. }
  83. }
  84. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  85. {
  86. var first = Parent.GetOrderedEnumerables (options);
  87. var second = Second.GetOrderedEnumerables (options);
  88. if (first.Count != second.Count)
  89. throw new InvalidOperationException ("Internal size mismatch");
  90. var store1 = new KeyValuePair<long, TFirst>[first.Count];
  91. var store2 = new KeyValuePair<long, TSecond>[second.Count];
  92. var barrier = new Barrier (first.Count, delegate {
  93. Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
  94. Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
  95. });
  96. return first
  97. .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
  98. .ToList ();
  99. }
  100. IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
  101. IEnumerable<KeyValuePair<long, TSecond>> second,
  102. int index,
  103. KeyValuePair<long, TFirst>[] store1,
  104. KeyValuePair<long, TSecond>[] store2,
  105. Barrier barrier)
  106. {
  107. var eFirst = first.GetEnumerator ();
  108. var eSecond = second.GetEnumerator ();
  109. try {
  110. while (eFirst.MoveNext ()) {
  111. if (!eSecond.MoveNext ()) {
  112. if (Strict)
  113. throw new QueryZipException ();
  114. else
  115. break;
  116. }
  117. store1[index] = eFirst.Current;
  118. store2[index] = eSecond.Current;
  119. barrier.SignalAndWait ();
  120. yield return new KeyValuePair<long, TResult> (store1[index].Key,
  121. resultSelector (store1[index].Value, store2[index].Value));
  122. }
  123. if (Strict && eSecond.MoveNext ())
  124. throw new QueryZipException ();
  125. } finally {
  126. barrier.RemoveParticipant ();
  127. eFirst.Dispose ();
  128. eSecond.Dispose ();
  129. }
  130. }
  131. }
  132. internal class QueryZipException : Exception
  133. {
  134. }
  135. }
  136. #endif