QueryHeadWorkerNode.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // QueryConcatNode.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. using System;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. namespace System.Linq.Parallel.QueryNodes
  31. {
  32. internal interface QueryHeadWorkerNode : IVisitableNode {
  33. int? Count { get; }
  34. }
  35. /* This is the QueryNode used by Take(While) operator
  36. * it symbolize operators that are preferably working on the head elements of a query and can prematurely
  37. * stop providing elements following the one they were processing is of a greater value in a specific
  38. * order to be defined by the instance (e.g. simple numerical order when working on indexes).
  39. */
  40. internal class QueryHeadWorkerNode<TSource> : QueryStreamNode<TSource, TSource>, QueryHeadWorkerNode
  41. {
  42. /* This variable will receive an index value that represent the "stop point"
  43. * when used with GetOrderedEnumerables i.e. values that are above the indexes are discarded
  44. * (if the partitioner is ordered in a partition it even stop the processing) and value below are still tested just
  45. * in case and can still lower this gap limit.
  46. */
  47. readonly int count;
  48. readonly Func<TSource, int, bool> predicate;
  49. internal QueryHeadWorkerNode (QueryBaseNode<TSource> parent, int count)
  50. : base (parent, false)
  51. {
  52. this.count = count;
  53. }
  54. internal QueryHeadWorkerNode (QueryBaseNode<TSource> parent, Func<TSource, int, bool> predicate, bool indexed)
  55. : base (parent, indexed)
  56. {
  57. this.predicate = predicate;
  58. }
  59. public int? Count {
  60. get {
  61. return predicate == null ? count : (int?)null;
  62. }
  63. }
  64. internal override IEnumerable<TSource> GetSequential ()
  65. {
  66. IEnumerable<TSource> parent = Parent.GetSequential ();
  67. return predicate == null ? parent.Take (count) : parent.TakeWhile (predicate);
  68. }
  69. public override void Visit (INodeVisitor visitor)
  70. {
  71. visitor.Visit ((QueryHeadWorkerNode)this);
  72. }
  73. internal override IList<IEnumerable<TSource>> GetEnumerablesIndexed (QueryOptions options)
  74. {
  75. return Parent.GetOrderedEnumerables (options)
  76. .Select ((i) => i.TakeWhile ((e) => predicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
  77. .ToList ();
  78. }
  79. internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
  80. {
  81. return Parent.GetEnumerables (options)
  82. .Select (GetSelector (count))
  83. .ToList ();
  84. }
  85. Func<IEnumerable<TSource>, IEnumerable<TSource>> GetSelector (int c)
  86. {
  87. if (predicate == null)
  88. return (i) => i.TakeWhile ((e) => c > 0 && Interlocked.Decrement (ref c) >= 0);
  89. else
  90. return (i) => i.TakeWhile ((e) => predicate (e, -1));
  91. }
  92. internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
  93. {
  94. return Parent.GetOrderedEnumerables (options)
  95. .Select ((i) => GetEnumerableInternal (i, options))
  96. .ToList ();
  97. }
  98. IEnumerable<KeyValuePair<long, TSource>> GetEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source, QueryOptions options)
  99. {
  100. IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
  101. long gapIndex = predicate == null ? count : long.MaxValue;
  102. Func<KeyValuePair<long, TSource>, bool> cond;
  103. if (predicate == null)
  104. cond = (kv) => kv.Key < count;
  105. else
  106. cond = (kv) => predicate (kv.Value, (int)kv.Key);
  107. try {
  108. while (current.MoveNext ()) {
  109. KeyValuePair<long, TSource> kvp = current.Current;
  110. /* When filtering is based on a predicate, this short-circuit is only valid
  111. * if the partitioner used ensure items are ordered in each partition
  112. * (valid w/ default partitioners)
  113. */
  114. if (kvp.Key >= gapIndex && options.PartitionerSettings.Item2)
  115. break;
  116. if (!cond (kvp)) {
  117. if (gapIndex > kvp.Key && predicate != null)
  118. gapIndex = kvp.Key;
  119. continue;
  120. }
  121. yield return kvp;
  122. }
  123. } finally {
  124. current.Dispose ();
  125. }
  126. }
  127. }
  128. }