QuerySelectManyNode.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // This one is gonna be tricky
  78. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
  83. Func<T, IEnumerable<TCollection>> collectionner,
  84. Func<T, TCollection, TResult> packer)
  85. {
  86. foreach (T element in source)
  87. foreach (TCollection item in collectionner (element))
  88. yield return packer (element, item);
  89. }
  90. IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
  91. Barrier barrier)
  92. {
  93. foreach (KeyValuePair<long, TSource> element in source) {
  94. IEnumerable<TCollection> collection = collectionSelectorIndexed (element.Value, (int)element.Key);
  95. foreach (TCollection item in collection)
  96. yield return new KeyValuePair<long, TResult> (-1, resultSelector (element.Value, item));
  97. }
  98. }
  99. }
  100. }
  101. #endif