QueryWhereNode.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // QueryWhereNode.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.Linq;
  29. using System.Threading;
  30. using System.Collections.Generic;
  31. namespace System.Linq.Parallel.QueryNodes
  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>> GetEnumerablesIndexed (QueryOptions options)
  56. {
  57. return Parent.GetOrderedEnumerables (options)
  58. .Select ((i) => i.Where ((e) => indexedPredicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
  59. .ToArray ();
  60. }
  61. internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
  62. {
  63. return Parent.GetEnumerables (options)
  64. .Select ((i) => i.Where (predicate))
  65. .ToArray ();
  66. }
  67. internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
  68. {
  69. IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
  70. Tuple<TSource, long, bool>[] store = new Tuple<TSource, long, bool>[sources.Count];
  71. long lastIndex = 0;
  72. Barrier barrier = new Barrier (sources.Count, delegate (Barrier b) {
  73. // Sort the store
  74. Array.Sort (store, ArraySortMethod);
  75. // Reassign a good index
  76. int i = 0;
  77. for (i = 0; i < store.Length && store[i].Item3; i++) {
  78. Tuple<TSource, long, bool> old = store[i];
  79. store[i] = Tuple.Create (old.Item1, lastIndex + i, old.Item3);
  80. }
  81. // Update lastIndex for next round
  82. lastIndex += i;
  83. });
  84. return sources
  85. .Select ((s, i) => GetEnumerator (s, barrier, store, i))
  86. .ToArray ();
  87. }
  88. static int ArraySortMethod (Tuple<TSource, long, bool> lhs, Tuple<TSource, long, bool> rhs)
  89. {
  90. if (lhs.Item3 && !rhs.Item3)
  91. return -1;
  92. if (!lhs.Item3 && rhs.Item3)
  93. return 1;
  94. if (!lhs.Item3 && !rhs.Item3)
  95. return 0;
  96. return (lhs.Item2 < rhs.Item2) ? -1 : 1;
  97. }
  98. IEnumerable<KeyValuePair<long, TSource>> GetEnumerator (IEnumerable<KeyValuePair<long, TSource>> source,
  99. Barrier barrier,
  100. Tuple<TSource, long, bool>[] store, int index)
  101. {
  102. IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
  103. bool result;
  104. Tuple<TSource, long, bool> reset = Tuple.Create (default (TSource), long.MaxValue, false);
  105. try {
  106. while (current.MoveNext ()) {
  107. KeyValuePair<long, TSource> curr = current.Current;
  108. result = IsIndexed ? indexedPredicate (curr.Value, (int)curr.Key) : predicate (curr.Value);
  109. store[index] = Tuple.Create (curr.Value, curr.Key, result);
  110. barrier.SignalAndWait ();
  111. Tuple<TSource, long, bool> value = store [index];
  112. if (value.Item3)
  113. yield return new KeyValuePair<long, TSource> (value.Item2, value.Item1);
  114. // Reset
  115. store[index] = reset;
  116. }
  117. } finally {
  118. // Remove our participation
  119. barrier.RemoveParticipant ();
  120. current.Dispose ();
  121. }
  122. }
  123. }
  124. }
  125. #endif