QuerySelectManyNode.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // QueryConcatNode.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.Collections;
  30. using System.Collections.Generic;
  31. namespace System.Linq.Parallel.QueryNodes
  32. {
  33. internal class QuerySelectManyNode<TSource, TCollection, TResult> : QueryStreamNode<TResult, TSource>
  34. {
  35. Func<TSource, IEnumerable<TCollection>> collectionSelector;
  36. Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed;
  37. Func<TSource, TCollection, TResult> resultSelector;
  38. internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
  39. Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed,
  40. Func<TSource, TCollection, TResult> resultSelector)
  41. : base (parent, true)
  42. {
  43. this.collectionSelectorIndexed = collectionSelectorIndexed;
  44. this.resultSelector = resultSelector;
  45. }
  46. internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
  47. Func<TSource, IEnumerable<TCollection>> collectionSelector,
  48. Func<TSource, TCollection, TResult> resultSelector)
  49. : base (parent, false)
  50. {
  51. this.collectionSelector = collectionSelector;
  52. this.resultSelector = resultSelector;
  53. }
  54. internal override IEnumerable<TResult> GetSequential ()
  55. {
  56. IEnumerable<TSource> source = Parent.GetSequential ();
  57. return IsIndexed ?
  58. source.SelectMany (collectionSelectorIndexed, resultSelector) :
  59. source.SelectMany (collectionSelector, resultSelector);
  60. }
  61. internal override IList<IEnumerable<TResult>> GetEnumerablesIndexed (QueryOptions options)
  62. {
  63. return Parent.GetOrderedEnumerables (options)
  64. .Select ((i) => GetEnumerableInternal (i,
  65. (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
  66. (e, c) => resultSelector (e.Value, c)))
  67. .ToList ();
  68. }
  69. internal override IList<IEnumerable<TResult>> GetEnumerablesNonIndexed (QueryOptions options)
  70. {
  71. return Parent.GetEnumerables (options)
  72. .Select ((i) => GetEnumerableInternal (i,
  73. collectionSelector,
  74. (e, c) => resultSelector (e, c)))
  75. .ToList ();
  76. }
  77. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  78. {
  79. var source = Parent.GetOrderedEnumerables (options);
  80. var sizeRequests = new Tuple<long, int, IEnumerable<TCollection>> [options.PartitionCount];
  81. if (collectionSelectorIndexed == null)
  82. collectionSelectorIndexed = (e, i) => collectionSelector (e);
  83. long deviation = 0;
  84. Barrier barrier = new Barrier (options.PartitionCount, delegate {
  85. Array.Sort (sizeRequests, (e1, e2) => e1.Item1.CompareTo (e2.Item1));
  86. long newDeviation = deviation;
  87. for (int i = sizeRequests.Length - 1; i >= 0; i--) {
  88. var reqi = sizeRequests[i];
  89. long newIndex = reqi.Item1 + deviation;
  90. newDeviation += reqi.Item2 - 1;
  91. for (int j = i - 1; j >= 0; j--)
  92. newIndex += sizeRequests[j].Item2 - 1;
  93. sizeRequests[i] = Tuple.Create (newIndex, reqi.Item2, reqi.Item3);
  94. }
  95. deviation = newDeviation;
  96. });
  97. return source
  98. .Select ((i, ind) => GetOrderedEnumerableInternal (i, sizeRequests, ind, barrier))
  99. .ToList ();
  100. }
  101. IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
  102. Func<T, IEnumerable<TCollection>> collectionner,
  103. Func<T, TCollection, TResult> packer)
  104. {
  105. foreach (T element in source)
  106. foreach (TCollection item in collectionner (element))
  107. yield return packer (element, item);
  108. }
  109. IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
  110. Tuple<long, int, IEnumerable<TCollection>>[] sizeRequests,
  111. int index,
  112. Barrier barrier)
  113. {
  114. try {
  115. foreach (KeyValuePair<long, TSource> element in source) {
  116. IEnumerable<TCollection> collection = collectionSelectorIndexed (element.Value, (int)element.Key);
  117. sizeRequests[index] = Tuple.Create (element.Key, GetCount (ref collection), collection);
  118. barrier.SignalAndWait ();
  119. long i = sizeRequests[index].Item1;
  120. collection = sizeRequests[index].Item3;
  121. foreach (TCollection item in collection)
  122. yield return new KeyValuePair<long, TResult> (i++, resultSelector (element.Value, item));
  123. }
  124. } finally {
  125. barrier.RemoveParticipant ();
  126. }
  127. }
  128. /* If getting Count is a O(1) operation (i.e. actual is a ICollection) then return it immediatly
  129. * if not process the IEnumerable into a List and return the Count from that (i.e. enumerable
  130. * processing will only happen once in case of e.g. a Linq query)
  131. */
  132. static int GetCount<T> (ref IEnumerable<T> actual)
  133. {
  134. ICollection coll = actual as ICollection;
  135. if (coll != null)
  136. return coll.Count;
  137. var foo = actual.ToList ();
  138. actual = foo;
  139. return foo.Count;
  140. }
  141. }
  142. }
  143. #endif