QueryWhereNode.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #if NET_4_0
  2. //
  3. // QueryWhereNode.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.Linq;
  29. using System.Threading;
  30. using System.Collections.Generic;
  31. namespace System.Linq
  32. {
  33. internal class QueryWhereNode<TSource> : QueryStreamNode<TSource, TSource>
  34. {
  35. Func<TSource, int, bool> indexedPredicate;
  36. Func<TSource, bool> predicate;
  37. internal QueryWhereNode (QueryBaseNode<TSource> parent, Func<TSource, bool> predicate)
  38. : base (parent, false)
  39. {
  40. this.predicate = predicate;
  41. }
  42. internal QueryWhereNode (QueryBaseNode<TSource> parent, Func<TSource, int, bool> predicate)
  43. : base (parent, true)
  44. {
  45. this.indexedPredicate = predicate;
  46. }
  47. internal override IEnumerable<TSource> GetSequential ()
  48. {
  49. IEnumerable<TSource> parent = Parent.GetSequential ();
  50. if (indexedPredicate != null)
  51. return parent.Where (indexedPredicate);
  52. else
  53. return parent.Where (predicate);
  54. }
  55. internal override IList<IEnumerable<TSource>> GetEnumerables (QueryOptions options)
  56. {
  57. if (IsIndexed) {
  58. IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
  59. IEnumerable<TSource>[] results = new IEnumerable<TSource>[sources.Count];
  60. for (int i = 0; i < results.Length; i++)
  61. results[i] =
  62. sources[i].Where ((e) => indexedPredicate (e.Value, (int)e.Key)).Select ((e) => e.Value);
  63. return results;
  64. } else {
  65. IList<IEnumerable<TSource>> sources = Parent.GetEnumerables (options);
  66. IEnumerable<TSource>[] results = new IEnumerable<TSource>[sources.Count];
  67. for (int i = 0; i < results.Length; i++)
  68. results[i] = sources[i].Where (predicate);
  69. return results;
  70. }
  71. }
  72. internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
  73. {
  74. IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
  75. IEnumerable<KeyValuePair<long, TSource>>[] results = new IEnumerable<KeyValuePair<long, TSource>>[sources.Count];
  76. Tuple<TSource, long, bool>[] store = new Tuple<TSource, long, bool>[results.Length];
  77. long lastIndex = 0;
  78. Barrier barrier = new Barrier (results.Length, delegate (Barrier b) {
  79. // Sort the store
  80. Array.Sort (store, ArraySortMethod);
  81. // Reassign a good index
  82. int i = 0;
  83. for (i = 0; i < store.Length && store[i].Item3; i++) {
  84. Tuple<TSource, long, bool> old = store[i];
  85. store[i] = Tuple.Create (old.Item1, lastIndex + i, old.Item3);
  86. }
  87. // Update lastIndex for next round
  88. lastIndex += i;
  89. });
  90. for (int j = 0; j < results.Length; j++)
  91. results[j] = GetEnumerator (sources[j], barrier, store, j);
  92. return results;
  93. }
  94. static int ArraySortMethod (Tuple<TSource, long, bool> lhs, Tuple<TSource, long, bool> rhs)
  95. {
  96. if (lhs.Item3 && !rhs.Item3)
  97. return -1;
  98. if (!lhs.Item3 && rhs.Item3)
  99. return 1;
  100. if (!lhs.Item3 && !rhs.Item3)
  101. return 0;
  102. return (lhs.Item2 < rhs.Item2) ? -1 : 1;
  103. }
  104. IEnumerable<KeyValuePair<long, TSource>> GetEnumerator (IEnumerable<KeyValuePair<long, TSource>> source,
  105. Barrier barrier,
  106. Tuple<TSource, long, bool>[] store, int index)
  107. {
  108. IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
  109. bool result;
  110. while (current.MoveNext ()) {
  111. KeyValuePair<long, TSource> curr = current.Current;
  112. if (IsIndexed)
  113. result = indexedPredicate (curr.Value, (int)curr.Key);
  114. else
  115. result = predicate (curr.Value);
  116. store[index] = Tuple.Create (curr.Value, curr.Key, result);
  117. barrier.SignalAndWait ();
  118. Tuple<TSource, long, bool> value = store [index];
  119. if (value.Item3)
  120. yield return new KeyValuePair<long, TSource> (value.Item2, value.Item1);
  121. // Reset
  122. store[index] = Tuple.Create (default (TSource), long.MaxValue, false);
  123. }
  124. // Remove our participation
  125. barrier.RemoveParticipant ();
  126. }
  127. }
  128. }
  129. #endif