QuerySelectManyNode.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 SizeRequest[source.Count];
  81. long deviation = 0;
  82. Barrier barrier = new Barrier (source.Count, delegate (Barrier b) {
  83. Array.Sort (sizeRequests, KeyComparator);
  84. for (int i = 0; i < b.ParticipantCount; ++i) {
  85. if (sizeRequests[i].Key == -1)
  86. continue;
  87. sizeRequests[i].Key = deviation;
  88. deviation += sizeRequests[i].Size;
  89. }
  90. });
  91. return source
  92. .Select ((i, ind) => GetOrderedEnumerableInternal (i, sizeRequests, ind, barrier))
  93. .ToList ();
  94. }
  95. IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
  96. Func<T, IEnumerable<TCollection>> collectionner,
  97. Func<T, TCollection, TResult> packer)
  98. {
  99. foreach (T element in source)
  100. foreach (TCollection item in collectionner (element))
  101. yield return packer (element, item);
  102. }
  103. IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
  104. SizeRequest[] sizeRequests,
  105. int index,
  106. Barrier barrier)
  107. {
  108. IEnumerator<KeyValuePair<long, TSource>> enumerator = source.GetEnumerator ();
  109. bool isIndexed = IsIndexed;
  110. try {
  111. while (true) {
  112. KeyValuePair<long, TSource> element;
  113. IEnumerable<TCollection> collection;
  114. if (enumerator.MoveNext ()) {
  115. element = enumerator.Current;
  116. collection = isIndexed ?
  117. collectionSelectorIndexed (element.Value, (int)element.Key) :
  118. collectionSelector (element.Value);
  119. var count = GetCount (ref collection);
  120. sizeRequests[index].Update (element.Key, count, collection, element.Value);
  121. }
  122. barrier.SignalAndWait ();
  123. long i = sizeRequests[index].Key;
  124. collection = sizeRequests[index].Collection;
  125. var elementValue = sizeRequests[index].ElementValue;
  126. sizeRequests[index].Clear ();
  127. if (i == -1)
  128. break;
  129. foreach (TCollection item in collection)
  130. yield return new KeyValuePair<long, TResult> (i++, resultSelector (elementValue, item));
  131. }
  132. } finally {
  133. barrier.RemoveParticipant ();
  134. enumerator.Dispose ();
  135. }
  136. }
  137. /* If getting Count is a O(1) operation (i.e. actual is a ICollection) then return it immediatly
  138. * if not process the IEnumerable into a List and return the Count from that (i.e. enumerable
  139. * processing will only happen once in case of e.g. a Linq query)
  140. */
  141. static int GetCount<T> (ref IEnumerable<T> actual)
  142. {
  143. ICollection coll = actual as ICollection;
  144. if (coll != null)
  145. return coll.Count;
  146. var foo = actual.ToList ();
  147. actual = foo;
  148. return foo.Count;
  149. }
  150. static int KeyComparator (SizeRequest e1, SizeRequest e2)
  151. {
  152. return e1.Key.CompareTo (e2.Key);
  153. }
  154. struct SizeRequest
  155. {
  156. public long Key;
  157. public int Size;
  158. public IEnumerable<TCollection> Collection;
  159. public TSource ElementValue;
  160. public void Update (long k, int s, IEnumerable<TCollection> c, TSource ev)
  161. {
  162. Key = k;
  163. Size = s;
  164. Collection = c;
  165. ElementValue = ev;
  166. }
  167. public void Clear ()
  168. {
  169. Key = -1;
  170. Size = 0;
  171. Collection = null;
  172. ElementValue = default (TSource);
  173. }
  174. }
  175. }
  176. }
  177. #endif