QueryWhereNode.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. .ToList ();
  60. }
  61. internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
  62. {
  63. return Parent.GetEnumerables (options)
  64. .Select (i => i.Where (predicate))
  65. .ToList ();
  66. }
  67. internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
  68. {
  69. IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
  70. ProcessingSlot[] store = new ProcessingSlot[sources.Count];
  71. Comparison<ProcessingSlot> arrayComparison = ArraySortMethod;
  72. long lastIndex = 0;
  73. Barrier barrier = new Barrier (sources.Count, delegate (Barrier b) {
  74. // Sort the store
  75. Array.Sort (store, arrayComparison);
  76. // Reassign a good index
  77. int i = 0;
  78. for (i = 0; i < store.Length && store[i].IsValid; ++i)
  79. store[i].Index = lastIndex + i;
  80. // Update lastIndex for next round
  81. lastIndex += i;
  82. });
  83. return sources
  84. .Select ((s, i) => GetEnumerator (s, barrier, options.MergedToken, store, i))
  85. .ToList ();
  86. }
  87. static int ArraySortMethod (ProcessingSlot lhs, ProcessingSlot rhs)
  88. {
  89. if (lhs.IsValid && !rhs.IsValid)
  90. return -1;
  91. if (!lhs.IsValid && rhs.IsValid)
  92. return 1;
  93. if (!lhs.IsValid && !rhs.IsValid)
  94. return 0;
  95. return lhs.Index < rhs.Index ? -1 : 1;
  96. }
  97. IEnumerable<KeyValuePair<long, TSource>> GetEnumerator (IEnumerable<KeyValuePair<long, TSource>> source,
  98. Barrier barrier,
  99. CancellationToken token,
  100. ProcessingSlot[] store, int index)
  101. {
  102. IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
  103. bool isIndexed = IsIndexed;
  104. try {
  105. while (current.MoveNext ()) {
  106. KeyValuePair<long, TSource> curr = current.Current;
  107. bool result = isIndexed ? indexedPredicate (curr.Value, (int)curr.Key) : predicate (curr.Value);
  108. store[index].Update (curr.Value, curr.Key, result);
  109. barrier.SignalAndWait (token);
  110. var value = store [index];
  111. if (value.IsValid)
  112. yield return new KeyValuePair<long, TSource> (value.Index, value.Value);
  113. // Reset
  114. store[index].Clear ();
  115. }
  116. } finally {
  117. // Remove our participation
  118. barrier.RemoveParticipant ();
  119. current.Dispose ();
  120. }
  121. }
  122. struct ProcessingSlot
  123. {
  124. public TSource Value;
  125. public long Index;
  126. public bool IsValid;
  127. public void Update (TSource v, long i, bool t)
  128. {
  129. Value = v;
  130. Index = i;
  131. IsValid = t;
  132. }
  133. public void Clear ()
  134. {
  135. Update (default (TSource), long.MaxValue, false);
  136. }
  137. }
  138. }
  139. }
  140. #endif