QueryWhereNode.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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].IsValid; ++i)
  78. store[i].Index = lastIndex + i;
  79. // Update lastIndex for next round
  80. lastIndex += i;
  81. });
  82. return sources
  83. .Select ((s, i) => GetEnumerator (s, barrier, options.MergedToken, store, i))
  84. .ToList ();
  85. }
  86. static int ArraySortMethod (ProcessingSlot lhs, ProcessingSlot rhs)
  87. {
  88. if (lhs.IsValid && !rhs.IsValid)
  89. return -1;
  90. if (!lhs.IsValid && rhs.IsValid)
  91. return 1;
  92. if (!lhs.IsValid && !rhs.IsValid)
  93. return 0;
  94. return lhs.Index < rhs.Index ? -1 : 1;
  95. }
  96. IEnumerable<KeyValuePair<long, TSource>> GetEnumerator (IEnumerable<KeyValuePair<long, TSource>> source,
  97. Barrier barrier,
  98. CancellationToken token,
  99. ProcessingSlot[] store, int index)
  100. {
  101. IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
  102. bool isIndexed = IsIndexed;
  103. try {
  104. while (current.MoveNext ()) {
  105. KeyValuePair<long, TSource> curr = current.Current;
  106. bool result = isIndexed ? indexedPredicate (curr.Value, (int)curr.Key) : predicate (curr.Value);
  107. store[index].Update (curr.Value, curr.Key, result);
  108. barrier.SignalAndWait (token);
  109. var value = store [index];
  110. if (value.IsValid)
  111. yield return new KeyValuePair<long, TSource> (value.Index, value.Value);
  112. // Reset
  113. store[index].Clear ();
  114. }
  115. } finally {
  116. // Remove our participation
  117. barrier.RemoveParticipant ();
  118. current.Dispose ();
  119. }
  120. }
  121. struct ProcessingSlot
  122. {
  123. public TSource Value;
  124. public long Index;
  125. public bool IsValid;
  126. public void Update (TSource v, long i, bool t)
  127. {
  128. Value = v;
  129. Index = i;
  130. IsValid = t;
  131. }
  132. public void Clear ()
  133. {
  134. Update (default (TSource), long.MaxValue, false);
  135. }
  136. }
  137. }
  138. }
  139. #endif