QuerySelectManyNode.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if NET_4_0
  2. //
  3. // QueryConcatNode.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.Collections;
  30. using System.Collections.Generic;
  31. namespace System.Linq
  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. if (IsIndexed)
  58. return source.SelectMany (collectionSelectorIndexed, resultSelector);
  59. else
  60. return source.SelectMany (collectionSelector, resultSelector);
  61. }
  62. internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
  63. {
  64. IEnumerable<TResult>[] result = null;
  65. if (IsIndexed) {
  66. IList<IEnumerable<KeyValuePair<long, TSource>>> enumerables = Parent.GetOrderedEnumerables (options);
  67. result = new IEnumerable<TResult>[enumerables.Count];
  68. for (int i = 0; i < enumerables.Count; i++)
  69. result[i] = GetEnumerableInternal<KeyValuePair<long, TSource>> (enumerables[i],
  70. (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
  71. (e, c) => resultSelector (e.Value, c));
  72. } else {
  73. IList<IEnumerable<TSource>> enumerables = Parent.GetEnumerables (options);
  74. result = new IEnumerable<TResult>[enumerables.Count];
  75. for (int i = 0; i < enumerables.Count; i++)
  76. result[i] = GetEnumerableInternal<TSource> (enumerables[i],
  77. collectionSelector,
  78. (e, c) => resultSelector (e, c));
  79. }
  80. return result;
  81. }
  82. // This one is gonna be tricky
  83. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  84. {
  85. throw new NotImplementedException ();
  86. }
  87. IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
  88. Func<T, IEnumerable<TCollection>> collectionner,
  89. Func<T, TCollection, TResult> packer)
  90. {
  91. foreach (T element in source)
  92. foreach (TCollection item in collectionner (element))
  93. yield return packer (element, item);
  94. }
  95. IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
  96. Barrier barrier)
  97. {
  98. foreach (KeyValuePair<long, TSource> element in source) {
  99. IEnumerable<TCollection> collection = collectionSelectorIndexed (element.Value, (int)element.Key);
  100. foreach (TCollection item in collection)
  101. yield return new KeyValuePair<long, TResult> (-1, resultSelector (element.Value, item));
  102. }
  103. }
  104. }
  105. }
  106. #endif