QueryZipNode.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #if NET_4_0
  2. //
  3. // QueryZipNode.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2010 Jérémie "Garuma" Laval
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  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
  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. IList<IEnumerable<TFirst>> first = Parent.GetEnumerables (options);
  51. IList<IEnumerable<TSecond>> second = Second.GetEnumerables (options);
  52. if (first.Count != second.Count)
  53. throw new InvalidOperationException ("Internal size mismatch");
  54. IEnumerable<TResult>[] result = new IEnumerable<TResult>[first.Count];
  55. for (int i = 0; i < result.Length; i++)
  56. result[i] = GetEnumerable (first[i], second[i]);
  57. return result;
  58. }
  59. IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
  60. {
  61. IEnumerator<TFirst> eFirst = first.GetEnumerator ();
  62. IEnumerator<TSecond> eSecond = second.GetEnumerator ();
  63. while (eFirst.MoveNext ()) {
  64. if (!eSecond.MoveNext ())
  65. yield break;
  66. yield return resultSelector (eFirst.Current, eSecond.Current);
  67. }
  68. }
  69. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  70. {
  71. IList<IEnumerable<KeyValuePair<long, TFirst>>> first = Parent.GetOrderedEnumerables (options);
  72. IList<IEnumerable<KeyValuePair<long, TSecond>>> second = Second.GetOrderedEnumerables (options);
  73. if (first.Count != second.Count)
  74. throw new InvalidOperationException ("Internal size mismatch");
  75. IEnumerable<KeyValuePair<long, TResult>>[] result = new IEnumerable<KeyValuePair<long, TResult>>[first.Count];
  76. KeyValuePair<long, TFirst>[] store1 = new KeyValuePair<long, TFirst>[result.Length];
  77. KeyValuePair<long, TSecond>[] store2 = new KeyValuePair<long, TSecond>[result.Length];
  78. Barrier barrier = new Barrier (result.Length, delegate {
  79. Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
  80. Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
  81. });
  82. for (int i = 0; i < result.Length; i++)
  83. result[i] = GetEnumerable (first[i], second[i], i, store1, store2, barrier);
  84. return result;
  85. }
  86. IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,
  87. IEnumerable<KeyValuePair<long, TSecond>> second,
  88. int index,
  89. KeyValuePair<long, TFirst>[] store1,
  90. KeyValuePair<long, TSecond>[] store2,
  91. Barrier barrier)
  92. {
  93. IEnumerator<KeyValuePair<long, TFirst>> eFirst = first.GetEnumerator ();
  94. IEnumerator<KeyValuePair<long, TSecond>> eSecond = second.GetEnumerator ();
  95. while (eFirst.MoveNext ()) {
  96. if (!eSecond.MoveNext ())
  97. break;
  98. store1[index] = eFirst.Current;
  99. store2[index] = eSecond.Current;
  100. barrier.SignalAndWait ();
  101. yield return new KeyValuePair<long, TResult> (store1[index].Key,
  102. resultSelector (store1[index].Value, store2[index].Value));
  103. }
  104. barrier.RemoveParticipant ();
  105. }
  106. }
  107. }
  108. #endif