QueryZipNode.cs 5.1 KB

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