QuerySetNode.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // QueryMuxNode.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.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. namespace System.Linq.Parallel.QueryNodes
  32. {
  33. internal class QuerySetNode<TSource> : QueryMuxNode<TSource, TSource, TSource>
  34. {
  35. readonly SetInclusion setInclusion;
  36. readonly IEqualityComparer<TSource> comparer;
  37. internal QuerySetNode (SetInclusion setInclusion, IEqualityComparer<TSource> comparer,
  38. QueryBaseNode<TSource> first, QueryBaseNode<TSource> second)
  39. : base (first, second)
  40. {
  41. this.setInclusion = setInclusion;
  42. this.comparer = comparer;
  43. }
  44. internal override IEnumerable<TSource> GetSequential ()
  45. {
  46. var first = Parent.GetSequential ();
  47. var second = Second == null ? null : Second.GetSequential ();
  48. // We try to do some guessing based on the default
  49. switch (setInclusion) {
  50. case SetInclusionDefaults.Union:
  51. return first.Union (second, comparer);
  52. case SetInclusionDefaults.Intersect:
  53. return first.Intersect (second, comparer);
  54. case SetInclusionDefaults.Except:
  55. return first.Except (second, comparer);
  56. case SetInclusionDefaults.Distinct:
  57. return first.Distinct (comparer);
  58. }
  59. // Default is we return the bare source enumerable
  60. return first;
  61. }
  62. internal override IList<IEnumerable<TSource>> GetEnumerables (QueryOptions options)
  63. {
  64. var first = Parent.GetEnumerables (options);
  65. var second = Second.GetEnumerables (options);
  66. var checker = new ConcurrentSkipList<TSource> (comparer);
  67. InitConcurrentSkipList (checker, second, (e) => e);
  68. return first
  69. .Select ((f, i) => GetEnumerable<TSource> (f, second[i], checker, (e) => e))
  70. .ToList ();
  71. }
  72. internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
  73. {
  74. var first = Parent.GetOrderedEnumerables (options);
  75. var second = Second.GetOrderedEnumerables (options);
  76. var checker = new ConcurrentSkipList<TSource> (comparer);
  77. InitConcurrentSkipList (checker, second, (e) => e.Value);
  78. return first
  79. .Select ((f, i) => GetEnumerable<KeyValuePair<long, TSource>> (f, second[i], checker, (e) => e.Value))
  80. .ToList ();
  81. }
  82. void InitConcurrentSkipList<TExtract> (ConcurrentSkipList<TSource> checker,
  83. IList<IEnumerable<TExtract>> feeds,
  84. Func<TExtract, TSource> extractor)
  85. {
  86. if ((setInclusion & SetInclusion.Preload) == 0)
  87. return;
  88. foreach (IEnumerable<TExtract> feed in feeds)
  89. foreach (TExtract item in feed)
  90. checker.TryAdd (extractor (item));
  91. }
  92. IEnumerable<TExtract> GetEnumerable<TExtract> (IEnumerable<TExtract> first,
  93. IEnumerable<TExtract> second,
  94. ConcurrentSkipList<TSource> checker,
  95. Func<TExtract, TSource> extractor)
  96. {
  97. IEnumerator<TExtract> eFirst = first.GetEnumerator ();
  98. IEnumerator<TExtract> eSecond = second == null ? null : second.GetEnumerator ();
  99. IEnumerator<TExtract> current = eFirst;
  100. bool outInclusion = (setInclusion & SetInclusion.Out) > 0;
  101. bool preload = (setInclusion & SetInclusion.Preload) > 0;
  102. bool relaxed = (setInclusion & SetInclusion.Relaxed) > 0;
  103. try {
  104. while (current != null) {
  105. while (current.MoveNext ()) {
  106. bool result = relaxed ?
  107. checker.Contains (extractor (current.Current)) : checker.TryAdd (extractor (current.Current));
  108. if ((result && outInclusion)
  109. || (!result && !outInclusion))
  110. yield return current.Current;
  111. }
  112. if (current == eFirst && !preload)
  113. current = eSecond;
  114. else
  115. break;
  116. }
  117. } finally {
  118. eFirst.Dispose ();
  119. eSecond.Dispose ();
  120. }
  121. }
  122. }
  123. }
  124. #endif